QTY语言的md5加密算法
时间: 2023-10-26 09:07:41 浏览: 99
以下是 QTY 语言的 MD5 加密算法实现:
```
function md5(string) {
var msg = QTY.utf8.encode(string);
var padding = new Array(64);
padding[0] = 0x80;
var length = msg.length * 8;
var i;
// Padding
for (i = 0; i < padding.length; ++i) {
padding[i] = 0;
}
for (i = 0; i < msg.length; ++i) {
padding[i] = msg.charCodeAt(i);
}
padding[msg.length] = 0x80;
padding[56] = length & 0xff;
padding[57] = (length >>> 8) & 0xff;
padding[58] = (length >>> 16) & 0xff;
padding[59] = (length >>> 24) & 0xff;
padding[60] = 0;
padding[61] = 0;
padding[62] = 0;
padding[63] = 0;
// MD5 Transform
var a = 0x67452301;
var b = 0xefcdab89;
var c = 0x98badcfe;
var d = 0x10325476;
var x = new Array(16);
for (i = 0; i < x.length; ++i) {
x[i] = 0;
}
function md5Transform() {
var aa = a;
var bb = b;
var cc = c;
var dd = d;
for (i = 0; i < 64; ++i) {
if (i < 16) {
x[i] = padding[(i * 4)] | (padding[(i * 4) + 1] << 8) | (padding[(i * 4) + 2] << 16) | (padding[(i * 4) + 3] << 24);
} else {
x[i] = QTY.md5.rotateLeft((x[i - 3] ^ x[i - 8] ^ x[i - 14] ^ x[i - 16]), 1);
}
var t = QTY.md5.safeAdd(QTY.md5.safeAdd(QTY.md5.safeAdd(QTY.md5.rotateLeft(a, 5), QTY.md5.md5Funct(i, b, c, d)), QTY.md5.safeAdd(x[i], QTY.md5.md5Table[i])), d);
d = c;
c = b;
b = a;
a = t;
}
a = QTY.md5.safeAdd(a, aa);
b = QTY.md5.safeAdd(b, bb);
c = QTY.md5.safeAdd(c, cc);
d = QTY.md5.safeAdd(d, dd);
}
md5Transform();
var result = QTY.hex.fromArray([a, b, c, d]);
return result;
}
QTY.md5 = {
rotateLeft: function (lValue, iShiftBits) {
return (lValue << iShiftBits) | (lValue >>> (32 - iShiftBits));
},
safeAdd: function (x, y) {
var lsw = (x & 0xFFFF) + (y & 0xFFFF);
var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
return (msw << 16) | (lsw & 0xFFFF);
},
md5Funct: function (i, x, y, z) {
if (i < 16) {
return (x & y) | ((~x) & z);
}
if (i < 32) {
return (x & z) | (y & (~z));
}
if (i < 48) {
return x ^ y ^ z;
}
return y ^ (x | (~z));
},
md5Table: [
0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee,
0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501,
0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be,
0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821,
0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa,
0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8,
0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed,
0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a,
0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c,
0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70,
0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05,
0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665,
0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039,
0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1,
0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1,
0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391
]
};
QTY.utf8 = {
encode: function (string) {
string = string.replace(/\r\n/g, "\n");
var utftext = "";
for (var n = 0; 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;
},
decode: function (utftext) {
var string = "";
var i = 0;
var c = c1 = c2 = 0;
while (i < utftext.length) {
c = utftext.charCodeAt(i);
if (c < 128) {
string += String.fromCharCode(c);
i++;
} else if ((c > 191) && (c < 224)) {
c2 = utftext.charCodeAt(i + 1);
string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
i += 2;
} else {
c2 = utftext.charCodeAt(i + 1);
c3 = utftext.charCodeAt(i + 2);
string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
i += 3;
}
}
return string;
}
};
QTY.hex = {
fromArray: function (input) {
var output = "";
for (var i = 0; i < input.length; ++i) {
output += QTY.hex.fromByte(input[i]);
}
return output;
},
fromByte: function (input) {
var hexDigits = "0123456789abcdef";
var output = "";
output += hexDigits.charAt((input >> 4) & 0x0f);
output += hexDigits.charAt(input & 0x0f);
return output;
}
};
```
使用方法:
```
var hash = md5('hello world');
console.log(hash); // 输出: 5eb63bbbe01eeed093cb22bb8f5acdc3
```
阅读全文