@enbox/crypto 0.0.2 → 0.0.4

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 (226) hide show
  1. package/README.md +34 -102
  2. package/dist/browser.mjs +6 -10
  3. package/dist/browser.mjs.map +4 -4
  4. package/dist/esm/algorithms/aes-ctr.js +1 -1
  5. package/dist/esm/algorithms/aes-gcm.js +35 -2
  6. package/dist/esm/algorithms/aes-gcm.js.map +1 -1
  7. package/dist/esm/algorithms/aes-kw.js +154 -0
  8. package/dist/esm/algorithms/aes-kw.js.map +1 -0
  9. package/dist/esm/algorithms/ecdsa.js +119 -6
  10. package/dist/esm/algorithms/ecdsa.js.map +1 -1
  11. package/dist/esm/algorithms/eddsa.js +99 -6
  12. package/dist/esm/algorithms/eddsa.js.map +1 -1
  13. package/dist/esm/algorithms/hkdf.js +53 -0
  14. package/dist/esm/algorithms/hkdf.js.map +1 -0
  15. package/dist/esm/algorithms/pbkdf2.js +55 -0
  16. package/dist/esm/algorithms/pbkdf2.js.map +1 -0
  17. package/dist/esm/algorithms/sha-2.js +2 -2
  18. package/dist/esm/algorithms/sha-2.js.map +1 -1
  19. package/dist/esm/algorithms/x25519.js +125 -0
  20. package/dist/esm/algorithms/x25519.js.map +1 -0
  21. package/dist/esm/crypto-error.js +41 -0
  22. package/dist/esm/crypto-error.js.map +1 -0
  23. package/dist/esm/index.js +8 -0
  24. package/dist/esm/index.js.map +1 -1
  25. package/dist/esm/jose/jwk.js +52 -26
  26. package/dist/esm/jose/jwk.js.map +1 -1
  27. package/dist/esm/local-key-manager.js +9 -3
  28. package/dist/esm/local-key-manager.js.map +1 -1
  29. package/dist/esm/primitives/aes-ctr.js.map +1 -1
  30. package/dist/esm/primitives/aes-gcm.js.map +1 -1
  31. package/dist/esm/primitives/aes-kw.js +246 -0
  32. package/dist/esm/primitives/aes-kw.js.map +1 -0
  33. package/dist/esm/primitives/concat-kdf.js +1 -1
  34. package/dist/esm/primitives/concat-kdf.js.map +1 -1
  35. package/dist/esm/primitives/ecies-secp256k1.js +79 -0
  36. package/dist/esm/primitives/ecies-secp256k1.js.map +1 -0
  37. package/dist/esm/primitives/ed25519.js +3 -3
  38. package/dist/esm/primitives/ed25519.js.map +1 -1
  39. package/dist/esm/primitives/hkdf.js +79 -0
  40. package/dist/esm/primitives/hkdf.js.map +1 -0
  41. package/dist/esm/primitives/pbkdf2.js +49 -0
  42. package/dist/esm/primitives/pbkdf2.js.map +1 -1
  43. package/dist/esm/primitives/secp256k1.js +4 -4
  44. package/dist/esm/primitives/secp256k1.js.map +1 -1
  45. package/dist/esm/primitives/secp256r1.js +4 -4
  46. package/dist/esm/primitives/secp256r1.js.map +1 -1
  47. package/dist/esm/primitives/x25519.js +10 -17
  48. package/dist/esm/primitives/x25519.js.map +1 -1
  49. package/dist/esm/primitives/xchacha20-poly1305.js +48 -3
  50. package/dist/esm/primitives/xchacha20-poly1305.js.map +1 -1
  51. package/dist/esm/primitives/xchacha20.js +1 -1
  52. package/dist/esm/primitives/xchacha20.js.map +1 -1
  53. package/dist/esm/utils.js +30 -0
  54. package/dist/esm/utils.js.map +1 -1
  55. package/dist/types/algorithms/aes-ctr.d.ts +2 -2
  56. package/dist/types/algorithms/aes-ctr.d.ts.map +1 -1
  57. package/dist/types/algorithms/aes-gcm.d.ts +25 -5
  58. package/dist/types/algorithms/aes-gcm.d.ts.map +1 -1
  59. package/dist/types/algorithms/aes-kw.d.ts +129 -0
  60. package/dist/types/algorithms/aes-kw.d.ts.map +1 -0
  61. package/dist/types/algorithms/ecdsa.d.ts +49 -4
  62. package/dist/types/algorithms/ecdsa.d.ts.map +1 -1
  63. package/dist/types/algorithms/eddsa.d.ts +49 -4
  64. package/dist/types/algorithms/eddsa.d.ts.map +1 -1
  65. package/dist/types/algorithms/hkdf.d.ts +35 -0
  66. package/dist/types/algorithms/hkdf.d.ts.map +1 -0
  67. package/dist/types/algorithms/pbkdf2.d.ts +35 -0
  68. package/dist/types/algorithms/pbkdf2.d.ts.map +1 -0
  69. package/dist/types/algorithms/sha-2.d.ts +2 -2
  70. package/dist/types/algorithms/sha-2.d.ts.map +1 -1
  71. package/dist/types/algorithms/x25519.d.ts +76 -0
  72. package/dist/types/algorithms/x25519.d.ts.map +1 -0
  73. package/dist/types/crypto-error.d.ts +29 -0
  74. package/dist/types/crypto-error.d.ts.map +1 -0
  75. package/dist/types/index.d.ts +8 -0
  76. package/dist/types/index.d.ts.map +1 -1
  77. package/dist/types/jose/jwk.d.ts.map +1 -1
  78. package/dist/types/local-key-manager.d.ts +6 -6
  79. package/dist/types/local-key-manager.d.ts.map +1 -1
  80. package/dist/types/primitives/aes-kw.d.ts +103 -0
  81. package/dist/types/primitives/aes-kw.d.ts.map +1 -0
  82. package/dist/types/primitives/concat-kdf.d.ts +1 -1
  83. package/dist/types/primitives/concat-kdf.d.ts.map +1 -1
  84. package/dist/types/primitives/ecies-secp256k1.d.ts +53 -0
  85. package/dist/types/primitives/ecies-secp256k1.d.ts.map +1 -0
  86. package/dist/types/primitives/hkdf.d.ts +90 -0
  87. package/dist/types/primitives/hkdf.d.ts.map +1 -0
  88. package/dist/types/primitives/pbkdf2.d.ts +58 -0
  89. package/dist/types/primitives/pbkdf2.d.ts.map +1 -1
  90. package/dist/types/primitives/x25519.d.ts +9 -16
  91. package/dist/types/primitives/x25519.d.ts.map +1 -1
  92. package/dist/types/primitives/xchacha20-poly1305.d.ts +47 -0
  93. package/dist/types/primitives/xchacha20-poly1305.d.ts.map +1 -1
  94. package/dist/types/types/cipher.d.ts +1 -1
  95. package/dist/types/types/crypto-api.d.ts +54 -6
  96. package/dist/types/types/crypto-api.d.ts.map +1 -1
  97. package/dist/types/types/key-converter.d.ts +37 -15
  98. package/dist/types/types/key-converter.d.ts.map +1 -1
  99. package/dist/types/types/key-deriver.d.ts +41 -0
  100. package/dist/types/types/key-deriver.d.ts.map +1 -1
  101. package/dist/types/types/key-io.d.ts +37 -0
  102. package/dist/types/types/key-io.d.ts.map +1 -1
  103. package/dist/types/types/params-direct.d.ts +96 -1
  104. package/dist/types/types/params-direct.d.ts.map +1 -1
  105. package/dist/types/types/params-kms.d.ts +55 -0
  106. package/dist/types/types/params-kms.d.ts.map +1 -1
  107. package/dist/types/utils.d.ts +19 -0
  108. package/dist/types/utils.d.ts.map +1 -1
  109. package/dist/utils.js +1 -1
  110. package/dist/utils.js.map +4 -4
  111. package/package.json +29 -45
  112. package/src/algorithms/aes-ctr.ts +2 -2
  113. package/src/algorithms/aes-gcm.ts +41 -4
  114. package/src/algorithms/aes-kw.ts +182 -0
  115. package/src/algorithms/ecdsa.ts +145 -8
  116. package/src/algorithms/eddsa.ts +117 -10
  117. package/src/algorithms/hkdf.ts +54 -0
  118. package/src/algorithms/pbkdf2.ts +57 -0
  119. package/src/algorithms/sha-2.ts +3 -3
  120. package/src/algorithms/x25519.ts +153 -0
  121. package/src/crypto-error.ts +45 -0
  122. package/src/index.ts +8 -0
  123. package/src/jose/jwk.ts +32 -32
  124. package/src/local-key-manager.ts +22 -16
  125. package/src/primitives/aes-ctr.ts +1 -1
  126. package/src/primitives/aes-gcm.ts +5 -5
  127. package/src/primitives/aes-kw.ts +269 -0
  128. package/src/primitives/concat-kdf.ts +4 -2
  129. package/src/primitives/ecies-secp256k1.ts +113 -0
  130. package/src/primitives/ed25519.ts +6 -6
  131. package/src/primitives/hkdf.ts +121 -0
  132. package/src/primitives/pbkdf2.ts +91 -0
  133. package/src/primitives/secp256k1.ts +6 -6
  134. package/src/primitives/secp256r1.ts +6 -6
  135. package/src/primitives/x25519.ts +12 -19
  136. package/src/primitives/xchacha20-poly1305.ts +57 -4
  137. package/src/primitives/xchacha20.ts +1 -1
  138. package/src/types/cipher.ts +1 -1
  139. package/src/types/crypto-api.ts +129 -11
  140. package/src/types/key-converter.ts +33 -7
  141. package/src/types/key-deriver.ts +49 -0
  142. package/src/types/key-io.ts +40 -0
  143. package/src/types/params-direct.ts +118 -1
  144. package/src/types/params-kms.ts +67 -0
  145. package/src/utils.ts +55 -2
  146. package/dist/browser.js +0 -64
  147. package/dist/browser.js.map +0 -7
  148. package/dist/cjs/algorithms/aes-ctr.js +0 -188
  149. package/dist/cjs/algorithms/aes-ctr.js.map +0 -1
  150. package/dist/cjs/algorithms/aes-gcm.js +0 -196
  151. package/dist/cjs/algorithms/aes-gcm.js.map +0 -1
  152. package/dist/cjs/algorithms/crypto-algorithm.js +0 -13
  153. package/dist/cjs/algorithms/crypto-algorithm.js.map +0 -1
  154. package/dist/cjs/algorithms/ecdsa.js +0 -352
  155. package/dist/cjs/algorithms/ecdsa.js.map +0 -1
  156. package/dist/cjs/algorithms/eddsa.js +0 -325
  157. package/dist/cjs/algorithms/eddsa.js.map +0 -1
  158. package/dist/cjs/algorithms/sha-2.js +0 -119
  159. package/dist/cjs/algorithms/sha-2.js.map +0 -1
  160. package/dist/cjs/index.js +0 -41
  161. package/dist/cjs/index.js.map +0 -1
  162. package/dist/cjs/jose/jwe.js +0 -3
  163. package/dist/cjs/jose/jwe.js.map +0 -1
  164. package/dist/cjs/jose/jwk.js +0 -278
  165. package/dist/cjs/jose/jwk.js.map +0 -1
  166. package/dist/cjs/jose/jws.js +0 -3
  167. package/dist/cjs/jose/jws.js.map +0 -1
  168. package/dist/cjs/jose/jwt.js +0 -3
  169. package/dist/cjs/jose/jwt.js.map +0 -1
  170. package/dist/cjs/jose/utils.js +0 -60
  171. package/dist/cjs/jose/utils.js.map +0 -1
  172. package/dist/cjs/local-key-manager.js +0 -521
  173. package/dist/cjs/local-key-manager.js.map +0 -1
  174. package/dist/cjs/package.json +0 -1
  175. package/dist/cjs/primitives/aes-ctr.js +0 -398
  176. package/dist/cjs/primitives/aes-ctr.js.map +0 -1
  177. package/dist/cjs/primitives/aes-gcm.js +0 -425
  178. package/dist/cjs/primitives/aes-gcm.js.map +0 -1
  179. package/dist/cjs/primitives/concat-kdf.js +0 -215
  180. package/dist/cjs/primitives/concat-kdf.js.map +0 -1
  181. package/dist/cjs/primitives/ed25519.js +0 -651
  182. package/dist/cjs/primitives/ed25519.js.map +0 -1
  183. package/dist/cjs/primitives/pbkdf2.js +0 -120
  184. package/dist/cjs/primitives/pbkdf2.js.map +0 -1
  185. package/dist/cjs/primitives/secp256k1.js +0 -958
  186. package/dist/cjs/primitives/secp256k1.js.map +0 -1
  187. package/dist/cjs/primitives/secp256r1.js +0 -959
  188. package/dist/cjs/primitives/secp256r1.js.map +0 -1
  189. package/dist/cjs/primitives/sha256.js +0 -93
  190. package/dist/cjs/primitives/sha256.js.map +0 -1
  191. package/dist/cjs/primitives/x25519.js +0 -498
  192. package/dist/cjs/primitives/x25519.js.map +0 -1
  193. package/dist/cjs/primitives/xchacha20-poly1305.js +0 -340
  194. package/dist/cjs/primitives/xchacha20-poly1305.js.map +0 -1
  195. package/dist/cjs/primitives/xchacha20.js +0 -316
  196. package/dist/cjs/primitives/xchacha20.js.map +0 -1
  197. package/dist/cjs/types/cipher.js +0 -3
  198. package/dist/cjs/types/cipher.js.map +0 -1
  199. package/dist/cjs/types/crypto-api.js +0 -3
  200. package/dist/cjs/types/crypto-api.js.map +0 -1
  201. package/dist/cjs/types/hasher.js +0 -3
  202. package/dist/cjs/types/hasher.js.map +0 -1
  203. package/dist/cjs/types/identifier.js +0 -3
  204. package/dist/cjs/types/identifier.js.map +0 -1
  205. package/dist/cjs/types/key-compressor.js +0 -3
  206. package/dist/cjs/types/key-compressor.js.map +0 -1
  207. package/dist/cjs/types/key-converter.js +0 -3
  208. package/dist/cjs/types/key-converter.js.map +0 -1
  209. package/dist/cjs/types/key-deriver.js +0 -3
  210. package/dist/cjs/types/key-deriver.js.map +0 -1
  211. package/dist/cjs/types/key-generator.js +0 -3
  212. package/dist/cjs/types/key-generator.js.map +0 -1
  213. package/dist/cjs/types/key-io.js +0 -3
  214. package/dist/cjs/types/key-io.js.map +0 -1
  215. package/dist/cjs/types/key-wrapper.js +0 -3
  216. package/dist/cjs/types/key-wrapper.js.map +0 -1
  217. package/dist/cjs/types/params-direct.js +0 -3
  218. package/dist/cjs/types/params-direct.js.map +0 -1
  219. package/dist/cjs/types/params-enclosed.js +0 -3
  220. package/dist/cjs/types/params-enclosed.js.map +0 -1
  221. package/dist/cjs/types/params-kms.js +0 -3
  222. package/dist/cjs/types/params-kms.js.map +0 -1
  223. package/dist/cjs/types/signer.js +0 -3
  224. package/dist/cjs/types/signer.js.map +0 -1
  225. package/dist/cjs/utils.js +0 -173
  226. package/dist/cjs/utils.js.map +0 -1
