Golang crypto/sha256 functions example
package crypto/sha256
Package sha256 implements the SHA224 and SHA256 hash algorithms as defined in FIPS 180-4.
Golang crypto/sha256 functions usage example
package main
import (
"crypto/sha256"
"fmt"
"io"
)
func main() {
h256 := sha256.New()
io.WriteString(h256, "Hello money money!")
fmt.Printf("Hash256 : %x\n", h256.Sum(nil))
h224 := sha256.New224()
io.WriteString(h224, "Hello money money!")
fmt.Printf("Hash224 : %x\n", h224.Sum(nil))
// const Size224 = 28 bytes
data224 := []byte("Hello World!")
fmt.Printf("SHA224 checksum : %x\n", sha256.Sum224(data224))
data := []byte("Hello World!") // const Size = 32 bytes
fmt.Printf("SHA256 checksum : %x\n", sha256.Sum256(data))
}
Output :
go run sha256.go
Hash256 : 2a590667731adefb6afabc7fc539d4934ccd1dd43cc808a76e2d9528d237537c
Hash224 : 90653e56b9a4fa7927f7f3087e77f338d25eacb61cd2339968948115
SHA224 checksum : 4575bb4ec129df6380cedde6d71217fe0536f8ffc4e18bca530a7a1b
SHA256 checksum : 7f83b1657ff1fc53b92dc18148a1d65dfc2d4b1fa3d677284addd200126d9069
Reference :
Advertisement
Something interesting
Tutorials
+18.5k Golang : Write file with io.WriteString
+9.5k Golang : Extract or copy items from map based on value
+6k Golang : Convert Chinese UTF8 characters to Pin Yin
+5.6k Javascript : How to refresh page with JQuery ?
+11.8k Golang : Verify Linux user password again before executing a program example
+16.8k Golang : read gzipped http response
+6.1k Golang : Measure execution time for a function
+7.8k Golang : Scan files for certain pattern and rename part of the files
+12k Golang : Convert a rune to unicode style string \u
+17.8k Golang : Iterate linked list example
+12.3k Golang : How to display image file or expose CSS, JS files from localhost?