You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
1.5 KiB

4 years ago
  1. 'use strict';
  2. var utils = require('../utils');
  3. var common = require('../common');
  4. var shaCommon = require('./common');
  5. var rotl32 = utils.rotl32;
  6. var sum32 = utils.sum32;
  7. var sum32_5 = utils.sum32_5;
  8. var ft_1 = shaCommon.ft_1;
  9. var BlockHash = common.BlockHash;
  10. var sha1_K = [
  11. 0x5A827999, 0x6ED9EBA1,
  12. 0x8F1BBCDC, 0xCA62C1D6
  13. ];
  14. function SHA1() {
  15. if (!(this instanceof SHA1))
  16. return new SHA1();
  17. BlockHash.call(this);
  18. this.h = [
  19. 0x67452301, 0xefcdab89, 0x98badcfe,
  20. 0x10325476, 0xc3d2e1f0 ];
  21. this.W = new Array(80);
  22. }
  23. utils.inherits(SHA1, BlockHash);
  24. module.exports = SHA1;
  25. SHA1.blockSize = 512;
  26. SHA1.outSize = 160;
  27. SHA1.hmacStrength = 80;
  28. SHA1.padLength = 64;
  29. SHA1.prototype._update = function _update(msg, start) {
  30. var W = this.W;
  31. for (var i = 0; i < 16; i++)
  32. W[i] = msg[start + i];
  33. for(; i < W.length; i++)
  34. W[i] = rotl32(W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16], 1);
  35. var a = this.h[0];
  36. var b = this.h[1];
  37. var c = this.h[2];
  38. var d = this.h[3];
  39. var e = this.h[4];
  40. for (i = 0; i < W.length; i++) {
  41. var s = ~~(i / 20);
  42. var t = sum32_5(rotl32(a, 5), ft_1(s, b, c, d), e, W[i], sha1_K[s]);
  43. e = d;
  44. d = c;
  45. c = rotl32(b, 30);
  46. b = a;
  47. a = t;
  48. }
  49. this.h[0] = sum32(this.h[0], a);
  50. this.h[1] = sum32(this.h[1], b);
  51. this.h[2] = sum32(this.h[2], c);
  52. this.h[3] = sum32(this.h[3], d);
  53. this.h[4] = sum32(this.h[4], e);
  54. };
  55. SHA1.prototype._digest = function digest(enc) {
  56. if (enc === 'hex')
  57. return utils.toHex32(this.h, 'big');
  58. else
  59. return utils.split32(this.h, 'big');
  60. };