@@ -1,11 +1,49 @@
1
- import type { Jwk } from '../jose/jwk.js';
2
1
  import type { AlgorithmIdentifier } from './identifier.js';
2
+ import type { Jwk } from '../jose/jwk.js';
3
+
4
+ /**
5
+ * Parameters for converting raw private key bytes to a JWK.
6
+ */
7
+ export interface BytesToPrivateKeyParams {
8
+ /** The algorithm identifier. */
9
+ algorithm: AlgorithmIdentifier;
10
+
11
+ /** The raw private key bytes. */
12
+ privateKeyBytes: Uint8Array;
13
+ }
14
+
15
+ /**
16
+ * Parameters for converting raw public key bytes to a JWK.
17
+ */
18
+ export interface BytesToPublicKeyParams {
19
+ /** The algorithm identifier. */
20
+ algorithm: AlgorithmIdentifier;
21
+
22
+ /** The raw public key bytes. */
23
+ publicKeyBytes: Uint8Array;
24
+ }
3
25
 
4
26
  /**
5
27
  * Parameters for computing a public key.
6
28
  */
7
29
  export interface ComputePublicKeyParams extends GetPublicKeyParams { }
8
30
 
31
+ /**
32
+ * Parameters for encryption and decryption operations.
33
+ *
34
+ * Intended for use with a Key Management System.
35
+ */
36
+ export interface CipherParams {
37
+ /** A {@link Jwk} containing the key to be used for encryption or decryption. */
38
+ key: Jwk;
39
+
40
+ /** Data to be encrypted or decrypted. */
41
+ data: Uint8Array;
42
+
43
+ /** Additional algorithm-specific parameters for encryption or decryption. */
44
+ [key: string]: unknown;
45
+ }
46
+
9
47
  /**
10
48
  * Parameters for decrypting data.
11
49
  */
