Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * A JavaScript implementation of the Secure Hash Algorithm, SHA-1, as defined
  3. * in FIPS PUB 180-1
  4. * Version 2.1a Copyright Paul Johnston 2000 - 2002.
  5. * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
  6. * Distributed under the BSD License
  7. * See http://pajhome.org.uk/crypt/md5 for details.
  8. */
  9. var inherits = require('inherits')
  10. var Hash = require('./hash')
  11. var Buffer = require('safe-buffer').Buffer
  12. var K = [
  13. 0x5a827999, 0x6ed9eba1, 0x8f1bbcdc | 0, 0xca62c1d6 | 0
  14. ]
  15. var W = new Array(80)
  16. function Sha1 () {
  17. this.init()
  18. this._w = W
  19. Hash.call(this, 64, 56)
  20. }
  21. inherits(Sha1, Hash)
  22. Sha1.prototype.init = function () {
  23. this._a = 0x67452301
  24. this._b = 0xefcdab89
  25. this._c = 0x98badcfe
  26. this._d = 0x10325476
  27. this._e = 0xc3d2e1f0
  28. return this
  29. }
  30. function rotl1 (num) {
  31. return (num << 1) | (num >>> 31)
  32. }
  33. function rotl5 (num) {
  34. return (num << 5) | (num >>> 27)
  35. }
  36. function rotl30 (num) {
  37. return (num << 30) | (num >>> 2)
  38. }
  39. function ft (s, b, c, d) {
  40. if (s === 0) return (b & c) | ((~b) & d)
  41. if (s === 2) return (b & c) | (b & d) | (c & d)
  42. return b ^ c ^ d
  43. }
  44. Sha1.prototype._update = function (M) {
  45. var W = this._w
  46. var a = this._a | 0
  47. var b = this._b | 0
  48. var c = this._c | 0
  49. var d = this._d | 0
  50. var e = this._e | 0
  51. for (var i = 0; i < 16; ++i) W[i] = M.readInt32BE(i * 4)
  52. for (; i < 80; ++i) W[i] = rotl1(W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16])
  53. for (var j = 0; j < 80; ++j) {
  54. var s = ~~(j / 20)
  55. var t = (rotl5(a) + ft(s, b, c, d) + e + W[j] + K[s]) | 0
  56. e = d
  57. d = c
  58. c = rotl30(b)
  59. b = a
  60. a = t
  61. }
  62. this._a = (a + this._a) | 0
  63. this._b = (b + this._b) | 0
  64. this._c = (c + this._c) | 0
  65. this._d = (d + this._d) | 0
  66. this._e = (e + this._e) | 0
  67. }
  68. Sha1.prototype._hash = function () {
  69. var H = Buffer.allocUnsafe(20)
  70. H.writeInt32BE(this._a | 0, 0)
  71. H.writeInt32BE(this._b | 0, 4)
  72. H.writeInt32BE(this._c | 0, 8)
  73. H.writeInt32BE(this._d | 0, 12)
  74. H.writeInt32BE(this._e | 0, 16)
  75. return H
  76. }
  77. module.exports = Sha1