Go语言的哈希函数
? ?Go提供了MD5、SHA-1等幾個哈希函數:
import ("crypto/md5""crypto/sha1""fmt" )func main() {TestString := "Hi, pandaman!"Md5Inst := md5.New()Md5Inst.Write([]byte(TestString))Result := Md5Inst.Sum([]byte(""))fmt.Printf("%x\n\n", Result)ShalInst := sha1.New()ShalInst.Write([]byte(TestString))Result = ShalInst.Sum([]byte(""))fmt.Printf("%x\n\n", Result)}? 輸出結果
b08dad36bde5f406bdcfb32bfcadbb6b 00aa75c24404f4c81583b99b50534879adc3985d? 對文件內容計算SHA1
?
?
package mainimport ("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 %s\n", md5h.Sum([]byte("")), TestFile)sha1h := sha1.New()io.Copy(sha1h, infile)fmt.Printf("%x %s\n", sha1h.Sum([]byte("")), TestFile)} else {fmt.Println(inerr)os.Exit(1)} }?
參考:
https://www.yuque.com/docs/share/943f769c-3d53-4829-9f67-62a7b7f77b4c
?
總結
- 上一篇: ROS入门教程(一)
- 下一篇: shell脚本小程序之特洛伊木马示例