当前位置: 首页 > news >正文

加密摘要算法MD5、SHA、HMAC:学习笔记

使用场景:

1、数据完整性校验:下载文件时验证文件是否被篡改。

2、密码存储:将用户密码加密后存储,防止明文泄露。

3、数字签名:用于验证签名和数据的完整性,例如在区块链交易。

4、数据索引:在数据库中用于快速查找和比较数据

MD5:

MD5特点:
1、固定长度的字符串,长度为:32位
2、md5得到的值一般都是十六进制,数据取值范围(a-f 0-9)
3、md5是不可逆的(如果对一串字符串进行md5的编码是无法回复成原来的字符串的)
4、md5可以完成数据的校验(可以保证源数据不被篡改,可以保证数据的一致性)

使用python实现的MD5编码:

import hashlib def md5_(string): md5 = hashlib.md5() md5.update(string.encode('utf-8')) return md5.hexdigest() print(md5_('1'))

使用javascript实现的MD5编码:

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加密:

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加密:

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的编码:

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编码:

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'));
http://www.jsqmd.com/news/348995/

相关文章:

  • 【Matlab】MATLAB if-elseif-else语句详解:多条件分支与复杂条件判断应用
  • 2026年最新SVG互动设计案例丨5个免费公众号排版工具推荐与实操指南 - peipei33
  • 技术速递|GitHub Copilot CLI 斜杠命令速查表
  • 期刊 On Hold 什么意思?
  • 跨平台学习新时代!随时随地培训考试的系统源码
  • HoRain--通过Xshell连接linux——安装jdk
  • 源码全开源无加密:自主可控的企业培训考试平台源码
  • Prolactin抗体在内分泌疾病诊断中有何关键作用?
  • 全国接地模块降阻剂优质厂家有哪些?优先选哪些维度筛选? - 非研科技
  • ADDI-DATA多功能板卡APCI-3120
  • 基于微信小程序的自习室座位预约系统【源码+文档+调试】
  • 2026 CRM 系统排行榜:八大主流品牌核心能力深度对比 - 毛毛鱼的夏天
  • 精选七大耐用的橱柜拉篮品牌,打造井井有条的现代厨房
  • Cas9抗体在基因编辑研究中发挥何种质量控制作用?
  • Spark核心数据(RDD、DataFrame 和 Dataset) - 教程
  • 基于鲸鱼优化的LSTM深度学习网络模型(WOA-LSTM)的一维时间序列预测算法matlab仿真
  • 2026年知名的臭氧负离子发生器/杀菌消毒负离子发生器优质供应商推荐(信赖) - 行业平台推荐
  • 2026十三款工具横评:从CRM到一体化云企业管理系统选型指南 - 毛毛鱼的夏天
  • 百考通AI:智能文献综述神器,让学术研究事半功倍!
  • postgresql日常维护和检查一-处理表、索引膨胀
  • 聚氨酯筛板加工厂哪家品牌好用,江苏地区高性价比选择有哪些 - 工业品牌热点
  • 2026销售管理系统盘点:10款全链路CRM+供应链系统横向对比 - 毛毛鱼的夏天
  • 百考通AI:开题报告智能生成专家,让学术起点不再迷茫!
  • 2026CRM系统排行榜:9 大主流品牌数字化工具核心能力横评 - 毛毛鱼的夏天
  • 百考通AI:智能问卷设计,让市场调研与用户洞察变得简单高效!
  • 传统成果转化困局如何破?生态协同机制助力学术产业双赢
  • catman Easy/AP | 应变四分之一桥温度补偿的理论与实践
  • PPT 批量转图片:在 Web 预览中实现翻页效果(C#/VB.NET) - E
  • 百考通AI:毕业论文写作的智能“加速器”,轻松搞定学术难题!
  • 构建成果转化新生态,赋能高校科研创新