The Go language's method of SHA1 hash operations on strings
- 2020-05-26 09:21:16
- OfStack
The example in this article shows how the Go language performs SHA1 hash operations on strings. Share with you for your reference. The details are as follows:
package main
import (
"fmt"
"crypto/md5"
"crypto/sha1"
"io"
)
// I'm going to do the string MD5 The hash
func a(data string) string {
t := md5.New();
io.WriteString(t,data);
return fmt.Sprintf("%x",t.Sum(nil));
}
// I'm going to do the string SHA1 The hash
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";
fmt.Printf("MD5 : %s\n",a(data));
fmt.Printf("SHA1 : %s\n",b(data));
}
The output results are as follows:
D:\workspace\golang>GetMd5AndSha1.exe
MD5 : 900150983cd24fb0d6963f7d28e17f72
SHA1 : a9993e364706816aba3e25717850c26c9cd0d89d
I hope this article has been helpful to you in the programming of Go language.