create a blake2b hash from a string in go

Here's an example of using the golang.org/x/crypto/blake2b package to create a Blake2b hash from a string:

main.go
package main

import (
    "fmt"
    "golang.org/x/crypto/blake2b"
)

func main() {
    data := []byte("hello world")
    hash := blake2b.Sum256(data)
    fmt.Printf("%x\n", hash[:])
}
185 chars
13 lines

In the code above, we first import the golang.org/x/crypto/blake2b package. We then create a byte slice containing our string "hello world". We pass this byte slice to the blake2b.Sum256 function to generate a Blake2b hash with a 256-bit output size. Finally, we print out the hexadecimal representation of the hash using the %x format specifier.

Note that Blake2b is a cryptographic hash function and should be used with care. Always ensure that you properly handle sensitive data and follow secure coding practices when working with cryptographic functions.

gistlibby LogSnag