常用的Hash算法

md4, md5

md5: 生成hash长度的长度:128位。sha256: 256位

sha256的加密会复杂点,同样的系统开销会多。md5、sha256加密不可逆,也就是加密后不能再根据加密的秘钥去解密,网上MD5解密实际上是暴力破解。如果需要可逆的话,请选择DES、AES、异或、凯撒、RSA等。

package main

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

//方式一
func md5Test1() []byte {
	//创建一个哈希器
	hasher := md5.New()
	io.WriteString(hasher,"hello")
	io.WriteString(hasher,"world")

	//执行sum得到哈希值
	hash := hasher.Sum(nil)
	return hash
}

//方式2 直接使用Sum
func md5Test2(str []byte) []byte {
	hash := md5.Sum(str)
	return hash[:]
}
func main()  {
	hash := md5Test1()
	fmt.Printf("方式一:%x\n",hash)

	str:= []byte("helloworld")
	hash2 := md5Test2(str)
	fmt.Printf("方式二:%x\n",hash2)