1 | require('webcrypto-shim');
|
2 | var b64u = require('b64u-lite/bundle/b64u-lite');
|
3 | var str2buf = require('str2buf');
|
4 |
|
5 | var isEdge = navigator.userAgent.indexOf('Edge') > -1;
|
6 | var isIE11 = !!window.MSInputMethodContext && !!document.documentMode;
|
7 |
|
8 |
|
9 | function assign(target) {
|
10 | if (target == null) {
|
11 | throw new TypeError('Cannot convert undefined or null to object');
|
12 | }
|
13 |
|
14 | var to = Object(target);
|
15 |
|
16 | for (var index = 1; index < arguments.length; index++) {
|
17 | var nextSource = arguments[index];
|
18 |
|
19 | if (nextSource != null) {
|
20 | for (var nextKey in nextSource) {
|
21 | if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
|
22 | to[nextKey] = nextSource[nextKey];
|
23 | }
|
24 | }
|
25 | }
|
26 | }
|
27 | return to;
|
28 | }
|
29 |
|
30 | function toArray(item) {
|
31 | return Array.prototype.slice.call(item);
|
32 | }
|
33 |
|
34 | if (isEdge || isIE11) {
|
35 | var originalGenerateKey = crypto.subtle.generateKey;
|
36 | crypto.subtle.generateKey = function() {
|
37 | const args = toArray(arguments);
|
38 | var algo = assign({}, args[0]);
|
39 | if (algo.name === 'ECDSA') {
|
40 | delete algo.hash
|
41 | }
|
42 | args[0] = algo;
|
43 | return originalGenerateKey.apply(crypto.subtle, args);
|
44 | };
|
45 |
|
46 | var originalExportKey = crypto.subtle.exportKey;
|
47 | crypto.subtle.exportKey = function() {
|
48 | var args = toArray(arguments);
|
49 | var key = args[1];
|
50 | return originalExportKey.apply(crypto.subtle, args)
|
51 | .then(function(res) {
|
52 | if (!res.key_ops || !res.key_ops.length) {
|
53 | res.key_ops = key.usages;
|
54 | }
|
55 | return res;
|
56 | });;
|
57 | };
|
58 |
|
59 | var originalImportKey = crypto.subtle.importKey;
|
60 | crypto.subtle.importKey = function() {
|
61 | var args = toArray(arguments);
|
62 | var jwk = args[1];
|
63 | var algo = args[2];
|
64 |
|
65 | if (algo.name === 'RSASSA-PKCS1-v1_5' && algo.hash.name === 'SHA-256') {
|
66 | delete jwk.qi;
|
67 | }
|
68 |
|
69 | return originalImportKey.apply(crypto.subtle, args)
|
70 | .then(function(res) {
|
71 | var algo = res.algorithm;
|
72 | if (algo.name === 'RSASSA-PKCS1-v1_5') {
|
73 | algo.modulusLength = str2buf.toUint8Array(b64u.toBinaryString(jwk.n)).length * 8;
|
74 | algo.publicExponent = str2buf.toUint8Array(b64u.toBinaryString(jwk.e));
|
75 | }
|
76 | return res;
|
77 | });
|
78 | };
|
79 | }
|
80 |
|
81 | module.exports = window.crypto;
|