web-vault-crypto 0.0.1

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/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 - now: Johanna Herrmann
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,77 @@
1
+ # web-vault-crypto
2
+
3
+ [![NPM](https://nodei.co/npm/web-vault-crypto.svg?style=shields&data=n,v,u,d,s&color=blue)](https://nodei.co/npm/web-vault-crypto/) \
4
+ [![QA-Main](https://github.com/web-vault-org/web-vault-crypto/actions/workflows/qa_main.yml/badge.svg?branch=main)](https://github.com/web-vault-org/web-vault-crypto/actions/workflows/qa_main.yml)
5
+
6
+ Crypto-Library for e2e encrypted web vaults,
7
+ using [native web crypto api](https://developer.mozilla.org/en-US/docs/Web/API/Web_Crypto_API)
8
+ and [argon2-browser](https://www.npmjs.com/package/argon2-browser),
9
+ written in typescript, easy to use.
10
+
11
+ ## Introduction
12
+ This package provides easy to use crypto-functions, suitable for web-vaults (examples: password-safes, e2e encrypted video collections).
13
+
14
+ Used crypto-libraries:
15
+ * [argon2-browser](https://www.npmjs.com/package/argon2-browser) for password-based-key-derivation and password hashing,
16
+ using argon2id
17
+ * [native web crypto api](https://developer.mozilla.org/en-US/docs/Web/API/Web_Crypto_API) for everything else
18
+
19
+ ## Features
20
+ * key generation
21
+ * password-based key derivation
22
+ * password hashing
23
+ * key wrapping and unwrapping
24
+ * encryption and decryption − `AEAD` using `AES-256-GCM`
25
+ * signing and verification − using `hmac`
26
+
27
+ ## Usage
28
+
29
+ ### npm
30
+
31
+ Install:
32
+ ```bash
33
+ npm install web-vault-crypto
34
+ ```
35
+
36
+ Import:
37
+ ```js
38
+
39
+ // Replace createKey with a comma-separated list of the functions, you need
40
+ import { createKey } from 'web-vault-crypto';
41
+ const key = await createKey({ sizeInBytes: 32 });
42
+
43
+ // or import all functions
44
+ import webVaultCrypto from 'web-vault-crypto';
45
+ const key = await webVaultCrypto.createKey({ sizeInBytes: 32 });
46
+ ```
47
+
48
+ ### Browser
49
+
50
+ ```html
51
+ <!-- Load the file from jsDelivr -->
52
+ <script src="https://cdn.jsdelivr.net/npm/web-vault-crypto@latest/dist/web-vault-crypto.js"></script>
53
+
54
+ <!-- or load the file from unpkg -->
55
+ <script src="https://unpkg.com/web-vault-crypto@latest/dist/web-vault-crypto.js"></script>
56
+
57
+ <!-- or download the file and host it yourself -->
58
+ <script src="/js/web-vault-crypto.js"></script>
59
+
60
+ <script>
61
+ // Replace createKey with a comma-separated list of the functions, you need
62
+ const { createKey } = webVaultCrypto;
63
+ const key = await createKey({ sizeInBytes: 32 });
64
+
65
+ // or use functions directly
66
+ const key = await webVaultCrypto.createKey({ sizeInBytes: 32 });
67
+ </script>
68
+ ```
69
+
70
+ ## API
71
+ Read [API Documentation](./api.md) to see how to use the functions
72
+
73
+ ## Crypto/Security
74
+ Read [Security Concept](./crypto.md) to see the crypto/security concepts
75
+
76
+ ## License
77
+ This project is licensed under the [MIT License](./LICENSE.txt)
@@ -0,0 +1,4 @@
1
+ import { Argon2BrowserHashOptions, Argon2BrowserHashResult } from 'argon2-browser';
2
+ declare const getArgon2Hash: () => (options: Argon2BrowserHashOptions) => Promise<Argon2BrowserHashResult>;
3
+ export { getArgon2Hash };
4
+ //# sourceMappingURL=argon2.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"argon2.d.ts","sourceRoot":"","sources":["../lib/argon2.ts"],"names":[],"mappings":"AAAA,OAAO,EAAQ,wBAAwB,EAAE,uBAAuB,EAAE,MAAM,gBAAgB,CAAC;AAEzF,QAAA,MAAM,aAAa,QAAgB,CAAC,OAAO,EAAE,wBAAwB,KAAK,OAAO,CAAC,uBAAuB,CAExG,CAAC;AAEF,OAAO,EAAE,aAAa,EAAE,CAAC"}
@@ -0,0 +1,6 @@
1
+ import { hash } from 'argon2-browser';
2
+ const getArgon2Hash = function () {
3
+ return hash;
4
+ };
5
+ export { getArgon2Hash };
6
+ //# sourceMappingURL=argon2.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"argon2.js","sourceRoot":"","sources":["../lib/argon2.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAqD,MAAM,gBAAgB,CAAC;AAEzF,MAAM,aAAa,GAAG;IACpB,OAAO,IAAI,CAAC;AACd,CAAC,CAAC;AAEF,OAAO,EAAE,aAAa,EAAE,CAAC"}
@@ -0,0 +1,4 @@
1
+ declare const encode: (bytes: Uint8Array) => string;
2
+ declare const decode: (text: string) => Uint8Array;
3
+ export { encode, decode };
4
+ //# sourceMappingURL=base64.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"base64.d.ts","sourceRoot":"","sources":["../lib/base64.ts"],"names":[],"mappings":"AAAA,QAAA,MAAM,MAAM,UAAoB,UAAU,KAAG,MAS5C,CAAC;AAEF,QAAA,MAAM,MAAM,SAAmB,MAAM,KAAG,UASvC,CAAC;AAEF,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC"}
@@ -0,0 +1,18 @@
1
+ const encode = function (bytes) {
2
+ let binary = '';
3
+ const chunkSize = 0x8000; // 32 KB
4
+ for (let i = 0; i < bytes.length; i += chunkSize) {
5
+ binary += String.fromCharCode(...bytes.subarray(i, i + chunkSize));
6
+ }
7
+ return btoa(binary);
8
+ };
9
+ const decode = function (text) {
10
+ const binary = atob(text);
11
+ const bytes = new Uint8Array(binary.length);
12
+ for (let i = 0; i < binary.length; i++) {
13
+ bytes[i] = binary.charCodeAt(i);
14
+ }
15
+ return bytes;
16
+ };
17
+ export { encode, decode };
18
+ //# sourceMappingURL=base64.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"base64.js","sourceRoot":"","sources":["../lib/base64.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,GAAG,UAAU,KAAiB;IACxC,IAAI,MAAM,GAAG,EAAE,CAAC;IAChB,MAAM,SAAS,GAAG,MAAM,CAAC,CAAC,QAAQ;IAElC,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,KAAK,CAAC,MAAM,EAAE,CAAC,IAAI,SAAS,EAAE,CAAC;QACjD,MAAM,IAAI,MAAM,CAAC,YAAY,CAAC,GAAG,KAAK,CAAC,QAAQ,CAAC,CAAC,EAAE,CAAC,GAAG,SAAS,CAAC,CAAC,CAAC;IACrE,CAAC;IAED,OAAO,IAAI,CAAC,MAAM,CAAC,CAAC;AACtB,CAAC,CAAC;AAEF,MAAM,MAAM,GAAG,UAAU,IAAY;IACnC,MAAM,MAAM,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC;IAC1B,MAAM,KAAK,GAAG,IAAI,UAAU,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IAE5C,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QACvC,KAAK,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;IAClC,CAAC;IAED,OAAO,KAAK,CAAC;AACf,CAAC,CAAC;AAEF,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC"}
@@ -0,0 +1,10 @@
1
+ /**
2
+ * generates a random key with desired size.
3
+ * @param sizeInBytes - desired key size in bytes
4
+ * @returns Promise with desired key as Uint8Array
5
+ */
6
+ declare const createKey: ({ sizeInBytes }: {
7
+ sizeInBytes: number;
8
+ }) => Promise<Uint8Array>;
9
+ export { createKey };
10
+ //# sourceMappingURL=createKey.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createKey.d.ts","sourceRoot":"","sources":["../lib/createKey.ts"],"names":[],"mappings":"AAEA;;;;GAIG;AACH,QAAA,MAAM,SAAS,oBAAoC;IAAE,WAAW,EAAE,MAAM,CAAA;CAAE,KAAG,OAAO,CAAC,UAAU,CAS9F,CAAC;AAEF,OAAO,EAAE,SAAS,EAAE,CAAC"}
@@ -0,0 +1,17 @@
1
+ import { getCrypto } from './crypto.js';
2
+ /**
3
+ * generates a random key with desired size.
4
+ * @param sizeInBytes - desired key size in bytes
5
+ * @returns Promise with desired key as Uint8Array
6
+ */
7
+ const createKey = async function ({ sizeInBytes }) {
8
+ if (sizeInBytes < 8) {
9
+ throw new Error('Invalid length. Must be at least 8');
10
+ }
11
+ const crypto = getCrypto();
12
+ const key = new Uint8Array(sizeInBytes);
13
+ crypto.getRandomValues(key);
14
+ return key;
15
+ };
16
+ export { createKey };
17
+ //# sourceMappingURL=createKey.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createKey.js","sourceRoot":"","sources":["../lib/createKey.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AAErC;;;;GAIG;AACH,MAAM,SAAS,GAAG,KAAK,WAAW,EAAE,WAAW,EAA2B;IACxE,IAAI,WAAW,GAAG,CAAC,EAAE,CAAC;QACpB,MAAM,IAAI,KAAK,CAAC,oCAAoC,CAAC,CAAC;IACxD,CAAC;IAED,MAAM,MAAM,GAAG,SAAS,EAAE,CAAC;IAC3B,MAAM,GAAG,GAAG,IAAI,UAAU,CAAC,WAAW,CAAC,CAAC;IACxC,MAAM,CAAC,eAAe,CAAC,GAAG,CAAC,CAAC;IAC5B,OAAO,GAAG,CAAC;AACb,CAAC,CAAC;AAEF,OAAO,EAAE,SAAS,EAAE,CAAC"}
@@ -0,0 +1,3 @@
1
+ declare const getCrypto: () => Crypto;
2
+ export { getCrypto };
3
+ //# sourceMappingURL=crypto.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"crypto.d.ts","sourceRoot":"","sources":["../lib/crypto.ts"],"names":[],"mappings":"AAAA,QAAA,MAAM,SAAS,QAAgB,MAM9B,CAAC;AAEF,OAAO,EAAE,SAAS,EAAE,CAAC"}
@@ -0,0 +1,10 @@
1
+ const getCrypto = function () {
2
+ if (!globalThis.crypto?.subtle) {
3
+ throw new Error('crypto.subtle not available. Wrong Environment or unsecure context');
4
+ }
5
+ else {
6
+ return globalThis.crypto;
7
+ }
8
+ };
9
+ export { getCrypto };
10
+ //# sourceMappingURL=crypto.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"crypto.js","sourceRoot":"","sources":["../lib/crypto.ts"],"names":[],"mappings":"AAAA,MAAM,SAAS,GAAG;IAChB,IAAI,CAAC,UAAU,CAAC,MAAM,EAAE,MAAM,EAAE,CAAC;QAC/B,MAAM,IAAI,KAAK,CAAC,oEAAoE,CAAC,CAAC;IACxF,CAAC;SAAM,CAAC;QACN,OAAO,UAAU,CAAC,MAAM,CAAC;IAC3B,CAAC;AACH,CAAC,CAAC;AAEF,OAAO,EAAE,SAAS,EAAE,CAAC"}
@@ -0,0 +1,30 @@
1
+ /**
2
+ * encrypts a string or a Uint8Array
3
+ * @param content - plaintext as string or Uint8Array
4
+ * @param key - key as Uint8Array
5
+ * @param encode - boolean, if plaintext should be base64-encoded
6
+ * @param additionalData - array of strings, with additional Data for integrity and authenticity checks
7
+ * @returns Promise with ciphertext, as base64-encoded string if `encode` is true, as Uint8Array if not
8
+ */
9
+ declare const encrypt: ({ content, key, encode, additionalData }: {
10
+ content: string | Uint8Array;
11
+ key: Uint8Array;
12
+ encode?: boolean;
13
+ additionalData?: string[];
14
+ }) => Promise<Uint8Array | string>;
15
+ /**
16
+ * decrypts a string or a Uint8Array
17
+ * @param content - ciphertext as string or Uint8Array
18
+ * @param key - key as Uint8Array
19
+ * @param asString - boolean, if plaintext should be returned as string
20
+ * @param additionalData - array of strings, with additional Data for integrity and authenticity checks
21
+ * @returns Promise with plaintext, as string if `asString` is true, as Uint8Array if not
22
+ */
23
+ declare const decrypt: ({ content, key, asString, additionalData }: {
24
+ content: string | Uint8Array;
25
+ key: Uint8Array;
26
+ asString?: boolean;
27
+ additionalData?: string[];
28
+ }) => Promise<Uint8Array | string>;
29
+ export { encrypt, decrypt };
30
+ //# sourceMappingURL=encryption.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"encryption.d.ts","sourceRoot":"","sources":["../lib/encryption.ts"],"names":[],"mappings":"AA2BA;;;;;;;GAOG;AACH,QAAA,MAAM,OAAO,6CAKV;IACD,OAAO,EAAE,MAAM,GAAG,UAAU,CAAC;IAC7B,GAAG,EAAE,UAAU,CAAC;IAChB,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,cAAc,CAAC,EAAE,MAAM,EAAE,CAAC;CAC3B,KAAG,OAAO,CAAC,UAAU,GAAG,MAAM,CAkB9B,CAAC;AAEF;;;;;;;GAOG;AACH,QAAA,MAAM,OAAO,+CAKV;IACD,OAAO,EAAE,MAAM,GAAG,UAAU,CAAC;IAC7B,GAAG,EAAE,UAAU,CAAC;IAChB,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,cAAc,CAAC,EAAE,MAAM,EAAE,CAAC;CAC3B,KAAG,OAAO,CAAC,UAAU,GAAG,MAAM,CAmB9B,CAAC;AAEF,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,CAAC"}
@@ -0,0 +1,71 @@
1
+ import { importKey, splitByLengths } from './util.js';
2
+ import { encode as encodeBase64, decode as decodeBase64 } from './base64.js';
3
+ import { getCrypto } from './crypto.js';
4
+ const encoder = new TextEncoder();
5
+ const decoder = new TextDecoder();
6
+ const toUint8Array = function (data) {
7
+ return typeof data === 'string' ? encoder.encode(data) : data;
8
+ };
9
+ const concat = function (...arrays) {
10
+ const total = arrays.reduce((s, a) => s + a.length, 0);
11
+ const out = new Uint8Array(total);
12
+ let offset = 0;
13
+ for (const a of arrays) {
14
+ out.set(a, offset);
15
+ offset += a.length;
16
+ }
17
+ return out;
18
+ };
19
+ const encodeAAD = function (additionalData) {
20
+ if (!additionalData || additionalData.length === 0)
21
+ return undefined;
22
+ return encoder.encode(additionalData.join('\u0000'));
23
+ };
24
+ /**
25
+ * encrypts a string or a Uint8Array
26
+ * @param content - plaintext as string or Uint8Array
27
+ * @param key - key as Uint8Array
28
+ * @param encode - boolean, if plaintext should be base64-encoded
29
+ * @param additionalData - array of strings, with additional Data for integrity and authenticity checks
30
+ * @returns Promise with ciphertext, as base64-encoded string if `encode` is true, as Uint8Array if not
31
+ */
32
+ const encrypt = async function ({ content, key, encode, additionalData }) {
33
+ if (key.length !== 16 && key.length !== 24 && key.length !== 32) {
34
+ throw new Error('Invalid key length. Must be 16, 24 or 32');
35
+ }
36
+ const crypto = getCrypto();
37
+ const plaintext = toUint8Array(content);
38
+ const aad = encodeAAD(additionalData);
39
+ const contentKey = await crypto.subtle.generateKey({ name: 'AES-GCM', length: key.length * 8 }, true, ['encrypt']);
40
+ const iv = crypto.getRandomValues(new Uint8Array(12));
41
+ const ciphertext = new Uint8Array(await crypto.subtle.encrypt({ name: 'AES-GCM', iv, additionalData: aad }, contentKey, plaintext));
42
+ const kek = await importKey(key, 'AES-KW', ['wrapKey']);
43
+ const wrappedKey = new Uint8Array(await crypto.subtle.wrapKey('raw', contentKey, kek, { name: 'AES-KW' }));
44
+ const result = concat(wrappedKey, iv, ciphertext);
45
+ return encode ? encodeBase64(result) : result;
46
+ };
47
+ /**
48
+ * decrypts a string or a Uint8Array
49
+ * @param content - ciphertext as string or Uint8Array
50
+ * @param key - key as Uint8Array
51
+ * @param asString - boolean, if plaintext should be returned as string
52
+ * @param additionalData - array of strings, with additional Data for integrity and authenticity checks
53
+ * @returns Promise with plaintext, as string if `asString` is true, as Uint8Array if not
54
+ */
55
+ const decrypt = async function ({ content, key, asString, additionalData }) {
56
+ if (key.length !== 16 && key.length !== 24 && key.length !== 32) {
57
+ throw new Error('Invalid key length. Must be 16, 24 or 32');
58
+ }
59
+ const crypto = getCrypto();
60
+ const data = typeof content === 'string' ? decodeBase64(content) : content;
61
+ const aad = encodeAAD(additionalData);
62
+ const [wrappedKey, iv, ciphertext] = splitByLengths(data, [key.length + 8, 12]);
63
+ const kek = await importKey(key, 'AES-KW', ['unwrapKey']);
64
+ const contentKey = await crypto.subtle.unwrapKey('raw', wrappedKey.buffer, kek, { name: 'AES-KW' }, { name: 'AES-GCM', length: 256 }, false, [
65
+ 'decrypt'
66
+ ]);
67
+ const plaintext = new Uint8Array(await crypto.subtle.decrypt({ name: 'AES-GCM', iv, additionalData: aad }, contentKey, ciphertext));
68
+ return asString ? decoder.decode(plaintext) : plaintext;
69
+ };
70
+ export { encrypt, decrypt };
71
+ //# sourceMappingURL=encryption.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"encryption.js","sourceRoot":"","sources":["../lib/encryption.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,cAAc,EAAE,MAAM,QAAQ,CAAC;AACnD,OAAO,EAAE,MAAM,IAAI,YAAY,EAAE,MAAM,IAAI,YAAY,EAAE,MAAM,UAAU,CAAC;AAC1E,OAAO,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AAErC,MAAM,OAAO,GAAG,IAAI,WAAW,EAAE,CAA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C"}
@@ -0,0 +1,26 @@
1
+ /**
2
+ * derives key from password
3
+ * @param password - password to derive the key from
4
+ * @param sizeInBytes - desired key size in bytes
5
+ * @param salt - salt as string (optional, new random salt will be used if not provided)
6
+ * @returns Promise with array containing: salt as string, desired key as Uint8Array
7
+ */
8
+ declare const derivePasswordKey: ({ password, sizeInBytes, salt }: {
9
+ password: string;
10
+ sizeInBytes: number;
11
+ salt?: string;
12
+ }) => Promise<[string, Uint8Array]>;
13
+ /**
14
+ * hashes password
15
+ * @param password - password to hash
16
+ * @param sizeInBytes - desired hash size in bytes
17
+ * @param salt - salt as string (optional, new random salt will be used if not provided)
18
+ * @returns Promise with array containing: salt as string, hash as base64-encoded string
19
+ */
20
+ declare const hashPassword: ({ password, sizeInBytes, salt }: {
21
+ password: string;
22
+ sizeInBytes: number;
23
+ salt?: string;
24
+ }) => Promise<[string, string]>;
25
+ export { derivePasswordKey, hashPassword };
26
+ //# sourceMappingURL=hashing.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hashing.d.ts","sourceRoot":"","sources":["../lib/hashing.ts"],"names":[],"mappings":"AASA;;;;;;GAMG;AACH,QAAA,MAAM,iBAAiB,oCAIpB;IACD,QAAQ,EAAE,MAAM,CAAC;IACjB,WAAW,EAAE,MAAM,CAAC;IACpB,IAAI,CAAC,EAAE,MAAM,CAAC;CACf,KAAG,OAAO,CAAC,CAAC,MAAM,EAAE,UAAU,CAAC,CAqB/B,CAAC;AAEF;;;;;;GAMG;AACH,QAAA,MAAM,YAAY,oCAIf;IACD,QAAQ,EAAE,MAAM,CAAC;IACjB,WAAW,EAAE,MAAM,CAAC;IACpB,IAAI,CAAC,EAAE,MAAM,CAAC;CACf,KAAG,OAAO,CAAC,CAAC,MAAM,EAAE,MAAM,CAAC,CAG3B,CAAC;AAEF,OAAO,EAAE,iBAAiB,EAAE,YAAY,EAAE,CAAC"}
@@ -0,0 +1,46 @@
1
+ import { getArgon2Hash } from './argon2.js';
2
+ import { createKey } from './createKey.js';
3
+ import { encode } from './base64.js';
4
+ const createNewSalt = async function () {
5
+ const key = await createKey({ sizeInBytes: 16 });
6
+ return encode(key);
7
+ };
8
+ /**
9
+ * derives key from password
10
+ * @param password - password to derive the key from
11
+ * @param sizeInBytes - desired key size in bytes
12
+ * @param salt - salt as string (optional, new random salt will be used if not provided)
13
+ * @returns Promise with array containing: salt as string, desired key as Uint8Array
14
+ */
15
+ const derivePasswordKey = async function ({ password, sizeInBytes, salt }) {
16
+ if (sizeInBytes < 8) {
17
+ throw new Error('Invalid key length. Must be at least 8');
18
+ }
19
+ salt = salt || (await createNewSalt());
20
+ if (salt.length < 16) {
21
+ throw new Error('Invalid salt length. Must be at least 16');
22
+ }
23
+ const result = await getArgon2Hash()({
24
+ salt,
25
+ pass: password,
26
+ type: 2,
27
+ parallelism: 1,
28
+ hashLen: sizeInBytes,
29
+ time: 2,
30
+ mem: 24_576
31
+ });
32
+ return [salt, result.hash];
33
+ };
34
+ /**
35
+ * hashes password
36
+ * @param password - password to hash
37
+ * @param sizeInBytes - desired hash size in bytes
38
+ * @param salt - salt as string (optional, new random salt will be used if not provided)
39
+ * @returns Promise with array containing: salt as string, hash as base64-encoded string
40
+ */
41
+ const hashPassword = async function ({ password, sizeInBytes, salt }) {
42
+ const [salt_, hash] = await derivePasswordKey({ password, sizeInBytes, salt });
43
+ return [salt_, encode(hash)];
44
+ };
45
+ export { derivePasswordKey, hashPassword };
46
+ //# sourceMappingURL=hashing.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hashing.js","sourceRoot":"","sources":["../lib/hashing.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AACzC,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACxC,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAElC,MAAM,aAAa,GAAG,KAAK;IACzB,MAAM,GAAG,GAAG,MAAM,SAAS,CAAC,EAAE,WAAW,EAAE,EAAE,EAAE,CAAC,CAAC;IACjD,OAAO,MAAM,CAAC,GAAG,CAAC,CAAC;AACrB,CAAC,CAAC;AAEF;;;;;;GAMG;AACH,MAAM,iBAAiB,GAAG,KAAK,WAAW,EACxC,QAAQ,EACR,WAAW,EACX,IAAI,EAKL;IACC,IAAI,WAAW,GAAG,CAAC,EAAE,CAAC;QACpB,MAAM,IAAI,KAAK,CAAC,wCAAwC,CAAC,CAAC;IAC5D,CAAC;IAED,IAAI,GAAG,IAAI,IAAI,CAAC,MAAM,aAAa,EAAE,CAAC,CAAC;IAEvC,IAAI,IAAI,CAAC,MAAM,GAAG,EAAE,EAAE,CAAC;QACrB,MAAM,IAAI,KAAK,CAAC,0CAA0C,CAAC,CAAC;IAC9D,CAAC;IAED,MAAM,MAAM,GAAG,MAAM,aAAa,EAAE,CAAC;QACnC,IAAI;QACJ,IAAI,EAAE,QAAQ;QACd,IAAI,EAAE,CAAC;QACP,WAAW,EAAE,CAAC;QACd,OAAO,EAAE,WAAW;QACpB,IAAI,EAAE,CAAC;QACP,GAAG,EAAE,MAAM;KACZ,CAAC,CAAC;IACH,OAAO,CAAC,IAAI,EAAE,MAAM,CAAC,IAAI,CAAC,CAAC;AAC7B,CAAC,CAAC;AAEF;;;;;;GAMG;AACH,MAAM,YAAY,GAAG,KAAK,WAAW,EACnC,QAAQ,EACR,WAAW,EACX,IAAI,EAKL;IACC,MAAM,CAAC,KAAK,EAAE,IAAI,CAAC,GAAG,MAAM,iBAAiB,CAAC,EAAE,QAAQ,EAAE,WAAW,EAAE,IAAI,EAAE,CAAC,CAAC;IAC/E,OAAO,CAAC,KAAK,EAAE,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC;AAC/B,CAAC,CAAC;AAEF,OAAO,EAAE,iBAAiB,EAAE,YAAY,EAAE,CAAC"}
@@ -0,0 +1,56 @@
1
+ import { createKey } from './createKey.js';
2
+ import { derivePasswordKey, hashPassword } from './hashing.js';
3
+ import { wrapKeys, unwrapKeys } from './wrapping.js';
4
+ import { encrypt, decrypt } from './encryption.js';
5
+ import { sign, verify } from './signing.js';
6
+ declare const _default: {
7
+ createKey: ({ sizeInBytes }: {
8
+ sizeInBytes: number;
9
+ }) => Promise<Uint8Array>;
10
+ derivePasswordKey: ({ password, sizeInBytes, salt }: {
11
+ password: string;
12
+ sizeInBytes: number;
13
+ salt?: string;
14
+ }) => Promise<[string, Uint8Array]>;
15
+ hashPassword: ({ password, sizeInBytes, salt }: {
16
+ password: string;
17
+ sizeInBytes: number;
18
+ salt?: string;
19
+ }) => Promise<[string, string]>;
20
+ wrapKeys: ({ keys, kek, encode }: {
21
+ keys: Uint8Array[];
22
+ kek: Uint8Array;
23
+ encode?: boolean;
24
+ }) => Promise<Uint8Array | string>;
25
+ unwrapKeys: ({ wrappedKeys, kek, lengths }: {
26
+ wrappedKeys: Uint8Array | string;
27
+ kek: Uint8Array;
28
+ lengths?: number[];
29
+ }) => Promise<Uint8Array[]>;
30
+ encrypt: ({ content, key, encode, additionalData }: {
31
+ content: string | Uint8Array;
32
+ key: Uint8Array;
33
+ encode?: boolean;
34
+ additionalData?: string[];
35
+ }) => Promise<Uint8Array | string>;
36
+ decrypt: ({ content, key, asString, additionalData }: {
37
+ content: string | Uint8Array;
38
+ key: Uint8Array;
39
+ asString?: boolean;
40
+ additionalData?: string[];
41
+ }) => Promise<Uint8Array | string>;
42
+ sign: ({ data, key, exclude }: {
43
+ data: Record<string, unknown>;
44
+ key: Uint8Array;
45
+ exclude?: string[];
46
+ }) => Promise<string>;
47
+ verify: ({ data, key, signature, exclude }: {
48
+ data: Record<string, unknown>;
49
+ key: Uint8Array;
50
+ signature: string;
51
+ exclude?: string[];
52
+ }) => Promise<boolean>;
53
+ };
54
+ export default _default;
55
+ export { createKey, derivePasswordKey, hashPassword, wrapKeys, unwrapKeys, encrypt, decrypt, sign, verify };
56
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACxC,OAAO,EAAE,iBAAiB,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AAC5D,OAAO,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAClD,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAChD,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;;;;;;;;YAK4K,CAAC;;;;;YAAk4B,CAAC;;;;;cAA98B,CAAC;;;;;eAA6pC,CAAC;;;;;cAA1X,CAAC;sBAA2B,CAAC;;;;;gBAA25C,CAAC;sBAA2B,CAAC;;;;;eAArsD,CAAC;;;;;;eAAukB,CAAC;;;AAHzwC,wBAAoH;AAEpH,OAAO,EAAE,SAAS,EAAE,iBAAiB,EAAE,YAAY,EAAE,QAAQ,EAAE,UAAU,EAAE,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC"}
package/built/index.js ADDED
@@ -0,0 +1,8 @@
1
+ import { createKey } from './createKey.js';
2
+ import { derivePasswordKey, hashPassword } from './hashing.js';
3
+ import { wrapKeys, unwrapKeys } from './wrapping.js';
4
+ import { encrypt, decrypt } from './encryption.js';
5
+ import { sign, verify } from './signing.js';
6
+ export default { createKey, derivePasswordKey, hashPassword, wrapKeys, unwrapKeys, encrypt, decrypt, sign, verify };
7
+ export { createKey, derivePasswordKey, hashPassword, wrapKeys, unwrapKeys, encrypt, decrypt, sign, verify };
8
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACxC,OAAO,EAAE,iBAAiB,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AAC5D,OAAO,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAClD,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAChD,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AAEzC,eAAe,EAAE,SAAS,EAAE,iBAAiB,EAAE,YAAY,EAAE,QAAQ,EAAE,UAAU,EAAE,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC;AAEpH,OAAO,EAAE,SAAS,EAAE,iBAAiB,EAAE,YAAY,EAAE,QAAQ,EAAE,UAAU,EAAE,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC"}
@@ -0,0 +1,28 @@
1
+ /**
2
+ * signs an object
3
+ * @param data - object to sign
4
+ * @param key - signing key
5
+ * @param exclude - array with names of properties to exclude
6
+ * @returns Promise with signature as base64-encoded string
7
+ */
8
+ declare const sign: ({ data, key, exclude }: {
9
+ data: Record<string, unknown>;
10
+ key: Uint8Array;
11
+ exclude?: string[];
12
+ }) => Promise<string>;
13
+ /**
14
+ * verifies an object
15
+ * @param data - object to verify
16
+ * @param key - signing key
17
+ * @param signature - signature as string to verify the object against
18
+ * @param exclude - array with names of properties to exclude
19
+ * @returns Promise with boolean, stating if object is authentic and integer
20
+ */
21
+ declare const verify: ({ data, key, signature, exclude }: {
22
+ data: Record<string, unknown>;
23
+ key: Uint8Array;
24
+ signature: string;
25
+ exclude?: string[];
26
+ }) => Promise<boolean>;
27
+ export { sign, verify };
28
+ //# sourceMappingURL=signing.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"signing.d.ts","sourceRoot":"","sources":["../lib/signing.ts"],"names":[],"mappings":"AAwBA;;;;;;GAMG;AACH,QAAA,MAAM,IAAI,2BAA2C;IAAE,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAAC,GAAG,EAAE,UAAU,CAAC;IAAC,OAAO,CAAC,EAAE,MAAM,EAAE,CAAA;CAAE,KAAG,OAAO,CAAC,MAAM,CAG3I,CAAC;AAEF;;;;;;;GAOG;AACH,QAAA,MAAM,MAAM,sCAKT;IACD,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAC9B,GAAG,EAAE,UAAU,CAAC;IAChB,SAAS,EAAE,MAAM,CAAC;IAClB,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;CACpB,KAAG,OAAO,CAAC,OAAO,CAGlB,CAAC;AAEF,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC"}
@@ -0,0 +1,45 @@
1
+ import { getCrypto } from './crypto.js';
2
+ const createDataString = function (data, exclude) {
3
+ const filteredData = { ...data };
4
+ for (const ex of exclude ?? []) {
5
+ delete filteredData[ex];
6
+ }
7
+ return JSON.stringify(filteredData);
8
+ };
9
+ const hmac = async function (dataString, key) {
10
+ if (key.length < 8) {
11
+ throw new Error('Invalid key length. Must be at least 8');
12
+ }
13
+ const crypto = getCrypto();
14
+ const encoder = new TextEncoder();
15
+ const cryptoKey = await crypto.subtle.importKey('raw', key, { name: 'HMAC', hash: 'SHA-256' }, false, ['sign']);
16
+ const data = encoder.encode(dataString);
17
+ const signature = await crypto.subtle.sign('HMAC', cryptoKey, data);
18
+ const hmacBytes = new Uint8Array(signature);
19
+ return btoa(String.fromCharCode(...hmacBytes));
20
+ };
21
+ /**
22
+ * signs an object
23
+ * @param data - object to sign
24
+ * @param key - signing key
25
+ * @param exclude - array with names of properties to exclude
26
+ * @returns Promise with signature as base64-encoded string
27
+ */
28
+ const sign = async function ({ data, key, exclude }) {
29
+ const dataString = createDataString(data, exclude);
30
+ return hmac(dataString, key);
31
+ };
32
+ /**
33
+ * verifies an object
34
+ * @param data - object to verify
35
+ * @param key - signing key
36
+ * @param signature - signature as string to verify the object against
37
+ * @param exclude - array with names of properties to exclude
38
+ * @returns Promise with boolean, stating if object is authentic and integer
39
+ */
40
+ const verify = async function ({ data, key, signature, exclude }) {
41
+ const actualSignature = await sign({ data, key, exclude });
42
+ return actualSignature === signature;
43
+ };
44
+ export { sign, verify };
45
+ //# sourceMappingURL=signing.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"signing.js","sourceRoot":"","sources":["../lib/signing.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AAErC,MAAM,gBAAgB,GAAG,UAAU,IAA6B,EAAE,OAAkB;IAClF,MAAM,YAAY,GAAG,EAAE,GAAG,IAAI,EAAE,CAAC;IACjC,KAAK,MAAM,EAAE,IAAI,OAAO,IAAI,EAAE,EAAE,CAAC;QAC/B,OAAO,YAAY,CAAC,EAAE,CAAC,CAAC;IAC1B,CAAC;IACD,OAAO,IAAI,CAAC,SAAS,CAAC,YAAY,CAAC,CAAC;AACtC,CAAC,CAAC;AAEF,MAAM,IAAI,GAAG,KAAK,WAAW,UAAkB,EAAE,GAAe;IAC9D,IAAI,GAAG,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QACnB,MAAM,IAAI,KAAK,CAAC,wCAAwC,CAAC,CAAC;IAC5D,CAAC;IAED,MAAM,MAAM,GAAG,SAAS,EAAE,CAAC;IAC3B,MAAM,OAAO,GAAG,IAAI,WAAW,EAAE,CAAC;IAClC,MAAM,SAAS,GAAG,MAAM,MAAM,CAAC,MAAM,CAAC,SAAS,CAAC,KAAK,EAAE,GAAG,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,SAAS,EAAE,EAAE,KAAK,EAAE,CAAC,MAAM,CAAC,CAAC,CAAC;IAChH,MAAM,IAAI,GAAG,OAAO,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC;IACxC,MAAM,SAAS,GAAG,MAAM,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,EAAE,SAAS,EAAE,IAAI,CAAC,CAAC;IACpE,MAAM,SAAS,GAAG,IAAI,UAAU,CAAC,SAAS,CAAC,CAAC;IAC5C,OAAO,IAAI,CAAC,MAAM,CAAC,YAAY,CAAC,GAAG,SAAS,CAAC,CAAC,CAAC;AACjD,CAAC,CAAC;AAEF;;;;;;GAMG;AACH,MAAM,IAAI,GAAG,KAAK,WAAW,EAAE,IAAI,EAAE,GAAG,EAAE,OAAO,EAA0E;IACzH,MAAM,UAAU,GAAG,gBAAgB,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;IACnD,OAAO,IAAI,CAAC,UAAU,EAAE,GAAG,CAAC,CAAC;AAC/B,CAAC,CAAC;AAEF;;;;;;;GAOG;AACH,MAAM,MAAM,GAAG,KAAK,WAAW,EAC7B,IAAI,EACJ,GAAG,EACH,SAAS,EACT,OAAO,EAMR;IACC,MAAM,eAAe,GAAG,MAAM,IAAI,CAAC,EAAE,IAAI,EAAE,GAAG,EAAE,OAAO,EAAE,CAAC,CAAC;IAC3D,OAAO,eAAe,KAAK,SAAS,CAAC;AACvC,CAAC,CAAC;AAEF,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC"}
@@ -0,0 +1,5 @@
1
+ declare const importKey: (bytes: Uint8Array, name: string, usages: KeyUsage[]) => Promise<CryptoKey>;
2
+ declare const mergeUint8Array: (arrays: Uint8Array[]) => Uint8Array;
3
+ declare const splitByLengths: (array: Uint8Array, lengths: number[]) => Uint8Array[];
4
+ export { importKey, mergeUint8Array, splitByLengths };
5
+ //# sourceMappingURL=util.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"util.d.ts","sourceRoot":"","sources":["../lib/util.ts"],"names":[],"mappings":"AAEA,QAAA,MAAM,SAAS,UAA0B,UAAU,QAAQ,MAAM,UAAU,QAAQ,EAAE,KAAG,OAAO,CAAC,SAAS,CAGxG,CAAC;AAEF,QAAA,MAAM,eAAe,WAAqB,UAAU,EAAE,KAAG,UAWxD,CAAC;AAEF,QAAA,MAAM,cAAc,UAAoB,UAAU,WAAW,MAAM,EAAE,KAAG,UAAU,EAuBjF,CAAC;AAEF,OAAO,EAAE,SAAS,EAAE,eAAe,EAAE,cAAc,EAAE,CAAC"}
package/built/util.js ADDED
@@ -0,0 +1,37 @@
1
+ import { getCrypto } from './crypto.js';
2
+ const importKey = async function (bytes, name, usages) {
3
+ const crypto = getCrypto();
4
+ return await crypto.subtle.importKey('raw', bytes, { name }, true, usages);
5
+ };
6
+ const mergeUint8Array = function (arrays) {
7
+ const totalLength = arrays.reduce((sum, a) => sum + a.length, 0);
8
+ const result = new Uint8Array(totalLength);
9
+ let offset = 0;
10
+ for (const a of arrays) {
11
+ result.set(a, offset);
12
+ offset += a.length;
13
+ }
14
+ return result;
15
+ };
16
+ const splitByLengths = function (array, lengths) {
17
+ if (lengths.length === 0) {
18
+ return [array];
19
+ }
20
+ const result = [];
21
+ let offset = 0;
22
+ for (const len of lengths) {
23
+ if (offset + len > array.length) {
24
+ result.push(array.slice(offset));
25
+ offset = array.length;
26
+ break;
27
+ }
28
+ result.push(array.slice(offset, offset + len));
29
+ offset += len;
30
+ }
31
+ if (offset < array.length) {
32
+ result.push(array.slice(offset));
33
+ }
34
+ return result;
35
+ };
36
+ export { importKey, mergeUint8Array, splitByLengths };
37
+ //# sourceMappingURL=util.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"util.js","sourceRoot":"","sources":["../lib/util.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AAErC,MAAM,SAAS,GAAG,KAAK,WAAW,KAAiB,EAAE,IAAY,EAAE,MAAkB;IACnF,MAAM,MAAM,GAAG,SAAS,EAAE,CAAC;IAC3B,OAAO,MAAM,MAAM,CAAC,MAAM,CAAC,SAAS,CAAC,KAAK,EAAE,KAAK,EAAE,EAAE,IAAI,EAAE,EAAE,IAAI,EAAE,MAAM,CAAC,CAAC;AAC7E,CAAC,CAAC;AAEF,MAAM,eAAe,GAAG,UAAU,MAAoB;IACpD,MAAM,WAAW,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,CAAC,EAAE,EAAE,CAAC,GAAG,GAA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26 @@
1
+ /**
2
+ * wraps/encrypts keys
3
+ * @param keys - keys to wrap/encrypt (length (in bytes) for each key must be multiple of 8)
4
+ * @param kek - key used to encrypt the keys
5
+ * @param encode - boolean, if wrapped keys should be base64-encoded
6
+ * @returns Promise with wrappedKeys, as base64-encoded string if `encode` is true, as Uint8Array if not
7
+ */
8
+ declare const wrapKeys: ({ keys, kek, encode }: {
9
+ keys: Uint8Array[];
10
+ kek: Uint8Array;
11
+ encode?: boolean;
12
+ }) => Promise<Uint8Array | string>;
13
+ /**
14
+ * unwraps/decrypts keys
15
+ * @param wrappedKeys - keys to unwrap/decrypt
16
+ * @param kek - key used to encrypt the keys
17
+ * @param lengths - An array of `number`s, providing the lengths of the unwrapped keys
18
+ * @returns Promise with an array of unwrapped keys
19
+ */
20
+ declare const unwrapKeys: ({ wrappedKeys, kek, lengths }: {
21
+ wrappedKeys: Uint8Array | string;
22
+ kek: Uint8Array;
23
+ lengths?: number[];
24
+ }) => Promise<Uint8Array[]>;
25
+ export { wrapKeys, unwrapKeys };
26
+ //# sourceMappingURL=wrapping.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"wrapping.d.ts","sourceRoot":"","sources":["../lib/wrapping.ts"],"names":[],"mappings":"AAIA;;;;;;GAMG;AACH,QAAA,MAAM,QAAQ,0BAA0C;IAAE,IAAI,EAAE,UAAU,EAAE,CAAC;IAAC,GAAG,EAAE,UAAU,CAAC;IAAC,MAAM,CAAC,EAAE,OAAO,CAAA;CAAE,KAAG,OAAO,CAAC,UAAU,GAAG,MAAM,CAmB9I,CAAC;AAEF;;;;;;GAMG;AACH,QAAA,MAAM,UAAU,kCAIb;IACD,WAAW,EAAE,UAAU,GAAG,MAAM,CAAC;IACjC,GAAG,EAAE,UAAU,CAAC;IAChB,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;CACpB,KAAG,OAAO,CAAC,UAAU,EAAE,CAkBvB,CAAC;AAEF,OAAO,EAAE,QAAQ,EAAE,UAAU,EAAE,CAAC"}
@@ -0,0 +1,52 @@
1
+ import { getCrypto } from './crypto.js';
2
+ import { importKey, mergeUint8Array, splitByLengths } from './util.js';
3
+ import { decode, encode as encodeBase64 } from './base64.js';
4
+ /**
5
+ * wraps/encrypts keys
6
+ * @param keys - keys to wrap/encrypt (length (in bytes) for each key must be multiple of 8)
7
+ * @param kek - key used to encrypt the keys
8
+ * @param encode - boolean, if wrapped keys should be base64-encoded
9
+ * @returns Promise with wrappedKeys, as base64-encoded string if `encode` is true, as Uint8Array if not
10
+ */
11
+ const wrapKeys = async function ({ keys, kek, encode }) {
12
+ if (kek.length !== 16 && kek.length !== 24 && kek.length !== 32) {
13
+ throw new Error('Invalid kek length. Must be 16, 24 or 32');
14
+ }
15
+ const keysLength = keys.reduce((a, b) => a + b.length, 0);
16
+ if (keysLength <= 0 || keysLength % 8 > 0) {
17
+ throw new Error('Invalid keys length. Must be multiple of 8 bytes');
18
+ }
19
+ const crypto = getCrypto();
20
+ const key = mergeUint8Array(keys);
21
+ const keyMaterial = await importKey(key, 'AES-KW', ['wrapKey']);
22
+ const kekMaterial = await importKey(kek, 'AES-KW', ['wrapKey']);
23
+ const wrapped = await crypto.subtle.wrapKey('raw', keyMaterial, kekMaterial, { name: 'AES-KW' });
24
+ if (encode) {
25
+ return encodeBase64(new Uint8Array(wrapped));
26
+ }
27
+ return new Uint8Array(wrapped);
28
+ };
29
+ /**
30
+ * unwraps/decrypts keys
31
+ * @param wrappedKeys - keys to unwrap/decrypt
32
+ * @param kek - key used to encrypt the keys
33
+ * @param lengths - An array of `number`s, providing the lengths of the unwrapped keys
34
+ * @returns Promise with an array of unwrapped keys
35
+ */
36
+ const unwrapKeys = async function ({ wrappedKeys, kek, lengths }) {
37
+ if (kek.length !== 16 && kek.length !== 24 && kek.length !== 32) {
38
+ throw new Error('Invalid kek length. Must be 16, 24 or 32');
39
+ }
40
+ const crypto = getCrypto();
41
+ const kekMaterial = await importKey(kek, 'AES-KW', ['unwrapKey']);
42
+ const wrappedKey = typeof wrappedKeys === 'string' ? decode(wrappedKeys) : wrappedKeys;
43
+ const unwrappedKey = await crypto.subtle.unwrapKey('raw', wrappedKey.buffer, kekMaterial, { name: 'AES-KW' }, { name: 'AES-KW' }, true, [
44
+ 'wrapKey',
45
+ 'unwrapKey'
46
+ ]);
47
+ const rawKey = await crypto.subtle.exportKey('raw', unwrappedKey);
48
+ const key = new Uint8Array(rawKey);
49
+ return splitByLengths(key, lengths ?? []);
50
+ };
51
+ export { wrapKeys, unwrapKeys };
52
+ //# sourceMappingURL=wrapping.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"wrapping.js","sourceRoot":"","sources":["../lib/wrapping.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AACrC,OAAO,EAAE,SAAS,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,QAAQ,CAAC;AACpE,OAAO,EAAE,MAAM,EAAE,MAAM,IAAI,YAAY,EAAE,MAAM,UAAU,CAAC;AAE1D;;;;;;GAMG;AACH,MAAM,QAAQ,GAAG,KAAK,WAAW,EAAE,IAAI,EAAE,GAAG,EAAE,MAAM,EAA6D;IAC/G,IAAI,GAAG,CAAC,MAAM,KAAK,EAAE,IAAI,GAAG,CAAC,MAAM,KAAK,EAAE,IAAI,GAAG,CAAC,MAAM,KAAK,EAAE,EAAE,CAAC;QAChE,MAAM,IAAI,KAAK,CAAC,0CAA0C,CAAC,CAAC;IAC9D,CAAC;IACD,MAAM,UAAU,GAAG,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC;IAC1D,IAAI,UAAU,IAAI,CAAC,IAAI,UAAU,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC;QAC1C,MAAM,IAAI,KAAK,CAAC,kDAAkD,CAAC,CAAC;IACtE,CAAC;IAED,MAAM,MAAM,GAAG,SAAS,E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,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,EAAE,IAAI,EAAE;QACtI,SAAS;QACT,WAAW;KACZ,CAAC,CAAC;IAEH,MAAM,MAAM,GAAG,MAAM,MAAM,CAAC,MAAM,CAAC,SAAS,CAAC,KAAK,EAAE,YAAY,CAAC,CAAC;IAClE,MAAM,GAAG,GAAG,IAAI,UAAU,CAAC,MAAM,CAAC,CAAC;IAEnC,OAAO,cAAc,CAAC,GAAG,EAAE,OAAO,IAAI,EAAE,CAAC,CAAC;AAC5C,CAAC,CAAC;AAEF,OAAO,EAAE,QAAQ,EAAE,UAAU,EAAE,CAAC"}
package/package.json ADDED
@@ -0,0 +1,60 @@
1
+ {
2
+ "name": "web-vault-crypto",
3
+ "version": "0.0.1",
4
+ "description": "Crypto-Library for e2e encrypted web vaults, using native web crypto api, written in typescript, easy to use.",
5
+ "private": false,
6
+ "type": "module",
7
+ "main": "build/index.js",
8
+ "types": "build/index.d.ts",
9
+ "files": [
10
+ "built"
11
+ ],
12
+ "scripts": {
13
+ "lint": "eslint .",
14
+ "lint:fix": "eslint . --fix",
15
+ "test": "jest --runInBand --verbose --detectOpenHandles --forceExit",
16
+ "audit": "better-npm-audit audit",
17
+ "qa": "npm run lint && npm run audit && npm run test",
18
+ "build": "tsc --project tsconfig.json && tsc-alias -f -p tsconfig.json && webpack"
19
+ },
20
+ "devDependencies": {
21
+ "@eslint/compat": "^2.0.2",
22
+ "@eslint/eslintrc": "^3.3.3",
23
+ "@eslint/js": "^9.26.0",
24
+ "@types/argon2-browser": "^1.18.4",
25
+ "@types/jest": "29.5.13",
26
+ "@typescript-eslint/eslint-plugin": "8.10.0",
27
+ "argon2": "^0.44.0",
28
+ "base64-loader": "^1.0.0",
29
+ "better-npm-audit": "^3.11.0",
30
+ "eslint": "9.26.0",
31
+ "eslint-config-prettier": "9.1.0",
32
+ "eslint-plugin-prettier": "^5.5.5",
33
+ "globals": "^17.3.0",
34
+ "jest": "29.7.0",
35
+ "jest-environment-jsdom": "^30.2.0",
36
+ "prettier": "3.3.3",
37
+ "ts-jest": "29.2.5",
38
+ "ts-node": "^10.9.2",
39
+ "tsc-alias": "^1.8.16",
40
+ "typescript": "5.6.3",
41
+ "typescript-eslint": "^8.54.0",
42
+ "webpack": "^5.105.1",
43
+ "webpack-cli": "^6.0.1"
44
+ },
45
+ "repository": {
46
+ "type": "git",
47
+ "url": "git+https://github.com/johanna-herrmann/web-vault-org/web-vault-crypto.git"
48
+ },
49
+ "keywords": [
50
+ "encryption",
51
+ "crypto",
52
+ "web",
53
+ "vault"
54
+ ],
55
+ "author": "Johanna Herrmann",
56
+ "license": "MIT",
57
+ "dependencies": {
58
+ "argon2-browser": "^1.18.0"
59
+ }
60
+ }