@tstdl/base 0.93.178 → 0.93.180

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.
Files changed (207) hide show
  1. package/api/response.js +4 -3
  2. package/api/server/gateway.js +9 -3
  3. package/audit/auditor.d.ts +1 -2
  4. package/audit/drizzle/{0000_lumpy_thunderball.sql → 0000_shallow_elektra.sql} +1 -1
  5. package/audit/drizzle/meta/0000_snapshot.json +2 -2
  6. package/audit/drizzle/meta/_journal.json +2 -2
  7. package/authentication/README.md +87 -42
  8. package/authentication/authentication.api.d.ts +392 -53
  9. package/authentication/authentication.api.js +133 -28
  10. package/authentication/client/api.client.d.ts +3 -3
  11. package/authentication/client/api.client.js +4 -4
  12. package/authentication/client/authentication.service.d.ts +93 -23
  13. package/authentication/client/authentication.service.js +113 -28
  14. package/authentication/client/http-client.middleware.d.ts +1 -1
  15. package/authentication/client/http-client.middleware.js +5 -4
  16. package/authentication/client/module.d.ts +1 -1
  17. package/authentication/client/module.js +2 -2
  18. package/authentication/errors/index.d.ts +1 -1
  19. package/authentication/errors/index.js +1 -1
  20. package/authentication/errors/password-requirements.error.d.ts +5 -0
  21. package/authentication/errors/{secret-requirements.error.js → password-requirements.error.js} +2 -2
  22. package/authentication/models/authentication-password.model.d.ts +8 -0
  23. package/authentication/models/{authentication-credentials.model.js → authentication-password.model.js} +11 -17
  24. package/authentication/models/authentication-session.model.d.ts +0 -2
  25. package/authentication/models/authentication-session.model.js +1 -7
  26. package/authentication/models/authentication-totp-recovery-code.model.d.ts +6 -0
  27. package/authentication/models/authentication-totp-recovery-code.model.js +34 -0
  28. package/authentication/models/authentication-totp.model.d.ts +19 -0
  29. package/authentication/models/authentication-totp.model.js +51 -0
  30. package/authentication/models/authentication-used-totp-token.model.d.ts +5 -0
  31. package/authentication/models/authentication-used-totp-token.model.js +32 -0
  32. package/authentication/models/index.d.ts +6 -3
  33. package/authentication/models/index.js +6 -3
  34. package/authentication/models/{init-secret-reset-data.model.d.ts → init-password-reset-data.model.d.ts} +3 -3
  35. package/authentication/models/{init-secret-reset-data.model.js → init-password-reset-data.model.js} +5 -5
  36. package/authentication/models/password-check-result.model.d.ts +3 -0
  37. package/authentication/models/{secret-check-result.model.js → password-check-result.model.js} +6 -6
  38. package/authentication/models/subject.model.d.ts +0 -6
  39. package/authentication/models/subject.model.js +0 -6
  40. package/authentication/models/token.model.d.ts +16 -2
  41. package/authentication/server/authentication-ancillary.service.d.ts +6 -6
  42. package/authentication/server/authentication-ancillary.service.js +1 -1
  43. package/authentication/server/authentication-password-requirements.validator.d.ts +55 -0
  44. package/authentication/server/{authentication-secret-requirements.validator.js → authentication-password-requirements.validator.js} +22 -22
  45. package/authentication/server/authentication.api-controller.d.ts +55 -27
  46. package/authentication/server/authentication.api-controller.js +214 -39
  47. package/authentication/server/authentication.audit.d.ts +42 -5
  48. package/authentication/server/authentication.service.d.ts +182 -93
  49. package/authentication/server/authentication.service.js +628 -206
  50. package/authentication/server/drizzle/{0000_soft_tag.sql → 0000_odd_echo.sql} +59 -13
  51. package/authentication/server/drizzle/meta/0000_snapshot.json +345 -32
  52. package/authentication/server/drizzle/meta/_journal.json +2 -2
  53. package/authentication/server/helper.d.ts +16 -16
  54. package/authentication/server/helper.js +33 -34
  55. package/authentication/server/index.d.ts +1 -1
  56. package/authentication/server/index.js +1 -1
  57. package/authentication/server/module.d.ts +2 -2
  58. package/authentication/server/module.js +4 -2
  59. package/authentication/server/schemas.d.ts +11 -7
  60. package/authentication/server/schemas.js +7 -3
  61. package/authentication/tests/authentication-password-requirements.validator.test.js +29 -0
  62. package/authentication/tests/authentication.api-controller.test.js +49 -15
  63. package/authentication/tests/authentication.client-error-handling.test.js +3 -2
  64. package/authentication/tests/authentication.client-middleware.test.js +5 -5
  65. package/authentication/tests/authentication.client-service-methods.test.js +28 -14
  66. package/authentication/tests/authentication.client-service-refresh.test.js +7 -6
  67. package/authentication/tests/authentication.client-service.test.js +10 -8
  68. package/authentication/tests/authentication.service.test.js +37 -29
  69. package/authentication/tests/authentication.test-ancillary-service.d.ts +1 -1
  70. package/authentication/tests/authentication.test-ancillary-service.js +1 -1
  71. package/authentication/tests/brute-force-protection.test.js +211 -0
  72. package/authentication/tests/helper.test.js +25 -21
  73. package/authentication/tests/password-requirements.error.test.js +14 -0
  74. package/authentication/tests/remember.api.test.js +22 -14
  75. package/authentication/tests/remember.service.test.js +23 -16
  76. package/authentication/tests/subject.service.test.js +2 -2
  77. package/authentication/tests/suspended-subject.test.d.ts +1 -0
  78. package/authentication/tests/suspended-subject.test.js +120 -0
  79. package/authentication/tests/totp.enrollment.test.d.ts +1 -0
  80. package/authentication/tests/totp.enrollment.test.js +123 -0
  81. package/authentication/tests/totp.login.test.d.ts +1 -0
  82. package/authentication/tests/totp.login.test.js +213 -0
  83. package/authentication/tests/totp.recovery-codes.test.d.ts +1 -0
  84. package/authentication/tests/totp.recovery-codes.test.js +97 -0
  85. package/authentication/tests/totp.status.test.d.ts +1 -0
  86. package/authentication/tests/totp.status.test.js +72 -0
  87. package/circuit-breaker/postgres/drizzle/{0000_cooing_korath.sql → 0000_same_captain_cross.sql} +1 -1
  88. package/circuit-breaker/postgres/drizzle/meta/0000_snapshot.json +2 -2
  89. package/circuit-breaker/postgres/drizzle/meta/_journal.json +2 -2
  90. package/cryptography/cryptography.d.ts +336 -0
  91. package/cryptography/cryptography.js +328 -0
  92. package/cryptography/index.d.ts +4 -0
  93. package/cryptography/index.js +4 -0
  94. package/{utils → cryptography}/jwt.d.ts +22 -4
  95. package/{utils → cryptography}/jwt.js +36 -18
  96. package/cryptography/module.d.ts +35 -0
  97. package/cryptography/module.js +148 -0
  98. package/cryptography/tests/cryptography.test.d.ts +1 -0
  99. package/cryptography/tests/cryptography.test.js +175 -0
  100. package/cryptography/tests/jwt.test.d.ts +1 -0
  101. package/cryptography/tests/jwt.test.js +54 -0
  102. package/cryptography/tests/modern.test.d.ts +1 -0
  103. package/cryptography/tests/modern.test.js +105 -0
  104. package/cryptography/tests/module.test.d.ts +1 -0
  105. package/cryptography/tests/module.test.js +100 -0
  106. package/cryptography/tests/totp.test.d.ts +1 -0
  107. package/cryptography/tests/totp.test.js +108 -0
  108. package/cryptography/totp.d.ts +96 -0
  109. package/cryptography/totp.js +123 -0
  110. package/document-management/server/drizzle/{0000_curious_nighthawk.sql → 0000_sharp_scream.sql} +21 -21
  111. package/document-management/server/drizzle/meta/0000_snapshot.json +22 -22
  112. package/document-management/server/drizzle/meta/_journal.json +2 -2
  113. package/document-management/server/services/document-file.service.js +1 -1
  114. package/errors/errors.localization.d.ts +2 -2
  115. package/errors/errors.localization.js +2 -2
  116. package/errors/index.d.ts +1 -0
  117. package/errors/index.js +1 -0
  118. package/errors/too-many-requests.error.d.ts +5 -0
  119. package/errors/too-many-requests.error.js +7 -0
  120. package/examples/api/authentication.js +5 -5
  121. package/examples/api/custom-authentication.js +4 -3
  122. package/file/server/mime-type.js +1 -1
  123. package/http/http-body.d.ts +1 -0
  124. package/http/http-body.js +3 -0
  125. package/image-service/imgproxy/imgproxy-image-service.d.ts +0 -1
  126. package/image-service/imgproxy/imgproxy-image-service.js +9 -27
  127. package/key-value-store/postgres/drizzle/{0000_shocking_slipstream.sql → 0000_moaning_calypso.sql} +1 -1
  128. package/key-value-store/postgres/drizzle/meta/0000_snapshot.json +2 -2
  129. package/key-value-store/postgres/drizzle/meta/_journal.json +2 -2
  130. package/lock/postgres/drizzle/{0000_busy_tattoo.sql → 0000_nappy_wraith.sql} +1 -1
  131. package/lock/postgres/drizzle/meta/0000_snapshot.json +2 -2
  132. package/lock/postgres/drizzle/meta/_journal.json +2 -2
  133. package/logger/formatters/json.js +1 -1
  134. package/logger/formatters/pretty-print.js +1 -1
  135. package/mail/drizzle/{0000_numerous_the_watchers.sql → 0000_cultured_quicksilver.sql} +2 -2
  136. package/mail/drizzle/meta/0000_snapshot.json +4 -4
  137. package/mail/drizzle/meta/_journal.json +2 -9
  138. package/notification/server/drizzle/{0000_wise_pyro.sql → 0000_new_tenebrous.sql} +6 -6
  139. package/notification/server/drizzle/meta/0000_snapshot.json +7 -7
  140. package/notification/server/drizzle/meta/_journal.json +2 -2
  141. package/notification/tests/notification-flow.test.js +1 -8
  142. package/notification/tests/notification-type.service.test.js +3 -3
  143. package/openid-connect/oidc.service.js +2 -3
  144. package/orm/data-types/common.js +1 -1
  145. package/orm/server/drizzle/schema-converter.js +9 -4
  146. package/orm/server/encryption.js +1 -1
  147. package/orm/server/module.d.ts +0 -1
  148. package/orm/server/module.js +0 -4
  149. package/orm/server/repository.d.ts +2 -1
  150. package/orm/server/repository.js +7 -10
  151. package/orm/tests/encryption.test.js +4 -6
  152. package/orm/tests/repository-extra-coverage.test.js +0 -2
  153. package/orm/tests/repository-regression.test.js +0 -3
  154. package/package.json +9 -8
  155. package/password/README.md +1 -1
  156. package/password/have-i-been-pwned.js +1 -1
  157. package/rate-limit/postgres/drizzle/{0000_watery_rage.sql → 0000_serious_sauron.sql} +1 -1
  158. package/rate-limit/postgres/drizzle/meta/0000_snapshot.json +2 -2
  159. package/rate-limit/postgres/drizzle/meta/_journal.json +2 -2
  160. package/rate-limit/postgres/postgres-rate-limiter.d.ts +1 -1
  161. package/rate-limit/postgres/postgres-rate-limiter.js +1 -1
  162. package/rate-limit/rate-limiter.d.ts +1 -1
  163. package/rpc/tests/rpc.integration.test.js +25 -31
  164. package/supports.d.ts +1 -0
  165. package/supports.js +1 -0
  166. package/task-queue/postgres/drizzle/{0000_faithful_daimon_hellstrom.sql → 0000_dark_ronan.sql} +5 -5
  167. package/task-queue/postgres/drizzle/meta/0000_snapshot.json +10 -10
  168. package/task-queue/postgres/drizzle/meta/_journal.json +2 -9
  169. package/task-queue/postgres/task-queue.js +2 -2
  170. package/task-queue/tests/coverage-enhancement.test.js +2 -2
  171. package/test/drizzle/{0000_natural_cannonball.sql → 0000_organic_gamora.sql} +2 -2
  172. package/test/drizzle/meta/0000_snapshot.json +3 -4
  173. package/test/drizzle/meta/_journal.json +2 -9
  174. package/testing/integration-setup.d.ts +7 -3
  175. package/testing/integration-setup.js +119 -96
  176. package/utils/alphabet.d.ts +1 -0
  177. package/utils/alphabet.js +1 -0
  178. package/utils/base32.d.ts +4 -0
  179. package/utils/base32.js +49 -0
  180. package/utils/base64.d.ts +0 -2
  181. package/utils/base64.js +6 -70
  182. package/utils/equals.d.ts +13 -3
  183. package/utils/equals.js +29 -9
  184. package/utils/index.d.ts +1 -2
  185. package/utils/index.js +1 -2
  186. package/utils/random.d.ts +1 -0
  187. package/utils/random.js +14 -8
  188. package/authentication/errors/secret-requirements.error.d.ts +0 -5
  189. package/authentication/models/authentication-credentials.model.d.ts +0 -10
  190. package/authentication/models/secret-check-result.model.d.ts +0 -3
  191. package/authentication/server/authentication-secret-requirements.validator.d.ts +0 -55
  192. package/authentication/tests/authentication-ancillary.service.test.js +0 -13
  193. package/authentication/tests/authentication-secret-requirements.validator.test.js +0 -29
  194. package/authentication/tests/secret-requirements.error.test.js +0 -14
  195. package/mail/drizzle/0001_married_tarantula.sql +0 -12
  196. package/mail/drizzle/meta/0001_snapshot.json +0 -69
  197. package/orm/server/tokens.d.ts +0 -1
  198. package/orm/server/tokens.js +0 -2
  199. package/task-queue/postgres/drizzle/0001_rapid_infant_terrible.sql +0 -16
  200. package/task-queue/postgres/drizzle/meta/0001_snapshot.json +0 -753
  201. package/test/drizzle/0001_closed_the_captain.sql +0 -2
  202. package/test/drizzle/meta/0001_snapshot.json +0 -117
  203. package/utils/cryptography.d.ts +0 -137
  204. package/utils/cryptography.js +0 -201
  205. /package/authentication/tests/{authentication-ancillary.service.test.d.ts → authentication-password-requirements.validator.test.d.ts} +0 -0
  206. /package/authentication/tests/{authentication-secret-requirements.validator.test.d.ts → brute-force-protection.test.d.ts} +0 -0
  207. /package/authentication/tests/{secret-requirements.error.test.d.ts → password-requirements.error.test.d.ts} +0 -0
