import "crypto/md5"
md5包实现了MD5哈希算法,
Constants
func Sum(data []byte) [Size]byte
func New() hash.Hash
const BlockSize = 64
MD5字节块大小。
const Size = 16
MD5校验和字节数。
func Sum(data []byte) [Size]byte
返回数据data的MD5校验和。
package main
import (
"crypto/md5"
"fmt"
)
func main() {
data := []byte("These pretzels are making me thirsty.")
fmt.Printf("%x", md5.Sum(data))
}
func New() hash.Hash
package main
import (
"crypto/md5"
"fmt"
"io"
)
func main() {
h := md5.New()
io.WriteString(h, "The fog is getting thicker!")
io.WriteString(h, "And Leon's getting laaarger!")
fmt.Printf("%x", h.Sum(nil))
}
package main
import (
"crypto/md5"
"fmt"
"io"
"log"
"os"
)
func main() {
f, err := os.Open("file.txt")
if err != nil {
log.Fatal(err)
}
defer f.Close()
h := md5.New()
if _, err := io.Copy(h, f); err != nil {
log.Fatal(err)
}
fmt.Printf("%x", h.Sum(nil))
}
func md5_1(s string) {
m := md5.New()
m.Write([]byte (s))
fmt.Println(hex.EncodeToString(m.Sum(nil)))
}
func md5_2(s string) {
m := md5.Sum([]byte (s))
fmt.Println(hex.EncodeToString(m[:]))
}func md5_3(s string) {
m := md5.Sum([]byte(s))
fmt.Printf("%x", m)
fmt.Println()
}
func md5_4(s string) {
m := md5.New()
io.WriteString(m, s)
fmt.Println(hex.EncodeToString(m.Sum(nil)))
}
参考资料:
Go语言中文文档
http://www.golang.ltd/
Go语言官方文档
https://golang.google.cn/