GO中MD5的使用

package main

import (
	"crypto/md5"
	"fmt"
	"io"
)

func main() {
	// 第一种
	var str = "golang"
	slice := []byte(str)
	res := md5.Sum(slice)
	fmt.Println(res) // [33 204 40 64 151 41 86 95 193 164 210 221 146 219 38 159]
	fmt.Printf("%x
", res) // 21cc28409729565fc1a4d2dd92db269f

	// 第二种
	var str2 = "golang"
	w := md5.New()
	io.WriteString(w, str2)
	// 引用类型可以传nil,引用类型var出来就是nil,基本数据类型var是零值
	// 引用类型--> slice map chan
	// Sum(b []byte) []byte
	res2 := w.Sum(nil)
	fmt.Println(res2) // [33 204 40 64 151 41 86 95 193 164 210 221 146 219 38 159]
	fmt.Printf("%x
", res) // 21cc28409729565fc1a4d2dd92db269f
}

  

原文地址:https://www.cnblogs.com/yzg-14/p/13282150.html