@@ -42,6 +80,38 @@ export interface DeriveKeyParams {
42
80
  derivedKeyParams: unknown
43
81
  }
44
82
 
83
+ /**
84
+ * Parameters for deriving a key from raw byte-based key material.
85
+ *
86
+ * Unlike {@link DeriveKeyParams} which operates on JWK keys, this interface works with raw
87
+ * byte arrays as the base key input, making it suitable for agent-level key derivation where
88
+ * keys originate from passphrases, seed phrases, or other byte-oriented sources.
89
+ */
90
+ export interface DeriveKeyFromBytesParams {
91
+ /** The algorithm identifier. */
92
+ algorithm: string;
93
+
94
+ /** The base key to be used for derivation as a byte array. */
95
+ baseKeyBytes: Uint8Array;
96
+
97
+ /** The algorithm identifier for the derived key. */
98
+ derivedKeyAlgorithm?: string;
99
+
100
+ /** Additional algorithm-specific parameters for key derivation. */
101
+ [key: string]: unknown;
102
+ }
103
+
104
+ /**
105
+ * Parameters for derivation of cryptographic byte arrays.
106
+ */
107
+ export interface DeriveKeyBytesParams {
108
+ /** The base key to be used for derivation as a byte array. */
109
+ baseKeyBytes: Uint8Array;
110
+
111
+ /** The desired length of the derived key in bits. */
112
+ length: number;
113
+ }
114
+
45
115
  /**
46
116
  * Parameters for computing a hash digest.
47
117
  */
