Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
"browser" in package.json to prevent "crypto" shim
- Loading branch information
1 parent
7821df5
commit 2f1f7e9
Showing
4 changed files
with
89 additions
and
90 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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
var crypto = require('crypto'); | ||
|
||
// Node.js has its own Crypto function that can handle this natively | ||
var sha256 = module.exports = function(message, options) { | ||
var c = crypto.createHash('sha256'); | ||
|
||
if (Buffer.isBuffer(message)) { | ||
c.update(message); | ||
} else if (Array.isArray(message)) { | ||
// Array of byte values | ||
c.update(new Buffer(message)); | ||
} else { | ||
// Otherwise, treat as a binary string | ||
c.update(new Buffer(message, 'binary')); | ||
} | ||
var buf = c.digest(); | ||
|
||
if (options && options.asBytes) { | ||
// Array of bytes as decimal integers | ||
var a = []; | ||
for(var i = 0; i < buf.length; i++) { | ||
a.push(buf[i]); | ||
} | ||
return a; | ||
} else if (options && options.asString) { | ||
// Binary string | ||
return buf.toString('binary'); | ||
} else { | ||
// String of hex characters | ||
return buf.toString('hex'); | ||
} | ||
} | ||
|
||
sha256.x2 = function(message, options) { | ||
return sha256(sha256(message, { asBytes:true }), options) | ||
} |
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,50 +1,53 @@ | ||
var sha256 = require('../lib/sha256') | ||
require('terst'); | ||
|
||
require('terst') | ||
['../lib/sha256.js', '../lib/nodecrypto.js'].forEach(function(sha256) { | ||
var implementation = sha256.slice(7) | ||
sha256 = require(sha256); | ||
|
||
describe('+ sha256(input)', function() { | ||
describe('> when input is a string', function() { | ||
it('should compute the sha256 hash as if it were binary string', function() { | ||
var input = "1184CD2CDD640CA42CFC3A091C51D549B2F016D454B2774019C2B2D2E08529FD" // Interpret as binary, not hex | ||
EQ (sha256(input), "1c94d91f93ec9ed6bf647c384445b329c84a042c6b3832f8ee904dc55f117342") | ||
describe(implementation, function() { | ||
describe('+ sha256(input)', function() { | ||
describe('> when input is a string', function() { | ||
it('should compute the sha256 hash as if it were binary string', function() { | ||
var input = "1184CD2CDD640CA42CFC3A091C51D549B2F016D454B2774019C2B2D2E08529FD" // Interpret as binary, not hex | ||
EQ (sha256(input), "1c94d91f93ec9ed6bf647c384445b329c84a042c6b3832f8ee904dc55f117342") | ||
|
||
var input = "hello" | ||
//printf "hello" | shasum -a 256 | ||
EQ (sha256(input), "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824") | ||
}), | ||
it('should return an array when asked', function() { | ||
var test = sha256('hello', { asBytes:true }); | ||
T (Array.isArray(test)); | ||
EQ (test.join(','), '44,242,77,186,95,176,163,14,38,232,59,42,197,185,226,158,27,22,30,92,31,167,66,94,115,4,51,98,147,139,152,36'); | ||
}), | ||
it('should return a binary string when asked', function() { | ||
var test = sha256('hello', { asString:true }); | ||
var chars = [44,242,77,186,95,176,163,14,38,232,59,42,197,185,226,158,27,22,30,92,31,167,66,94,115,4,51,98,147,139,152,36]; | ||
EQ (test, chars.reduce(function(prev, cur, index, arr) { | ||
return prev+String.fromCharCode(cur); | ||
}, '')); | ||
}) | ||
}) | ||
}) | ||
var input = "hello" | ||
//printf "hello" | shasum -a 256 | ||
EQ (sha256(input), "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824") | ||
}), | ||
it('should return an array when asked', function() { | ||
var test = sha256('hello', { asBytes:true }); | ||
T (Array.isArray(test)); | ||
EQ (test.join(','), '44,242,77,186,95,176,163,14,38,232,59,42,197,185,226,158,27,22,30,92,31,167,66,94,115,4,51,98,147,139,152,36'); | ||
}), | ||
it('should return a binary string when asked', function() { | ||
var test = sha256('hello', { asString:true }); | ||
var chars = [44,242,77,186,95,176,163,14,38,232,59,42,197,185,226,158,27,22,30,92,31,167,66,94,115,4,51,98,147,139,152,36]; | ||
EQ (test, chars.reduce(function(prev, cur, index, arr) { | ||
return prev+String.fromCharCode(cur); | ||
}, '')); | ||
}) | ||
}) | ||
}) | ||
|
||
describe('+ x2(input)', function() { | ||
describe('> when input is a string', function() { | ||
it('should compute the sha256 twice', function() { | ||
EQ (sha256.x2("hello"), "9595c9df90075148eb06860365df33584b75bff782a510c6cd4883a419833d50") | ||
}), | ||
it('should return an array when asked', function() { | ||
var test = sha256.x2('hello', { asBytes:true }); | ||
T (Array.isArray(test)); | ||
EQ (test.join(','), '149,149,201,223,144,7,81,72,235,6,134,3,101,223,51,88,75,117,191,247,130,165,16,198,205,72,131,164,25,131,61,80'); | ||
}), | ||
it('should return a binary string when asked', function() { | ||
var test = sha256.x2('hello', { asString:true }); | ||
var chars = [149,149,201,223,144,7,81,72,235,6,134,3,101,223,51,88,75,117,191,247,130,165,16,198,205,72,131,164,25,131,61,80]; | ||
EQ (test, chars.reduce(function(prev, cur, index, arr) { | ||
return prev+String.fromCharCode(cur); | ||
}, '')); | ||
}) | ||
describe('+ x2(input)', function() { | ||
describe('> when input is a string', function() { | ||
it('should compute the sha256 twice', function() { | ||
EQ (sha256.x2("hello"), "9595c9df90075148eb06860365df33584b75bff782a510c6cd4883a419833d50") | ||
}), | ||
it('should return an array when asked', function() { | ||
var test = sha256.x2('hello', { asBytes:true }); | ||
T (Array.isArray(test)); | ||
EQ (test.join(','), '149,149,201,223,144,7,81,72,235,6,134,3,101,223,51,88,75,117,191,247,130,165,16,198,205,72,131,164,25,131,61,80'); | ||
}), | ||
it('should return a binary string when asked', function() { | ||
var test = sha256.x2('hello', { asString:true }); | ||
var chars = [149,149,201,223,144,7,81,72,235,6,134,3,101,223,51,88,75,117,191,247,130,165,16,198,205,72,131,164,25,131,61,80]; | ||
EQ (test, chars.reduce(function(prev, cur, index, arr) { | ||
return prev+String.fromCharCode(cur); | ||
}, '')); | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
|
||
|