-
Notifications
You must be signed in to change notification settings - Fork 619
Feature/sha256 #18
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
Feature/sha256 #18
Conversation
private readonly hash: Hash; | ||
|
||
constructor(secret?: SourceData) { | ||
if (supportsWebCrypto(window)) { |
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.
Similar to what you did for randomBytes, maybe self
should be used instead of window
.
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.
I'll split what I did from randomBytes off into a separate module. It'll be a tiny function, but it's a bit tricky to test.
constructor(secret?: SourceData) { | ||
if (secret) { | ||
this.operation = getKeyPromise(secret).then(keyData => ( | ||
(window as MsWindow).msCrypto.subtle |
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.
self
here too.
this.operation.catch(() => {}); | ||
} else { | ||
this.operation = Promise.resolve( | ||
(window as MsWindow).msCrypto.subtle |
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.
self
here too (If I say 'self' enough, does it become self-aware?)
} | ||
|
||
digest(): Promise<Uint8Array> { | ||
return this.operation.then(operation => new Promise(( |
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.
If our data is empty, should we short-circuit by returning the result from emptyDataSha256
?
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.
We don't actually know in IE11 if any data has been processed, since the WebCrypto implementation there performs incremental hashes as data is provided. It would make sense to add to the standard webcrypto one, though
packages/crypto-sha256-node/index.ts
Outdated
} | ||
|
||
if (toCast instanceof ArrayBuffer) { | ||
return Buffer.from(toCast); |
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.
I have a feeling we may need to do what we do in V2 and use feature detection to determine if we can use Buffer.from. I know the latest version of node 4.x supports Buffer.from
, but the version in Lambda doesn't. That probably makes sense in a separate package if we go down that route.
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.
Yeah, a separate package seems like the way to go.
"target": "es5", | ||
"module": "commonjs", | ||
"lib": [ | ||
"DOM", |
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.
Does the node version need DOM
?
@@ -0,0 +1,65 @@ | |||
export function fromUtf8(input: string): Uint8Array { |
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.
Should probably mention where this came from.
@@ -0,0 +1,9 @@ | |||
{ | |||
"compilerOptions": { | |||
"target": "es6", |
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.
Target should be set to es5
to stay consistent (and for our IE friends...)
…tional offset and length
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.
Fully embrace that feature detection!
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs and link to relevant comments in this thread. |
Includes #13 as the browser module requires UTF-8 support.
This PR adds a node, browser, and universal module for calculating SHA-256 hashes and HMACs. Most of this PR is tests, which could probably be simplified a bit once DefinitelyTyped/DefinitelyTyped#16645 is merged and released.