Generating a SHA Hash for a Given String in Golang
One of the most typical cryptographic operations performed with strings is computing their SHA hash. The SHA (Secure Hash Algorithm) algorithm generates a unique fingerprint by taking a string as input. In Golang, creating a SHA hash is a straightforward process.
Example Usage
For instance, let's compute the SHA hash of the string "beautiful." The following code snippet provides a practical example:
import ( "crypto/sha1" "encoding/base64" ) func main() { myPassword := "beautiful" hasher := sha1.New() hasher.Write([]byte(myPassword)) sha := base64.URLEncoding.EncodeToString(hasher.Sum(nil)) println(sha) }
This code demonstrates:
The resulting sha variable contains the base64-encoded SHA hash of the original string, which can be conveniently displayed or compared.
The above is the detailed content of How do you generate a SHA hash for a given string in Golang?. For more information, please follow other related articles on the PHP Chinese website!