Created
September 15, 2021 23:32
-
-
Save zneix/0b63f6770f3140ec6a768b893b30af53 to your computer and use it in GitHub Desktop.
random hash in go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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