如何使用JavaScript或jQuery对字符串进行SHA-1哈希加密?
嘿,我来帮你搞定用JavaScript(包括配合jQuery)生成SHA-1哈希的问题!其实jQuery本身并没有内置加密功能,核心还是靠JavaScript实现,下面分几种常用场景给你详细讲:
一、现代浏览器推荐:使用原生Web Crypto API
这是浏览器原生支持的安全加密API,比自己手写的纯JS实现更可靠,不过只支持现代浏览器(Chrome 37+、Firefox 34+、Edge 12+等)。步骤很清晰:
async function sha1Hash(str) { // 把字符串转成UTF-8编码的二进制数据 const encoder = new TextEncoder(); const data = encoder.encode(str); // 调用原生API计算SHA-1哈希 const hashBuffer = await crypto.subtle.digest('SHA-1', data); // 把二进制结果转成人类可读的十六进制字符串 const hashArray = Array.from(new Uint8Array(hashBuffer)); const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join(''); return hashHex; } // 调用示例 sha1Hash('Hello World!').then(hash => { console.log('SHA-1哈希结果:', hash); // 输出:0a0a9f2a6772942557ab5355d76af442f8f65e01 });
二、兼容旧环境:纯JavaScript实现SHA-1
如果需要支持IE这类旧浏览器,或者不想依赖Web Crypto,可以用纯JS实现的SHA-1函数。下面是一个经过验证的轻量实现:
function sha1(str) { function rotateLeft(n, s) { return (n << s) | (n >>> (32 - s)); } function toHexString(n) { const hexChars = '0123456789abcdef'; let result = ''; for (let i = 7; i >= 0; i--) { result += hexChars.charAt((n >> (i * 4)) & 0x0F); } return result; } let blockstart; let i, j; const W = new Array(80); let H0 = 0x67452301; let H1 = 0xEFCDAB89; let H2 = 0x98BADCFE; let H3 = 0x10325476; let H4 = 0xC3D2E1F0; let A, B, C, D, E; let temp; str = unescape(encodeURIComponent(str)); const strLen = str.length; const wordArray = []; for (i = 0; i < strLen - 3; i += 4) { j = str.charCodeAt(i) << 24 | str.charCodeAt(i + 1) << 16 | str.charCodeAt(i + 2) << 8 | str.charCodeAt(i + 3); wordArray.push(j); } switch (strLen % 4) { case 0: i = 0x080000000; break; case 1: i = str.charCodeAt(strLen - 1) << 24 | 0x0800000; break; case 2: i = str.charCodeAt(strLen - 2) << 24 | str.charCodeAt(strLen - 1) << 16 | 0x08000; break; case 3: i = str.charCodeAt(strLen - 3) << 24 | str.charCodeAt(strLen - 2) << 16 | str.charCodeAt(strLen - 1) << 8 | 0x80; break; } wordArray.push(i); while ((wordArray.length % 16) !== 14) wordArray.push(0); wordArray.push(strLen >>> 29); wordArray.push((strLen << 3) & 0x0FFFFFFFF); for (blockstart = 0; blockstart < wordArray.length; blockstart += 16) { for (i = 0; i < 16; i++) W[i] = wordArray[blockstart + i]; for (i = 16; i < 80; i++) W[i] = rotateLeft(W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16], 1); A = H0; B = H1; C = H2; D = H3; E = H4; for (i = 0; i < 20; i++) { temp = (rotateLeft(A, 5) + ((B & C) | (~B & D)) + E + W[i] + 0x5A827999) & 0x0FFFFFFFF; E = D; D = C; C = rotateLeft(B, 30); B = A; A = temp; } for (i = 20; i < 40; i++) { temp = (rotateLeft(A, 5) + (B ^ C ^ D) + E + W[i] + 0x6ED9EBA1) & 0x0FFFFFFFF; E = D; D = C; C = rotateLeft(B, 30); B = A; A = temp; } for (i = 40; i < 60; i++) { temp = (rotateLeft(A, 5) + ((B & C) | (B & D) | (C & D)) + E + W[i] + 0x8F1BBCDC) & 0x0FFFFFFFF; E = D; D = C; C = rotateLeft(B, 30); B = A; A = temp; } for (i = 60; i < 80; i++) { temp = (rotateLeft(A, 5) + (B ^ C ^ D) + E + W[i] + 0xCA62C1D6) & 0x0FFFFFFFF; E = D; D = C; C = rotateLeft(B, 30); B = A; A = temp; } H0 = (H0 + A) & 0x0FFFFFFFF; H1 = (H1 + B) & 0x0FFFFFFFF; H2 = (H2 + C) & 0x0FFFFFFFF; H3 = (H3 + D) & 0x0FFFFFFFF; H4 = (H4 + E) & 0x0FFFFFFFF; } return toHexString(H0) + toHexString(H1) + toHexString(H2) + toHexString(H3) + toHexString(H4); } // 调用示例 console.log('SHA-1哈希结果:', sha1('Hello World!')); // 同样输出:0a0a9f2a6772942557ab5355d76af442f8f65e01
三、配合jQuery使用
jQuery本身不提供加密能力,但可以用它快速获取页面上的字符串(比如输入框内容),再传给上面的哈希函数:
<input type="text" id="inputStr" placeholder="输入要哈希的字符串"> <button id="hashBtn">生成SHA-1哈希</button> <div id="result"></div> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script> // 复用前面的sha1Hash函数 async function sha1Hash(str) { /* ... */ } $(document).ready(function() { $('#hashBtn').click(async function() { const input = $('#inputStr').val(); if (!input) { $('#result').text('请输入字符串!'); return; } const hash = await sha1Hash(input); $('#result').text(`SHA-1哈希:${hash}`); }); }); </script>
最后提醒一句:SHA-1现在已经被认为是不安全的加密算法,如果是用于密码存储、数据校验等安全场景,建议使用SHA-256或更安全的算法——上面的Web Crypto API只需要把'SHA-1'改成'SHA-256'就能直接用哦!
内容的提问来源于stack exchange,提问作者Ankit Tyagi
相关产品推荐
相关产品推荐

