This example describes the SHA-1 encryption algorithm implemented by JavaScript. Share to everyone for your reference, specific as follows:
/** * Secure Hash algorithm (SHA1) * http://www.webtoolkit.info/* **/function SHA1 (msg) {function rotate_left (n,s ) {var T4 = (n<<s) |
(N>>> (32-s));
return t4;
};
Function Lsb_hex (val) {var str= "";
var i;
var vh;
var vl;
For (i=0 i<=6; i+=2) {VH = (val>>> (i*4+4)) &0x0f;
VL = (val>>> (i*4)) &0x0f;
STR + + vh.tostring + vl.tostring (16);
return str;
};
Function Cvt_hex (val) {var str= "";
var i;
var V;
For (i=7 i>=0; i--) {v = (val>>> (i*4)) &0x0f;
str = v.tostring (16);
return str;
};
function Utf8encode (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;
};
var Blockstart;
var i, J;
var W = new Array (80);
var H0 = 0x67452301;
var H1 = 0xefcdab89;
var H2 = 0x98badcfe;
var H3 = 0x10325476;
var H4 = 0xc3d2e1f0;
var A, B, C, D, E;
var temp;
msg = Utf8encode (msg);
var msg_len = msg.length;
var word_array = new Array ();
For (i=0 i<msg_len-3; i+=4) {j = msg.charcodeat (i) <<24 | msg.charcodeat (i+1) <<16 | Msg.charcodeat (i+2) <<8 |
Msg.charcodeat (i+3);
Word_array.push (j);
Switch (msg_len% 4) {case 0:i = 0x080000000;
Break Case 1:i = msg.charcodeat (msg_len-1) <<24 |
0x0800000;
Break Case 2:i = Msg.charcodeat (msg_len-2) <<24 | Msg.charcodeat (msg_len-1) <<16 |
0x08000;
Break Case 3:i = msg.charcodeat (msg_len-3) <<24 | Msg.charcodeat (msg_len-2) <<16 | Msg.charcodeat (msg_len-1) <<8 |
0x80
Break
} word_array.push (i);
while ((word_array.length%)!= word_array.push (0);
Word_array.push (msg_len>>>29);
Word_array.push ((msg_len<<3) &0x0ffffffff); For (blockstart=0 blockstart<word_array.length; blockstart+=16) {for (i=0; i<16; i++) w[i] = Word_array[blo
Ckstart+i];
For (i=16 i<=79; i++) w[i] = Rotate_left (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<=19; i++) {temp = (Rotate_left (a,5) + (b&c) | (~b&d))
+ E + w[i] + 0x5a827999) & 0x0ffffffff;
E = D;
D = C;
C = Rotate_left (b,30);
B = A;
A = temp; For (i=20 i<=39; i++) {
temp = (Rotate_left (a,5) + (B ^ C ^ D) + E + w[i] + 0x6ed9eba1) & 0x0ffffffff;
E = D;
D = C;
C = Rotate_left (b,30);
B = A;
A = temp; For (i=40 i<=59; i++) {temp = (Rotate_left (a,5) + (b&c) | (b&d) | (c&d))
+ E + w[i] + 0X8F1BBCDC) & 0x0ffffffff;
E = D;
D = C;
C = Rotate_left (b,30);
B = A;
A = temp; For (i=60 i<=79; i++) {temp = (Rotate_left (a,5) + (B ^ C ^ D) + E + w[i] + 0xca62c1d6) & 0X0FFFFFFFF
;
E = D;
D = C;
C = Rotate_left (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;
var temp = Cvt_hex (H0) + Cvt_hex (H1) + Cvt_hex (H2) + Cvt_hex (H3) + Cvt_hex (H4);
return Temp.tolowercase ();
}
More readers interested in JavaScript-related content can view this site: "JavaScript data structure and algorithm skills summary" and "JavaScript Mathematical operation Summary"
I hope this article will help you with JavaScript programming.