package
main
import (
"crypto/md5"
"crypto/sha1"
"fmt"
"io"
)
//对字符串进行MD5哈希
func a(data string) string {
t := md5.New()
io.WriteString(t, data)
return fmt.Sprintf("%x", t.Sum(nil))
}
//对字符串进行SHA1哈希
func b(data string) string {
t := sha1.New()
io.WriteString(t, data)
return fmt.Sprintf("%x", t.Sum(nil))
}
func main() {
var data string = "abc"
ay := a(data)
ah := b(data)
fmt.Println(ay)
fmt.Printf("SHA2 : %s\n", ah)
fmt.Printf("SHA1 : %s\n", ah)
}
结果:
900150983cd24fb0d6963f7d28e17f72
SHA2 : a9993e364706816aba3e25717850c26c9cd0d89d
SHA1 : a9993e364706816aba3e25717850c26c9cd0d89d