This repository has been archived by the owner on Aug 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
In Node.js the underlying hashing functions have not changed, but the browser now uses `webcrypto` instead of JavaScript based methods for `SHA1`, `SHA2-256` and `SHA2-512`. Also `SHA3` support was added in both Node.js and the browser. BREAKING CHANGE: The api was changed to be callback based, as webcrypto only exposes async methods. Closes #10
- Loading branch information
1 parent
f3b1478
commit af7e003
Showing
6 changed files
with
225 additions
and
102 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
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 |
---|---|---|
@@ -0,0 +1,66 @@ | ||
'use strict' | ||
|
||
const SHA3 = require('browserify-sha3') | ||
|
||
const webCrypto = getWebCrypto() | ||
|
||
function getWebCrypto () { | ||
if (typeof window !== 'undefined') { | ||
if (window.crypto) { | ||
return window.crypto.subtle || window.crypto.webkitSubtle | ||
} | ||
|
||
if (window.msCrypto) { | ||
return window.msCrypto.subtle | ||
} | ||
} | ||
} | ||
|
||
function webCryptoHash (type) { | ||
if (!webCrypto) { | ||
throw new Error('Please use a browser with webcrypto support') | ||
} | ||
|
||
return (data, callback) => { | ||
const res = webCrypto.digest({ name: type }, data) | ||
|
||
if (typeof res.then !== 'function') { // IE11 | ||
res.onerror = () => { | ||
callback(`Error hashing data using ${type}`) | ||
} | ||
res.oncomplete = (e) => { | ||
callback(null, e.target.result) | ||
} | ||
return | ||
} | ||
|
||
return res.then((arrbuf) => { | ||
callback(null, new Buffer(new Uint8Array(arrbuf))) | ||
}).catch((err) => callback(err)) | ||
} | ||
} | ||
|
||
function sha1 (buf, callback) { | ||
webCryptoHash('SHA-1')(buf, callback) | ||
} | ||
|
||
function sha2256 (buf, callback) { | ||
webCryptoHash('SHA-256')(buf, callback) | ||
} | ||
|
||
function sha2512 (buf, callback) { | ||
webCryptoHash('SHA-512')(buf, callback) | ||
} | ||
|
||
function sha3 (buf, callback) { | ||
const d = new SHA3.SHA3Hash() | ||
const digest = new Buffer(d.update(buf).digest('hex'), 'hex') | ||
callback(null, digest) | ||
} | ||
|
||
module.exports = { | ||
sha1: sha1, | ||
sha2256: sha2256, | ||
sha2512: sha2512, | ||
sha3: sha3 | ||
} |
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,32 @@ | ||
'use strict' | ||
|
||
const SHA3 = require('sha3') | ||
const crypto = require('crypto') | ||
|
||
function sha1 (buf, callback) { | ||
const digest = crypto.createHash('sha1').update(buf).digest() | ||
callback(null, digest) | ||
} | ||
|
||
function sha2256 (buf, callback) { | ||
const digest = crypto.createHash('sha256').update(buf).digest() | ||
callback(null, digest) | ||
} | ||
|
||
function sha2512 (buf, callback) { | ||
const digest = crypto.createHash('sha512').update(buf).digest() | ||
callback(null, digest) | ||
} | ||
|
||
function sha3 (buf, callback) { | ||
const d = new SHA3.SHA3Hash() | ||
const digest = new Buffer(d.update(buf).digest('hex'), 'hex') | ||
callback(null, digest) | ||
} | ||
|
||
module.exports = { | ||
sha1: sha1, | ||
sha2256: sha2256, | ||
sha2512: sha2512, | ||
sha3: sha3 | ||
} |
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,55 +1,75 @@ | ||
'use strict' | ||
|
||
const multihash = require('multihashes') | ||
const crypto = require('webcrypto') | ||
const crypto = require('./crypto') | ||
|
||
const mh = module.exports = Multihashing | ||
module.exports = Multihashing | ||
|
||
mh.Buffer = Buffer // for browser things | ||
function Multihashing (buf, func, length, callback) { | ||
if (typeof length === 'function') { | ||
callback = length | ||
length = undefined | ||
} | ||
|
||
Multihashing.digest(buf, func, length, (err, digest) => { | ||
if (err) { | ||
return callback(err) | ||
} | ||
|
||
function Multihashing (buf, func, length) { | ||
return multihash.encode(mh.digest(buf, func, length), func, length) | ||
callback(null, multihash.encode(digest, func, length)) | ||
}) | ||
} | ||
|
||
Multihashing.Buffer = Buffer // for browser things | ||
|
||
// expose multihash itself, to avoid silly double requires. | ||
mh.multihash = multihash | ||
Multihashing.multihash = multihash | ||
|
||
Multihashing.digest = function (buf, func, length, callback) { | ||
if (typeof length === 'function') { | ||
callback = length | ||
length = undefined | ||
} | ||
|
||
mh.digest = function (buf, func, length) { | ||
let digest = mh.createHash(func).update(buf).digest() | ||
if (!callback) { | ||
throw new Error('Missing callback') | ||
} | ||
|
||
let cb = callback | ||
if (length) { | ||
digest = digest.slice(0, length) | ||
cb = (err, digest) => { | ||
if (err) { | ||
return callback(err) | ||
} | ||
|
||
callback(null, digest.slice(0, length)) | ||
} | ||
} | ||
|
||
return digest | ||
let hash | ||
try { | ||
hash = Multihashing.createHash(func) | ||
} catch (err) { | ||
return cb(err) | ||
} | ||
|
||
hash(buf, cb) | ||
} | ||
|
||
mh.createHash = function (func, length) { | ||
Multihashing.createHash = function (func) { | ||
func = multihash.coerceCode(func) | ||
if (!mh.functions[func]) { | ||
if (!Multihashing.functions[func]) { | ||
throw new Error('multihash function ' + func + ' not yet supported') | ||
} | ||
|
||
return mh.functions[func]() | ||
return Multihashing.functions[func] | ||
} | ||
|
||
mh.functions = { | ||
0x11: gsha1, | ||
0x12: gsha2_256, | ||
0x13: gsha2_512 | ||
// 0x14: gsha3 // not implemented yet | ||
Multihashing.functions = { | ||
0x11: crypto.sha1, | ||
0x12: crypto.sha2256, | ||
0x13: crypto.sha2512, | ||
0x14: crypto.sha3 | ||
// 0x40: blake2b, // not implemented yet | ||
// 0x41: blake2s, // not implemented yet | ||
} | ||
|
||
function gsha1 () { | ||
return crypto.createHash('sha1') | ||
} | ||
|
||
function gsha2_256 () { | ||
return crypto.createHash('sha256') | ||
} | ||
|
||
function gsha2_512 () { | ||
return crypto.createHash('sha512') | ||
} |
Oops, something went wrong.