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 :

http://golang.org/pkg/crypto/sha256/

Advertisement