@@ -91,6 +161,39 @@ export interface SignParams {
91
161
  data: Uint8Array;
92
162
  }
93
163
 
164
+ /**
165
+ * Parameters for converting a private key JWK to raw bytes.
166
+ */
167
+ export interface PrivateKeyToBytesParams {
168
+ /** The private key in JWK format. */
169
+ privateKey: Jwk;
170
+ }
171
+
172
+ /**
173
+ * Parameters for converting a public key JWK to raw bytes.
174
+ */
175
+ export interface PublicKeyToBytesParams {
176
+ /** The public key in JWK format. */
177
+ publicKey: Jwk;
178
+ }
179
+
180
+ /**
181
+ * Parameters for unwrapping a key.
182
+ */
183
+ export interface UnwrapKeyParams {
184
+ /** A {@link Jwk} containing the key used to decrypt the unwrapped key. */
185
+ decryptionKey: Jwk;
186
+
187
+ /** The wrapped private key as a byte array. */
188
+ wrappedKeyBytes: Uint8Array;
189
+
190
+ /** The algorithm identifier of the key encrypted in `wrappedKeyBytes`. */
191
+ wrappedKeyAlgorithm: string;
192
+
193
+ /** An object defining the algorithm-specific parameters for decrypting the `wrappedKeyBytes`. */
194
+ decryptParams?: unknown;
195
+ }
196
+
94
197
  /**
95
198
  * Parameters for verifying a signature.
96
199
  */
