sha1.js 6.9 KB

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