@@ -1,2 +0,0 @@
1
- DROP INDEX "test"."test_parade_idx";--> statement-breakpoint
2
- CREATE INDEX "test_parade_idx" ON "test"."test" USING bm25 ("id","language","title","content","tags",(("title" || ' ' || "content" || ' ' || "tags")::pdb.simple('alias=search_text')),(('foo')::pdb.simple('alias=foo'))) WITH (key_field='id');
@@ -1,117 +0,0 @@
1
- {
2
- "id": "68a0bdde-47d3-4390-b409-27a74dd269f3",
3
- "prevId": "bbffc0f2-678b-42e9-8121-1731a04b8987",
4
- "version": "7",
5
- "dialect": "postgresql",
6
- "tables": {
7
- "test.test": {
8
- "name": "test",
9
- "schema": "test",
10
- "columns": {
11
- "id": {
12
- "name": "id",
13
- "type": "uuid",
14
- "primaryKey": true,
15
- "notNull": true,
16
- "default": "gen_random_uuid()"
17
- },
18
- "title": {
19
- "name": "title",
20
- "type": "text",
21
- "primaryKey": false,
22
- "notNull": true
23
- },
24
- "content": {
25
- "name": "content",
26
- "type": "text",
27
- "primaryKey": false,
28
- "notNull": true
29
- },
30
- "tags": {
31
- "name": "tags",
32
- "type": "text",
33
- "primaryKey": false,
34
- "notNull": true
35
- },
36
- "language": {
37
- "name": "language",
38
- "type": "text",
39
- "primaryKey": false,
40
- "notNull": true
41
- }
42
- },
43
- "indexes": {
44
- "test_parade_idx": {
45
- "name": "test_parade_idx",
46
- "columns": [
47
- {
48
- "expression": "id",
49
- "isExpression": false,
50
- "asc": true,
51
- "nulls": "last"
52
- },
53
- {
54
- "expression": "\"language\"",
55
- "asc": true,
56
- "isExpression": true,
57
- "nulls": "last"
58
- },
59
- {
60
- "expression": "\"title\"",
61
- "asc": true,
62
- "isExpression": true,
63
- "nulls": "last"
64
- },
65
- {
66
- "expression": "\"content\"",
67
- "asc": true,
68
- "isExpression": true,
69
- "nulls": "last"
70
- },
71
- {
72
- "expression": "\"tags\"",
73
- "asc": true,
74
- "isExpression": true,
75
- "nulls": "last"
76
- },
77
- {
78
- "expression": "((\"title\" || ' ' || \"content\" || ' ' || \"tags\")::pdb.simple('alias=search_text'))",
79
- "asc": true,
80
- "isExpression": true,
81
- "nulls": "last"
82
- },
83
- {
84
- "expression": "(('foo')::pdb.simple('alias=foo'))",
85
- "asc": true,
86
- "isExpression": true,
87
- "nulls": "last"
88
- }
89
- ],
90
- "isUnique": false,
91
- "concurrently": false,
92
- "method": "bm25",
93
- "with": {
94
- "key_field": "'id'"
95
- }
96
- }
97
- },
98
- "foreignKeys": {},
99
- "compositePrimaryKeys": {},
100
- "uniqueConstraints": {},
101
- "policies": {},
102
- "checkConstraints": {},
103
- "isRLSEnabled": false
104
- }
105
- },
106
- "enums": {},
107
- "schemas": {},
108
- "sequences": {},
109
- "roles": {},
110
- "policies": {},
111
- "views": {},
112
- "_meta": {
113
- "columns": {},
114
- "schemas": {},
115
- "tables": {}
116
- }
117
- }
@@ -1,137 +0,0 @@
1
- import type { BinaryData, TypedExtract } from '../types/index.js';
2
- import type { ReadonlyTuple } from 'type-fest';
3
- export type AesMode = 'CBC' | 'CTR' | 'GCM' | 'KW';
4
- export type EcdsaCurve = 'P-256' | 'P-384' | 'P-521';
5
- export type HashAlgorithm = 'SHA-1' | 'SHA-256' | 'SHA-384' | 'SHA-512';
6
- export type SymmetricAlgorithm = `AES-${AesMode}`;
7
- export type AsymmetricAlgorithm = 'RSASSA-PKCS1-v1_5' | 'RSA-PSS' | 'RSA-OAEP' | 'ECDSA' | 'ECDH' | 'NODE-DSA' | 'NODE-DH' | 'NODE-ED25519' | 'NODE-ED448';
8
- export type CryptionAlgorithm = Parameters<typeof globalThis.crypto.subtle.encrypt>[0];
9
- export type SignAlgorithm = Parameters<typeof globalThis.crypto.subtle.sign>[0];
10
- export type KeyAlgorithm = Parameters<typeof globalThis.crypto.subtle.generateKey>[0];
11
- export type DeriveAlgorithm = Parameters<typeof globalThis.crypto.subtle.deriveBits>['0'];
12
- export type KeyType = 'raw' | 'pkcs8' | 'spki' | 'jwk';
13
- export type Key = JsonWebKey | BinaryData<ArrayBuffer>;
14
- export type ScryptOptions = {
15
- cost?: number;
16
- blockSize?: number;
17
- parallelization?: number;
18
- maximumMemory?: number;
19
- };
20
- export interface CryptionResult {
21
- toBuffer(): Promise<ArrayBuffer>;
22
- toHex(): Promise<string>;
23
- toBase64(): Promise<string>;
24
- toBase64Url(): Promise<string>;
25
- toZBase32(): Promise<string>;
26
- }
27
- export interface DecryptionResult extends CryptionResult {
28
- toUtf8(): Promise<string>;
29
- }
30
- export type DigestResult = CryptionResult;
31
- export type SignResult = CryptionResult;
32
- /**
33
- * Encrypt data
34
- * @param algorithm algorithm as supported by Web Crypto API
35
- * @param key key
36
- * @param data data to encrypt. Encodes string to utf8
37
- */
38
- export declare function encrypt(algorithm: CryptionAlgorithm, key: CryptoKey, data: BinaryData<ArrayBuffer> | string): CryptionResult;
39
- /**
40
- * Decrypt data
41
- * @param algorithm algorithm as supported by Web Crypto API
42
- * @param key key
43
- * @param data data to decrypt
44
- */
45
- export declare function decrypt(algorithm: CryptionAlgorithm, key: CryptoKey, bytes: BinaryData<ArrayBuffer>): DecryptionResult;
46
- /**
47
- * Hashes data
48
- * @param algorithm algorithm as supported by Web Crypto API
49
- * @param data data to encrypt. Encodes string to utf8
50
- */
51
- export declare function digest(algorithm: HashAlgorithmIdentifier, data: BinaryData<ArrayBuffer> | string): DigestResult;
52
- /**
53
- * Signs data
54
- * @param algorithm algorithm as supported by Web Crypto API
55
- * @param key key
56
- * @param data data to sign
57
- */
58
- export declare function sign(algorithm: SignAlgorithm, key: CryptoKey, data: BinaryData<ArrayBuffer> | string): SignResult;
59
- /**
60
- * Verifies data
61
- * @param algorithm algorithm as supported by Web Crypto API
62
- * @param key key
63
- * @param signature signature
64
- * @param data data to verify using provided signature
65
- */
66
- export declare function verify(algorithm: SignAlgorithm, key: CryptoKey, signature: BinaryData<ArrayBuffer> | string, data: BinaryData<ArrayBuffer> | string): Promise<boolean>;
67
- /**
68
- * Imports a HMAC CryptoKey
69
- * @param algorithm hash algorithm
70
- * @param key JWK or binary key
71
- * @param extractable whether the key can be used for exportKey
72
- */
73
- export declare function importHmacKey(algorithm: HashAlgorithmIdentifier, key: Key | string, extractable?: boolean): Promise<CryptoKey>;
74
- /**
75
- * Imports a CryptoKey for symmetric encryption
76
- * @param algorithm symmetric algorithm
77
- * @param length key length
78
- * @param key JWK or binary key
79
- * @param extractable whether the key can be used for exportKey
80
- */
81
- export declare function importSymmetricKey(algorithm: SymmetricAlgorithm, length: 128 | 192 | 256, key: Key | string, extractable?: boolean): Promise<CryptoKey>;
82
- /**
83
- * Imports an ECDSA CryptoKey
84
- * @param curve ECDSA curve
85
- * @param key JWK or DER encoded key
86
- * @param extractable whether the key can be used for exportKey
87
- */
88
- export declare function importEcdsaKey(curve: EcdsaCurve, key: Key | string, extractable?: boolean): Promise<CryptoKey>;
89
- /**
90
- * Import a HKDF CryptoKey
91
- * @param key binary key
92
- * @param extractable whether the key can be used for exportKey
93
- */
94
- export declare function importHkdfKey(key: BinaryData<ArrayBuffer> | string, extractable?: boolean): Promise<CryptoKey>;
95
- /**
96
- * Import a pbkdf2 CryptoKey
97
- * @param key binary key
98
- * @param extractable whether the key can be used for exportKey
99
- */
100
- export declare function importPbkdf2Key(key: BinaryData<ArrayBuffer> | string, extractable?: boolean): Promise<CryptoKey>;
101
- /**
102
- * Generates a new ECDSA CryptoKeyPair
103
- * @param curve ECDSA cruve to use
104
- * @param extractable whether the key can be used for exportKey
105
- * @param usages whether to generate a key for signing, verifiying or both. Defaults to both
106
- */
107
- export declare function generateEcdsaKey(curve: EcdsaCurve, extractable?: boolean, usages?: TypedExtract<KeyUsage, 'sign' | 'verify'>[]): Promise<CryptoKeyPair>;
108
- /**
109
- * Convenience wrapper for HKDF derivation
110
- * @param baseKey The imported HKDF key
111
- * @param lengths Array of lengths for each derived secret
112
- * @param info Contextual string to prevent different apps from deriving the same keys
113
- * @param salt Optional salt (usually empty if the input key is high-entropy)
114
- * @param hash Hash algorithm to use
115
- */
116
- export declare function deriveHkdfSecrets<const Lengths extends readonly number[]>(baseKey: CryptoKey, lengths: Lengths, info?: string | BinaryData<ArrayBuffer>, salt?: BinaryData<ArrayBuffer>, hash?: HashAlgorithm): Promise<Readonly<import("type-fest").If<import("type-fest").IsAny<Lengths["length"]>, Uint8Array<ArrayBuffer>[], import("type-fest").If<import("type-fest").IsNever<Lengths["length"]>, [], number extends import("type-fest").If<import("type-fest").IsNegative<Lengths["length"]>, 0, Lengths["length"]> ? Uint8Array<ArrayBuffer>[] : import("type-fest").If<import("type-fest").IsNegative<Lengths["length"]>, 0, Lengths["length"]> extends infer T ? T extends import("type-fest").If<import("type-fest").IsNegative<Lengths["length"]>, 0, Lengths["length"]> ? T extends 0 ? [] : number extends T ? Uint8Array<ArrayBuffer>[] : T extends 1 ? [Uint8Array<ArrayBuffer>] : number extends T ? Uint8Array<ArrayBuffer>[] : T extends 2 ? [Uint8Array<ArrayBuffer>, Uint8Array<ArrayBuffer>] : number extends T ? Uint8Array<ArrayBuffer>[] : T extends 3 ? [Uint8Array<ArrayBuffer>, Uint8Array<ArrayBuffer>, Uint8Array<ArrayBuffer>] : number extends T ? Uint8Array<ArrayBuffer>[] : T extends 4 ? [Uint8Array<ArrayBuffer>, Uint8Array<ArrayBuffer>, Uint8Array<ArrayBuffer>, Uint8Array<ArrayBuffer>] : number extends T ? Uint8Array<ArrayBuffer>[] : T extends 5 ? [Uint8Array<ArrayBuffer>, Uint8Array<ArrayBuffer>, Uint8Array<ArrayBuffer>, Uint8Array<ArrayBuffer>, Uint8Array<ArrayBuffer>] : number extends T ? Uint8Array<ArrayBuffer>[] : T extends 6 ? [Uint8Array<ArrayBuffer>, Uint8Array<ArrayBuffer>, Uint8Array<ArrayBuffer>, Uint8Array<ArrayBuffer>, Uint8Array<ArrayBuffer>, Uint8Array<ArrayBuffer>] : number extends T ? Uint8Array<ArrayBuffer>[] : T extends 7 ? [Uint8Array<ArrayBuffer>, Uint8Array<ArrayBuffer>, Uint8Array<ArrayBuffer>, Uint8Array<ArrayBuffer>, Uint8Array<ArrayBuffer>, Uint8Array<ArrayBuffer>, Uint8Array<ArrayBuffer>] : number extends T ? Uint8Array<ArrayBuffer>[] : T extends 8 ? [Uint8Array<ArrayBuffer>, Uint8Array<ArrayBuffer>, Uint8Array<ArrayBuffer>, Uint8Array<ArrayBuffer>, Uint8Array<ArrayBuffer>, Uint8Array<ArrayBuffer>, Uint8Array<ArrayBuffer>, Uint8Array<ArrayBuffer>] : number extends T ? Uint8Array<ArrayBuffer>[] : T extends 9 ? [Uint8Array<ArrayBuffer>, Uint8Array<ArrayBuffer>, Uint8Array<ArrayBuffer>, Uint8Array<ArrayBuffer>, Uint8Array<ArrayBuffer>, Uint8Array<ArrayBuffer>, Uint8Array<ArrayBuffer>, Uint8Array<ArrayBuffer>, Uint8Array<ArrayBuffer>] : number extends T ? Uint8Array<ArrayBuffer>[] : T extends 10 ? [Uint8Array<ArrayBuffer>, Uint8Array<ArrayBuffer>, Uint8Array<ArrayBuffer>, Uint8Array<ArrayBuffer>, Uint8Array<ArrayBuffer>, Uint8Array<ArrayBuffer>, Uint8Array<ArrayBuffer>, Uint8Array<ArrayBuffer>, Uint8Array<ArrayBuffer>, Uint8Array<ArrayBuffer>] : /*elided*/ any : never : never>>>>;
117
- /**
118
- * Generates a pbkdf2 CryptoKey
119
- * @param extractable whether the key can be used for exportKey
120
- */
121
- export declare function generatePbkdf2Key(extractable?: boolean): Promise<CryptoKey>;
122
- /**
123
- * Derive byte array from key
124
- * @param length length in bytes
125
- * @param algorithm algorithm to derive with
126
- * @param baseKey key to derive from
127
- */
128
- export declare function deriveBytes(algorithm: DeriveAlgorithm, baseKey: CryptoKey, length: number): Promise<Uint8Array>;
129
- /**
130
- * Derive multiply byte arrays from key
131
- * @param algorithm algorithm to derive with
132
- * @param baseKey key to derive from
133
- * @param length length of each Uint8Array in bytes, if single number is provided, it is used for every array
134
- * @param count how many Uint8Arrays to derive
135
- */
136
- export declare function deriveBytesMultiple<const Lengths extends readonly number[]>(algorithm: DeriveAlgorithm, baseKey: CryptoKey, lengths: Lengths): Promise<ReadonlyTuple<Uint8Array<ArrayBuffer>, Lengths['length']>>;
137
- export declare function deriveBytesMultiple<const C extends number>(algorithm: DeriveAlgorithm, baseKey: CryptoKey, length: C, count: number): Promise<ReadonlyTuple<Uint8Array<ArrayBuffer>, C>>;
@@ -1,201 +0,0 @@
1
- import { createArray } from './array/array.js';
2
- import { encodeBase64, encodeBase64Url } from './base64.js';
3
- import { decodeText, encodeHex, encodeUtf8 } from './encoding.js';
4
- import { getRandomBytes } from './random.js';
5
- import { isArray, isDefined, isString } from './type-guards.js';
6
- import { zBase32Encode } from './z-base32.js';
7
- /**
8
- * Encrypt data
9
- * @param algorithm algorithm as supported by Web Crypto API
10
- * @param key key
11
- * @param data data to encrypt. Encodes string to utf8
12
- */
13
- export function encrypt(algorithm, key, data) {
14
- const bytes = isString(data) ? encodeUtf8(data) : data;
15
- const encryptedBuffer = globalThis.crypto.subtle.encrypt(algorithm, key, bytes);
16
- return {
17
- toBuffer: async () => await encryptedBuffer,
18
- toHex: async () => encodeHex(await encryptedBuffer),
19
- toBase64: async () => encodeBase64(await encryptedBuffer),
20
- toBase64Url: async () => encodeBase64Url(await encryptedBuffer),
21
- toZBase32: async () => zBase32Encode(await encryptedBuffer),
22
- };
23
- }
24
- /**
25
- * Decrypt data
26
- * @param algorithm algorithm as supported by Web Crypto API
27
- * @param key key
28
- * @param data data to decrypt
29
- */
30
- export function decrypt(algorithm, key, bytes) {
31
- const decryptedBuffer = globalThis.crypto.subtle.decrypt(algorithm, key, bytes);
32
- return {
33
- toBuffer: async () => await decryptedBuffer,
34
- toHex: async () => encodeHex(await decryptedBuffer),
35
- toBase64: async () => encodeBase64(await decryptedBuffer),
36
- toBase64Url: async () => encodeBase64Url(await decryptedBuffer),
37
- toZBase32: async () => zBase32Encode(await decryptedBuffer),
38
- toUtf8: async () => decodeText(await decryptedBuffer),
39
- };
40
- }
41
- /**
42
- * Hashes data
43
- * @param algorithm algorithm as supported by Web Crypto API
44
- * @param data data to encrypt. Encodes string to utf8
45
- */
46
- export function digest(algorithm, data) {
47
- const bytes = isString(data) ? encodeUtf8(data) : data;
48
- const arrayBufferPromise = globalThis.crypto.subtle.digest(algorithm, bytes);
49
- const result = {
50
- toBuffer: async () => await arrayBufferPromise,
51
- toHex: async () => encodeHex(await arrayBufferPromise),
52
- toBase64: async () => encodeBase64(await arrayBufferPromise),
53
- toBase64Url: async () => encodeBase64Url(await arrayBufferPromise),
54
- toZBase32: async () => zBase32Encode(await arrayBufferPromise),
55
- };
56
- return result;
57
- }
58
- /**
59
- * Signs data
60
- * @param algorithm algorithm as supported by Web Crypto API
61
- * @param key key
62
- * @param data data to sign
63
- */
64
- export function sign(algorithm, key, data) {
65
- const bytes = isString(data) ? encodeUtf8(data) : data;
66
- const arrayBufferPromise = globalThis.crypto.subtle.sign(algorithm, key, bytes);
67
- const result = {
68
- toBuffer: async () => await arrayBufferPromise,
69
- toHex: async () => encodeHex(await arrayBufferPromise),
70
- toBase64: async () => encodeBase64(await arrayBufferPromise),
71
- toBase64Url: async () => encodeBase64Url(await arrayBufferPromise),
72
- toZBase32: async () => zBase32Encode(await arrayBufferPromise),
73
- };
74
- return result;
75
- }
76
- /**
77
- * Verifies data
78
- * @param algorithm algorithm as supported by Web Crypto API
79
- * @param key key
80
- * @param signature signature
81
- * @param data data to verify using provided signature
82
- */
83
- export async function verify(algorithm, key, signature, data) {
84
- const signatureBytes = isString(signature) ? encodeUtf8(signature) : signature;
85
- const dataBytes = isString(data) ? encodeUtf8(data) : data;
86
- return await globalThis.crypto.subtle.verify(algorithm, key, signatureBytes, dataBytes);
87
- }
88
- /**
89
- * Imports a HMAC CryptoKey
90
- * @param algorithm hash algorithm
91
- * @param key JWK or binary key
92
- * @param extractable whether the key can be used for exportKey
93
- */
94
- export async function importHmacKey(algorithm, key, extractable = false) {
95
- const binaryKey = isString(key) ? encodeUtf8(key) : key;
96
- if (isBinaryKey(binaryKey)) {
97
- return await globalThis.crypto.subtle.importKey('raw', binaryKey, { name: 'HMAC', hash: algorithm }, extractable, ['sign', 'verify']);
98
- }
99
- return await globalThis.crypto.subtle.importKey('jwk', binaryKey, { name: 'HMAC', hash: algorithm }, extractable, ['sign', 'verify']);
100
- }
101
- /**
102
- * Imports a CryptoKey for symmetric encryption
103
- * @param algorithm symmetric algorithm
104
- * @param length key length
105
- * @param key JWK or binary key
106
- * @param extractable whether the key can be used for exportKey
107
- */
108
- export async function importSymmetricKey(algorithm, length, key, extractable = false) {
109
- const binaryKey = isString(key) ? encodeUtf8(key) : key;
110
- if (isBinaryKey(binaryKey)) {
111
- return await globalThis.crypto.subtle.importKey('raw', binaryKey, { name: algorithm, length }, extractable, ['encrypt', 'decrypt']);
112
- }
113
- return await globalThis.crypto.subtle.importKey('jwk', binaryKey, { name: algorithm, length }, extractable, ['encrypt', 'decrypt']);
114
- }
115
- /**
116
- * Imports an ECDSA CryptoKey
117
- * @param curve ECDSA curve
118
- * @param key JWK or DER encoded key
119
- * @param extractable whether the key can be used for exportKey
120
- */
121
- export async function importEcdsaKey(curve, key, extractable = false) {
122
- const binaryKey = isString(key) ? encodeUtf8(key) : key;
123
- if (isBinaryKey(binaryKey)) {
124
- return await globalThis.crypto.subtle.importKey('spki', binaryKey, { name: 'ECDSA', namedCurve: curve }, extractable, ['verify']);
125
- }
126
- return await globalThis.crypto.subtle.importKey('jwk', binaryKey, { name: 'ECDSA', namedCurve: curve }, extractable, ['verify']);
127
- }
128
- /**
129
- * Import a HKDF CryptoKey
130
- * @param key binary key
131
- * @param extractable whether the key can be used for exportKey
132
- */
133
- export async function importHkdfKey(key, extractable = false) {
134
- const binaryKey = isString(key) ? encodeUtf8(key) : key;
135
- return await globalThis.crypto.subtle.importKey('raw', binaryKey, { name: 'HKDF' }, extractable, ['deriveKey', 'deriveBits']);
136
- }
137
- /**
138
- * Import a pbkdf2 CryptoKey
139
- * @param key binary key
140
- * @param extractable whether the key can be used for exportKey
141
- */
142
- export async function importPbkdf2Key(key, extractable = false) {
143
- const binaryKey = isString(key) ? encodeUtf8(key) : key;
144
- return await globalThis.crypto.subtle.importKey('raw', binaryKey, { name: 'PBKDF2' }, extractable, ['deriveKey', 'deriveBits']);
145
- }
146
- /**
147
- * Generates a new ECDSA CryptoKeyPair
148
- * @param curve ECDSA cruve to use
149
- * @param extractable whether the key can be used for exportKey
150
- * @param usages whether to generate a key for signing, verifiying or both. Defaults to both
151
- */
152
- export async function generateEcdsaKey(curve, extractable = false, usages = ['sign', 'verify']) {
153
- return await globalThis.crypto.subtle.generateKey({ name: 'ECDSA', namedCurve: curve }, extractable, usages);
154
- }
155
- /**
156
- * Convenience wrapper for HKDF derivation
157
- * @param baseKey The imported HKDF key
158
- * @param lengths Array of lengths for each derived secret
159
- * @param info Contextual string to prevent different apps from deriving the same keys
160
- * @param salt Optional salt (usually empty if the input key is high-entropy)
161
- * @param hash Hash algorithm to use
162
- */
163
- export async function deriveHkdfSecrets(baseKey, lengths, info = '', salt = new Uint8Array(), hash = 'SHA-512') {
164
- const infoBytes = isString(info) ? encodeUtf8(info) : info;
165
- const algorithm = { name: 'HKDF', hash, salt, info: infoBytes };
166
- return await deriveBytesMultiple(algorithm, baseKey, lengths);
167
- }
168
- /**
169
- * Generates a pbkdf2 CryptoKey
170
- * @param extractable whether the key can be used for exportKey
171
- */
172
- export async function generatePbkdf2Key(extractable = false) {
173
- const key = getRandomBytes(16);
174
- return await importPbkdf2Key(key, extractable);
175
- }
176
- /**
177
- * Derive byte array from key
178
- * @param length length in bytes
179
- * @param algorithm algorithm to derive with
180
- * @param baseKey key to derive from
181
- */
182
- export async function deriveBytes(algorithm, baseKey, length) {
183
- const bytes = await globalThis.crypto.subtle.deriveBits(algorithm, baseKey, length * 8);
184
- return new Uint8Array(bytes);
185
- }
186
- export async function deriveBytesMultiple(algorithm, baseKey, lengthOrLengths, countOrNothing) {
187
- const lengths = isArray(lengthOrLengths) ? lengthOrLengths : createArray(countOrNothing, () => lengthOrLengths);
188
- const totalBits = lengths.reduce((sum, length) => sum + length, 0) * 8;
189
- const bytes = await globalThis.crypto.subtle.deriveBits(algorithm, baseKey, totalBits);
190
- const arrays = [];
191
- for (let i = 0; i < bytes.byteLength;) {
192
- const slice = bytes.slice(i, i + lengths[arrays.length]);
193
- const array = new Uint8Array(slice);
194
- arrays.push(array);
195
- i += slice.byteLength;
196
- }
197
- return arrays;
198
- }
199
- function isBinaryKey(key) {
200
- return isDefined(key.byteLength);
201
- }