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.
- Loading branch information
Showing
7 changed files
with
253 additions
and
121 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
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 | ||
} |
Oops, something went wrong.