[Pkg-javascript-commits] [node-sha.js] 97/237: inject Buffer dep, so can test with different implementations
Bastien Roucariès
rouca at moszumanska.debian.org
Fri May 5 09:03:39 UTC 2017
This is an automated email from the git hooks/post-receive script.
rouca pushed a commit to branch master
in repository node-sha.js.
commit 3d8995821e8da1cbf85114589e15843b371b9285
Author: Dominic Tarr <dominic.tarr at gmail.com>
Date: Wed Jan 15 13:08:56 2014 +0700
inject Buffer dep, so can test with different implementations
---
index.js | 7 +-
sha1.js | 239 +++++++++++++++++++++++++++++---------------------------
sha256.js | 261 +++++++++++++++++++++++++++++++++-----------------------------
3 files changed, 268 insertions(+), 239 deletions(-)
diff --git a/index.js b/index.js
index d9d242c..8a0e135 100644
--- a/index.js
+++ b/index.js
@@ -4,6 +4,9 @@ var exports = module.exports = function (alg) {
return new Alg()
}
+var Buffer = require('buffer').Buffer
+var Hash = require('./hash')(Buffer)
+
exports.sha =
-exports.sha1 = require('./sha1')
-exports.sha256 = require('./sha256')
+exports.sha1 = require('./sha1')(Buffer, Hash)
+exports.sha256 = require('./sha256')(Buffer, Hash)
diff --git a/sha1.js b/sha1.js
index f52e96d..f6cdd5a 100644
--- a/sha1.js
+++ b/sha1.js
@@ -6,140 +6,151 @@
* Distributed under the BSD License
* See http://pajhome.org.uk/crypt/md5 for details.
*/
-module.exports = Sha1
+module.exports = function (Buffer, Hash) {
-var inherits = require('util').inherits
-var Hash = require('./hash')
+ var inherits = require('util').inherits
-var hexpp = require('./hexpp')
-var Buffer = require('./fakebuffer')
-inherits(Sha1, Hash)
+ var hexpp = require('./hexpp')
-var A = 0|0
-var B = 4|0
-var C = 8|0
-var D = 12|0
-var E = 16|0
+ inherits(Sha1, Hash)
-var BE = false
-var LE = true
+ var A = 0|0
+ var B = 4|0
+ var C = 8|0
+ var D = 12|0
+ var E = 16|0
-function Sha1 () {
- if(!(this instanceof Sha1)) return new Sha1()
+ var BE = false
+ var LE = true
- this._w = new Int32Array(80)
- Hash.call(this, 16*4, 14*4)
-
- this._h = new Buffer(20) //new Uint8Array(20)
- var H = this._dvH = this._h // = new DataView(this._h.buffer)
-// this._h32 = new Uint32Array(this._h.buffer)
-
- this._a = 0x67452301
- this._b = 0xefcdab89
- this._c = 0x98badcfe
- this._d = 0x10325476
- this._e = 0xc3d2e1f0
-
- this._len = 0
-}
+ function Sha1 () {
+ if(!(this instanceof Sha1)) return new Sha1()
+ this._w = new Int32Array(80)
+ Hash.call(this, 16*4, 14*4)
+
+ this._h = new Buffer(20) //new Uint8Array(20)
+ var H = this._dvH = this._h // = new DataView(this._h.buffer)
+ // this._h32 = new Uint32Array(this._h.buffer)
-// assume that array is a Uint32Array with length=16,
-// and that if it is the last block, it already has the length and the 1 bit appended.
-
-Sha1.prototype._update = function (array) {
-
- var X = this._block
-// var H = this._dvH
-
-// console.log(hexpp(X))
+ this._a = 0x67452301
+ this._b = 0xefcdab89
+ this._c = 0x98badcfe
+ this._d = 0x10325476
+ this._e = 0xc3d2e1f0
- var h = this._h
- var a, b, c, d, e, _a, _b, _c, _d, _e
+ this._len = 0
+ }
- a = _a = this._a
- b = _b = this._b
- c = _c = this._c
- d = _d = this._d
- e = _e = this._e
- var w = this._w
+ // assume that array is a Uint32Array with length=16,
+ // and that if it is the last block, it already has the length and the 1 bit appended.
- for(var j = 0; j < 80; j++) {
- var W = w[j]
- = j < 16
- ? /*X.getInt32(j*4, false)*/ X.readInt32BE(j*4)
- : rol(w[j - 3] ^ w[j - 8] ^ w[j - 14] ^ w[j - 16], 1)
- var t =
- add(
- add(rol(a, 5), sha1_ft(j, b, c, d)),
- add(add(e, W), sha1_kt(j))
- );
+ var isDV = new Buffer(1) instanceof DataView
+ function readInt32BE (X, i) {
+ return isDV
+ ? X.getInt32(i, false)
+ : X.readInt32BE(i)
+ }
- e = d
- d = c
- c = rol(b, 30)
- b = a
- a = t
+ Sha1.prototype._update = function (array) {
+
+ var X = this._block
+ // var H = this._dvH
+
+ // console.log(hexpp(X))
+
+ var h = this._h
+ var a, b, c, d, e, _a, _b, _c, _d, _e
+
+ a = _a = this._a
+ b = _b = this._b
+ c = _c = this._c
+ d = _d = this._d
+ e = _e = this._e
+
+ var w = this._w
+
+ for(var j = 0; j < 80; j++) {
+ var W = w[j]
+ = j < 16
+ //? X.getInt32(j*4, false)
+ //? readInt32BE(X, j*4) //*/ X.readInt32BE(j*4) //*/
+ ? X.readInt32BE(j*4)
+ : rol(w[j - 3] ^ w[j - 8] ^ w[j - 14] ^ w[j - 16], 1)
+
+ var t =
+ add(
+ add(rol(a, 5), sha1_ft(j, b, c, d)),
+ add(add(e, W), sha1_kt(j))
+ );
+
+ e = d
+ d = c
+ c = rol(b, 30)
+ b = a
+ a = t
+ }
+
+ this._a = add(a, _a)
+ this._b = add(b, _b)
+ this._c = add(c, _c)
+ this._d = add(d, _d)
+ this._e = add(e, _e)
}
- this._a = add(a, _a)
- this._b = add(b, _b)
- this._c = add(c, _c)
- this._d = add(d, _d)
- this._e = add(e, _e)
-}
+ Sha1.prototype._hash = function () {
+ var H = this._dvH //new DataView(new ArrayBuffer(20))
-Sha1.prototype._hash = function () {
- var H = this._dvH //new DataView(new ArrayBuffer(20))
+ //console.log(this._a|0, this._b|0, this._c|0, this._d|0, this._e|0)
+ H.writeInt32BE(this._a|0, A)
+ H.writeInt32BE(this._b|0, B)
+ H.writeInt32BE(this._c|0, C)
+ H.writeInt32BE(this._d|0, D)
+ H.writeInt32BE(this._e|0, E)
+ return H
+ }
- //console.log(this._a|0, this._b|0, this._c|0, this._d|0, this._e|0)
- H.writeInt32BE(this._a|0, A)
- H.writeInt32BE(this._b|0, B)
- H.writeInt32BE(this._c|0, C)
- H.writeInt32BE(this._d|0, D)
- H.writeInt32BE(this._e|0, E)
- return H
-}
+ /*
+ * Perform the appropriate triplet combination function for the current
+ * iteration
+ */
+ function sha1_ft(t, b, c, d) {
+ if(t < 20) return (b & c) | ((~b) & d);
+ if(t < 40) return b ^ c ^ d;
+ if(t < 60) return (b & c) | (b & d) | (c & d);
+ return b ^ c ^ d;
+ }
-/*
- * Perform the appropriate triplet combination function for the current
- * iteration
- */
-function sha1_ft(t, b, c, d) {
- if(t < 20) return (b & c) | ((~b) & d);
- if(t < 40) return b ^ c ^ d;
- if(t < 60) return (b & c) | (b & d) | (c & d);
- return b ^ c ^ d;
-}
+ /*
+ * Determine the appropriate additive constant for the current iteration
+ */
+ function sha1_kt(t) {
+ return (t < 20) ? 1518500249 : (t < 40) ? 1859775393 :
+ (t < 60) ? -1894007588 : -899497514;
+ }
-/*
- * Determine the appropriate additive constant for the current iteration
- */
-function sha1_kt(t) {
- return (t < 20) ? 1518500249 : (t < 40) ? 1859775393 :
- (t < 60) ? -1894007588 : -899497514;
-}
+ /*
+ * Add integers, wrapping at 2^32. This uses 16-bit operations internally
+ * to work around bugs in some JS interpreters.
+ * //dominictarr: this is 10 years old, so maybe this can be dropped?)
+ *
+ */
+ function add(x, y) {
+ return (x + y ) | 0
+ //lets see how this goes on testling.
+ // var lsw = (x & 0xFFFF) + (y & 0xFFFF);
+ // var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
+ // return (msw << 16) | (lsw & 0xFFFF);
+ }
-/*
- * Add integers, wrapping at 2^32. This uses 16-bit operations internally
- * to work around bugs in some JS interpreters.
- * //dominictarr: this is 10 years old, so maybe this can be dropped?)
- *
- */
-function add(x, y) {
- return (x + y ) | 0
-//lets see how this goes on testling.
-// var lsw = (x & 0xFFFF) + (y & 0xFFFF);
-// var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
-// return (msw << 16) | (lsw & 0xFFFF);
-}
+ /*
+ * Bitwise rotate a 32-bit number to the left.
+ */
+ function rol(num, cnt) {
+ return (num << cnt) | (num >>> (32 - cnt));
+ }
-/*
- * Bitwise rotate a 32-bit number to the left.
- */
-function rol(num, cnt) {
- return (num << cnt) | (num >>> (32 - cnt));
+ return Sha1
}
-
diff --git a/sha256.js b/sha256.js
index 8d96c1a..e679c7d 100644
--- a/sha256.js
+++ b/sha256.js
@@ -7,146 +7,161 @@
*
*/
-var Hash = require('./hash')
var inherits = require('util').inherits
var BE = false
var LE = true
var hexpp = require('./hexpp')
var to = require('bops/typedarray/from')
var u = require('./util')
-//var assert = require('assert')
-
-module.exports = Sha256
-
-var K = [
- 0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5,
- 0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5,
- 0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3,
- 0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174,
- 0xE49B69C1, 0xEFBE4786, 0x0FC19DC6, 0x240CA1CC,
- 0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA,
- 0x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7,
- 0xC6E00BF3, 0xD5A79147, 0x06CA6351, 0x14292967,
- 0x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13,
- 0x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85,
- 0xA2BFE8A1, 0xA81A664B, 0xC24B8B70, 0xC76C51A3,
- 0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070,
- 0x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5,
- 0x391C0CB3, 0x4ED8AA4A, 0x5B9CCA4F, 0x682E6FF3,
- 0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208,
- 0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2
- ]
-
-inherits(Sha256, Hash)
-
-function Sha256() {
- this._data = new Uint32Array([
- 0x67e6096a, 0x85ae67bb, 0x72f36e3c, 0x3af54fa5,
- 0x7f520e51, 0x8c68059b, 0xabd9831f, 0x19cde05b
- ])
-
- this._a = 0x6a09e667|0
- this._b = 0xbb67ae85|0
- this._c = 0x3c6ef372|0
- this._d = 0xa54ff53a|0
- this._e = 0x510e527f|0
- this._f = 0x9b05688c|0
- this._g = 0x1f83d9ab|0
- this._h = 0x5be0cd19|0
-
- var DV = this._dvH = new DataView(this._data.buffer)
- this._w = new Array(64) //new Uint32Array(64);
-
- Hash.call(this, 16*4, 14*4)
-};
-
-var safe_add = function(x, y) {
- var lsw = (x & 0xFFFF) + (y & 0xFFFF);
- var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
- return (msw << 16) | (lsw & 0xFFFF);
-}
-
-function S (X, n) {
- return (X >>> n) | (X << (32 - n));
-}
-function R (X, n) {
- return (X >>> n);
-}
-
-function Ch (x, y, z) {
- return ((x & y) ^ ((~x) & z));
-}
-
-function Maj (x, y, z) {
- return ((x & y) ^ (x & z) ^ (y & z));
-}
+module.exports = function (Buffer, Hash) {
+
+ var K = [
+ 0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5,
+ 0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5,
+ 0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3,
+ 0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174,
+ 0xE49B69C1, 0xEFBE4786, 0x0FC19DC6, 0x240CA1CC,
+ 0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA,
+ 0x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7,
+ 0xC6E00BF3, 0xD5A79147, 0x06CA6351, 0x14292967,
+ 0x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13,
+ 0x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85,
+ 0xA2BFE8A1, 0xA81A664B, 0xC24B8B70, 0xC76C51A3,
+ 0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070,
+ 0x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5,
+ 0x391C0CB3, 0x4ED8AA4A, 0x5B9CCA4F, 0x682E6FF3,
+ 0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208,
+ 0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2
+ ]
+
+ inherits(Sha256, Hash)
+
+ function Sha256() {
+ this._data = new Buffer(32)
+
+ //new Uint32Array([
+ // 0x67e6096a, 0x85ae67bb, 0x72f36e3c, 0x3af54fa5,
+ // 0x7f520e51, 0x8c68059b, 0xabd9831f, 0x19cde05b
+ // ])
+
+ this._a = 0x6a09e667|0
+ this._b = 0xbb67ae85|0
+ this._c = 0x3c6ef372|0
+ this._d = 0xa54ff53a|0
+ this._e = 0x510e527f|0
+ this._f = 0x9b05688c|0
+ this._g = 0x1f83d9ab|0
+ this._h = 0x5be0cd19|0
+
+ //var DV = this._dvH = new DataView(this._data.buffer)
+
+ this._w = new Array(64) //new Uint32Array(64);
+
+ Hash.call(this, 16*4, 14*4)
+ };
+
+ var safe_add = function(x, y) {
+ var lsw = (x & 0xFFFF) + (y & 0xFFFF);
+ var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
+ return (msw << 16) | (lsw & 0xFFFF);
+ }
-function Sigma0256 (x) {
- return (S(x, 2) ^ S(x, 13) ^ S(x, 22));
-}
+ function S (X, n) {
+ return (X >>> n) | (X << (32 - n));
+ }
-function Sigma1256 (x) {
- return (S(x, 6) ^ S(x, 11) ^ S(x, 25));
-}
+ function R (X, n) {
+ return (X >>> n);
+ }
-function Gamma0256 (x) {
- return (S(x, 7) ^ S(x, 18) ^ R(x, 3));
-}
+ function Ch (x, y, z) {
+ return ((x & y) ^ ((~x) & z));
+ }
-function Gamma1256 (x) {
- return (S(x, 17) ^ S(x, 19) ^ R(x, 10));
-}
+ function Maj (x, y, z) {
+ return ((x & y) ^ (x & z) ^ (y & z));
+ }
-Sha256.prototype._update = function(m) {
- var M = this._dv
- var W = this._w
- var a, b, c, d, e, f, g, h
- var T1, T2
-
- a = this._a | 0
- b = this._b | 0
- c = this._c | 0
- d = this._d | 0
- e = this._e | 0
- f = this._f | 0
- g = this._g | 0
- h = this._h | 0
-
- for (var j = 0; j < 64; j++) {
- var w = W[j] = j < 16
- ? M.getUint32(j * 4, BE)
- : Gamma1256(W[j - 2]) + W[j - 7] + Gamma0256(W[j - 15]) + W[j - 16]
-
- T1 = h + Sigma1256(e) + Ch(e, f, g) + K[j] + w
-
- T2 = Sigma0256(a) + Maj(a, b, c);
- h = g; g = f; f = e; e = d + T1; d = c; c = b; b = a; a = T1 + T2;
+ function Sigma0256 (x) {
+ return (S(x, 2) ^ S(x, 13) ^ S(x, 22));
}
- 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
- this._f = (f + this._f) | 0
- this._g = (g + this._g) | 0
- this._h = (h + this._h) | 0
+ function Sigma1256 (x) {
+ return (S(x, 6) ^ S(x, 11) ^ S(x, 25));
+ }
-};
+ function Gamma0256 (x) {
+ return (S(x, 7) ^ S(x, 18) ^ R(x, 3));
+ }
-Sha256.prototype._hash = function () {
+ function Gamma1256 (x) {
+ return (S(x, 17) ^ S(x, 19) ^ R(x, 10));
+ }
- var H = this._dvH
+ Sha256.prototype._update = function(m) {
+ var M = this._block
+ var W = this._w
+ var a, b, c, d, e, f, g, h
+ var T1, T2
+
+ a = this._a | 0
+ b = this._b | 0
+ c = this._c | 0
+ d = this._d | 0
+ e = this._e | 0
+ f = this._f | 0
+ g = this._g | 0
+ h = this._h | 0
+
+ for (var j = 0; j < 64; j++) {
+ var w = W[j] = j < 16
+ //? M.getUint32(j * 4, BE)
+ ? M.readInt32BE(j * 4)
+ : Gamma1256(W[j - 2]) + W[j - 7] + Gamma0256(W[j - 15]) + W[j - 16]
+
+ T1 = h + Sigma1256(e) + Ch(e, f, g) + K[j] + w
+
+ T2 = Sigma0256(a) + Maj(a, b, c);
+ h = g; g = f; f = e; e = d + T1; d = c; c = b; b = a; a = T1 + T2;
+ }
+
+ 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
+ this._f = (f + this._f) | 0
+ this._g = (g + this._g) | 0
+ this._h = (h + this._h) | 0
+
+ };
+
+ Sha256.prototype._hash = function () {
+
+ var H = this._data
+
+ // H.setUint32( 0, this._a, BE);
+ // H.setUint32( 4, this._b, BE);
+ // H.setUint32( 8, this._c, BE);
+ // H.setUint32(12, this._d, BE);
+ // H.setUint32(16, this._e, BE);
+ // H.setUint32(20, this._f, BE);
+ // H.setUint32(24, this._g, BE);
+ // H.setUint32(28, this._h, BE);
+ //
+ H.writeInt32BE(this._a, 0)
+ H.writeInt32BE(this._b, 4)
+ H.writeInt32BE(this._c, 8)
+ H.writeInt32BE(this._d, 12)
+ H.writeInt32BE(this._e, 16)
+ H.writeInt32BE(this._f, 20)
+ H.writeInt32BE(this._g, 24)
+ H.writeInt32BE(this._h, 28)
+
+ return H
+ }
- H.setUint32( 0, this._a, BE);
- H.setUint32( 4, this._b, BE);
- H.setUint32( 8, this._c, BE);
- H.setUint32(12, this._d, BE);
- H.setUint32(16, this._e, BE);
- H.setUint32(20, this._f, BE);
- H.setUint32(24, this._g, BE);
- H.setUint32(28, this._h, BE);
+ return Sha256
- return H
}
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-sha.js.git
More information about the Pkg-javascript-commits
mailing list