3 * Copyright 2019 Google LLC. All Rights Reserved.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
18 return new (P || (P = Promise))(function (resolve, reject) {
19 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
20 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
21 function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
22 step((generator = generator.apply(thisArg, _arguments || [])).next());
25 Object.defineProperty(exports, "__esModule", { value: true });
26 // This file implements crypto functions we need using in-browser
27 // SubtleCrypto interface `window.crypto.subtle`.
28 const base64js = require("base64-js");
29 const isbrowser_1 = require("../../isbrowser");
30 // Not all browsers support `TextEncoder`. The following `require` will
31 // provide a fast UTF8-only replacement for those browsers that don't support
32 // text encoding natively.
33 if (isbrowser_1.isBrowser() && typeof TextEncoder === 'undefined') {
34 require('fast-text-encoding');
38 if (typeof (window) === 'undefined' || window.crypto === undefined ||
39 window.crypto.subtle === undefined) {
40 throw new Error('SubtleCrypto not found. Make sure it\'s an https:// website.');
43 sha256DigestBase64(str) {
44 return __awaiter(this, void 0, void 0, function* () {
45 // SubtleCrypto digest() method is async, so we must make
46 // this method async as well.
47 // To calculate SHA256 digest using SubtleCrypto, we first
48 // need to convert an input string to an ArrayBuffer:
49 const inputBuffer = new TextEncoder().encode(str);
50 // Result is ArrayBuffer as well.
51 const outputBuffer = yield window.crypto.subtle.digest('SHA-256', inputBuffer);
52 return base64js.fromByteArray(new Uint8Array(outputBuffer));
55 randomBytesBase64(count) {
56 const array = new Uint8Array(count);
57 window.crypto.getRandomValues(array);
58 return base64js.fromByteArray(array);
60 verify(pubkey, data, signature) {
61 return __awaiter(this, void 0, void 0, function* () {
63 name: 'RSASSA-PKCS1-v1_5',
64 hash: { name: 'SHA-256' },
66 const dataArray = new TextEncoder().encode(data);
67 // base64js requires padding, so let's add some '='
68 while (signature.length % 4 !== 0) {
71 const signatureArray = base64js.toByteArray(signature);
72 const cryptoKey = yield window.crypto.subtle.importKey('jwk', pubkey, algo, true, ['verify']);
73 // SubtleCrypto's verify method is async so we must make
74 // this method async as well.
75 const result = yield window.crypto.subtle.verify(algo, cryptoKey, signatureArray, dataArray);
79 createSign(algorithm) {
80 throw new Error('createSign is not implemented in BrowserCrypto');
82 decodeBase64StringUtf8(base64) {
83 const uint8array = base64js.toByteArray(base64);
84 const result = new TextDecoder().decode(uint8array);
87 encodeBase64StringUtf8(text) {
88 const uint8array = new TextEncoder().encode(text);
89 const result = base64js.fromByteArray(uint8array);
93 exports.BrowserCrypto = BrowserCrypto;
94 //# sourceMappingURL=crypto.js.map