md5加密
- 要求1:密文
- 要求2:密文不可反解
- 非对称加密和对称加密
- 加密算法:DES、AES、RSA、SM2、SM3、SM4、3DES、RC4、MD5
- md5 信息摘要算法
md5的盐值加密
生成一个指定长度的摘要字符串
1.压缩性 任意长度数据 算出的md5值固定长度
2.容易计算 从源数据计算出md5值很容易
3.抗修改性 对元数据进行任何一个字节修改,md5值差别就很大
4.强碰撞
5.不可逆,不可反解
package main
import (
"crypto/md5"
"encoding/hex"
"fmt"
"io"
"github.com/anaskhan96/go-password-encoder"
)
func main() {
// md5
// 生成md5
// e10adc3949ba59abbe56e057f20f883e
// e10adc3949ba59abbe56e057f20f883e
md5Str := getMd5("123456")
fmt.Print(md5Str)
}
func getMd5(code string) string {
// 获取md5
hash := md5.New()
_, _ = io.WriteString(hash, code)
return hex.EncodeToString(hash.Sum(nil))
}
https://github.com/anaskhan96/go-password-encoder
进行密码随机生成和加密
// md5
// 生成md5
// e10adc3949ba59abbe56e057f20f883e
// e10adc3949ba59abbe56e057f20f883e
// Using the default options
//生成salt 和加密后的密码
salt, encodedPwd := password.Encode("generic password", nil)
check := password.Verify("generic password", salt, encodedPwd, nil)
//Using custom options
options := &password.Options{16, 100, 32, sha512.New}
salt, encodedPwd = password.Encode("generic password", options)
// 生成新的密码
newPassword := fmt.Sprintf("$pbkdf2-sha512$%s$%s", salt, encodedPwd)
fmt.Println(salt, encodedPwd)
// 解析
passwordInfo := strings.Split(newPassword, "$")
check = password.Verify("generic password", passwordInfo[2], passwordInfo[3], options)
fmt.Println(check) // true