Skip to content

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

Merged
merged 9 commits into from
Jun 12, 2017
Merged

Feature/sha256 #18

merged 9 commits into from
Jun 12, 2017

Conversation

jeskew
Copy link
Contributor

@jeskew jeskew commented Jun 5, 2017

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.

@jeskew jeskew requested review from awood45 and chrisradek June 5, 2017 23:25
@jeskew jeskew mentioned this pull request Jun 5, 2017
7 tasks
private readonly hash: Hash;

constructor(secret?: SourceData) {
if (supportsWebCrypto(window)) {
Copy link
Contributor

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.

Copy link
Contributor Author

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
Copy link
Contributor

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
Copy link
Contributor

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((
Copy link
Contributor

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?

Copy link
Contributor Author

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

}

if (toCast instanceof ArrayBuffer) {
return Buffer.from(toCast);
Copy link
Contributor

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.

Copy link
Contributor Author

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",
Copy link
Contributor

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 {
Copy link
Contributor

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",
Copy link
Contributor

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...)

Copy link
Contributor

@chrisradek chrisradek left a 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!

@jeskew jeskew merged commit 91323bc into aws:master Jun 12, 2017
@jeskew jeskew deleted the feature/sha256 branch June 12, 2017 22:13
trivikr referenced this pull request in trivikr/aws-sdk-js-v3 Dec 10, 2018
@lock
Copy link

lock bot commented Sep 26, 2019

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.

@lock lock bot locked as resolved and limited conversation to collaborators Sep 26, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants