-
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add a new noble-based implementation
- Loading branch information
Showing
5 changed files
with
150 additions
and
53 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,30 +1,9 @@ | ||
'use strict' | ||
var inherits = require('inherits') | ||
var MD5 = require('md5.js') | ||
var RIPEMD160 = require('ripemd160') | ||
var sha = require('sha.js') | ||
var Base = require('cipher-base') | ||
|
||
function Hash (hash) { | ||
Base.call(this, 'digest') | ||
|
||
this._hash = hash | ||
} | ||
|
||
inherits(Hash, Base) | ||
|
||
Hash.prototype._update = function (data) { | ||
this._hash.update(data) | ||
} | ||
|
||
Hash.prototype._final = function () { | ||
return this._hash.digest() | ||
} | ||
|
||
module.exports = function createHash (alg) { | ||
alg = alg.toLowerCase() | ||
if (alg === 'md5') return new MD5() | ||
if (alg === 'rmd160' || alg === 'ripemd160') return new RIPEMD160() | ||
|
||
return new Hash(sha(alg)) | ||
if (typeof BigInt !== 'undefined') { | ||
try { | ||
module.exports = require('./browser.noble.js') | ||
} catch (err) { | ||
module.exports = require('./browser.old.js') | ||
} | ||
} else { | ||
module.exports = require('./browser.old.js') | ||
} |
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,58 @@ | ||
'use strict' | ||
var sha1 = require('@noble/hashes/sha1') | ||
var ripemd160 = require('@noble/hashes/ripemd160') | ||
var sha2 = require('@noble/hashes/sha2') | ||
var sha3 = require('@noble/hashes/sha3') | ||
var blake2b = require('@noble/hashes/blake2b') | ||
var blake2s = require('@noble/hashes/blake2s') | ||
var inherits = require('inherits') | ||
var MD5 = require('md5.js') | ||
var Base = require('cipher-base') | ||
|
||
function Hash (hash) { | ||
Base.call(this, 'digest') | ||
|
||
this._hash = hash | ||
} | ||
|
||
inherits(Hash, Base) | ||
|
||
Hash.prototype._update = function (data) { | ||
this._hash.update(data) | ||
} | ||
|
||
Hash.prototype._final = function () { | ||
return this._hash.digest() | ||
} | ||
|
||
const hashes = { | ||
// Supported by browser.old.js | ||
sha1: sha1.sha1, | ||
sha224: sha2.sha224, | ||
sha256: sha2.sha256, | ||
sha384: sha2.sha384, | ||
sha512: sha2.sha512, | ||
ripemd160: ripemd160.ripemd160, | ||
rmd160: ripemd160.ripemd160, | ||
|
||
// Not supported by browser.old.js (until sha.js updates?) | ||
'sha512-224': sha2.sha512_224, // for browser.old.js: https://github.com/browserify/sha.js/pull/67 | ||
'sha512-256': sha2.sha512_256, // for browser.old.js: https://github.com/browserify/sha.js/pull/67 | ||
'sha3-224': sha3.sha3_224, | ||
'sha3-256': sha3.sha3_256, | ||
'sha3-384': sha3.sha3_384, | ||
'sha3-512': sha3.sha3_512, | ||
blake2b512: blake2b.blake2b, // 512 is the default size | ||
blake2s256: blake2s.blake2s // 256 is the default size | ||
} | ||
|
||
function getNobleHash (alg) { | ||
if (Object.prototype.hasOwnProperty.call(hashes, alg)) return hashes[alg] | ||
throw new Error('Digest method not supported') | ||
} | ||
|
||
module.exports = function createHash (alg) { | ||
alg = alg.toLowerCase() | ||
if (alg === 'md5') return new MD5() | ||
return new Hash(getNobleHash(alg).create()) | ||
} |
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' | ||
var inherits = require('inherits') | ||
var MD5 = require('md5.js') | ||
var RIPEMD160 = require('ripemd160') | ||
var sha = require('sha.js') | ||
var Base = require('cipher-base') | ||
|
||
function Hash (hash) { | ||
Base.call(this, 'digest') | ||
|
||
this._hash = hash | ||
} | ||
|
||
inherits(Hash, Base) | ||
|
||
Hash.prototype._update = function (data) { | ||
this._hash.update(data) | ||
} | ||
|
||
Hash.prototype._final = function () { | ||
return this._hash.digest() | ||
} | ||
|
||
module.exports = function createHash (alg) { | ||
alg = alg.toLowerCase() | ||
if (alg === 'md5') return new MD5() | ||
if (alg === 'rmd160' || alg === 'ripemd160') return new RIPEMD160() | ||
|
||
return new Hash(sha(alg)) | ||
} |
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