package main import ( "crypto/md5" "crypto/sha1" "fmt" ) func main() { TestString := "http://c.biancheng.net/golang/" Md5Inst := md5.New() Md5Inst.Write([]byte(TestString)) Result := Md5Inst.Sum([]byte("")) fmt.Printf("%xnn", Result) Sha1Inst := sha1.New() Sha1Inst.Write([]byte(TestString)) Result = Sha1Inst.Sum([]byte("")) fmt.Printf("%xnn", Result) }這個程式的執行結果為:
go run main.go
6dc42d81095839903edf352ef1ec0a6a
32313d69e3f0e4bbf6738858274e7e2c9a46d293
package main import ( "crypto/md5" "crypto/sha1" "fmt" "io" "os" ) func main() { TestFile := "123.txt" infile, inerr := os.Open(TestFile) if inerr == nil { md5h := md5.New() io.Copy(md5h, infile) fmt.Printf("%x %sn", md5h.Sum([]byte("")), TestFile) sha1h := sha1.New() io.Copy(sha1h, infile) fmt.Printf("%x %sn", sha1h.Sum([]byte("")), TestFile) } else { fmt.Println(inerr) os.Exit(1) } }若要執行上面的程式碼,當前目錄下需要包含一個 123.txt 檔案,執行結果如下:
go run main.go
6dc42d81095839903edf352ef1ec0a6a 123.txt
da39a3ee5e6b4b0d3255bfef95601890afd80709 123.txt