使用场景:
1、数据完整性校验:下载文件时验证文件是否被篡改。
2、密码存储:将用户密码加密后存储,防止明文泄露。
3、数字签名:用于验证签名和数据的完整性,例如在区块链交易。
4、数据索引:在数据库中用于快速查找和比较数据
MD5:
MD5特点:
1、固定长度的字符串,长度为:32位
2、md5得到的值一般都是十六进制,数据取值范围(a-f 0-9)
3、md5是不可逆的(如果对一串字符串进行md5的编码是无法回复成原来的字符串的)
4、md5可以完成数据的校验(可以保证源数据不被篡改,可以保证数据的一致性)
使用python实现的MD5编码:
python
import hashlib
def md5_(string):
md5 = hashlib.md5()
md5.update(string.encode('utf-8'))
return md5.hexdigest()
print(md5_('1'))
使用javascript实现的MD5编码:
javascript
const crypto = require('crypto');
function md5_(text) {
return crypto.MD5(text).toString();
}
console.log(md5_('1'));
SHA系列算法:
安全哈希算法是由美国国家安全局 NSA 设计,主要适用于数字签名标准里面定义的数字签名算法,SHA算法通常指SHA家族里面的五个算法:SHA-1、SHA-224、SHA-256、SHA-384、SHA-512,SHA是比MD5更安全一点的摘要算法,MD5的密文是32位,而SHA-1是40位,版本越强,密文越长,代价就是速度越慢。
使用python实现的SHA加密:
python
import hashlib
def sha1(text):
sha_obj = hashlib.sha1()
sha_obj.update(text.encode("utf-8"))
return sha_obj.hexdigest()
def sha224(text):
sha_obj = hashlib.sha224()
sha_obj.update(text.encode("utf-8"))
return sha_obj.hexdigest()
def sha256(text):
sha_obj = hashlib.sha256()
sha_obj.update(text.encode("utf-8"))
return sha_obj.hexdigest()
def sha384(text):
sha_obj = hashlib.sha384()
sha_obj.update(text.encode("utf-8"))
return sha_obj.hexdigest()
def sha512(text):
sha_obj = hashlib.sha512()
sha_obj.update(text.encode("utf-8"))
return sha_obj.hexdigest()
print("SHA1算法:", sha1("1"), "数据长度:", len(sha1("1")))
print("SHA224算法:", sha224("1"), "数据长度:", len(sha224("1")))
print("SHA256算法:", sha256("1"), "数据长度:", len(sha256("1")))
print("SHA384算法:", sha384("1"), "数据长度:", len(sha384("1")))
print("SHA512算法:", sha512("1"), "数据长度:", len(sha512("1")))
使用javascript实现的SHA加密:
javascript
const crypto = require('crypto');
// SHA1 算法实现
function sha1(data){
// return crypto.SHA1('1').toString() 也可以实现
const hash = crypto.createHash('sha1');
hash.update(data, 'utf8');
return hash.digest('hex');
}
// SHA224 算法实现
function sha224(text) {
// return crypto.SHA224('1').toString() 也可以实现
const hash = crypto.createHash('sha224');
hash.update(text, 'utf8');
return hash.digest('hex');
}
// SHA256 算法实现
function sha256(text) {
// return crypto.SHA256('1').toString() 也可以实现
const hash = crypto.createHash('sha256');
hash.update(text, 'utf8');
return hash.digest('hex');
}
// SHA384 算法实现
function sha384(text) {
// return crypto.SHA384('1').toString() 也可以实现
const hash = crypto.createHash('sha384');
hash.update(text, 'utf8');
return hash.digest('hex');
}
// SHA512 算法实现
function sha512(text) {
// return crypto.SHA512('1').toString() 也可以实现
const hash = crypto.createHash('sha512');
hash.update(text, 'utf8');
return hash.digest('hex');
}
// 测试输出
console.log("SHA1算法:", sha1("1"), "数据长度:", sha1("1").length);
console.log("SHA224算法:", sha224("1"), "数据长度:", sha224("1").length);
console.log("SHA256算法:", sha256("1"), "数据长度:", sha256("1").length);
console.log("SHA384算法:", sha384("1"), "数据长度:", sha384("1").length);
console.log("SHA512算法:", sha512("1"), "数据长度:", sha512("1").length);
HMAC散列消息认证码:
该算法在1996年被提出,1997年作为RFC2104被公布,HMAC加密算法是一种基于安全加密Hash函数和共享密钥的消息认证协议。它要求通讯双方共享密钥key并指出某一种算法对报文Hash运算,形成固定的长度的认证码。通讯双方通过认证码的校验来确定报文的合法性。
python实现HMAC的编码:
python
import hmac
def hmac_encode_md5(key, data):
hmac_md5 = hmac.new(key.encode('utf-8'), data.encode('utf-8'), digestmod='md5')
return hmac_md5.hexdigest()
def hmac_encode_sha1(key, data):
hmac_sha1 = hmac.new(key.encode('utf-8'), data.encode('utf-8'), digestmod='sha1')
return hmac_sha1.hexdigest()
def hmac_encode_sha224(key, data):
hmac_sha224 = hmac.new(key.encode('utf-8'), data.encode('utf-8'), digestmod='sha224')
return hmac_sha224.hexdigest()
def hmac_encode_sha256(key, data):
hmac_sha256 = hmac.new(key.encode('utf-8'), data.encode('utf-8'), digestmod='sha256')
return hmac_sha256.hexdigest()
def hmac_encode_sha384(key, data):
hmac_sha384 = hmac.new(key.encode('utf-8'), data.encode('utf-8'), digestmod='sha384')
return hmac_sha384.hexdigest()
def hmac_encode_sha512(key, data):
hmac_sha512 = hmac.new(key.encode('utf-8'), data.encode('utf-8'), digestmod='sha512')
return hmac_sha512.hexdigest()
print(hmac_encode_md5('1', '1'))
print(hmac_encode_sha1('1', '1'))
print(hmac_encode_sha224('1', '1'))
print(hmac_encode_sha256('1', '1'))
print(hmac_encode_sha384('1', '1'))
print(hmac_encode_sha512('1', '1'))
使用javascript实现HMAC编码:
javascript
const crypto = require('crypto');
// HMAC-MD5 实现
function hmacEncodeMd5(key, data) {
return crypto.createHmac('md5', key).update(data).digest('hex');
}
// HMAC-SHA1 实现
function hmacEncodeSha1(key, data) {
return crypto.createHmac('sha1', key).update(data).digest('hex');
}
// HMAC-SHA224 实现
function hmacEncodeSha224(key, data) {
return crypto.createHmac('sha224', key).update(data).digest('hex');
}
// HMAC-SHA256 实现
function hmacEncodeSha256(key, data) {
return crypto.createHmac('sha256', key).update(data).digest('hex');
}
// HMAC-SHA384 实现
function hmacEncodeSha384(key, data) {
return crypto.createHmac('sha384', key).update(data).digest('hex');
}
// HMAC-SHA512 实现
function hmacEncodeSha512(key, data) {
return crypto.createHmac('sha512', key).update(data).digest('hex');
}
// 测试输出
console.log(hmacEncodeMd5('1', '1'));
console.log(hmacEncodeSha1('1', '1'));
console.log(hmacEncodeSha224('1', '1'));
console.log(hmacEncodeSha256('1', '1'));
console.log(hmacEncodeSha384('1', '1'));
console.log(hmacEncodeSha512('1', '1'));