Skip to content

Instantly share code, notes, and snippets.

@zneix
Created September 15, 2021 23:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zneix/0b63f6770f3140ec6a768b893b30af53 to your computer and use it in GitHub Desktop.
Save zneix/0b63f6770f3140ec6a768b893b30af53 to your computer and use it in GitHub Desktop.
random hash in go
package main
import (
"crypto/rand"
"crypto/sha256"
"encoding/hex"
"fmt"
)
func GenerateRandomBytes(n int) ([]byte, error) {
b := make([]byte, n)
_, err := rand.Read(b)
// Note that err == nil only if we read len(b) bytes.
if err != nil {
return nil, err
}
return b, nil
}
func main() {
str := "dankpassword"
hasher := sha256.New()
hasher.Write([]byte(str))
random, err := GenerateRandomBytes(256)
if err != nil {
fmt.Println(err)
return
}
hasher.Write(random)
sha := hex.EncodeToString(hasher.Sum(nil))
fmt.Println(sha)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment