sha1.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. //REF:: http://www.movable-type.co.uk/scripts/sha1.html
  2. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  3. /* SHA-1 implementation in JavaScript | (c) Chris Veness 2002-2010 | www.movable-type.co.uk */
  4. /* - see http://csrc.nist.gov/groups/ST/toolkit/secure_hashing.html */
  5. /* http://csrc.nist.gov/groups/ST/toolkit/examples.html */
  6. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  7. var Sha1 = {}; // Sha1 namespace
  8. /**
  9. * Generates SHA-1 hash of string
  10. *
  11. * @param {String} msg String to be hashed
  12. * @param {Boolean} [utf8encode=true] Encode msg as UTF-8 before generating hash
  13. * @returns {String} Hash of msg as hex character string
  14. */
  15. Sha1.hash = function(msg, utf8encode) {
  16. utf8encode = (typeof utf8encode == 'undefined') ? true : utf8encode;
  17. // convert string to UTF-8, as SHA only deals with byte-streams
  18. if (utf8encode) msg = Utf8.encode(msg);
  19. // constants [§4.2.1]
  20. var K = [0x5a827999, 0x6ed9eba1, 0x8f1bbcdc, 0xca62c1d6];
  21. // PREPROCESSING
  22. msg += String.fromCharCode(0x80); // add trailing '1' bit (+ 0's padding) to string [§5.1.1]
  23. // convert string msg into 512-bit/16-integer blocks arrays of ints [§5.2.1]
  24. var l = msg.length/4 + 2; // length (in 32-bit integers) of msg + ‘1’ + appended length
  25. var N = Math.ceil(l/16); // number of 16-integer-blocks required to hold 'l' ints
  26. var M = new Array(N);
  27. for (var i=0; i<N; i++) {
  28. M[i] = new Array(16);
  29. for (var j=0; j<16; j++) { // encode 4 chars per integer, big-endian encoding
  30. M[i][j] = (msg.charCodeAt(i*64+j*4)<<24) | (msg.charCodeAt(i*64+j*4+1)<<16) |
  31. (msg.charCodeAt(i*64+j*4+2)<<8) | (msg.charCodeAt(i*64+j*4+3));
  32. } // note running off the end of msg is ok 'cos bitwise ops on NaN return 0
  33. }
  34. // add length (in bits) into final pair of 32-bit integers (big-endian) [§5.1.1]
  35. // note: most significant word would be (len-1)*8 >>> 32, but since JS converts
  36. // bitwise-op args to 32 bits, we need to simulate this by arithmetic operators
  37. M[N-1][14] = ((msg.length-1)*8) / Math.pow(2, 32); M[N-1][14] = Math.floor(M[N-1][14])
  38. M[N-1][15] = ((msg.length-1)*8) & 0xffffffff;
  39. // set initial hash value [§5.3.1]
  40. var H0 = 0x67452301;
  41. var H1 = 0xefcdab89;
  42. var H2 = 0x98badcfe;
  43. var H3 = 0x10325476;
  44. var H4 = 0xc3d2e1f0;
  45. // HASH COMPUTATION [§6.1.2]
  46. var W = new Array(80); var a, b, c, d, e;
  47. for (var i=0; i<N; i++) {
  48. // 1 - prepare message schedule 'W'
  49. for (var t=0; t<16; t++) W[t] = M[i][t];
  50. for (var t=16; t<80; t++) W[t] = Sha1.ROTL(W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16], 1);
  51. // 2 - initialise five working variables a, b, c, d, e with previous hash value
  52. a = H0; b = H1; c = H2; d = H3; e = H4;
  53. // 3 - main loop
  54. for (var t=0; t<80; t++) {
  55. var s = Math.floor(t/20); // seq for blocks of 'f' functions and 'K' constants
  56. var T = (Sha1.ROTL(a,5) + Sha1.f(s,b,c,d) + e + K[s] + W[t]) & 0xffffffff;
  57. e = d;
  58. d = c;
  59. c = Sha1.ROTL(b, 30);
  60. b = a;
  61. a = T;
  62. }
  63. // 4 - compute the new intermediate hash value
  64. H0 = (H0+a) & 0xffffffff; // note 'addition modulo 2^32'
  65. H1 = (H1+b) & 0xffffffff;
  66. H2 = (H2+c) & 0xffffffff;
  67. H3 = (H3+d) & 0xffffffff;
  68. H4 = (H4+e) & 0xffffffff;
  69. }
  70. return Sha1.toHexStr(H0) + Sha1.toHexStr(H1) +
  71. Sha1.toHexStr(H2) + Sha1.toHexStr(H3) + Sha1.toHexStr(H4);
  72. }
  73. //
  74. // function 'f' [§4.1.1]
  75. //
  76. Sha1.f = function(s, x, y, z) {
  77. switch (s) {
  78. case 0: return (x & y) ^ (~x & z); // Ch()
  79. case 1: return x ^ y ^ z; // Parity()
  80. case 2: return (x & y) ^ (x & z) ^ (y & z); // Maj()
  81. case 3: return x ^ y ^ z; // Parity()
  82. }
  83. }
  84. //
  85. // rotate left (circular left shift) value x by n positions [§3.2.5]
  86. //
  87. Sha1.ROTL = function(x, n) {
  88. return (x<<n) | (x>>>(32-n));
  89. }
  90. //
  91. // hexadecimal representation of a number
  92. // (note toString(16) is implementation-dependant, and
  93. // in IE returns signed numbers when used on full words)
  94. //
  95. Sha1.toHexStr = function(n) {
  96. var s="", v;
  97. for (var i=7; i>=0; i--) { v = (n>>>(i*4)) & 0xf; s += v.toString(16); }
  98. return s;
  99. }
  100. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  101. /* Utf8 class: encode / decode between multi-byte Unicode characters and UTF-8 multiple */
  102. /* single-byte character encoding (c) Chris Veness 2002-2010 */
  103. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  104. var Utf8 = {}; // Utf8 namespace
  105. /**
  106. * Encode multi-byte Unicode string into utf-8 multiple single-byte characters
  107. * (BMP / basic multilingual plane only)
  108. *
  109. * Chars in range U+0080 - U+07FF are encoded in 2 chars, U+0800 - U+FFFF in 3 chars
  110. *
  111. * @param {String} strUni Unicode string to be encoded as UTF-8
  112. * @returns {String} encoded string
  113. */
  114. Utf8.encode = function(strUni) {
  115. // use regular expressions & String.replace callback function for better efficiency
  116. // than procedural approaches
  117. var strUtf = strUni.replace(
  118. /[\u0080-\u07ff]/g, // U+0080 - U+07FF => 2 bytes 110yyyyy, 10zzzzzz
  119. function(c) {
  120. var cc = c.charCodeAt(0);
  121. return String.fromCharCode(0xc0 | cc>>6, 0x80 | cc&0x3f); }
  122. );
  123. strUtf = strUtf.replace(
  124. /[\u0800-\uffff]/g, // U+0800 - U+FFFF => 3 bytes 1110xxxx, 10yyyyyy, 10zzzzzz
  125. function(c) {
  126. var cc = c.charCodeAt(0);
  127. return String.fromCharCode(0xe0 | cc>>12, 0x80 | cc>>6&0x3F, 0x80 | cc&0x3f); }
  128. );
  129. return strUtf;
  130. }
  131. /**
  132. * Decode utf-8 encoded string back into multi-byte Unicode characters
  133. *
  134. * @param {String} strUtf UTF-8 string to be decoded back to Unicode
  135. * @returns {String} decoded string
  136. */
  137. Utf8.decode = function(strUtf) {
  138. // note: decode 3-byte chars first as decoded 2-byte strings could appear to be 3-byte char!
  139. var strUni = strUtf.replace(
  140. /[\u00e0-\u00ef][\u0080-\u00bf][\u0080-\u00bf]/g, // 3-byte chars
  141. function(c) { // (note parentheses for precence)
  142. var cc = ((c.charCodeAt(0)&0x0f)<<12) | ((c.charCodeAt(1)&0x3f)<<6) | ( c.charCodeAt(2)&0x3f);
  143. return String.fromCharCode(cc); }
  144. );
  145. strUni = strUni.replace(
  146. /[\u00c0-\u00df][\u0080-\u00bf]/g, // 2-byte chars
  147. function(c) { // (note parentheses for precence)
  148. var cc = (c.charCodeAt(0)&0x1f)<<6 | c.charCodeAt(1)&0x3f;
  149. return String.fromCharCode(cc); }
  150. );
  151. return strUni;
  152. }
  153. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */