-
-
Notifications
You must be signed in to change notification settings - Fork 60
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
use hash-base #36
use hash-base #36
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
node_modules | ||
|
||
npm-debug.log |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ node_js: | |
- "0.12" | ||
- "4" | ||
- "5" | ||
- "6" | ||
env: | ||
matrix: | ||
- TEST_SUITE=unit | ||
|
This file was deleted.
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,15 @@ | ||
var exports = module.exports = function SHA (algorithm) { | ||
algorithm = algorithm.toLowerCase() | ||
algorithm = algorithm.toUpperCase() | ||
|
||
var Algorithm = exports[algorithm] | ||
if (!Algorithm) throw new Error(algorithm + ' is not supported (we accept pull requests)') | ||
|
||
return new Algorithm() | ||
} | ||
|
||
exports.sha = require('./sha') | ||
exports.sha1 = require('./sha1') | ||
exports.sha224 = require('./sha224') | ||
exports.sha256 = require('./sha256') | ||
exports.sha384 = require('./sha384') | ||
exports.sha512 = require('./sha512') | ||
exports.SHA = require('./lib/sha') | ||
exports.SHA1 = require('./lib/sha1') | ||
exports.SHA224 = require('./lib/sha224') | ||
exports.SHA256 = require('./lib/sha256') | ||
exports.SHA384 = require('./lib/sha384') | ||
exports.SHA512 = require('./lib/sha512') | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
'use strict' | ||
exports.block64 = function () { | ||
this._block[this._blockOffset++] = 0x80 | ||
if (this._blockOffset > 56) { | ||
this._block.fill(0, this._blockOffset, 64) | ||
this._update() | ||
this._blockOffset = 0 | ||
} | ||
|
||
this._block.fill(0, this._blockOffset, 56) | ||
this._block.writeUInt32BE(this._length[1], 56) | ||
this._block.writeUInt32BE(this._length[0], 60) | ||
this._update() | ||
} | ||
|
||
exports.block128 = function () { | ||
this._block[this._blockOffset++] = 0x80 | ||
if (this._blockOffset > 112) { | ||
this._block.fill(0, this._blockOffset, 128) | ||
this._update() | ||
this._blockOffset = 0 | ||
} | ||
|
||
this._block.fill(0, this._blockOffset, 112) | ||
this._block.writeUInt32BE(this._length[3], 112) | ||
this._block.writeUInt32BE(this._length[2], 116) | ||
this._block.writeUInt32BE(this._length[1], 120) | ||
this._block.writeUInt32BE(this._length[0], 124) | ||
this._update() | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
'use strict' | ||
var inherits = require('inherits') | ||
var HashBase = require('hash-base') | ||
var padding = require('./padding') | ||
|
||
var W = new Array(80) | ||
var K = [0x5a827999, 0x6ed9eba1, 0x8f1bbcdc, 0xca62c1d6] | ||
|
||
function SHA () { | ||
HashBase.call(this, 64) | ||
|
||
this._a = 0x67452301 | ||
this._b = 0xefcdab89 | ||
this._c = 0x98badcfe | ||
this._d = 0x10325476 | ||
this._e = 0xc3d2e1f0 | ||
} | ||
|
||
inherits(SHA, HashBase) | ||
|
||
SHA.prototype._expandMessage = function (W) { | ||
for (var i = 0; i < 16; ++i) W[i] = this._block.readInt32BE(i * 4) | ||
for (; i < 80; ++i) W[i] = W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where did this come from? |
||
} | ||
|
||
SHA.prototype._update = function () { | ||
this._expandMessage(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 < 80; ++i) { | ||
var t | ||
if (i < 20) { | ||
t = (e + fn0(b, c, d) + rotl5(a) + W[i] + K[0]) | 0 | ||
} else if (i < 40) { | ||
t = (e + fn1(b, c, d) + rotl5(a) + W[i] + K[1]) | 0 | ||
} else if (i < 60) { | ||
t = (e + fn2(b, c, d) + rotl5(a) + W[i] + K[2]) | 0 | ||
} else { // i < 80 | ||
t = (e + fn1(b, c, d) + rotl5(a) + W[i] + K[3]) | 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._digest = function () { | ||
padding.block64.call(this) | ||
|
||
var buffer = new Buffer(20) | ||
buffer.writeInt32BE(this._a, 0) | ||
buffer.writeInt32BE(this._b, 4) | ||
buffer.writeInt32BE(this._c, 8) | ||
buffer.writeInt32BE(this._d, 12) | ||
buffer.writeInt32BE(this._e, 16) | ||
return buffer | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we opt for definition functions before use? |
||
function rotl5 (num) { | ||
return (num << 5) | (num >>> 27) | ||
} | ||
|
||
function rotl30 (num) { | ||
return (num << 30) | (num >>> 2) | ||
} | ||
|
||
function fn0 (b, c, d) { | ||
return (b & c) | ((~b) & d) | ||
} | ||
|
||
function fn1 (b, c, d) { | ||
return b ^ c ^ d | ||
} | ||
|
||
function fn2 (b, c, d) { | ||
return (b & c) | (b & d) | (c & d) | ||
} | ||
|
||
module.exports = SHA |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sigh