sha1.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*!
  2. * Crypto-JS v1.1.0
  3. * http://code.google.com/p/crypto-js/
  4. * Copyright (c) 2009, Jeff Mott. All rights reserved.
  5. * http://code.google.com/p/crypto-js/wiki/License
  6. */
  7. import Crypto from './crypto'
  8. // const Crypto = require('./crypto.js');
  9. (function(){
  10. // Shortcut
  11. var util = Crypto.util;
  12. // Public API
  13. var SHA1 = Crypto.SHA1 = function (message, options) {
  14. var digestbytes = util.wordsToBytes(SHA1._sha1(message));
  15. return options && options.asBytes ? digestbytes :
  16. options && options.asString ? util.bytesToString(digestbytes) :
  17. util.bytesToHex(digestbytes);
  18. };
  19. // The core
  20. SHA1._sha1 = function (message) {
  21. var m = util.stringToWords(message),
  22. l = message.length * 8,
  23. w = [],
  24. H0 = 1732584193,
  25. H1 = -271733879,
  26. H2 = -1732584194,
  27. H3 = 271733878,
  28. H4 = -1009589776;
  29. // Padding
  30. m[l >> 5] |= 0x80 << (24 - l % 32);
  31. m[((l + 64 >>> 9) << 4) + 15] = l;
  32. for (var i = 0; i < m.length; i += 16) {
  33. var a = H0,
  34. b = H1,
  35. c = H2,
  36. d = H3,
  37. e = H4;
  38. for (var j = 0; j < 80; j++) {
  39. if (j < 16) w[j] = m[i + j];
  40. else {
  41. var n = w[j-3] ^ w[j-8] ^ w[j-14] ^ w[j-16];
  42. w[j] = (n << 1) | (n >>> 31);
  43. }
  44. var t = ((H0 << 5) | (H0 >>> 27)) + H4 + (w[j] >>> 0) + (
  45. j < 20 ? (H1 & H2 | ~H1 & H3) + 1518500249 :
  46. j < 40 ? (H1 ^ H2 ^ H3) + 1859775393 :
  47. j < 60 ? (H1 & H2 | H1 & H3 | H2 & H3) - 1894007588 :
  48. (H1 ^ H2 ^ H3) - 899497514);
  49. H4 = H3;
  50. H3 = H2;
  51. H2 = (H1 << 30) | (H1 >>> 2);
  52. H1 = H0;
  53. H0 = t;
  54. }
  55. H0 += a;
  56. H1 += b;
  57. H2 += c;
  58. H3 += d;
  59. H4 += e;
  60. }
  61. return [H0, H1, H2, H3, H4];
  62. };
  63. // Package private blocksize
  64. SHA1._blocksize = 16;
  65. })();
  66. export default Crypto;