Golang hash.Hash type examples
package hash
Hash is the common interface implemented by all hash functions.
Golang hash.Hash type usage examples
Example 1:
// createMac creates a message authentication code (MAC).
func createMac(h hash.Hash, value []byte) []byte {
h.Write(value)
return h.Sum(nil)
}
Example 2:
package main
import (
"crypto/md5"
"fmt"
"io"
)
func main() {
hash := md5.New()
io.WriteString(hash, "I don't want anyone else to know about this string message!") // append into the hash
fmt.Printf("%x \n", hash.Sum(nil))
fmt.Printf("Size : %d \n", hash.Size())
fmt.Printf("BlockSize : %d \n", hash.BlockSize())
hash.Reset()
// after reset
fmt.Printf("%x \n", hash.Sum(nil))
}
Example 3:
func New() MultiHashContext {
contexts := make(map[string]hash.Hash)
contexts["adler32"] = adler32.New()
contexts["crc32"] = crc32.NewIEEE()
contexts["md5"] = md5.New()
contexts["ripemd160"] = ripemd160.New()
contexts["sha1"] = sha1.New()
contexts["sha2-256"] = sha256.New()
contexts["sha2-512"] = sha512.New()
contexts["sha3-256"] = sha3.NewKeccak256()
s := SizeWriter(0)
return MultiHashContext{contexts: contexts, sw: &s}
}
References :
https://github.com/chrisoei/multidigest/blob/master/multidigest.go
https://www.socketloop.com/tutorials/generate-md5-hash-of-a-string-go
Advertisement
Something interesting
Tutorials
+9.2k Golang : Write multiple lines or divide string into multiple lines
+15k Golang : package is not in GOROOT during compilation
+14.6k Golang : GUI with Qt and OpenCV to capture image from camera
+10.1k Golang : Check a web page existence with HEAD request example
+6.1k Golang : Get missing location after unmarshal binary and gob decode time.
+18.4k Golang : Logging with logrus
+9.4k Golang : Web(Javascript) to server-side websocket example
+18.7k Unmarshal/Load CSV record into struct in Go
+5.3k Javascript : Change page title to get viewer attention
+6.7k Golang : Check if password length meet the requirement
+7.7k Golang : Error reading timestamp with GORM or SQL driver