-
-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from crypto-browserify/feature/hash-base
use hash-base
- Loading branch information
Showing
24 changed files
with
543 additions
and
984 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
node_modules | ||
|
||
npm-debug.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
} | ||
|
||
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 | ||
} | ||
|
||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
'use strict' | ||
var inherits = require('inherits') | ||
var HashBase = require('hash-base') | ||
var SHA = require('./sha') | ||
|
||
function SHA1 () { | ||
SHA.call(this) | ||
} | ||
|
||
inherits(SHA1, HashBase) | ||
|
||
SHA1.prototype._expandMessage = function (W) { | ||
for (var i = 0; i < 16; ++i) W[i] = this._block.readInt32BE(i * 4) | ||
for (; i < 80; ++i) W[i] = rotl1(W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16]) | ||
} | ||
|
||
SHA1.prototype._update = SHA.prototype._update | ||
SHA1.prototype._digest = SHA.prototype._digest | ||
|
||
function rotl1 (num) { | ||
return (num << 1) | (num >>> 31) | ||
} | ||
|
||
module.exports = SHA1 |
Oops, something went wrong.