lib0 0.2.64 → 0.2.65
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/coverage/tmp/coverage-64278-1678297615727-0.json +1 -0
- package/crypto/aes-gcm.d.ts +10 -0
- package/crypto/aes-gcm.d.ts.map +1 -0
- package/crypto/aes-gcm.js +124 -0
- package/crypto/ecdsa.d.ts +13 -0
- package/crypto/ecdsa.d.ts.map +1 -0
- package/crypto/ecdsa.js +93 -0
- package/crypto/rsa-oaep.d.ts +13 -0
- package/crypto/rsa-oaep.d.ts.map +1 -0
- package/crypto/rsa-oaep.js +84 -0
- package/crypto.test.d.ts +2 -0
- package/crypto.test.d.ts.map +1 -1
- package/dist/crypto/aes-gcm.d.ts +10 -0
- package/dist/crypto/aes-gcm.d.ts.map +1 -0
- package/dist/crypto/ecdsa.d.ts +13 -0
- package/dist/crypto/ecdsa.d.ts.map +1 -0
- package/dist/crypto/rsa-oaep.d.ts +13 -0
- package/dist/crypto/rsa-oaep.d.ts.map +1 -0
- package/dist/crypto.test.d.ts +2 -0
- package/dist/crypto.test.d.ts.map +1 -1
- package/dist/testing.cjs +15 -0
- package/dist/testing.cjs.map +1 -1
- package/dist/testing.d.ts +1 -0
- package/dist/testing.d.ts.map +1 -1
- package/dist/testing.test.d.ts.map +1 -1
- package/package.json +18 -6
- package/test.html +3 -1
- package/testing.d.ts +1 -0
- package/testing.d.ts.map +1 -1
- package/testing.js +14 -0
- package/testing.test.d.ts.map +1 -1
- package/coverage/tmp/coverage-17397-1678220559587-0.json +0 -1
- package/crypto.d.ts +0 -22
- package/crypto.d.ts.map +0 -1
- package/crypto.js +0 -186
- package/dist/crypto.cjs +0 -232
- package/dist/crypto.cjs.map +0 -1
- package/dist/crypto.d.ts +0 -22
- package/dist/crypto.d.ts.map +0 -1
package/crypto.d.ts
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
export function deriveSymmetricKey(secret: string | Uint8Array, salt: string | Uint8Array, { extractable, usages }?: {
|
|
2
|
-
extractable?: boolean | undefined;
|
|
3
|
-
usages?: ("decrypt" | "encrypt" | "sign" | "verify")[] | undefined;
|
|
4
|
-
}): PromiseLike<CryptoKey>;
|
|
5
|
-
export function generateAsymmetricKey({ extractable, usages }?: {
|
|
6
|
-
extractable?: boolean | undefined;
|
|
7
|
-
usages?: ("decrypt" | "encrypt" | "sign" | "verify")[] | undefined;
|
|
8
|
-
}): Promise<CryptoKeyPair>;
|
|
9
|
-
export function exportKey(key: CryptoKey): Promise<JsonWebKey>;
|
|
10
|
-
export function importSymmetricKey(jwk: any, { extractable, usages }?: {
|
|
11
|
-
extractable?: boolean | undefined;
|
|
12
|
-
usages?: ("decrypt" | "encrypt" | "sign" | "verify")[] | undefined;
|
|
13
|
-
}): Promise<CryptoKey>;
|
|
14
|
-
export function importAsymmetricKey(jwk: any, { extractable, usages }?: {
|
|
15
|
-
extractable?: boolean | undefined;
|
|
16
|
-
usages?: ("decrypt" | "encrypt" | "sign" | "verify")[] | undefined;
|
|
17
|
-
}): Promise<CryptoKey>;
|
|
18
|
-
export function encrypt(data: Uint8Array, key: CryptoKey): PromiseLike<Uint8Array>;
|
|
19
|
-
export function decrypt(data: Uint8Array, key: CryptoKey): PromiseLike<Uint8Array>;
|
|
20
|
-
export function sign(data: Uint8Array, privateKey: CryptoKey): PromiseLike<Uint8Array>;
|
|
21
|
-
export function verify(signature: Uint8Array, data: Uint8Array, publicKey: CryptoKey): PromiseLike<boolean>;
|
|
22
|
-
//# sourceMappingURL=crypto.d.ts.map
|
package/crypto.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"crypto.d.ts","sourceRoot":"","sources":["crypto.js"],"names":[],"mappings":"AA0BO,2CAPI,MAAM,GAAG,UAAU,QACnB,MAAM,GAAG,UAAU;IAEJ,WAAW;IACwB,MAAM;IACvD,YAAY,SAAS,CAAC,CA4BjC;AAQM;IAHmB,WAAW;IACwB,MAAM;2BAUhE;AAMI,+BAFI,SAAS,uBAEkD;AAS/D,wCALI,GAAG;IAEY,WAAW;IACwB,MAAM;uBAGI;AAUhE,yCALI,GAAG;IAEY,WAAW;IACwB,MAAM;uBAGkC;AAW9F,8BAJI,UAAU,OACV,SAAS,GACR,YAAY,UAAU,CAAC,CAkBlC;AAWM,8BAJI,UAAU,OACV,SAAS,GACR,YAAY,UAAU,CAAC,CAclC;AAWM,2BAJI,UAAU,cACV,SAAS,GACR,YAAY,UAAU,CAAC,CAUa;AAYzC,kCALI,UAAU,QACV,UAAU,aACV,SAAS,GACR,YAAY,OAAO,CAAC,CAW7B"}
|
package/crypto.js
DELETED
|
@@ -1,186 +0,0 @@
|
|
|
1
|
-
/* eslint-env browser */
|
|
2
|
-
|
|
3
|
-
import * as encoding from './encoding.js'
|
|
4
|
-
import * as decoding from './decoding.js'
|
|
5
|
-
import * as string from './string.js'
|
|
6
|
-
import * as webcrypto from 'lib0/webcrypto'
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* @param {string | Uint8Array} data
|
|
10
|
-
* @return {Uint8Array}
|
|
11
|
-
*/
|
|
12
|
-
/* c8 ignore next */
|
|
13
|
-
const toBinary = data => typeof data === 'string' ? string.encodeUtf8(data) : data
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* @experimental The API is not final!
|
|
17
|
-
*
|
|
18
|
-
* Derive an symmetric key using the Password-Based-Key-Derivation-Function-2.
|
|
19
|
-
*
|
|
20
|
-
* @param {string | Uint8Array} secret
|
|
21
|
-
* @param {string | Uint8Array} salt
|
|
22
|
-
* @param {Object} opts
|
|
23
|
-
* @param {boolean} [opts.extractable]
|
|
24
|
-
* @param {Array<'sign'|'verify'|'encrypt'|'decrypt'>} [opts.usages]
|
|
25
|
-
* @return {PromiseLike<CryptoKey>}
|
|
26
|
-
*/
|
|
27
|
-
export const deriveSymmetricKey = (secret, salt, { extractable = false, usages = ['encrypt', 'decrypt'] } = {}) => {
|
|
28
|
-
const binSecret = toBinary(secret)
|
|
29
|
-
const binSalt = toBinary(salt)
|
|
30
|
-
return webcrypto.subtle.importKey(
|
|
31
|
-
'raw',
|
|
32
|
-
binSecret,
|
|
33
|
-
'PBKDF2',
|
|
34
|
-
false,
|
|
35
|
-
['deriveKey']
|
|
36
|
-
).then(keyMaterial =>
|
|
37
|
-
webcrypto.subtle.deriveKey(
|
|
38
|
-
{
|
|
39
|
-
name: 'PBKDF2',
|
|
40
|
-
salt: binSalt, // NIST recommends at least 64 bits
|
|
41
|
-
iterations: 600000, // OWASP recommends 600k iterations
|
|
42
|
-
hash: 'SHA-256'
|
|
43
|
-
},
|
|
44
|
-
keyMaterial,
|
|
45
|
-
{
|
|
46
|
-
name: 'AES-GCM',
|
|
47
|
-
length: 256
|
|
48
|
-
},
|
|
49
|
-
extractable,
|
|
50
|
-
usages
|
|
51
|
-
)
|
|
52
|
-
)
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/* c8 ignore next */
|
|
56
|
-
/**
|
|
57
|
-
* @param {Object} opts
|
|
58
|
-
* @param {boolean} [opts.extractable]
|
|
59
|
-
* @param {Array<'sign'|'verify'|'encrypt'|'decrypt'>} [opts.usages]
|
|
60
|
-
*/
|
|
61
|
-
export const generateAsymmetricKey = ({ extractable = false, usages = ['sign', 'verify'] } = {}) =>
|
|
62
|
-
webcrypto.subtle.generateKey(
|
|
63
|
-
{
|
|
64
|
-
name: 'ECDSA',
|
|
65
|
-
namedCurve: 'P-384'
|
|
66
|
-
},
|
|
67
|
-
extractable,
|
|
68
|
-
usages
|
|
69
|
-
)
|
|
70
|
-
|
|
71
|
-
/* c8 ignore next */
|
|
72
|
-
/**
|
|
73
|
-
* @param {CryptoKey} key - Symmetric or Asymmetric key
|
|
74
|
-
*/
|
|
75
|
-
export const exportKey = key => webcrypto.subtle.exportKey('jwk', key)
|
|
76
|
-
|
|
77
|
-
/* c8 ignore start */
|
|
78
|
-
/**
|
|
79
|
-
* @param {any} jwk
|
|
80
|
-
* @param {Object} opts
|
|
81
|
-
* @param {boolean} [opts.extractable]
|
|
82
|
-
* @param {Array<'sign'|'verify'|'encrypt'|'decrypt'>} [opts.usages]
|
|
83
|
-
*/
|
|
84
|
-
export const importSymmetricKey = (jwk, { extractable = false, usages = ['encrypt', 'decrypt'] } = {}) =>
|
|
85
|
-
webcrypto.subtle.importKey('jwk', jwk, 'PBKDF2', extractable, usages)
|
|
86
|
-
/* c8 ignore stop */
|
|
87
|
-
|
|
88
|
-
/* c8 ignore next */
|
|
89
|
-
/**
|
|
90
|
-
* @param {any} jwk
|
|
91
|
-
* @param {Object} opts
|
|
92
|
-
* @param {boolean} [opts.extractable]
|
|
93
|
-
* @param {Array<'sign'|'verify'|'encrypt'|'decrypt'>} [opts.usages]
|
|
94
|
-
*/
|
|
95
|
-
export const importAsymmetricKey = (jwk, { extractable = false, usages = ['encrypt', 'decrypt'] } = {}) =>
|
|
96
|
-
webcrypto.subtle.importKey('jwk', jwk, { name: 'ECDSA', namedCurve: 'P-384' }, extractable, usages)
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* @experimental The API is not final!
|
|
100
|
-
*
|
|
101
|
-
* Encrypt some data using AES-GCM method.
|
|
102
|
-
*
|
|
103
|
-
* @param {Uint8Array} data data to be encrypted
|
|
104
|
-
* @param {CryptoKey} key
|
|
105
|
-
* @return {PromiseLike<Uint8Array>} encrypted, base64 encoded message
|
|
106
|
-
*/
|
|
107
|
-
export const encrypt = (data, key) => {
|
|
108
|
-
const iv = webcrypto.getRandomValues(new Uint8Array(16)) // 92bit is enough. 128bit is recommended if space is not an issue.
|
|
109
|
-
return webcrypto.subtle.encrypt(
|
|
110
|
-
{
|
|
111
|
-
name: 'AES-GCM',
|
|
112
|
-
iv
|
|
113
|
-
},
|
|
114
|
-
key,
|
|
115
|
-
data
|
|
116
|
-
).then(cipher => {
|
|
117
|
-
const encryptedDataEncoder = encoding.createEncoder()
|
|
118
|
-
// iv may be sent in the clear to the other peers
|
|
119
|
-
encoding.writeUint8Array(encryptedDataEncoder, iv)
|
|
120
|
-
encoding.writeVarUint8Array(encryptedDataEncoder, new Uint8Array(cipher))
|
|
121
|
-
return encoding.toUint8Array(encryptedDataEncoder)
|
|
122
|
-
})
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
/**
|
|
126
|
-
* @experimental The API is not final!
|
|
127
|
-
*
|
|
128
|
-
* Decrypt some data using AES-GCM method.
|
|
129
|
-
*
|
|
130
|
-
* @param {Uint8Array} data
|
|
131
|
-
* @param {CryptoKey} key
|
|
132
|
-
* @return {PromiseLike<Uint8Array>} decrypted buffer
|
|
133
|
-
*/
|
|
134
|
-
export const decrypt = (data, key) => {
|
|
135
|
-
const dataDecoder = decoding.createDecoder(data)
|
|
136
|
-
const iv = decoding.readUint8Array(dataDecoder, 16)
|
|
137
|
-
const cipher = decoding.readVarUint8Array(dataDecoder)
|
|
138
|
-
return webcrypto.subtle.decrypt(
|
|
139
|
-
{
|
|
140
|
-
name: 'AES-GCM',
|
|
141
|
-
iv
|
|
142
|
-
},
|
|
143
|
-
key,
|
|
144
|
-
cipher
|
|
145
|
-
).then(data => new Uint8Array(data))
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
/**
|
|
149
|
-
* @experimental The API is not final!
|
|
150
|
-
*
|
|
151
|
-
* Sign a message
|
|
152
|
-
*
|
|
153
|
-
* @param {Uint8Array} data
|
|
154
|
-
* @param {CryptoKey} privateKey
|
|
155
|
-
* @return {PromiseLike<Uint8Array>} signature
|
|
156
|
-
*/
|
|
157
|
-
export const sign = (data, privateKey) =>
|
|
158
|
-
webcrypto.subtle.sign(
|
|
159
|
-
{
|
|
160
|
-
name: 'ECDSA',
|
|
161
|
-
hash: { name: 'SHA-384' }
|
|
162
|
-
},
|
|
163
|
-
privateKey,
|
|
164
|
-
data
|
|
165
|
-
).then(signature => new Uint8Array(signature))
|
|
166
|
-
|
|
167
|
-
/**
|
|
168
|
-
* @experimental The API is not final!
|
|
169
|
-
*
|
|
170
|
-
* Sign a message
|
|
171
|
-
*
|
|
172
|
-
* @param {Uint8Array} signature
|
|
173
|
-
* @param {Uint8Array} data
|
|
174
|
-
* @param {CryptoKey} publicKey
|
|
175
|
-
* @return {PromiseLike<boolean>} signature
|
|
176
|
-
*/
|
|
177
|
-
export const verify = (signature, data, publicKey) =>
|
|
178
|
-
webcrypto.subtle.verify(
|
|
179
|
-
{
|
|
180
|
-
name: 'ECDSA',
|
|
181
|
-
hash: { name: 'SHA-384' }
|
|
182
|
-
},
|
|
183
|
-
publicKey,
|
|
184
|
-
signature,
|
|
185
|
-
data
|
|
186
|
-
)
|
package/dist/crypto.cjs
DELETED
|
@@ -1,232 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
|
-
var encoding = require('./buffer-9b566988.cjs');
|
|
6
|
-
var string = require('./string-b1bee84b.cjs');
|
|
7
|
-
var webcrypto = require('lib0/webcrypto');
|
|
8
|
-
require('./environment-01c0e75b.cjs');
|
|
9
|
-
require('./map-9a5915e4.cjs');
|
|
10
|
-
require('./conditions-f5c0c102.cjs');
|
|
11
|
-
require('./storage.cjs');
|
|
12
|
-
require('./function-35e8ddea.cjs');
|
|
13
|
-
require('./array-a1682de6.cjs');
|
|
14
|
-
require('./set-0f209abb.cjs');
|
|
15
|
-
require('./object-aad630ed.cjs');
|
|
16
|
-
require('./binary-ac8e39e2.cjs');
|
|
17
|
-
require('./math-08e068f9.cjs');
|
|
18
|
-
require('./number-f97e141a.cjs');
|
|
19
|
-
require('./error-8582d695.cjs');
|
|
20
|
-
|
|
21
|
-
function _interopNamespace(e) {
|
|
22
|
-
if (e && e.__esModule) return e;
|
|
23
|
-
var n = Object.create(null);
|
|
24
|
-
if (e) {
|
|
25
|
-
Object.keys(e).forEach(function (k) {
|
|
26
|
-
if (k !== 'default') {
|
|
27
|
-
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
28
|
-
Object.defineProperty(n, k, d.get ? d : {
|
|
29
|
-
enumerable: true,
|
|
30
|
-
get: function () { return e[k]; }
|
|
31
|
-
});
|
|
32
|
-
}
|
|
33
|
-
});
|
|
34
|
-
}
|
|
35
|
-
n["default"] = e;
|
|
36
|
-
return Object.freeze(n);
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
var webcrypto__namespace = /*#__PURE__*/_interopNamespace(webcrypto);
|
|
40
|
-
|
|
41
|
-
/* eslint-env browser */
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* @param {string | Uint8Array} data
|
|
45
|
-
* @return {Uint8Array}
|
|
46
|
-
*/
|
|
47
|
-
/* c8 ignore next */
|
|
48
|
-
const toBinary = data => typeof data === 'string' ? string.encodeUtf8(data) : data;
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* @experimental The API is not final!
|
|
52
|
-
*
|
|
53
|
-
* Derive an symmetric key using the Password-Based-Key-Derivation-Function-2.
|
|
54
|
-
*
|
|
55
|
-
* @param {string | Uint8Array} secret
|
|
56
|
-
* @param {string | Uint8Array} salt
|
|
57
|
-
* @param {Object} opts
|
|
58
|
-
* @param {boolean} [opts.extractable]
|
|
59
|
-
* @param {Array<'sign'|'verify'|'encrypt'|'decrypt'>} [opts.usages]
|
|
60
|
-
* @return {PromiseLike<CryptoKey>}
|
|
61
|
-
*/
|
|
62
|
-
const deriveSymmetricKey = (secret, salt, { extractable = false, usages = ['encrypt', 'decrypt'] } = {}) => {
|
|
63
|
-
const binSecret = toBinary(secret);
|
|
64
|
-
const binSalt = toBinary(salt);
|
|
65
|
-
return webcrypto__namespace.subtle.importKey(
|
|
66
|
-
'raw',
|
|
67
|
-
binSecret,
|
|
68
|
-
'PBKDF2',
|
|
69
|
-
false,
|
|
70
|
-
['deriveKey']
|
|
71
|
-
).then(keyMaterial =>
|
|
72
|
-
webcrypto__namespace.subtle.deriveKey(
|
|
73
|
-
{
|
|
74
|
-
name: 'PBKDF2',
|
|
75
|
-
salt: binSalt, // NIST recommends at least 64 bits
|
|
76
|
-
iterations: 600000, // OWASP recommends 600k iterations
|
|
77
|
-
hash: 'SHA-256'
|
|
78
|
-
},
|
|
79
|
-
keyMaterial,
|
|
80
|
-
{
|
|
81
|
-
name: 'AES-GCM',
|
|
82
|
-
length: 256
|
|
83
|
-
},
|
|
84
|
-
extractable,
|
|
85
|
-
usages
|
|
86
|
-
)
|
|
87
|
-
)
|
|
88
|
-
};
|
|
89
|
-
|
|
90
|
-
/* c8 ignore next */
|
|
91
|
-
/**
|
|
92
|
-
* @param {Object} opts
|
|
93
|
-
* @param {boolean} [opts.extractable]
|
|
94
|
-
* @param {Array<'sign'|'verify'|'encrypt'|'decrypt'>} [opts.usages]
|
|
95
|
-
*/
|
|
96
|
-
const generateAsymmetricKey = ({ extractable = false, usages = ['sign', 'verify'] } = {}) =>
|
|
97
|
-
webcrypto__namespace.subtle.generateKey(
|
|
98
|
-
{
|
|
99
|
-
name: 'ECDSA',
|
|
100
|
-
namedCurve: 'P-384'
|
|
101
|
-
},
|
|
102
|
-
extractable,
|
|
103
|
-
usages
|
|
104
|
-
);
|
|
105
|
-
|
|
106
|
-
/* c8 ignore next */
|
|
107
|
-
/**
|
|
108
|
-
* @param {CryptoKey} key - Symmetric or Asymmetric key
|
|
109
|
-
*/
|
|
110
|
-
const exportKey = key => webcrypto__namespace.subtle.exportKey('jwk', key);
|
|
111
|
-
|
|
112
|
-
/* c8 ignore start */
|
|
113
|
-
/**
|
|
114
|
-
* @param {any} jwk
|
|
115
|
-
* @param {Object} opts
|
|
116
|
-
* @param {boolean} [opts.extractable]
|
|
117
|
-
* @param {Array<'sign'|'verify'|'encrypt'|'decrypt'>} [opts.usages]
|
|
118
|
-
*/
|
|
119
|
-
const importSymmetricKey = (jwk, { extractable = false, usages = ['encrypt', 'decrypt'] } = {}) =>
|
|
120
|
-
webcrypto__namespace.subtle.importKey('jwk', jwk, 'PBKDF2', extractable, usages);
|
|
121
|
-
/* c8 ignore stop */
|
|
122
|
-
|
|
123
|
-
/* c8 ignore next */
|
|
124
|
-
/**
|
|
125
|
-
* @param {any} jwk
|
|
126
|
-
* @param {Object} opts
|
|
127
|
-
* @param {boolean} [opts.extractable]
|
|
128
|
-
* @param {Array<'sign'|'verify'|'encrypt'|'decrypt'>} [opts.usages]
|
|
129
|
-
*/
|
|
130
|
-
const importAsymmetricKey = (jwk, { extractable = false, usages = ['encrypt', 'decrypt'] } = {}) =>
|
|
131
|
-
webcrypto__namespace.subtle.importKey('jwk', jwk, { name: 'ECDSA', namedCurve: 'P-384' }, extractable, usages);
|
|
132
|
-
|
|
133
|
-
/**
|
|
134
|
-
* @experimental The API is not final!
|
|
135
|
-
*
|
|
136
|
-
* Encrypt some data using AES-GCM method.
|
|
137
|
-
*
|
|
138
|
-
* @param {Uint8Array} data data to be encrypted
|
|
139
|
-
* @param {CryptoKey} key
|
|
140
|
-
* @return {PromiseLike<Uint8Array>} encrypted, base64 encoded message
|
|
141
|
-
*/
|
|
142
|
-
const encrypt = (data, key) => {
|
|
143
|
-
const iv = webcrypto__namespace.getRandomValues(new Uint8Array(16)); // 92bit is enough. 128bit is recommended if space is not an issue.
|
|
144
|
-
return webcrypto__namespace.subtle.encrypt(
|
|
145
|
-
{
|
|
146
|
-
name: 'AES-GCM',
|
|
147
|
-
iv
|
|
148
|
-
},
|
|
149
|
-
key,
|
|
150
|
-
data
|
|
151
|
-
).then(cipher => {
|
|
152
|
-
const encryptedDataEncoder = encoding.createEncoder();
|
|
153
|
-
// iv may be sent in the clear to the other peers
|
|
154
|
-
encoding.writeUint8Array(encryptedDataEncoder, iv);
|
|
155
|
-
encoding.writeVarUint8Array(encryptedDataEncoder, new Uint8Array(cipher));
|
|
156
|
-
return encoding.toUint8Array(encryptedDataEncoder)
|
|
157
|
-
})
|
|
158
|
-
};
|
|
159
|
-
|
|
160
|
-
/**
|
|
161
|
-
* @experimental The API is not final!
|
|
162
|
-
*
|
|
163
|
-
* Decrypt some data using AES-GCM method.
|
|
164
|
-
*
|
|
165
|
-
* @param {Uint8Array} data
|
|
166
|
-
* @param {CryptoKey} key
|
|
167
|
-
* @return {PromiseLike<Uint8Array>} decrypted buffer
|
|
168
|
-
*/
|
|
169
|
-
const decrypt = (data, key) => {
|
|
170
|
-
const dataDecoder = encoding.createDecoder(data);
|
|
171
|
-
const iv = encoding.readUint8Array(dataDecoder, 16);
|
|
172
|
-
const cipher = encoding.readVarUint8Array(dataDecoder);
|
|
173
|
-
return webcrypto__namespace.subtle.decrypt(
|
|
174
|
-
{
|
|
175
|
-
name: 'AES-GCM',
|
|
176
|
-
iv
|
|
177
|
-
},
|
|
178
|
-
key,
|
|
179
|
-
cipher
|
|
180
|
-
).then(data => new Uint8Array(data))
|
|
181
|
-
};
|
|
182
|
-
|
|
183
|
-
/**
|
|
184
|
-
* @experimental The API is not final!
|
|
185
|
-
*
|
|
186
|
-
* Sign a message
|
|
187
|
-
*
|
|
188
|
-
* @param {Uint8Array} data
|
|
189
|
-
* @param {CryptoKey} privateKey
|
|
190
|
-
* @return {PromiseLike<Uint8Array>} signature
|
|
191
|
-
*/
|
|
192
|
-
const sign = (data, privateKey) =>
|
|
193
|
-
webcrypto__namespace.subtle.sign(
|
|
194
|
-
{
|
|
195
|
-
name: 'ECDSA',
|
|
196
|
-
hash: { name: 'SHA-384' }
|
|
197
|
-
},
|
|
198
|
-
privateKey,
|
|
199
|
-
data
|
|
200
|
-
).then(signature => new Uint8Array(signature));
|
|
201
|
-
|
|
202
|
-
/**
|
|
203
|
-
* @experimental The API is not final!
|
|
204
|
-
*
|
|
205
|
-
* Sign a message
|
|
206
|
-
*
|
|
207
|
-
* @param {Uint8Array} signature
|
|
208
|
-
* @param {Uint8Array} data
|
|
209
|
-
* @param {CryptoKey} publicKey
|
|
210
|
-
* @return {PromiseLike<boolean>} signature
|
|
211
|
-
*/
|
|
212
|
-
const verify = (signature, data, publicKey) =>
|
|
213
|
-
webcrypto__namespace.subtle.verify(
|
|
214
|
-
{
|
|
215
|
-
name: 'ECDSA',
|
|
216
|
-
hash: { name: 'SHA-384' }
|
|
217
|
-
},
|
|
218
|
-
publicKey,
|
|
219
|
-
signature,
|
|
220
|
-
data
|
|
221
|
-
);
|
|
222
|
-
|
|
223
|
-
exports.decrypt = decrypt;
|
|
224
|
-
exports.deriveSymmetricKey = deriveSymmetricKey;
|
|
225
|
-
exports.encrypt = encrypt;
|
|
226
|
-
exports.exportKey = exportKey;
|
|
227
|
-
exports.generateAsymmetricKey = generateAsymmetricKey;
|
|
228
|
-
exports.importAsymmetricKey = importAsymmetricKey;
|
|
229
|
-
exports.importSymmetricKey = importSymmetricKey;
|
|
230
|
-
exports.sign = sign;
|
|
231
|
-
exports.verify = verify;
|
|
232
|
-
//# sourceMappingURL=crypto.cjs.map
|
package/dist/crypto.cjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"crypto.cjs","sources":["../crypto.js"],"sourcesContent":["/* eslint-env browser */\n\nimport * as encoding from './encoding.js'\nimport * as decoding from './decoding.js'\nimport * as string from './string.js'\nimport * as webcrypto from 'lib0/webcrypto'\n\n/**\n * @param {string | Uint8Array} data\n * @return {Uint8Array}\n */\n/* c8 ignore next */\nconst toBinary = data => typeof data === 'string' ? string.encodeUtf8(data) : data\n\n/**\n * @experimental The API is not final!\n *\n * Derive an symmetric key using the Password-Based-Key-Derivation-Function-2.\n *\n * @param {string | Uint8Array} secret\n * @param {string | Uint8Array} salt\n * @param {Object} opts\n * @param {boolean} [opts.extractable]\n * @param {Array<'sign'|'verify'|'encrypt'|'decrypt'>} [opts.usages]\n * @return {PromiseLike<CryptoKey>}\n */\nexport const deriveSymmetricKey = (secret, salt, { extractable = false, usages = ['encrypt', 'decrypt'] } = {}) => {\n const binSecret = toBinary(secret)\n const binSalt = toBinary(salt)\n return webcrypto.subtle.importKey(\n 'raw',\n binSecret,\n 'PBKDF2',\n false,\n ['deriveKey']\n ).then(keyMaterial =>\n webcrypto.subtle.deriveKey(\n {\n name: 'PBKDF2',\n salt: binSalt, // NIST recommends at least 64 bits\n iterations: 600000, // OWASP recommends 600k iterations\n hash: 'SHA-256'\n },\n keyMaterial,\n {\n name: 'AES-GCM',\n length: 256\n },\n extractable,\n usages\n )\n )\n}\n\n/* c8 ignore next */\n/**\n * @param {Object} opts\n * @param {boolean} [opts.extractable]\n * @param {Array<'sign'|'verify'|'encrypt'|'decrypt'>} [opts.usages]\n */\nexport const generateAsymmetricKey = ({ extractable = false, usages = ['sign', 'verify'] } = {}) =>\n webcrypto.subtle.generateKey(\n {\n name: 'ECDSA',\n namedCurve: 'P-384'\n },\n extractable,\n usages\n )\n\n/* c8 ignore next */\n/**\n * @param {CryptoKey} key - Symmetric or Asymmetric key\n */\nexport const exportKey = key => webcrypto.subtle.exportKey('jwk', key)\n\n/* c8 ignore start */\n/**\n * @param {any} jwk\n * @param {Object} opts\n * @param {boolean} [opts.extractable]\n * @param {Array<'sign'|'verify'|'encrypt'|'decrypt'>} [opts.usages]\n */\nexport const importSymmetricKey = (jwk, { extractable = false, usages = ['encrypt', 'decrypt'] } = {}) =>\n webcrypto.subtle.importKey('jwk', jwk, 'PBKDF2', extractable, usages)\n/* c8 ignore stop */\n\n/* c8 ignore next */\n/**\n * @param {any} jwk\n * @param {Object} opts\n * @param {boolean} [opts.extractable]\n * @param {Array<'sign'|'verify'|'encrypt'|'decrypt'>} [opts.usages]\n */\nexport const importAsymmetricKey = (jwk, { extractable = false, usages = ['encrypt', 'decrypt'] } = {}) =>\n webcrypto.subtle.importKey('jwk', jwk, { name: 'ECDSA', namedCurve: 'P-384' }, extractable, usages)\n\n/**\n * @experimental The API is not final!\n *\n * Encrypt some data using AES-GCM method.\n *\n * @param {Uint8Array} data data to be encrypted\n * @param {CryptoKey} key\n * @return {PromiseLike<Uint8Array>} encrypted, base64 encoded message\n */\nexport const encrypt = (data, key) => {\n const iv = webcrypto.getRandomValues(new Uint8Array(16)) // 92bit is enough. 128bit is recommended if space is not an issue.\n return webcrypto.subtle.encrypt(\n {\n name: 'AES-GCM',\n iv\n },\n key,\n data\n ).then(cipher => {\n const encryptedDataEncoder = encoding.createEncoder()\n // iv may be sent in the clear to the other peers\n encoding.writeUint8Array(encryptedDataEncoder, iv)\n encoding.writeVarUint8Array(encryptedDataEncoder, new Uint8Array(cipher))\n return encoding.toUint8Array(encryptedDataEncoder)\n })\n}\n\n/**\n * @experimental The API is not final!\n *\n * Decrypt some data using AES-GCM method.\n *\n * @param {Uint8Array} data\n * @param {CryptoKey} key\n * @return {PromiseLike<Uint8Array>} decrypted buffer\n */\nexport const decrypt = (data, key) => {\n const dataDecoder = decoding.createDecoder(data)\n const iv = decoding.readUint8Array(dataDecoder, 16)\n const cipher = decoding.readVarUint8Array(dataDecoder)\n return webcrypto.subtle.decrypt(\n {\n name: 'AES-GCM',\n iv\n },\n key,\n cipher\n ).then(data => new Uint8Array(data))\n}\n\n/**\n * @experimental The API is not final!\n *\n * Sign a message\n *\n * @param {Uint8Array} data\n * @param {CryptoKey} privateKey\n * @return {PromiseLike<Uint8Array>} signature\n */\nexport const sign = (data, privateKey) =>\n webcrypto.subtle.sign(\n {\n name: 'ECDSA',\n hash: { name: 'SHA-384' }\n },\n privateKey,\n data\n ).then(signature => new Uint8Array(signature))\n\n/**\n * @experimental The API is not final!\n *\n * Sign a message\n *\n * @param {Uint8Array} signature\n * @param {Uint8Array} data\n * @param {CryptoKey} publicKey\n * @return {PromiseLike<boolean>} signature\n */\nexport const verify = (signature, data, publicKey) =>\n webcrypto.subtle.verify(\n {\n name: 'ECDSA',\n hash: { name: 'SHA-384' }\n },\n publicKey,\n signature,\n data\n )\n"],"names":["string.encodeUtf8","webcrypto","encoding.createEncoder","encoding.writeUint8Array","encoding.writeVarUint8Array","encoding.toUint8Array","decoding.createDecoder","decoding.readUint8Array","decoding.readVarUint8Array"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAMA;AACA;AACA;AACA;AACA;AACA;AACA,MAAM,QAAQ,GAAG,IAAI,IAA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AAEA,oBAAS,CAAC,MAAM,CAAC,MAAM;AACzB,IAAI;AACJ,MAAM,IAAI,EAAE,OAAO;AACnB,MAAM,IAAI,EAAE,EAAE,IAAI,EAAE,SAAS,EAAE;AAC/B,KAAK;AACL,IAAI,SAAS;AACb,IAAI,SAAS;AACb,IAAI,IAAI;AACR;;;;;;;;;;;;"}
|
package/dist/crypto.d.ts
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
export function deriveSymmetricKey(secret: string | Uint8Array, salt: string | Uint8Array, { extractable, usages }?: {
|
|
2
|
-
extractable?: boolean | undefined;
|
|
3
|
-
usages?: ("decrypt" | "encrypt" | "sign" | "verify")[] | undefined;
|
|
4
|
-
}): PromiseLike<CryptoKey>;
|
|
5
|
-
export function generateAsymmetricKey({ extractable, usages }?: {
|
|
6
|
-
extractable?: boolean | undefined;
|
|
7
|
-
usages?: ("decrypt" | "encrypt" | "sign" | "verify")[] | undefined;
|
|
8
|
-
}): Promise<CryptoKeyPair>;
|
|
9
|
-
export function exportKey(key: CryptoKey): Promise<JsonWebKey>;
|
|
10
|
-
export function importSymmetricKey(jwk: any, { extractable, usages }?: {
|
|
11
|
-
extractable?: boolean | undefined;
|
|
12
|
-
usages?: ("decrypt" | "encrypt" | "sign" | "verify")[] | undefined;
|
|
13
|
-
}): Promise<CryptoKey>;
|
|
14
|
-
export function importAsymmetricKey(jwk: any, { extractable, usages }?: {
|
|
15
|
-
extractable?: boolean | undefined;
|
|
16
|
-
usages?: ("decrypt" | "encrypt" | "sign" | "verify")[] | undefined;
|
|
17
|
-
}): Promise<CryptoKey>;
|
|
18
|
-
export function encrypt(data: Uint8Array, key: CryptoKey): PromiseLike<Uint8Array>;
|
|
19
|
-
export function decrypt(data: Uint8Array, key: CryptoKey): PromiseLike<Uint8Array>;
|
|
20
|
-
export function sign(data: Uint8Array, privateKey: CryptoKey): PromiseLike<Uint8Array>;
|
|
21
|
-
export function verify(signature: Uint8Array, data: Uint8Array, publicKey: CryptoKey): PromiseLike<boolean>;
|
|
22
|
-
//# sourceMappingURL=crypto.d.ts.map
|
package/dist/crypto.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"crypto.d.ts","sourceRoot":"","sources":["../crypto.js"],"names":[],"mappings":"AA0BO,2CAPI,MAAM,GAAG,UAAU,QACnB,MAAM,GAAG,UAAU;IAEJ,WAAW;IACwB,MAAM;IACvD,YAAY,SAAS,CAAC,CA4BjC;AAQM;IAHmB,WAAW;IACwB,MAAM;2BAUhE;AAMI,+BAFI,SAAS,uBAEkD;AAS/D,wCALI,GAAG;IAEY,WAAW;IACwB,MAAM;uBAGI;AAUhE,yCALI,GAAG;IAEY,WAAW;IACwB,MAAM;uBAGkC;AAW9F,8BAJI,UAAU,OACV,SAAS,GACR,YAAY,UAAU,CAAC,CAkBlC;AAWM,8BAJI,UAAU,OACV,SAAS,GACR,YAAY,UAAU,CAAC,CAclC;AAWM,2BAJI,UAAU,cACV,SAAS,GACR,YAAY,UAAU,CAAC,CAUa;AAYzC,kCALI,UAAU,QACV,UAAU,aACV,SAAS,GACR,YAAY,OAAO,CAAC,CAW7B"}
|