@@ -103,4 +206,18 @@ export interface VerifyParams {
103
206
 
104
207
  /** The data associated with the signature. */
105
208
  data: Uint8Array;
209
+ }
210
+
211
+ /**
212
+ * Parameters for wrapping a key.
213
+ */
214
+ export interface WrapKeyParams {
215
+ /** A {@link Jwk} containing the key used to encrypt the unwrapped key. */
216
+ encryptionKey: Jwk;
217
+
218
+ /** A {@link Jwk} containing the private key to be wrapped. */
219
+ unwrappedKey: Jwk;
220
+
221
+ /** An object defining the algorithm-specific parameters for encrypting the `unwrappedKey`. */
222
+ encryptParams?: unknown;
106
223
  }
@@ -153,4 +153,71 @@ export interface KmsUnwrapKeyParams {
153
153
 
154
154
  /** Algorithm to be used for unwrapping. */
155
155
  unwrapAlgorithm: AlgorithmIdentifier;
156
+ }
157
+
158
+ /**
159
+ * Parameters for KMS-based encryption and decryption operations.
160
+ *
161
+ * Intended for use with a Key Management System where the key is referenced by URI.
162
+ */
163
+ export interface KmsCipherParams {
164
+ /** Identifier for the private key in the KMS. */
165
+ keyUri: KeyIdentifier;
166
+
167
+ /** Data to be encrypted or decrypted. */
168
+ data: Uint8Array;
169
+ }
170
+
171
+ /**
172
+ * Parameters for KMS-based derivation of a byte array from a given base key.
173
+ *
174
+ * Intended for use with a Key Management System.
175
+ */
176
+ export interface KmsDeriveKeyBytesParams {
177
+ /** Identifier for the base key used in derivation in the KMS. */
178
+ baseKeyUri: KeyIdentifier;
179
+
180
+ /** The desired length of the derived key in bits. */
181
+ length: number;
182
+ }
183
+
184
+ /**
185
+ * Parameters for KMS-based key unwrapping. Intended for use with a Key Management System where
186
+ * the decryption key is referenced by URI.
187
+ */
188
+ export interface KmsUriUnwrapKeyParams {
189
+ /** Identifier for the private key in the KMS used for decrypting the wrapped key. */
190
+ decryptionKeyUri: KeyIdentifier;
191
+
192
+ /** The wrapped private key as a byte array. */
193
+ wrappedKeyBytes: Uint8Array;
194
+
195
+ /** The algorithm identifier of the key encrypted in `wrappedKeyBytes`. */
196
+ wrappedKeyAlgorithm: string;
197
+
198
+ /** An object defining the algorithm-specific parameters for decrypting the `wrappedKeyBytes`. */
199
+ decryptParams?: unknown;
200
+ }
201
+
202
+ /**
203
+ * Parameters for KMS-based key wrapping. Intended for use with a Key Management System where
204
+ * the encryption key is referenced by URI.
205
+ */
206
+ export interface KmsUriWrapKeyParams {
207
+ /** Identifier for the private key in the KMS used for encrypting the unwrapped key. */
208
+ encryptionKeyUri: KeyIdentifier;
209
+
210
+ /** A {@link Jwk} containing the private key to be wrapped. */
211
+ unwrappedKey: Jwk;
212
+
213
+ /** An object defining the algorithm-specific parameters for encrypting the `unwrappedKey`. */
214
+ encryptParams?: unknown;
215
+ }
216
+
217
+ /**
218
+ * Parameters for KMS-based key deletion. Intended for use with a Key Management System.
219
+ */
220
+ export interface KmsDeleteKeyParams {
221
+ /** Identifier for the key to be deleted in the KMS. */
222
+ keyUri: KeyIdentifier;
156
223
  }
