-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use isomorphic-webcrypto (enable React Native). #43
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Copyright (c) 2021 Digital Bazaar, Inc. All rights reserved. | ||
*/ | ||
'use strict'; | ||
|
||
const crypto = require('isomorphic-webcrypto'); | ||
require('fast-text-encoding'); | ||
|
||
module.exports = class MessageDigest { | ||
/** | ||
* Creates a new MessageDigest. | ||
* | ||
* @param algorithm the algorithm to use. | ||
*/ | ||
constructor(algorithm) { | ||
// check if crypto.subtle is available | ||
// check is here rather than top-level to only fail if class is used | ||
if(!(crypto && crypto.subtle)) { | ||
throw new Error('crypto.subtle not found.'); | ||
} | ||
if(algorithm === 'sha256') { | ||
this.algorithm = {name: 'SHA-256'}; | ||
} else if(algorithm === 'sha1') { | ||
this.algorithm = {name: 'SHA-1'}; | ||
} else { | ||
throw new Error(`Unsupported algorithm "${algorithm}".`); | ||
} | ||
this._content = ''; | ||
} | ||
|
||
update(msg) { | ||
this._content += msg; | ||
} | ||
|
||
async digest() { | ||
const data = new TextEncoder().encode(this._content); | ||
const buffer = new Uint8Array( | ||
await crypto.subtle.digest(this.algorithm, data)); | ||
// return digest in hex | ||
let hex = ''; | ||
for(let i = 0; i < buffer.length; ++i) { | ||
hex += buffer[i].toString(16).padStart(2, '0'); | ||
} | ||
return hex; | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -26,7 +26,8 @@ | |||||||||||||||||||||||||
"lib/*.js" | ||||||||||||||||||||||||||
], | ||||||||||||||||||||||||||
"dependencies": { | ||||||||||||||||||||||||||
"setimmediate": "^1.0.5" | ||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @davidlehn -- can you speak to this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Restore the dependency, it's needed. See the |
||||||||||||||||||||||||||
"fast-text-encoding": "^1.0.3", | ||||||||||||||||||||||||||
"isomorphic-webcrypto": "^2.3.8" | ||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||
"devDependencies": { | ||||||||||||||||||||||||||
"benchmark": "^2.1.4", | ||||||||||||||||||||||||||
|
@@ -62,6 +63,14 @@ | |||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||
"browser": { | ||||||||||||||||||||||||||
"./lib/MessageDigest.js": "./lib/MessageDigest-browser.js", | ||||||||||||||||||||||||||
"./lib/MessageDigest-reactnative.js": false, | ||||||||||||||||||||||||||
"fast-text-encoding": false, | ||||||||||||||||||||||||||
"isomorphic-webcrypto": false, | ||||||||||||||||||||||||||
"rdf-canonize-native": false | ||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||
"react-native": { | ||||||||||||||||||||||||||
"./lib/MessageDigest.js": "./lib/MessageDigest-reactnative.js", | ||||||||||||||||||||||||||
"./lib/MessageDigest-browser.js": false, | ||||||||||||||||||||||||||
Comment on lines
+66
to
+73
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this can be simpler. No need to mask out deps that will never be required. The
Suggested change
|
||||||||||||||||||||||||||
"rdf-canonize-native": false | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's weird that this was put here, as I think it may have been required for some other reason (i.e., this function isn't supported in browsers but we use it elsewhere). @davidlehn, can you speak to this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
setImmediate
is used in the code, so a polyfill is needed. Seelib/URDNA2015.js
. This polyfill replaced custom code and was added in:4e10528
The browser testing of this package is indirect through jsonld.js, to avoid complexity of duplicating the testing framework. A side effect of that seems to be confusion as to why this is needed. jsonld.js tests will fail without this.
I'm not sure why it's in this file in particular. I probably thought it easier to just use this file rather than create a new empty node file and a browser only file with just that require.
Please restore. If a comment or moving to a new browser only file would help avoid confusion, that's fine.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahhh got it, thanks!!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@davidlehn interestingly, requiring setimmediate causes to fail a build in my case. I could provide a repro if you are interested and would be interested in understanding why.