Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80538 views
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
13
var W = new Array(80)
14
15
function Sha1() {
16
this.init()
17
this._w = W
18
19
Hash.call(this, 64, 56)
20
}
21
22
inherits(Sha1, Hash)
23
24
Sha1.prototype.init = function () {
25
this._a = 0x67452301
26
this._b = 0xefcdab89
27
this._c = 0x98badcfe
28
this._d = 0x10325476
29
this._e = 0xc3d2e1f0
30
31
return this
32
}
33
34
/*
35
* Bitwise rotate a 32-bit number to the left.
36
*/
37
function rol(num, cnt) {
38
return (num << cnt) | (num >>> (32 - cnt));
39
}
40
41
Sha1.prototype._update = function (M) {
42
var W = this._w
43
44
var a = this._a
45
var b = this._b
46
var c = this._c
47
var d = this._d
48
var e = this._e
49
50
var j = 0, k
51
52
function calcW() { return rol(W[j - 3] ^ W[j - 8] ^ W[j - 14] ^ W[j - 16], 1) }
53
function loop(w, f) {
54
W[j] = w
55
56
var t = rol(a, 5) + f + e + w + k
57
58
e = d
59
d = c
60
c = rol(b, 30)
61
b = a
62
a = t
63
j++
64
}
65
66
k = 1518500249
67
while (j < 16) loop(M.readInt32BE(j * 4), (b & c) | ((~b) & d))
68
while (j < 20) loop(calcW(), (b & c) | ((~b) & d))
69
k = 1859775393
70
while (j < 40) loop(calcW(), b ^ c ^ d)
71
k = -1894007588
72
while (j < 60) loop(calcW(), (b & c) | (b & d) | (c & d))
73
k = -899497514
74
while (j < 80) loop(calcW(), b ^ c ^ d)
75
76
this._a = (a + this._a) | 0
77
this._b = (b + this._b) | 0
78
this._c = (c + this._c) | 0
79
this._d = (d + this._d) | 0
80
this._e = (e + this._e) | 0
81
}
82
83
Sha1.prototype._hash = function () {
84
var H = new Buffer(20)
85
86
H.writeInt32BE(this._a|0, 0)
87
H.writeInt32BE(this._b|0, 4)
88
H.writeInt32BE(this._c|0, 8)
89
H.writeInt32BE(this._d|0, 12)
90
H.writeInt32BE(this._e|0, 16)
91
92
return H
93
}
94
95
module.exports = Sha1
96
97
98