Code with Go
About
  • Code With Go! 🚀
  • Hello World
  • Values
  • Variables
  • Constants
  • For Loop
  • If Else
  • Switch
  • Array
  • Slice
  • Map
  • Range
  • Functions
  • Multiple Return Values
  • Variadic Functions
  • Closure
  • Recursion
  • Pointers
  • Strings and Runes
  • Structs
  • Methods
  • Interfaces
  • Struct Embedding
  • Generics
  • Error
  • Goroutines
  • Channels
  • Channel Buffering
  • Channel Synchronization
  • Channel Directions
  • Select
  • Timeouts
  • Non-Blocking Channel Operations
  • Closing Channels
  • Range over Channels
  • Timers
  • Tickers
  • Worker Pools
  • Wait Groups
  • Rate Limiting
  • Atomic Counters
  • Mutexes
  • Stateful Goroutines
  • Sorting
  • Sorting by Functions
  • Panic
  • Defer
  • Recover
  • String Functions
  • String Formatting
  • Text Templates
  • Regular Expressions
  • JSON
  • XML
  • Time
  • Epoch
  • Time Formatting / Parsing
  • Random Numbers
  • Number Parsing
  • SHA256 Hashes
  • Base64 Encoding
  • Reading Files
  • Writing Files
  • Line Filters
  • File Paths
  • Directories
  • Temporary Files and Directories
  • Embed Directive
  • Testing and Benchmarking
  • Command-Line Arguments
  • Command-Line Flags
  • Command-Line Subcommands
  • Environment Variables
  • Logging
  • HTTP Client
  • HTTP Server
  • Context
  • Spawning Processes
  • Exec'ing Processes
  • Signals
  • Exit
  • Resize Image
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub

SHA256 Hashes

This Go code demonstrates how to use the crypto/sha256 package to calculate the SHA-256 hash of a string. Let's go through the code with inline comments and explanations:

// Importing necessary packages.
import (
	"crypto/sha256"
	"fmt"
)

// The main function, where the execution of the program begins.
func main() {
	// The string to be hashed.
	s := "sha256 this string"

	// Creating a new SHA-256 hash object.
	h := sha256.New()

	// Writing the bytes of the string to the hash object.
	h.Write([]byte(s))

	// Calculating the SHA-256 hash and getting the hash in bytes.
	bs := h.Sum(nil)

	// Printing the original string and the hexadecimal representation of the hash.
	fmt.Println(s)
	fmt.Printf("%x\n", bs)
}

Output

sha256 this string
1af1dfa857bf1d8814fe1af8983c18080019922e557f15a8a0d3db739d77aacb

Explanation:

  1. Creating SHA-256 Hash Object:

    • sha256.New() creates a new SHA-256 hash object.

  2. Writing Data to Hash:

    • h.Write([]byte(s)) writes the bytes of the string s to the hash object.

  3. Calculating Hash:

    • h.Sum(nil) calculates the SHA-256 hash and returns it as a byte slice.

  4. Printing Results:

    • fmt.Println(s) prints the original string.

    • fmt.Printf("%x\n", bs) prints the hexadecimal representation of the SHA-256 hash.

This code demonstrates a basic example of using SHA-256 hashing in Go. Hashing is commonly used for securely storing passwords, generating unique identifiers, and ensuring data integrity.

PreviousNumber ParsingNextBase64 Encoding

Last updated 1 year ago

Was this helpful?