1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
|
function sha1(msg)
{
// Initialisation des constantes
var H0 = 0x67452301;
var H1 = 0xEFCDAB89;
var H2 = 0x98BADCFE;
var H3 = 0x10325476;
var H4 = 0xC3D2E1F0;
var w = new Array(80);
var l = msg.length;
var temp;
// Tableau de mots de 32bits : 16 cases de suite = 1 block
var words = new Array();
for(i=0; i < l-3; i += 4 )
{
j = msg.charCodeAt(i)<<24 | msg.charCodeAt(i+1)<<16 | msg.charCodeAt(i+2)<<8 | msg.charCodeAt(i+3);
words.push(j);
}
// Additions des bits manquants
switch( l % 4 )
{
case 0:
i = 0x080000000;
break;
case 1:
i = msg.charCodeAt(l-1)<<24 | 0x0800000;
break;
case 2:
i = msg.charCodeAt(l-2)<<24 | msg.charCodeAt(l-1)<<16 | 0x08000;
break;
case 3:
i = msg.charCodeAt(l-3)<<24 | msg.charCodeAt(l-2)<<16 | msg.charCodeAt(l-1)<<8 | 0x80;
break;
}
words.push(i);
// Completion avec des zeros
while( (words.length % 16) != 14 )
{
words.push(0);
}
// Addition de la taille du message en bits sur 64 bits
words.push(l>>>29);
words.push((l<<3)&0x0ffffffff);
// Pour chaques bloques de 16 mots:
for(block=0; block<words.length; block+=16)
{
// On remplit le tableau w
for(i=0; i<16; i++)
{
w[i] = words[block + i];
}
for(i=16; i<80; i++)
{
w[i] = leftrotate(w[i-3] ^ w[i-8] ^ w[i-14] ^ w[i-16], 1);
}
// Initialisation de A, B, C, D et E
A = H0;
B = H1;
C = H2;
D = H3;
E = H4;
// Mise a jour des variables A, B, C, D et E
for(i=0; i<80; i++)
{
temp = leftrotate(A, 5) + F(i,B,C,D) + E + K(i) + w[i];
E = D;
D = C;
C = leftrotate(B, 30);
B = A;
A = temp;
}
// Calcul des valeurs de hachage intermédiaires
H0 = (H0 + A) & 0x0ffffffff;
H1 = (H1 + B) & 0x0ffffffff;
H2 = (H2 + C) & 0x0ffffffff;
H3 = (H3 + D) & 0x0ffffffff;
H4 = (H4 + E) & 0x0ffffffff;
}
return int2shex(H0) + int2shex(H1) + int2shex(H2) + int2shex(H3) + int2shex(H4);
}
// Convertion d'un entier sur 32 bits en chaine hexadecimal
function int2shex(val)
{
var shex = '';
var i;
var v;
for(i=7; i>=0; i-=1)
{
v = (val>>>(i*4)) & 0x0f;
shex += v.toString(16);
}
return shex;
}
// opération de rotation binaire vers la gauche
function leftrotate(x, n)
{
return ( x<<n ) | (x>>>(32-n));
}
// Fontion K(t)
function K(t)
{
if(0 <= t && t <= 19) return 0x5A827999;
else if(20 <= t && t <= 39) return 0x6ED9EBA1;
else if(40 <= t && t <= 59) return 0x8F1BBCDC;
else if(60 <= t && t <= 79) return 0xCA62C1D6;
else alert('Erreur dans la fonction K(' + t + '): 0 <= t <= 79');
}
function F(t, x, y, z)
{
if(0 <= t && t <= 19) return (x & y) | (~x & z);
else if(20 <= t && t <= 39) return x ^ y ^ z;
else if(40 <= t && t <= 59) return (x & y) | (x & z) | (y & z);
else if(60 <= t && t <= 79) return x ^ y ^ z;
else alert('Erreur dans la fonction F(' + t + ', ' + x + ', ' + y + ', ' + z + '): 0 <= t <= 79');
} |
Partager