常见模块
Keyed-Hash Message Authentication Code 密钥哈希消息认证码
HMac 使用密钥对消息签名的算法。
这个算法,就是在计算哈希的时候,嵌入一个密钥,对MD5,SHA算法都有用,本质上这个算法,就是提供一个SHA(message + salt)的通用计算公式。
1 | func hmacSign(key string, message string) { |
哈希计算 MD5
- 这个没什么说的,不建议使用的方法。
1 | // 计算字符串MD5 |
随机数生成 rand
在Linux上使用getrandom(2) 否则从/dev/urandom获取。
By default, getrandom() draws entropy from the urandom source (i.e.,
the same source as the /dev/urandom device). This behavior can be
changed via the flags argument.OpenBSD 使用getentropy(2)
其它Unix-like systems从/dev/urandom生成
Windows上使用CryptGenRandom API生成
1 | nonce := make([]byte, 12) |
块加密算法 rc4
- 不安全的加密算法,不建议使用,这个是加密算法最早用在流式加密中。
1 | ciper, err := rc4.NewCipher([]byte("HelloWorld")) |