求适用于IE11的ES5独立SHA-1字符串哈希算法实现
适配IE11的纯ES5自包含SHA-1实现方案
实现核心特性
- 完全基于ES5语法编写,无ES6及以上特性,原生兼容IE11全版本
- 无任何外部依赖,不需要引入Promise shim、TextEncoder polyfill、Web Crypto适配层等额外工具
- 内置UTF-8字符串转字节流的完整逻辑,可正确处理包含多字节字符、Unicode代理对的字符串
- 输出结果为小写十六进制格式,计算精度完全符合SHA-1标准规范,和主流实现的结果完全对齐,可直接用于类似「Have I Been Pwned」的哈希前缀查询场景
- 完全脱离浏览器原生Crypto API实现,不会在IE11环境下触发接口权限、版本兼容类报错
可直接落地使用的完整实现代码
var sha1 = function() { // 循环左移位运算 var _rotateLeft = function(n, shift) { return (n << shift) | (n >>> (32 - shift)); }; // 字符串转UTF-8字节数组(兼容所有Unicode字符) var stringToUtf8Bytes = function(str) { var byteList = []; for (var i = 0; i < str.length; i++) { var charCode = str.charCodeAt(i); // 处理BMP平面字符 if (charCode < 0x80) { byteList.push(charCode); } else if (charCode < 0x800) { byteList.push(0xc0 | (charCode >> 6), 0x80 | (charCode & 0x3f)); } else if (charCode < 0xd800 || charCode >= 0xe000) { byteList.push(0xe0 | (charCode >> 12), 0x80 | ((charCode >> 6) & 0x3f), 0x80 | (charCode & 0x3f)); } else { // 处理代理对(四字节UTF-8字符) i++; charCode = 0x10000 + (((charCode & 0x3ff) << 10) | (str.charCodeAt(i) & 0x3ff)); byteList.push( 0xf0 | (charCode >> 18), 0x80 | ((charCode >> 12) & 0x3f), 0x80 | ((charCode >> 6) & 0x3f), 0x80 | (charCode & 0x3f) ); } } return byteList; }; // 字节数组转十六进制字符串 var bytesToHex = function(byteList) { var hexMap = "0123456789abcdef"; var result = ""; for (var i = 0; i < byteList.length; i++) { var currentByte = byteList[i]; result += hexMap.charAt((currentByte >> 4) & 0xf) + hexMap.charAt(currentByte & 0xf); } return result; }; // SHA-1主计算逻辑 return function(inputStr) { var processedBytes = stringToUtf8Bytes(inputStr); var originalByteLen = processedBytes.length; // 标准SHA-1消息填充逻辑 processedBytes.push(0x80); while (processedBytes.length % 64 !== 56) { processedBytes.push(0); } // 追加原始消息的位长度 var highLen = Math.floor(originalByteLen / 0x20000000); var lowLen = (originalByteLen * 8) >>> 0; processedBytes.push( (highLen >>> 24) & 0xff, (highLen >>> 16) & 0xff, (highLen >>> 8) & 0xff, highLen & 0xff, (lowLen >>> 24) & 0xff, (lowLen >>> 16) & 0xff, (lowLen >>> 8) & 0xff, lowLen & 0xff ); // 初始化SHA-1标准初始向量 var hashA = 0x67452301; var hashB = 0xefcdab89; var hashC = 0x98badcfe; var hashD = 0x10325476; var hashE = 0xc3d2e1f0; // 按512位(64字节)块循环处理 for (var blockOffset = 0; blockOffset < processedBytes.length; blockOffset += 64) { var wordList = []; // 拆分块为16个32位字 for (var i = 0; i < 16; i++) { wordList[i] = ( (processedBytes[blockOffset + i*4] << 24) | (processedBytes[blockOffset + i*4 + 1] << 16) | (processedBytes[blockOffset + i*4 + 2] << 8) | processedBytes[blockOffset + i*4 + 3] ) >>> 0; } // 扩展为80个32位字 for (var i = 16; i < 80; i++) { wordList[i] = _rotateLeft(wordList[i-3] ^ wordList[i-8] ^ wordList[i-14] ^ wordList[i-16], 1) >>> 0; } var a = hashA, b = hashB, c = hashC, d = hashD, e = hashE, tempVal; // 第1轮计算 for (var i = 0; i < 20; i++) { tempVal = (_rotateLeft(a, 5) + ((b & c) ^ ((~b) & d)) + e + wordList[i] + 0x5a827999) >>> 0; e = d; d = c; c = _rotateLeft(b, 30); b = a; a = tempVal; } // 第2轮计算 for (var i = 20; i < 40; i++) { tempVal = (_rotateLeft(a, 5) + (b ^ c ^ d) + e + wordList[i] + 0x6ed9eba1) >>> 0; e = d; d = c; c = _rotateLeft(b, 30); b = a; a = tempVal; } // 第3轮计算 for (var i = 40; i < 60; i++) { tempVal = (_rotateLeft(a, 5) + ((b & c) ^ (b & d) ^ (c & d)) + e + wordList[i] + 0x8f1bbcdc) >>> 0; e = d; d = c; c = _rotateLeft(b, 30); b = a; a = tempVal; } // 第4轮计算 for (var i = 60; i < 80; i++) { tempVal = (_rotateLeft(a, 5) + (b ^ c ^ d) + e + wordList[i] + 0xca62c1d6) >>> 0; e = d; d = c; c = _rotateLeft(b, 30); b = a; a = tempVal; } // 更新最终哈希值 hashA = (hashA + a) >>> 0; hashB = (hashB + b) >>> 0; hashC = (hashC + c) >>> 0; hashD = (hashD + d) >>> 0; hashE = (hashE + e) >>> 0; } // 将32位哈希值拆解为字节序列 var finalHashBytes = [ (hashA >> 24) & 0xff, (hashA >> 16) & 0xff, (hashA >> 8) & 0xff, hashA & 0xff, (hashB >> 24) & 0xff, (hashB >> 16) & 0xff, (hashB >> 8) & 0xff, hashB & 0xff, (hashC >> 24) & 0xff, (hashC >> 16) & 0xff, (hashC >> 8) & 0xff, hashC & 0xff, (hashD >> 24) & 0xff, (hashD >> 16) & 0xff, (hashD >> 8) & 0xff, hashD & 0xff, (hashE >> 24) & 0xff, (hashE >> 16) & 0xff, (hashE >> 8) & 0xff, hashE & 0xff ]; return bytesToHex(finalHashBytes); }; }();
使用说明
直接调用全局可用的sha1函数,传入待计算的字符串,即可得到对应标准格式的SHA-1哈希结果,无需任何初始化或额外配置。例如调用sha1("test@example.com")会返回和主流标准实现完全一致的哈希值,可直接提取前5位、前6位作为哈希前缀查询的匹配参数。
注意:该实现仅适配非安全场景的哈希计算需求,包括哈希前缀查询、本地缓存键生成、非敏感内容的完整性校验等。由于SHA-1算法已被证实存在抗碰撞缺陷,严禁将其用于密码存储、数字签名、敏感数据加密等安全相关场景。
内容的提问来源于stack exchange,提问作者brightmatter
相关产品推荐
相关产品推荐

