sha1.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /* from http://www.movable-type.co.uk/scripts/sha1.html */
  2. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  3. /* SHA-1 implementation in JavaScript (c) Chris Veness 2002-2014 / MIT Licence */
  4. /* */
  5. /* - see http://csrc.nist.gov/groups/ST/toolkit/secure_hashing.html */
  6. /* http://csrc.nist.gov/groups/ST/toolkit/examples.html */
  7. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  8. (function (_window) {
  9. /* jshint node:true *//* global define, escape, unescape */
  10. 'use strict';
  11. /**
  12. * SHA-1 hash function reference implementation.
  13. *
  14. * @namespace
  15. */
  16. var Sha1 = {};
  17. _window.Sha1 = Sha1;
  18. /**
  19. * Generates SHA-1 hash of string.
  20. *
  21. * @param {string} msg - (Unicode) string to be hashed.
  22. * @returns {string} Hash of msg as hex character string.
  23. */
  24. Sha1.hash = function(msg) {
  25. // convert string to UTF-8, as SHA only deals with byte-streams
  26. msg = msg.utf8Encode();
  27. // constants [§4.2.1]
  28. var K = [ 0x5a827999, 0x6ed9eba1, 0x8f1bbcdc, 0xca62c1d6 ];
  29. // PREPROCESSING
  30. msg += String.fromCharCode(0x80); // add trailing '1' bit (+ 0's padding) to string [§5.1.1]
  31. // convert string msg into 512-bit/16-integer blocks arrays of ints [§5.2.1]
  32. var l = msg.length/4 + 2; // length (in 32-bit integers) of msg + ‘1’ + appended length
  33. var N = Math.ceil(l/16); // number of 16-integer-blocks required to hold 'l' ints
  34. var M = new Array(N);
  35. for (var i=0; i<N; i++) {
  36. M[i] = new Array(16);
  37. for (var j=0; j<16; j++) { // encode 4 chars per integer, big-endian encoding
  38. M[i][j] = (msg.charCodeAt(i*64+j*4)<<24) | (msg.charCodeAt(i*64+j*4+1)<<16) |
  39. (msg.charCodeAt(i*64+j*4+2)<<8) | (msg.charCodeAt(i*64+j*4+3));
  40. } // note running off the end of msg is ok 'cos bitwise ops on NaN return 0
  41. }
  42. // add length (in bits) into final pair of 32-bit integers (big-endian) [§5.1.1]
  43. // note: most significant word would be (len-1)*8 >>> 32, but since JS converts
  44. // bitwise-op args to 32 bits, we need to simulate this by arithmetic operators
  45. M[N-1][14] = ((msg.length-1)*8) / Math.pow(2, 32); M[N-1][14] = Math.floor(M[N-1][14]);
  46. M[N-1][15] = ((msg.length-1)*8) & 0xffffffff;
  47. // set initial hash value [§5.3.1]
  48. var H0 = 0x67452301;
  49. var H1 = 0xefcdab89;
  50. var H2 = 0x98badcfe;
  51. var H3 = 0x10325476;
  52. var H4 = 0xc3d2e1f0;
  53. // HASH COMPUTATION [§6.1.2]
  54. var W = new Array(80); var a, b, c, d, e;
  55. for (var i=0; i<N; i++) {
  56. // 1 - prepare message schedule 'W'
  57. for (var t=0; t<16; t++) W[t] = M[i][t];
  58. for (var t=16; t<80; t++) W[t] = Sha1.ROTL(W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16], 1);
  59. // 2 - initialise five working variables a, b, c, d, e with previous hash value
  60. a = H0; b = H1; c = H2; d = H3; e = H4;
  61. // 3 - main loop
  62. for (var t=0; t<80; t++) {
  63. var s = Math.floor(t/20); // seq for blocks of 'f' functions and 'K' constants
  64. var T = (Sha1.ROTL(a,5) + Sha1.f(s,b,c,d) + e + K[s] + W[t]) & 0xffffffff;
  65. e = d;
  66. d = c;
  67. c = Sha1.ROTL(b, 30);
  68. b = a;
  69. a = T;
  70. }
  71. // 4 - compute the new intermediate hash value (note 'addition modulo 2^32')
  72. H0 = (H0+a) & 0xffffffff;
  73. H1 = (H1+b) & 0xffffffff;
  74. H2 = (H2+c) & 0xffffffff;
  75. H3 = (H3+d) & 0xffffffff;
  76. H4 = (H4+e) & 0xffffffff;
  77. }
  78. return Sha1.toHexStr(H0) + Sha1.toHexStr(H1) + Sha1.toHexStr(H2) +
  79. Sha1.toHexStr(H3) + Sha1.toHexStr(H4);
  80. };
  81. /**
  82. * Function 'f' [§4.1.1].
  83. * @private
  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. * Rotates left (circular left shift) value x by n positions [§3.2.5].
  95. * @private
  96. */
  97. Sha1.ROTL = function(x, n) {
  98. return (x<<n) | (x>>>(32-n));
  99. };
  100. /**
  101. * Hexadecimal representation of a number.
  102. * @private
  103. */
  104. Sha1.toHexStr = function(n) {
  105. // note can't use toString(16) as it is implementation-dependant,
  106. // and in IE returns signed numbers when used on full words
  107. var s="", v;
  108. for (var i=7; i>=0; i--) { v = (n>>>(i*4)) & 0xf; s += v.toString(16); }
  109. return s;
  110. };
  111. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  112. /** Extend String object with method to encode multi-byte string to utf8
  113. * - monsur.hossa.in/2012/07/20/utf-8-in-javascript.html */
  114. if (typeof String.prototype.utf8Encode == 'undefined') {
  115. String.prototype.utf8Encode = function() {
  116. return unescape( encodeURIComponent( this ) );
  117. };
  118. }
  119. /** Extend String object with method to decode utf8 string to multi-byte */
  120. if (typeof String.prototype.utf8Decode == 'undefined') {
  121. String.prototype.utf8Decode = function() {
  122. try {
  123. return decodeURIComponent( escape( this ) );
  124. } catch (e) {
  125. return this; // invalid UTF-8? return as-is
  126. }
  127. };
  128. }
  129. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  130. if (typeof module != 'undefined' && module.exports) module.exports = Sha1; // CommonJs export
  131. if (typeof define == 'function' && define.amd) define([], function() { return Sha1; }); // AMD
  132. })(window)