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 |
|
---|
10 | var inherits = require('inherits')
|
---|
11 | var Hash = require('./hash')
|
---|
12 | var Buffer = require('safe-buffer').Buffer
|
---|
13 |
|
---|
14 | var K = [
|
---|
15 | 0x5a827999, 0x6ed9eba1, 0x8f1bbcdc | 0, 0xca62c1d6 | 0
|
---|
16 | ]
|
---|
17 |
|
---|
18 | var W = new Array(80)
|
---|
19 |
|
---|
20 | function Sha1 () {
|
---|
21 | this.init()
|
---|
22 | this._w = W
|
---|
23 |
|
---|
24 | Hash.call(this, 64, 56)
|
---|
25 | }
|
---|
26 |
|
---|
27 | inherits(Sha1, Hash)
|
---|
28 |
|
---|
29 | Sha1.prototype.init = function () {
|
---|
30 | this._a = 0x67452301
|
---|
31 | this._b = 0xefcdab89
|
---|
32 | this._c = 0x98badcfe
|
---|
33 | this._d = 0x10325476
|
---|
34 | this._e = 0xc3d2e1f0
|
---|
35 |
|
---|
36 | return this
|
---|
37 | }
|
---|
38 |
|
---|
39 | function rotl1 (num) {
|
---|
40 | return (num << 1) | (num >>> 31)
|
---|
41 | }
|
---|
42 |
|
---|
43 | function rotl5 (num) {
|
---|
44 | return (num << 5) | (num >>> 27)
|
---|
45 | }
|
---|
46 |
|
---|
47 | function rotl30 (num) {
|
---|
48 | return (num << 30) | (num >>> 2)
|
---|
49 | }
|
---|
50 |
|
---|
51 | function ft (s, b, c, d) {
|
---|
52 | if (s === 0) return (b & c) | ((~b) & d)
|
---|
53 | if (s === 2) return (b & c) | (b & d) | (c & d)
|
---|
54 | return b ^ c ^ d
|
---|
55 | }
|
---|
56 |
|
---|
57 | Sha1.prototype._update = function (M) {
|
---|
58 | var W = this._w
|
---|
59 |
|
---|
60 | var a = this._a | 0
|
---|
61 | var b = this._b | 0
|
---|
62 | var c = this._c | 0
|
---|
63 | var d = this._d | 0
|
---|
64 | var e = this._e | 0
|
---|
65 |
|
---|
66 | for (var i = 0; i < 16; ++i) W[i] = M.readInt32BE(i * 4)
|
---|
67 | for (; i < 80; ++i) W[i] = rotl1(W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16])
|
---|
68 |
|
---|
69 | for (var j = 0; j < 80; ++j) {
|
---|
70 | var s = ~~(j / 20)
|
---|
71 | var t = (rotl5(a) + ft(s, b, c, d) + e + W[j] + K[s]) | 0
|
---|
72 |
|
---|
73 | e = d
|
---|
74 | d = c
|
---|
75 | c = rotl30(b)
|
---|
76 | b = a
|
---|
77 | a = t
|
---|
78 | }
|
---|
79 |
|
---|
80 | this._a = (a + this._a) | 0
|
---|
81 | this._b = (b + this._b) | 0
|
---|
82 | this._c = (c + this._c) | 0
|
---|
83 | this._d = (d + this._d) | 0
|
---|
84 | this._e = (e + this._e) | 0
|
---|
85 | }
|
---|
86 |
|
---|
87 | Sha1.prototype._hash = function () {
|
---|
88 | var H = Buffer.allocUnsafe(20)
|
---|
89 |
|
---|
90 | H.writeInt32BE(this._a | 0, 0)
|
---|
91 | H.writeInt32BE(this._b | 0, 4)
|
---|
92 | H.writeInt32BE(this._c | 0, 8)
|
---|
93 | H.writeInt32BE(this._d | 0, 12)
|
---|
94 | H.writeInt32BE(this._e | 0, 16)
|
---|
95 |
|
---|
96 | return H
|
---|
97 | }
|
---|
98 |
|
---|
99 | module.exports = Sha1
|
---|