sha1-browser.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  2. /* SHA-1 (FIPS 180-4) implementation in JavaScript (c) Chris Veness 2002-2016 */
  3. /* MIT Licence */
  4. /* www.movable-type.co.uk/scripts/sha1.html */
  5. /* */
  6. /* - see http://csrc.nist.gov/groups/ST/toolkit/secure_hashing.html */
  7. /* http://csrc.nist.gov/groups/ST/toolkit/examples.html */
  8. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  9. 'use strict';
  10. function f(s, x, y, z) {
  11. switch (s) {
  12. case 0: return (x & y) ^ (~x & z); // Ch()
  13. case 1: return x ^ y ^ z; // Parity()
  14. case 2: return (x & y) ^ (x & z) ^ (y & z); // Maj()
  15. case 3: return x ^ y ^ z; // Parity()
  16. }
  17. }
  18. function ROTL(x, n) {
  19. return (x<<n) | (x>>>(32-n));
  20. }
  21. var Sha1 = {};
  22. Sha1.hash = function(msg, options) {
  23. var defaults = { msgFormat: 'string', outFormat: 'hex' };
  24. var opt = Object.assign(defaults, options);
  25. switch (opt.msgFormat) {
  26. default: // default is to convert string to UTF-8, as SHA only deals with byte-streams
  27. case 'string': msg = Sha1.utf8Encode(msg); break;
  28. case 'hex-bytes':msg = Sha1.hexBytesToString(msg); break; // mostly for running tests
  29. }
  30. // constants [¤4.2.1]
  31. var K = [ 0x5a827999, 0x6ed9eba1, 0x8f1bbcdc, 0xca62c1d6 ];
  32. // initial hash value [¤5.3.1]
  33. var H = [ 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0 ];
  34. // PREPROCESSING [¤6.1.1]
  35. msg += String.fromCharCode(0x80); // add trailing '1' bit (+ 0's padding) to string [¤5.1.1]
  36. // convert string msg into 512-bit/16-integer blocks arrays of ints [¤5.2.1]
  37. var l = msg.length/4 + 2; // length (in 32-bit integers) of msg + Ô1Õ + appended length
  38. var N = Math.ceil(l/16); // number of 16-integer-blocks required to hold 'l' ints
  39. var M = new Array(N);
  40. for (var i=0; i<N; i++) {
  41. M[i] = new Array(16);
  42. for (var j=0; j<16; j++) { // encode 4 chars per integer, big-endian encoding
  43. M[i][j] = (msg.charCodeAt(i*64+j*4)<<24) | (msg.charCodeAt(i*64+j*4+1)<<16) |
  44. (msg.charCodeAt(i*64+j*4+2)<<8) | (msg.charCodeAt(i*64+j*4+3));
  45. } // note running off the end of msg is ok 'cos bitwise ops on NaN return 0
  46. }
  47. // add length (in bits) into final pair of 32-bit integers (big-endian) [¤5.1.1]
  48. // note: most significant word would be (len-1)*8 >>> 32, but since JS converts
  49. // bitwise-op args to 32 bits, we need to simulate this by arithmetic operators
  50. M[N-1][14] = ((msg.length-1)*8) / Math.pow(2, 32); M[N-1][14] = Math.floor(M[N-1][14]);
  51. M[N-1][15] = ((msg.length-1)*8) & 0xffffffff;
  52. // HASH COMPUTATION [¤6.1.2]
  53. for (var i=0; i<N; i++) {
  54. var W = new Array(80);
  55. // 1 - prepare message schedule 'W'
  56. for (var t=0; t<16; t++) W[t] = M[i][t];
  57. for (var t=16; t<80; t++) W[t] = ROTL(W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16], 1);
  58. // 2 - initialise five working variables a, b, c, d, e with previous hash value
  59. var a = H[0], b = H[1], c = H[2], d = H[3], e = H[4];
  60. // 3 - main loop (use JavaScript '>>> 0' to emulate UInt32 variables)
  61. for (var t=0; t<80; t++) {
  62. var s = Math.floor(t/20); // seq for blocks of 'f' functions and 'K' constants
  63. var T = (ROTL(a,5) + f(s,b,c,d) + e + K[s] + W[t]) >>> 0;
  64. e = d;
  65. d = c;
  66. c = ROTL(b, 30) >>> 0;
  67. b = a;
  68. a = T;
  69. }
  70. // 4 - compute the new intermediate hash value (note 'addition modulo 2^32' Ð JavaScript
  71. // '>>> 0' coerces to unsigned UInt32 which achieves modulo 2^32 addition)
  72. H[0] = (H[0]+a) >>> 0;
  73. H[1] = (H[1]+b) >>> 0;
  74. H[2] = (H[2]+c) >>> 0;
  75. H[3] = (H[3]+d) >>> 0;
  76. H[4] = (H[4]+e) >>> 0;
  77. }
  78. // convert H0..H4 to hex strings (with leading zeros)
  79. for (var h=0; h<H.length; h++) H[h] = ('00000000'+H[h].toString(16)).slice(-8);
  80. // concatenate H0..H4, with separator if required
  81. var separator = opt.outFormat=='hex-w' ? ' ' : '';
  82. return H.join(separator);
  83. };
  84. Sha1.utf8Encode = function(str) {
  85. return unescape(encodeURIComponent(str));
  86. };
  87. Sha1.hexBytesToString = function(hexStr) {
  88. hexStr = hexStr.replace(' ', ''); // allow space-separated groups
  89. var str = '';
  90. for (var i=0; i<hexStr.length; i+=2) {
  91. str += String.fromCharCode(parseInt(hexStr.slice(i, i+2), 16));
  92. }
  93. return str;
  94. };
  95. module.exports = Sha1; // CommonJs export