Skip to content
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

[CryptoService] Leverage browser native Crypto API to hash strings. #3850

Merged
merged 6 commits into from
Jul 1, 2016

Conversation

lannka
Copy link
Contributor

@lannka lannka commented Jun 30, 2016

Highlights

  • Fallback to closure lib when native API is not available or failed to execute.
  • Exposed method base64() from sha384 closure library (used to convert native API result to web safe base64).

This is the 2nd PR to address #3690

…hen native API is not available or failed to execute.
@@ -20,29 +20,62 @@ import {getService} from '../../../src/service';
export class Crypto {

constructor(win) {
this.win = win;
if (win.crypto) {
this.subtle = win.crypto.subtle || win.crypto.webkitSubtle;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make private and @const and always set it (Possibly to null). Add type.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add typedef?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Closure externs should have webCrypto.SubtleCrypto as a type.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do I check that, are externs listed anywhere?
Didn't see here

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used Google's internal code search :)

@avimehta
Copy link
Contributor

lgtm

return this.subtle.digest('SHA-384', str2ab(str))
// [].slice.call(Unit8Array) is a shim for Array.from(Unit8Array)
.then(buffer => [].slice.call(new Uint8Array(buffer)),
() => lib.sha384(str));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dev.error?
Add comment about fallback.

@cramforce
Copy link
Member

Nice! Excited about this.

*/
sha384(str) {
if (this.subtle) {
try {
return this.subtle.digest('SHA-384', str2ab(str))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe these can take a UInt8Array, not just a ArrayBuffer.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point.

this.subtle = win.crypto.subtle || win.crypto.webkitSubtle;
}
/** @private @const {?SubtleCrypto} */
this.subtle = getSubtle(win);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still need to append a _ it can be minified better.

}

// A shim for TextEncoder
function str2ab(str) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add JSDoc.

@cramforce
Copy link
Member

Woop woop!

LGTM

@lannka lannka merged commit d24cd1b into ampproject:master Jul 1, 2016
@lannka lannka deleted the native_crypto branch July 1, 2016 20:58
jonasmattsson1 added a commit to widespace-os/amphtml that referenced this pull request Jul 5, 2016
* master: (236 commits)
  trim all the columns (ampproject#3894)
  Refactoring: Turn private custom element methods into functions. (ampproject#3882)
  Lower the load priority of ad shaped iframes. (ampproject#3863)
  JsDoc fix (ampproject#3892)
  Add screenshots for Opera to AMP Validator extension. (ampproject#3866)
  Fix renaming of generated JSCompiler_prototypeAlias variable. (ampproject#3887)
  fix typo in amp-sidebar.md (ampproject#3833)
  Validator Roll-up (ampproject#3885)
  [CryptoService] Leverage browser native Crypto API to hash strings. (ampproject#3850)
  Size update (ampproject#3883)
  copy amp-ad docs to builtins (ampproject#3879)
  move doc to extension (ampproject#3878)
  [amp-experiment] Exposes isDismissed() method in AmpUserNotification (ampproject#3832)
  fix action-impl warning on dist (ampproject#3867)
  Add params for microad (ampproject#3827)
  Fixed some A4A tests. (ampproject#3859)
  Updates to colanalytics vendor config for amp-analytics. (ampproject#3849)
  Changes to implement A4A (AMP ads for AMP pages) (ampproject#3534)
  Addresses comment left over from PR#3841 (ampproject#3853)
  Expose submit event with on=submit:el.action syntax. (ampproject#3739)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants