gobyexample/082-sha1.go
Mark McGranaghan 354a9d862f reorder
2012-09-21 07:54:20 -07:00

28 lines
1.5 KiB
Go

// `crypto/sha1` computes SHA1 hashes.
package main
import (
"fmt"
"crypto/sha1"
"encoding/hex"
)
func main() {
h := sha1.New() // The pattern is `sha1.New()`, `sha1.Write(bytes)`,
// then `sha1.Sum([]byte{})
h.Write([]byte("sha1 this string")) // `Write` expects bytes. If you have a string `s`
// use `[]byte(s)` to coerce it.
bs := h.Sum(nil) // Get the result. The argument to `Sum` can be used
// to append to an existing buffer: usually uneeded.
fmt.Println(hex.EncodeToString(bs)) // SHA1 values are often printed in hex, for example
// with git.
}
// You can compute other hashes like MD5 using the
// same pattern. Import `crypto/md5` and use
// `md5.New()`.