package/src/utils.ts CHANGED
@@ -1,4 +1,7 @@
1
+ import type { Cipher } from './types/cipher.js';
1
2
  import type { Jwk } from './jose/jwk.js';
3
+ import type { KeyWrapper } from './types/key-wrapper.js';
4
+ import type { KeyExporter, KeyImporter } from './types/key-io.js';
2
5
 
3
6
  import { crypto } from '@noble/hashes/crypto';
4
7
  import { randomBytes as nobleRandomBytes } from '@noble/hashes/utils';
@@ -158,7 +161,7 @@ export class CryptoUtils {
158
161
  const rejectionRange = Math.pow(10, length);
159
162
  do {
160
163
  // Adjust the byte generation based on length.
161
- const randomBuffer = CryptoUtils.randomBytes(Math.ceil(length / 2) ); // 2 digits per byte.
164
+ const randomBuffer = CryptoUtils.randomBytes(Math.ceil(length / 2) ); // 2 digits per byte.
162
165
  const view = new DataView(randomBuffer.buffer);
163
166
  // Convert the buffer to integer and take modulus based on length.
164
167
  pin = view.getUint16(0, false) % rejectionRange;
@@ -172,10 +175,60 @@ export class CryptoUtils {
172
175
  const view = new DataView(randomBuffer.buffer);
173
176
  // Transform bytes to number (big endian).
174
177
  pin = view.getUint32(0, false) % rejectionRange;
175
- } while (pin > max); // Reject if the number is outside the desired range.
178
+ } while (pin > max); // Reject if the number is outside the desired range.
176
179
  }
