Skip to content
This repository has been archived by the owner on Aug 24, 2021. It is now read-only.

Commit

Permalink
feat: add double SHA2-256 hashing
Browse files Browse the repository at this point in the history
The double SHA2-256 is e.g. used for Bitcoin and Zcash.

Fixes #25.
  • Loading branch information
vmx authored and daviddias committed Feb 27, 2018
1 parent d1fe2e6 commit cb3779d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
12 changes: 11 additions & 1 deletion src/crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ const toBuf = utils.toBuf
const fromString = utils.fromString
const fromNumberTo32BitBuf = utils.fromNumberTo32BitBuf

const dblSha2256 = (buf, cb) => {
sha.sha2256(buf, (err, firstHash) => {
if (err) {
cb(err)
}
sha.sha2256((Buffer.from(firstHash)), cb)
})
}

module.exports = {
sha1: sha.sha1,
sha2256: sha.sha2256,
Expand All @@ -27,5 +36,6 @@ module.exports = {
keccak512: toCallback(toBuf(sha3.keccak_512)),
murmur3128: toCallback(toBuf(fromString(murmur3.x64.hash128))),
murmur332: toCallback(fromNumberTo32BitBuf(fromString(murmur3.x86.hash32))),
addBlake: require('./blake')
addBlake: require('./blake'),
dblSha2256: dblSha2256
}
4 changes: 3 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,9 @@ Multihashing.functions = {
// murmur3-128
0x22: crypto.murmur3128,
// murmur3-32
0x23: crypto.murmur332
0x23: crypto.murmur332,
// dbl-sha2-256
0x56: crypto.dblSha2256
}

// add blake functions
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/encodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,8 @@ module.exports = [[
'beep boop',
'blake2s-256',
'e0e402204542eaca484e4311def8af74b546edd7fceb49eeb3cdcfd8a4a72ed0dc81d4c0'
], [
'beep boop',
'dbl-sha2-256',
'56209cd9115d76945c2455b1450295b05f4edeba2e7286bc24c23e266b48faf578c0'
]]

0 comments on commit cb3779d

Please sign in to comment.