Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

switch to class #72

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 57 additions & 56 deletions sha.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
* operation was added.
*/

var inherits = require('inherits')
var Hash = require('./hash')
var Buffer = require('safe-buffer').Buffer

Expand All @@ -16,23 +15,67 @@ var K = [

var W = new Array(80)

function Sha () {
this.init()
this._w = W
class Sha extends Hash {
constructor () {
super(64, 56)

Hash.call(this, 64, 56)
}
this.init()
this._w = W
}

init() {
this._a = 0x67452301
this._b = 0xefcdab89
this._c = 0x98badcfe
this._d = 0x10325476
this._e = 0xc3d2e1f0

return this
}

inherits(Sha, Hash)
_update(M) {
var W = this._w

var a = this._a | 0
var b = this._b | 0
var c = this._c | 0
var d = this._d | 0
var e = this._e | 0

for (var i = 0; i < 16; ++i)
W[i] = M.readInt32BE(i * 4)
for (; i < 80; ++i)
W[i] = W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16]

for (var j = 0; j < 80; ++j) {
var s = ~~(j / 20)
var t = (rotl5(a) + ft(s, b, c, d) + e + W[j] + K[s]) | 0

e = d
d = c
c = rotl30(b)
b = a
a = t
}

this._a = (a + this._a) | 0
this._b = (b + this._b) | 0
this._c = (c + this._c) | 0
this._d = (d + this._d) | 0
this._e = (e + this._e) | 0
}

_hash() {
var H = Buffer.allocUnsafe(20)

Sha.prototype.init = function () {
this._a = 0x67452301
this._b = 0xefcdab89
this._c = 0x98badcfe
this._d = 0x10325476
this._e = 0xc3d2e1f0
H.writeInt32BE(this._a | 0, 0)
H.writeInt32BE(this._b | 0, 4)
H.writeInt32BE(this._c | 0, 8)
H.writeInt32BE(this._d | 0, 12)
H.writeInt32BE(this._e | 0, 16)

return this
return H
}
}

function rotl5 (num) {
Expand All @@ -49,46 +92,4 @@ function ft (s, b, c, d) {
return b ^ c ^ d
}

Sha.prototype._update = function (M) {
var W = this._w

var a = this._a | 0
var b = this._b | 0
var c = this._c | 0
var d = this._d | 0
var e = this._e | 0

for (var i = 0; i < 16; ++i) W[i] = M.readInt32BE(i * 4)
for (; i < 80; ++i) W[i] = W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16]

for (var j = 0; j < 80; ++j) {
var s = ~~(j / 20)
var t = (rotl5(a) + ft(s, b, c, d) + e + W[j] + K[s]) | 0

e = d
d = c
c = rotl30(b)
b = a
a = t
}

this._a = (a + this._a) | 0
this._b = (b + this._b) | 0
this._c = (c + this._c) | 0
this._d = (d + this._d) | 0
this._e = (e + this._e) | 0
}

Sha.prototype._hash = function () {
var H = Buffer.allocUnsafe(20)

H.writeInt32BE(this._a | 0, 0)
H.writeInt32BE(this._b | 0, 4)
H.writeInt32BE(this._c | 0, 8)
H.writeInt32BE(this._d | 0, 12)
H.writeInt32BE(this._e | 0, 16)

return H
}

module.exports = Sha