177
180
 
178
181
  // Pad the PIN with leading zeros to the desired length.
179
182
  return pin.toString().padStart(length, '0');
180
183
  }
181
184
  }
185
+
186
+ /**
187
+ * Type guard that checks whether the given object implements the {@link Cipher} interface.
188
+ */
189
+ export function isCipher<EncryptInput, DecryptInput>(
190
+ obj: unknown
191
+ ): obj is Cipher<EncryptInput, DecryptInput> {
192
+ return (
193
+ obj !== null && typeof obj === 'object'
194
+ && 'encrypt' in obj && typeof obj.encrypt === 'function'
195
+ && 'decrypt' in obj && typeof obj.decrypt === 'function'
196
+ );
197
+ }
198
+
199
+ /**
200
+ * Type guard that checks whether the given object implements the {@link KeyExporter} interface.
201
+ */
202
+ export function isKeyExporter<ExportKeyInput, ExportKeyOutput>(
203
+ obj: unknown
204
+ ): obj is KeyExporter<ExportKeyInput, ExportKeyOutput> {
205
+ return (
206
+ obj !== null && typeof obj === 'object'
207
+ && 'exportKey' in obj && typeof obj.exportKey === 'function'
208
+ );
209
+ }
210
+
211
+ /**
212
+ * Type guard that checks whether the given object implements the {@link KeyImporter} interface.
213
+ */
214
+ export function isKeyImporter<ImportKeyInput, ImportKeyOutput>(
215
+ obj: unknown
216
+ ): obj is KeyImporter<ImportKeyInput, ImportKeyOutput> {
217
+ return (
218
+ obj !== null && typeof obj === 'object'
219
+ && 'importKey' in obj && typeof obj.importKey === 'function'
220
+ );
221
+ }
222
+
223
+ /**
224
+ * Type guard that checks whether the given object implements the {@link KeyWrapper} interface.
225
+ */
226
+ export function isKeyWrapper<WrapKeyInput, UnwrapKeyInput>(
227
+ obj: unknown
228
+ ): obj is KeyWrapper<WrapKeyInput, UnwrapKeyInput> {
229
+ return (
230
+ obj !== null && typeof obj === 'object'
231
+ && 'wrapKey' in obj && typeof obj.wrapKey === 'function'
232
+ && 'unwrapKey' in obj && typeof obj.unwrapKey === 'function'
233
+ );
234
+ }