sha1.js 1.7 KB

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