写出js版的md5算法
时间: 2023-08-31 17:42:40 浏览: 171
### 回答1:
JS版的MD5算法如下:```javascript
function md5(str) {
var rotateLeft = function(lValue, iShiftBits) {
return (lValue << iShiftBits) | (lValue >>> (32 - iShiftBits));
}; var addUnsigned = function(lX, lY) {
var lX4, lY4, lX8, lY8, lResult;
lX8 = (lX & x80000000);
lY8 = (lY & x80000000);
lX4 = (lX & x40000000);
lY4 = (lY & x40000000);
lResult = (lX & x3FFFFFFF) + (lY & x3FFFFFFF);
if (lX4 & lY4) {
return (lResult ^ x80000000 ^ lX8 ^ lY8);
}
if (lX4 | lY4) {
if (lResult & x40000000) {
return (lResult ^ xC000000 ^ lX8 ^ lY8);
} else {
return (lResult ^ x40000000 ^ lX8 ^ lY8);
}
} else {
return (lResult ^ lX8 ^ lY8);
}
}; var f = function(x, y, z) {
return (x & y) | ((~x) & z);
};
var g = function(x, y, z) {
return (x & z) | (y & (~z));
};
var h = function(x, y, z) {
return (x ^ y ^ z);
};
var i = function(x, y, z) {
return (y ^ (x | (~z)));
}; var ff = function(a, b, c, d, x, s, ac) {
a = addUnsigned(a, addUnsigned(addUnsigned(f(b, c, d), x), ac));
return addUnsigned(rotateLeft(a, s), b);
}; var gg = function(a, b, c, d, x, s, ac) {
a = addUnsigned(a, addUnsigned(addUnsigned(g(b, c, d), x), ac));
return addUnsigned(rotateLeft(a, s), b);
}; var hh = function(a, b, c, d, x, s, ac) {
a = addUnsigned(a, addUnsigned(addUnsigned(h(b, c, d), x), ac));
return addUnsigned(rotateLeft(a, s), b);
}; var ii = function(a, b, c, d, x, s, ac) {
a = addUnsigned(a, addUnsigned(addUnsigned(i(b, c, d), x), ac));
return addUnsigned(rotateLeft(a, s), b);
}; var convertToWordArray = function(str) {
var lWordCount;
var lMessageLength = str.length;
var lNumberOfWords_temp1 = lMessageLength + 8;
var lNumberOfWords_temp2 = (lNumberOfWords_temp1 - (lNumberOfWords_temp1 % 64)) / 64;
var lNumberOfWords = (lNumberOfWords_temp2 + 1) * 16;
var lWordArray = Array(lNumberOfWords - 1);
var lBytePosition = ;
var lByteCount = ;
while (lByteCount < lMessageLength) {
lWordCount = (lByteCount - (lByteCount % 4)) / 4;
lBytePosition = (lByteCount % 4) * 8;
lWordArray[lWordCount] = (lWordArray[lWordCount] | (str.charCodeAt(lByteCount) << lBytePosition));
lByteCount++;
}
lWordCount = (lByteCount - (lByteCount % 4)) / 4;
lBytePosition = (lByteCount % 4) * 8;
lWordArray[lWordCount] = lWordArray[lWordCount] | (x80 << lBytePosition);
lWordArray[lNumberOfWords - 2] = lMessageLength << 3;
lWordArray[lNumberOfWords - 1] = lMessageLength >>> 29;
return lWordArray;
}; var wordToHex = function(lValue) {
var wordToHexValue = "",
wordToHexValue_temp = "",
lByte, lCount;
for (lCount = ; lCount <= 3; lCount++) {
lByte = (lValue >>> (lCount * 8)) & 255;
wordToHexValue_temp = "" + lByte.toString(16);
wordToHexValue = wordToHexValue + wordToHexValue_temp.substr(wordToHexValue_temp.length - 2, 2);
}
return wordToHexValue;
}; var uTF8Encode = function(string) {
string = string.replace(/\r\n/g, "\n");
var utftext = "";
for (var n = ; n < string.length; n++) {
var c = string.charCodeAt(n);
if (c < 128) {
utftext += String.fromCharCode(c);
} else if ((c > 127) && (c < 2048)) {
utftext += String.fromCharCode((c >> 6) | 192);
utftext += String.fromCharCode((c & 63) | 128);
} else {
utftext += String.fromCharCode((c >> 12) | 224);
utftext += String.fromCharCode(((c >> 6) & 63) | 128);
utftext += String.fromCharCode((c & 63) | 128);
}
}
return utftext;
}; var x = Array();
var k, AA, BB, CC, DD, a, b, c, d;
var S11 = 7,
S12 = 12,
S13 = 17,
S14 = 22;
var S21 = 5,
S22 = 9,
S23 = 14,
S24 = 20;
var S31 = 4,
S32 = 11,
S33 = 16,
S34 = 23;
var S41 = 6,
S42 = 10,
S43 = 15,
S44 = 21; str = uTF8Encode(str); x = convertToWordArray(str); a = x67452301;
b = xEFCDAB89;
c = x98BADCFE;
d = x10325476; for (k = ; k < x.length; k += 16) {
AA = a;
BB = b;
CC = c;
DD = d;
a = ff(a, b, c, d, x[k + ], S11, xD76AA478);
d = ff(d, a, b, c, x[k + 1], S12, xE8C7B756);
c = ff(c, d, a, b, x[k + 2], S13, x242070DB);
b = ff(b, c, d, a, x[k + 3], S14, xC1BDCEEE);
a = ff(a, b, c, d, x[k + 4], S11, xF57CFAF);
d = ff(d, a, b, c, x[k + 5], S12, x4787C62A);
c = ff(c, d, a, b, x[k + 6], S13, xA8304613);
b = ff(b, c, d, a, x[k + 7], S14, xFD469501);
a = ff(a, b, c, d, x[k + 8], S11, x698098D8);
d = ff(d, a, b, c, x[k + 9], S12, x8B44F7AF);
c = ff(c, d, a, b, x[k + 10], S13, xFFFF5BB1);
b = ff(b, c, d, a, x[k + 11], S14, x895CD7BE);
a = ff(a, b, c, d, x[k + 12], S11, x6B901122);
d = ff(d, a, b, c, x[k + 13], S12, xFD987193);
c = ff(c, d, a, b, x[k + 14], S13, xA679438E);
b = ff(b, c, d, a, x[k + 15], S14, x49B40821);
a = gg(a, b, c, d, x[k + 1], S21, xF61E2562);
d = gg(d, a, b, c, x[k + 6], S22, xC040B340);
c = gg(c, d, a, b, x[k + 11], S23, x265E5A51);
b = gg(b, c, d, a, x[k + ], S24, xE9B6C7AA);
a = gg(a, b, c, d, x[k + 5], S21, xD62F105D);
d = gg(d, a, b, c, x[k + 10], S22, x2441453);
c = gg(c, d, a, b, x[k + 15], S23, xD8A1E681);
b = gg(b, c, d, a, x[k + 4], S24, xE7D3FBC8);
a = gg(a, b, c, d, x[k + 9], S21, x21E1CDE6);
d = gg(d, a, b, c, x[k + 14], S22, xC33707D6);
c = gg(c, d, a, b, x[k + 3], S23, xF4D50D87);
b = gg(b, c, d, a, x[k + 8], S24, x455A14ED);
a = gg(a, b, c, d, x[k + 13], S21, xA9E3E905);
d = gg(d, a, b, c, x[k + 2], S22, xFCEFA3F8);
c = gg(c, d, a, b, x[k + 7], S23, x676F02D9);
b = gg(b, c, d, a, x[k + 12], S24, x8D2A4C8A);
a = hh(a, b, c, d, x[k + 5], S31, xFFFA3942);
d = hh(d, a, b, c, x[k + 8], S32, x8771F681);
c = hh(c, d, a, b, x[k + 11], S33, x6D9D6122);
b = hh(b, c, d, a, x[k + 14], S34, xFDE538C);
a = hh(a, b, c, d, x[k + 1], S31, xA4BEEA44);
d = hh(d, a, b, c, x[k + 4], S32, x4BDECFA9);
c = hh(c, d, a, b, x[k + 7], S33, xF6BB4B60);
b = hh(b, c, d, a, x[k + 10], S34, xBEBFBC70);
a = hh(a, b, c, d, x[k + 13], S31, x289B7EC6);
d = hh(d, a, b, c, x[k + ], S32, xEAA127FA);
c = hh(c, d, a, b, x[k + 3], S33, xD4EF3085);
b = hh(b, c, d, a, x[k + 6], S34, x4881D05);
a = hh(a, b, c, d, x[k + 9], S31, xD9D4D039);
d = hh(d, a, b, c, x[k + 12], S32, xE6DB99E5);
c = hh(c, d, a, b, x[k + 15], S33, x1FA27CF8);
b = hh(b, c, d, a, x[k + 2], S34, xC4AC5665);
a = ii(a, b, c, d, x[k + ], S41, xF4292244);
d = ii(d, a, b, c, x[k + 7], S42, x432AFF97);
c = ii(c, d, a, b, x[k + 14], S43, xAB9423A7);
b = ii(b, c, d, a, x[k + 5], S44, xFC93A039);
a = ii(a, b, c, d, x[k + 12], S41, x655B59C3);
d = ii(d, a, b, c, x[k + 3], S42, x8FCCC92);
c = ii(c, d, a, b, x[k + 10], S43, xFFEFF47D);
b = ii(b, c, d, a, x[k + 1], S44, x85845DD1);
a = ii(a, b, c, d, x[k + 8], S41, x6FA87E4F);
d = ii(d, a, b, c, x[k + 15], S42, xFE2CE6E);
c = ii(c, d, a, b, x[k + 6], S43, xA3014314);
b = ii(b, c, d, a, x[k + 13], S44, x4E0811A1);
a = ii(a, b, c, d, x[k + 4], S41, xF7537E82);
d = ii(d, a, b, c, x[k + 11], S42, xBD3AF235);
c = ii(c, d, a, b, x[k + 2], S43, x2AD7D2BB);
b = ii(b, c, d, a, x[k + 9], S44, xEB86D391);
a = addUnsigned(a, AA);
b = addUnsigned(b, BB);
c = addUnsigned(c, CC);
d = addUnsigned(d, DD);
} var temp = wordToHex(a) + wordToHex(b) + wordToHex(c) + wordToHex(d); return temp.toLowerCase();
}
```
### 回答2:
JavaScript中已经有现成的md5算法库可以直接使用,例如crypto-js库。以下是使用crypto-js库实现的JavaScript版md5算法:
首先,需要在项目中引入crypto-js库:
```html
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/crypto-js.min.js"></script>
```
然后,使用以下代码进行md5加密:
```javascript
var message = '要加密的字符串';
var encryptedMessage = CryptoJS.MD5(message).toString();
console.log('加密后的字符串:' + encryptedMessage);
```
通过以上代码,我们可以将字符串message进行md5加密,并将加密后的结果打印出来。
需要注意的是,以上只是crypto-js库中md5算法的一种使用方式,crypto-js库还提供其他加密算法,可以根据需求选择相应的算法。
### 回答3:
JS版的MD5算法可以使用CryptoJS库来实现。以下是一个示例:
1. 首先,确保已经引入CryptoJS库,可以通过在HTML文件中添加以下脚本引入:
```html
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/crypto-js.min.js"></script>
```
2. 接下来,可以使用以下代码来计算MD5哈希值:
```javascript
var message = "要进行MD5计算的消息";
var hash = CryptoJS.MD5(message);
var md5 = hash.toString();
console.log(md5);
```
3. 在上面的代码中,首先定义了一个变量`message`,它是要进行MD5计算的消息。然后,通过调用`CryptoJS.MD5`方法,并将消息作为参数传递给它来计算哈希值。这将返回一个CryptoJS对象,我们可以将其转换为字符串表示形式(即MD5哈希值)使用`toString()`方法。
4. 最后,我们可以使用`console.log()`将计算得到的MD5值打印到控制台中。
这样就可以通过CryptoJS库在JavaScript中实现MD5算法了。
阅读全文