@enbox/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.
Files changed (283) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +708 -0
  3. package/dist/browser.js +64 -0
  4. package/dist/browser.js.map +7 -0
  5. package/dist/browser.mjs +64 -0
  6. package/dist/browser.mjs.map +7 -0
  7. package/dist/cjs/algorithms/aes-ctr.js +188 -0
  8. package/dist/cjs/algorithms/aes-ctr.js.map +1 -0
  9. package/dist/cjs/algorithms/aes-gcm.js +196 -0
  10. package/dist/cjs/algorithms/aes-gcm.js.map +1 -0
  11. package/dist/cjs/algorithms/crypto-algorithm.js +13 -0
  12. package/dist/cjs/algorithms/crypto-algorithm.js.map +1 -0
  13. package/dist/cjs/algorithms/ecdsa.js +352 -0
  14. package/dist/cjs/algorithms/ecdsa.js.map +1 -0
  15. package/dist/cjs/algorithms/eddsa.js +325 -0
  16. package/dist/cjs/algorithms/eddsa.js.map +1 -0
  17. package/dist/cjs/algorithms/sha-2.js +119 -0
  18. package/dist/cjs/algorithms/sha-2.js.map +1 -0
  19. package/dist/cjs/index.js +41 -0
  20. package/dist/cjs/index.js.map +1 -0
  21. package/dist/cjs/jose/jwe.js +3 -0
  22. package/dist/cjs/jose/jwe.js.map +1 -0
  23. package/dist/cjs/jose/jwk.js +278 -0
  24. package/dist/cjs/jose/jwk.js.map +1 -0
  25. package/dist/cjs/jose/jws.js +3 -0
  26. package/dist/cjs/jose/jws.js.map +1 -0
  27. package/dist/cjs/jose/jwt.js +3 -0
  28. package/dist/cjs/jose/jwt.js.map +1 -0
  29. package/dist/cjs/jose/utils.js +60 -0
  30. package/dist/cjs/jose/utils.js.map +1 -0
  31. package/dist/cjs/local-key-manager.js +521 -0
  32. package/dist/cjs/local-key-manager.js.map +1 -0
  33. package/dist/cjs/package.json +1 -0
  34. package/dist/cjs/primitives/aes-ctr.js +398 -0
  35. package/dist/cjs/primitives/aes-ctr.js.map +1 -0
  36. package/dist/cjs/primitives/aes-gcm.js +425 -0
  37. package/dist/cjs/primitives/aes-gcm.js.map +1 -0
  38. package/dist/cjs/primitives/concat-kdf.js +215 -0
  39. package/dist/cjs/primitives/concat-kdf.js.map +1 -0
  40. package/dist/cjs/primitives/ed25519.js +651 -0
  41. package/dist/cjs/primitives/ed25519.js.map +1 -0
  42. package/dist/cjs/primitives/pbkdf2.js +120 -0
  43. package/dist/cjs/primitives/pbkdf2.js.map +1 -0
  44. package/dist/cjs/primitives/secp256k1.js +958 -0
  45. package/dist/cjs/primitives/secp256k1.js.map +1 -0
  46. package/dist/cjs/primitives/secp256r1.js +959 -0
  47. package/dist/cjs/primitives/secp256r1.js.map +1 -0
  48. package/dist/cjs/primitives/sha256.js +93 -0
  49. package/dist/cjs/primitives/sha256.js.map +1 -0
  50. package/dist/cjs/primitives/x25519.js +498 -0
  51. package/dist/cjs/primitives/x25519.js.map +1 -0
  52. package/dist/cjs/primitives/xchacha20-poly1305.js +340 -0
  53. package/dist/cjs/primitives/xchacha20-poly1305.js.map +1 -0
  54. package/dist/cjs/primitives/xchacha20.js +316 -0
  55. package/dist/cjs/primitives/xchacha20.js.map +1 -0
  56. package/dist/cjs/types/cipher.js +3 -0
  57. package/dist/cjs/types/cipher.js.map +1 -0
  58. package/dist/cjs/types/crypto-api.js +3 -0
  59. package/dist/cjs/types/crypto-api.js.map +1 -0
  60. package/dist/cjs/types/hasher.js +3 -0
  61. package/dist/cjs/types/hasher.js.map +1 -0
  62. package/dist/cjs/types/identifier.js +3 -0
  63. package/dist/cjs/types/identifier.js.map +1 -0
  64. package/dist/cjs/types/key-compressor.js +3 -0
  65. package/dist/cjs/types/key-compressor.js.map +1 -0
  66. package/dist/cjs/types/key-converter.js +3 -0
  67. package/dist/cjs/types/key-converter.js.map +1 -0
  68. package/dist/cjs/types/key-deriver.js +3 -0
  69. package/dist/cjs/types/key-deriver.js.map +1 -0
  70. package/dist/cjs/types/key-generator.js +3 -0
  71. package/dist/cjs/types/key-generator.js.map +1 -0
  72. package/dist/cjs/types/key-io.js +3 -0
  73. package/dist/cjs/types/key-io.js.map +1 -0
  74. package/dist/cjs/types/key-wrapper.js +3 -0
  75. package/dist/cjs/types/key-wrapper.js.map +1 -0
  76. package/dist/cjs/types/params-direct.js +3 -0
  77. package/dist/cjs/types/params-direct.js.map +1 -0
  78. package/dist/cjs/types/params-enclosed.js +3 -0
  79. package/dist/cjs/types/params-enclosed.js.map +1 -0
  80. package/dist/cjs/types/params-kms.js +3 -0
  81. package/dist/cjs/types/params-kms.js.map +1 -0
  82. package/dist/cjs/types/signer.js +3 -0
  83. package/dist/cjs/types/signer.js.map +1 -0
  84. package/dist/cjs/utils.js +173 -0
  85. package/dist/cjs/utils.js.map +1 -0
  86. package/dist/esm/algorithms/aes-ctr.js +124 -0
  87. package/dist/esm/algorithms/aes-ctr.js.map +1 -0
  88. package/dist/esm/algorithms/aes-gcm.js +132 -0
  89. package/dist/esm/algorithms/aes-gcm.js.map +1 -0
  90. package/dist/esm/algorithms/crypto-algorithm.js +6 -0
  91. package/dist/esm/algorithms/crypto-algorithm.js.map +1 -0
  92. package/dist/esm/algorithms/ecdsa.js +237 -0
  93. package/dist/esm/algorithms/ecdsa.js.map +1 -0
  94. package/dist/esm/algorithms/eddsa.js +213 -0
  95. package/dist/esm/algorithms/eddsa.js.map +1 -0
  96. package/dist/esm/algorithms/sha-2.js +57 -0
  97. package/dist/esm/algorithms/sha-2.js.map +1 -0
  98. package/dist/esm/index.js +25 -0
  99. package/dist/esm/index.js.map +1 -0
  100. package/dist/esm/jose/jwe.js +2 -0
  101. package/dist/esm/jose/jwe.js.map +1 -0
  102. package/dist/esm/jose/jwk.js +241 -0
  103. package/dist/esm/jose/jwk.js.map +1 -0
  104. package/dist/esm/jose/jws.js +2 -0
  105. package/dist/esm/jose/jws.js.map +1 -0
  106. package/dist/esm/jose/jwt.js +2 -0
  107. package/dist/esm/jose/jwt.js.map +1 -0
  108. package/dist/esm/jose/utils.js +34 -0
  109. package/dist/esm/jose/utils.js.map +1 -0
  110. package/dist/esm/local-key-manager.js +417 -0
  111. package/dist/esm/local-key-manager.js.map +1 -0
  112. package/dist/esm/primitives/aes-ctr.js +327 -0
  113. package/dist/esm/primitives/aes-ctr.js.map +1 -0
  114. package/dist/esm/primitives/aes-gcm.js +347 -0
  115. package/dist/esm/primitives/aes-gcm.js.map +1 -0
  116. package/dist/esm/primitives/concat-kdf.js +185 -0
  117. package/dist/esm/primitives/concat-kdf.js.map +1 -0
  118. package/dist/esm/primitives/ed25519.js +521 -0
  119. package/dist/esm/primitives/ed25519.js.map +1 -0
  120. package/dist/esm/primitives/pbkdf2.js +78 -0
  121. package/dist/esm/primitives/pbkdf2.js.map +1 -0
  122. package/dist/esm/primitives/secp256k1.js +805 -0
  123. package/dist/esm/primitives/secp256k1.js.map +1 -0
  124. package/dist/esm/primitives/secp256r1.js +806 -0
  125. package/dist/esm/primitives/secp256r1.js.map +1 -0
  126. package/dist/esm/primitives/sha256.js +55 -0
  127. package/dist/esm/primitives/sha256.js.map +1 -0
  128. package/dist/esm/primitives/x25519.js +392 -0
  129. package/dist/esm/primitives/x25519.js.map +1 -0
  130. package/dist/esm/primitives/xchacha20-poly1305.js +270 -0
  131. package/dist/esm/primitives/xchacha20-poly1305.js.map +1 -0
  132. package/dist/esm/primitives/xchacha20.js +246 -0
  133. package/dist/esm/primitives/xchacha20.js.map +1 -0
  134. package/dist/esm/types/cipher.js +2 -0
  135. package/dist/esm/types/cipher.js.map +1 -0
  136. package/dist/esm/types/crypto-api.js +2 -0
  137. package/dist/esm/types/crypto-api.js.map +1 -0
  138. package/dist/esm/types/hasher.js +2 -0
  139. package/dist/esm/types/hasher.js.map +1 -0
  140. package/dist/esm/types/identifier.js +2 -0
  141. package/dist/esm/types/identifier.js.map +1 -0
  142. package/dist/esm/types/key-compressor.js +2 -0
  143. package/dist/esm/types/key-compressor.js.map +1 -0
  144. package/dist/esm/types/key-converter.js +2 -0
  145. package/dist/esm/types/key-converter.js.map +1 -0
  146. package/dist/esm/types/key-deriver.js +2 -0
  147. package/dist/esm/types/key-deriver.js.map +1 -0
  148. package/dist/esm/types/key-generator.js +2 -0
  149. package/dist/esm/types/key-generator.js.map +1 -0
  150. package/dist/esm/types/key-io.js +2 -0
  151. package/dist/esm/types/key-io.js.map +1 -0
  152. package/dist/esm/types/key-wrapper.js +2 -0
  153. package/dist/esm/types/key-wrapper.js.map +1 -0
  154. package/dist/esm/types/params-direct.js +2 -0
  155. package/dist/esm/types/params-direct.js.map +1 -0
  156. package/dist/esm/types/params-enclosed.js +2 -0
  157. package/dist/esm/types/params-enclosed.js.map +1 -0
  158. package/dist/esm/types/params-kms.js +2 -0
  159. package/dist/esm/types/params-kms.js.map +1 -0
  160. package/dist/esm/types/signer.js +2 -0
  161. package/dist/esm/types/signer.js.map +1 -0
  162. package/dist/esm/utils.js +165 -0
  163. package/dist/esm/utils.js.map +1 -0
  164. package/dist/types/algorithms/aes-ctr.d.ts +121 -0
  165. package/dist/types/algorithms/aes-ctr.d.ts.map +1 -0
  166. package/dist/types/algorithms/aes-gcm.d.ts +152 -0
  167. package/dist/types/algorithms/aes-gcm.d.ts.map +1 -0
  168. package/dist/types/algorithms/crypto-algorithm.d.ts +6 -0
  169. package/dist/types/algorithms/crypto-algorithm.d.ts.map +1 -0
  170. package/dist/types/algorithms/ecdsa.d.ts +154 -0
  171. package/dist/types/algorithms/ecdsa.d.ts.map +1 -0
  172. package/dist/types/algorithms/eddsa.d.ts +151 -0
  173. package/dist/types/algorithms/eddsa.d.ts.map +1 -0
  174. package/dist/types/algorithms/sha-2.d.ts +51 -0
  175. package/dist/types/algorithms/sha-2.d.ts.map +1 -0
  176. package/dist/types/index.d.ts +39 -0
  177. package/dist/types/index.d.ts.map +1 -0
  178. package/dist/types/jose/jwe.d.ts +135 -0
  179. package/dist/types/jose/jwe.d.ts.map +1 -0
  180. package/dist/types/jose/jwk.d.ts +439 -0
  181. package/dist/types/jose/jwk.d.ts.map +1 -0
  182. package/dist/types/jose/jws.d.ts +67 -0
  183. package/dist/types/jose/jws.d.ts.map +1 -0
  184. package/dist/types/jose/jwt.d.ts +139 -0
  185. package/dist/types/jose/jwt.d.ts.map +1 -0
  186. package/dist/types/jose/utils.d.ts +14 -0
  187. package/dist/types/jose/utils.d.ts.map +1 -0
  188. package/dist/types/local-key-manager.d.ts +307 -0
  189. package/dist/types/local-key-manager.d.ts.map +1 -0
  190. package/dist/types/primitives/aes-ctr.d.ts +219 -0
  191. package/dist/types/primitives/aes-ctr.d.ts.map +1 -0
  192. package/dist/types/primitives/aes-gcm.d.ts +245 -0
  193. package/dist/types/primitives/aes-gcm.d.ts.map +1 -0
  194. package/dist/types/primitives/concat-kdf.d.ts +160 -0
  195. package/dist/types/primitives/concat-kdf.d.ts.map +1 -0
  196. package/dist/types/primitives/ed25519.d.ts +359 -0
  197. package/dist/types/primitives/ed25519.d.ts.map +1 -0
  198. package/dist/types/primitives/pbkdf2.d.ts +94 -0
  199. package/dist/types/primitives/pbkdf2.d.ts.map +1 -0
  200. package/dist/types/primitives/secp256k1.d.ts +598 -0
  201. package/dist/types/primitives/secp256k1.d.ts.map +1 -0
  202. package/dist/types/primitives/secp256r1.d.ts +599 -0
  203. package/dist/types/primitives/secp256r1.d.ts.map +1 -0
  204. package/dist/types/primitives/sha256.d.ts +42 -0
  205. package/dist/types/primitives/sha256.d.ts.map +1 -0
  206. package/dist/types/primitives/x25519.d.ts +283 -0
  207. package/dist/types/primitives/x25519.d.ts.map +1 -0
  208. package/dist/types/primitives/xchacha20-poly1305.d.ts +210 -0
  209. package/dist/types/primitives/xchacha20-poly1305.d.ts.map +1 -0
  210. package/dist/types/primitives/xchacha20.d.ts +186 -0
  211. package/dist/types/primitives/xchacha20.d.ts.map +1 -0
  212. package/dist/types/types/cipher.d.ts +49 -0
  213. package/dist/types/types/cipher.d.ts.map +1 -0
  214. package/dist/types/types/crypto-api.d.ts +40 -0
  215. package/dist/types/types/crypto-api.d.ts.map +1 -0
  216. package/dist/types/types/hasher.d.ts +33 -0
  217. package/dist/types/types/hasher.d.ts.map +1 -0
  218. package/dist/types/types/identifier.d.ts +16 -0
  219. package/dist/types/types/identifier.d.ts.map +1 -0
  220. package/dist/types/types/key-compressor.d.ts +28 -0
  221. package/dist/types/types/key-compressor.d.ts.map +1 -0
  222. package/dist/types/types/key-converter.d.ts +57 -0
  223. package/dist/types/types/key-converter.d.ts.map +1 -0
  224. package/dist/types/types/key-deriver.d.ts +39 -0
  225. package/dist/types/types/key-deriver.d.ts.map +1 -0
  226. package/dist/types/types/key-generator.d.ts +105 -0
  227. package/dist/types/types/key-generator.d.ts.map +1 -0
  228. package/dist/types/types/key-io.d.ts +37 -0
  229. package/dist/types/types/key-io.d.ts.map +1 -0
  230. package/dist/types/types/key-wrapper.d.ts +38 -0
  231. package/dist/types/types/key-wrapper.d.ts.map +1 -0
  232. package/dist/types/types/params-direct.d.ts +90 -0
  233. package/dist/types/types/params-direct.d.ts.map +1 -0
  234. package/dist/types/types/params-enclosed.d.ts +47 -0
  235. package/dist/types/types/params-enclosed.d.ts.map +1 -0
  236. package/dist/types/types/params-kms.d.ts +131 -0
  237. package/dist/types/types/params-kms.d.ts.map +1 -0
  238. package/dist/types/types/signer.d.ts +46 -0
  239. package/dist/types/types/signer.d.ts.map +1 -0
  240. package/dist/types/utils.d.ts +112 -0
  241. package/dist/types/utils.d.ts.map +1 -0
  242. package/dist/utils.js +7 -0
  243. package/dist/utils.js.map +7 -0
  244. package/package.json +103 -0
  245. package/src/algorithms/aes-ctr.ts +156 -0
  246. package/src/algorithms/aes-gcm.ts +187 -0
  247. package/src/algorithms/crypto-algorithm.ts +4 -0
  248. package/src/algorithms/ecdsa.ts +269 -0
  249. package/src/algorithms/eddsa.ts +243 -0
  250. package/src/algorithms/sha-2.ts +65 -0
  251. package/src/index.ts +42 -0
  252. package/src/jose/jwe.ts +196 -0
  253. package/src/jose/jwk.ts +632 -0
  254. package/src/jose/jws.ts +95 -0
  255. package/src/jose/jwt.ts +147 -0
  256. package/src/jose/utils.ts +34 -0
  257. package/src/local-key-manager.ts +540 -0
  258. package/src/primitives/aes-ctr.ts +352 -0
  259. package/src/primitives/aes-gcm.ts +378 -0
  260. package/src/primitives/concat-kdf.ts +240 -0
  261. package/src/primitives/ed25519.ts +548 -0
  262. package/src/primitives/pbkdf2.ts +122 -0
  263. package/src/primitives/secp256k1.ts +848 -0
  264. package/src/primitives/secp256r1.ts +850 -0
  265. package/src/primitives/sha256.ts +47 -0
  266. package/src/primitives/x25519.ts +403 -0
  267. package/src/primitives/xchacha20-poly1305.ts +274 -0
  268. package/src/primitives/xchacha20.ts +247 -0
  269. package/src/types/cipher.ts +53 -0
  270. package/src/types/crypto-api.ts +56 -0
  271. package/src/types/hasher.ts +32 -0
  272. package/src/types/identifier.ts +16 -0
  273. package/src/types/key-compressor.ts +25 -0
  274. package/src/types/key-converter.ts +53 -0
  275. package/src/types/key-deriver.ts +43 -0
  276. package/src/types/key-generator.ts +119 -0
  277. package/src/types/key-io.ts +42 -0
  278. package/src/types/key-wrapper.ts +42 -0
  279. package/src/types/params-direct.ts +106 -0
  280. package/src/types/params-enclosed.ts +50 -0
  281. package/src/types/params-kms.ts +156 -0
  282. package/src/types/signer.ts +50 -0
  283. package/src/utils.ts +181 -0
@@ -0,0 +1,359 @@
1
+ import type { Jwk } from '../jose/jwk.js';
2
+ import type { ComputePublicKeyParams, GetPublicKeyParams, SignParams, VerifyParams } from '../types/params-direct.js';
3
+ /**
4
+ * The `Ed25519` class provides a comprehensive suite of utilities for working with the Ed25519
5
+ * elliptic curve, widely used in modern cryptographic applications. This class includes methods for
6
+ * key generation, conversion, signing, verification, and public key derivation.
7
+ *
8
+ * The class supports conversions between raw byte formats and JSON Web Key (JWK) formats. It
9
+ * follows the guidelines and specifications outlined in RFC8032 for EdDSA (Edwards-curve Digital
10
+ * Signature Algorithm) operations.
11
+ *
12
+ * Key Features:
13
+ * - Key Generation: Generate Ed25519 private keys in JWK format.
14
+ * - Key Conversion: Transform keys between raw byte arrays and JWK formats.
15
+ * - Public Key Derivation: Derive public keys from private keys.
16
+ * - Signing and Verification: Sign data and verify signatures with Ed25519 keys.
17
+ * - Key Validation: Validate the mathematical correctness of Ed25519 keys.
18
+ *
19
+ * The methods in this class are asynchronous, returning Promises to accommodate various
20
+ * JavaScript environments, and use `Uint8Array` for binary data handling.
21
+ *
22
+ * @example
23
+ * ```ts
24
+ * // Key Generation
25
+ * const privateKey = await Ed25519.generateKey();
26
+ *
27
+ * // Public Key Derivation
28
+ * const publicKey = await Ed25519.computePublicKey({ key: privateKey });
29
+ * console.log(publicKey === await Ed25519.getPublicKey({ key: privateKey })); // Output: true
30
+ *
31
+ * // EdDSA Signing
32
+ * const signature = await Ed25519.sign({
33
+ * key: privateKey,
34
+ * data: new TextEncoder().encode('Message')
35
+ * });
36
+ *
37
+ * // EdDSA Signature Verification
38
+ * const isValid = await Ed25519.verify({
39
+ * key: publicKey,
40
+ * signature: signature,
41
+ * data: new TextEncoder().encode('Message')
42
+ * });
43
+ *
44
+ * // Key Conversion
45
+ * const privateKeyBytes = await Ed25519.privateKeyToBytes({ privateKey });
46
+ * const publicKeyBytes = await Ed25519.publicKeyToBytes({ publicKey });
47
+ *
48
+ * // Key Validation
49
+ * const isPublicKeyValid = await Ed25519.validatePublicKey({ publicKeyBytes });
50
+ * ```
51
+ */
52
+ export declare class Ed25519 {
53
+ /**
54
+ * Converts a raw private key in bytes to its corresponding JSON Web Key (JWK) format.
55
+ *
56
+ * @remarks
57
+ * This method accepts a private key as a byte array (Uint8Array) for the Curve25519 curve in
58
+ * Twisted Edwards form and transforms it into a JWK object. The process involves first deriving
59
+ * the public key from the private key, then encoding both the private and public keys into
60
+ * base64url format.
61
+ *
62
+ * The resulting JWK object includes the following properties:
63
+ * - `kty`: Key Type, set to 'OKP' for Octet Key Pair.
64
+ * - `crv`: Curve Name, set to 'Ed25519'.
65
+ * - `d`: The private key component, base64url-encoded.
66
+ * - `x`: The computed public key, base64url-encoded.
67
+ *
68
+ * @example
69
+ * ```ts
70
+ * const privateKeyBytes = new Uint8Array([...]); // Replace with actual private key bytes
71
+ * const privateKey = await Ed25519.bytesToPrivateKey({ privateKeyBytes });
72
+ * ```
73
+ *
74
+ * @param params - The parameters for the private key conversion.
75
+ * @param params.privateKeyBytes - The raw private key as a Uint8Array.
76
+ *
77
+ * @returns A Promise that resolves to the private key in JWK format.
78
+ */
79
+ static bytesToPrivateKey({ privateKeyBytes }: {
80
+ privateKeyBytes: Uint8Array;
81
+ }): Promise<Jwk>;
82
+ /**
83
+ * Converts a raw private key in bytes to its corresponding JSON Web Key (JWK) format.
84
+ *
85
+ * @remarks
86
+ * This method accepts a public key as a byte array (Uint8Array) for the Curve25519 curve in
87
+ * Twisted Edwards form and transforms it into a JWK object. The process involves encoding the
88
+ * public key bytes into base64url format.
89
+ *
90
+ * The resulting JWK object includes the following properties:
91
+ * - `kty`: Key Type, set to 'OKP' for Octet Key Pair.
92
+ * - `crv`: Curve Name, set to 'X25519'.
93
+ * - `x`: The public key, base64url-encoded.
94
+ *
95
+ * @example
96
+ * ```ts
97
+ * const publicKeyBytes = new Uint8Array([...]); // Replace with actual public key bytes
98
+ * const publicKey = await X25519.bytesToPublicKey({ publicKeyBytes });
99
+ * ```
100
+ *
101
+ * @param params - The parameters for the public key conversion.
102
+ * @param params.publicKeyBytes - The raw public key as a `Uint8Array`.
103
+ *
104
+ * @returns A Promise that resolves to the public key in JWK format.
105
+ */
106
+ static bytesToPublicKey({ publicKeyBytes }: {
107
+ publicKeyBytes: Uint8Array;
108
+ }): Promise<Jwk>;
109
+ /**
110
+ * Derives the public key in JWK format from a given Ed25519 private key.
111
+ *
112
+ * @remarks
113
+ * This method takes a private key in JWK format and derives its corresponding public key,
114
+ * also in JWK format. The derivation process involves converting the private key to a
115
+ * raw byte array and then computing the corresponding public key on the Curve25519 curve in
116
+ * Twisted Edwards form. The public key is then encoded into base64url format to construct
117
+ * a JWK representation.
118
+ *
119
+ * @example
120
+ * ```ts
121
+ * const privateKey = { ... }; // A Jwk object representing an Ed25519 private key
122
+ * const publicKey = await Ed25519.computePublicKey({ key: privateKey });
123
+ * ```
124
+ *
125
+ * @param params - The parameters for the public key derivation.
126
+ * @param params.key - The private key in JWK format from which to derive the public key.
127
+ *
128
+ * @returns A Promise that resolves to the computed public key in JWK format.
129
+ */
130
+ static computePublicKey({ key }: ComputePublicKeyParams): Promise<Jwk>;
131
+ /**
132
+ * Converts an Ed25519 private key to its X25519 counterpart.
133
+ *
134
+ * @remarks
135
+ * This method enables the use of the same key pair for both digital signature (Ed25519)
136
+ * and key exchange (X25519) operations. It takes an Ed25519 private key and converts it
137
+ * to the corresponding X25519 format, facilitating interoperability between signing
138
+ * and encryption protocols.
139
+ *
140
+ * @example
141
+ * ```ts
142
+ * const ed25519PrivateKey = { ... }; // An Ed25519 private key in JWK format
143
+ * const x25519PrivateKey = await Ed25519.convertPrivateKeyToX25519({
144
+ * privateKey: ed25519PrivateKey
145
+ * });
146
+ * ```
147
+ *
148
+ * @param params - The parameters for the private key conversion.
149
+ * @param params.privateKey - The Ed25519 private key to convert, in JWK format.
150
+ *
151
+ * @returns A Promise that resolves to the X25519 private key in JWK format.
152
+ */
153
+ static convertPrivateKeyToX25519({ privateKey }: {
154
+ privateKey: Jwk;
155
+ }): Promise<Jwk>;
156
+ /**
157
+ * Converts an Ed25519 public key to its X25519 counterpart.
158
+ *
159
+ * @remarks
160
+ * This method enables the use of the same key pair for both digital signature (Ed25519)
161
+ * and key exchange (X25519) operations. It takes an Ed25519 public key and converts it
162
+ * to the corresponding X25519 format, facilitating interoperability between signing
163
+ * and encryption protocols.
164
+ *
165
+ * @example
166
+ * ```ts
167
+ * const ed25519PublicKey = { ... }; // An Ed25519 public key in JWK format
168
+ * const x25519PublicKey = await Ed25519.convertPublicKeyToX25519({
169
+ * publicKey: ed25519PublicKey
170
+ * });
171
+ * ```
172
+ *
173
+ * @param params - The parameters for the public key conversion.
174
+ * @param params.publicKey - The Ed25519 public key to convert, in JWK format.
175
+ *
176
+ * @returns A Promise that resolves to the X25519 public key in JWK format.
177
+ */
178
+ static convertPublicKeyToX25519({ publicKey }: {
179
+ publicKey: Jwk;
180
+ }): Promise<Jwk>;
181
+ /**
182
+ * Generates an Ed25519 private key in JSON Web Key (JWK) format.
183
+ *
184
+ * @remarks
185
+ * This method creates a new private key suitable for use with the Curve25519 elliptic curve in
186
+ * Twisted Edwards form. The key generation process involves using cryptographically secure
187
+ * random number generation to ensure the uniqueness and security of the key. The resulting
188
+ * private key adheres to the JWK format making it compatible with common cryptographic
189
+ * standards and easy to use in various cryptographic processes.
190
+ *
191
+ * The generated private key in JWK format includes the following components:
192
+ * - `kty`: Key Type, set to 'OKP' for Octet Key Pair.
193
+ * - `crv`: Curve Name, set to 'Ed25519'.
194
+ * - `d`: The private key component, base64url-encoded.
195
+ * - `x`: The derived public key, base64url-encoded.
196
+ *
197
+ * @example
198
+ * ```ts
199
+ * const privateKey = await Ed25519.generateKey();
200
+ * ```
201
+ *
202
+ * @returns A Promise that resolves to the generated private key in JWK format.
203
+ */
204
+ static generateKey(): Promise<Jwk>;
205
+ /**
206
+ * Retrieves the public key properties from a given private key in JWK format.
207
+ *
208
+ * @remarks
209
+ * This method extracts the public key portion from an Ed25519 private key in JWK format. It does
210
+ * so by removing the private key property 'd' and making a shallow copy, effectively yielding the
211
+ * public key. The method sets the 'kid' (key ID) property using the JWK thumbprint if it is not
212
+ * already defined. This approach is used under the assumption that a private key in JWK format
213
+ * always contains the corresponding public key properties.
214
+ *
215
+ * Note: This method offers a significant performance advantage, being about 100 times faster
216
+ * than `computePublicKey()`. However, it does not mathematically validate the private key, nor
217
+ * does it derive the public key from the private key. It simply extracts existing public key
218
+ * properties from the private key object. This makes it suitable for scenarios where speed is
219
+ * critical and the private key's integrity is already assured.
220
+ *
221
+ * @example
222
+ * ```ts
223
+ * const privateKey = { ... }; // A Jwk object representing an Ed25519 private key
224
+ * const publicKey = await Ed25519.getPublicKey({ key: privateKey });
225
+ * ```
226
+ *
227
+ * @param params - The parameters for retrieving the public key properties.
228
+ * @param params.key - The private key in JWK format.
229
+ *
230
+ * @returns A Promise that resolves to the public key in JWK format.
231
+ */
232
+ static getPublicKey({ key }: GetPublicKeyParams): Promise<Jwk>;
233
+ /**
234
+ * Converts a private key from JSON Web Key (JWK) format to a raw byte array (Uint8Array).
235
+ *
236
+ * @remarks
237
+ * This method accepts a private key in JWK format and extracts its raw byte representation.
238
+ *
239
+ * This method accepts a public key in JWK format and converts it into its raw binary
240
+ * form. The conversion process involves decoding the 'd' parameter of the JWK
241
+ * from base64url format into a byte array.
242
+ *
243
+ * @example
244
+ * ```ts
245
+ * const privateKey = { ... }; // An Ed25519 private key in JWK format
246
+ * const privateKeyBytes = await Ed25519.privateKeyToBytes({ privateKey });
247
+ * ```
248
+ *
249
+ * @param params - The parameters for the private key conversion.
250
+ * @param params.privateKey - The private key in JWK format.
251
+ *
252
+ * @returns A Promise that resolves to the private key as a Uint8Array.
253
+ */
254
+ static privateKeyToBytes({ privateKey }: {
255
+ privateKey: Jwk;
256
+ }): Promise<Uint8Array>;
257
+ /**
258
+ * Converts a public key from JSON Web Key (JWK) format to a raw byte array (Uint8Array).
259
+ *
260
+ * @remarks
261
+ * This method accepts a public key in JWK format and converts it into its raw binary form.
262
+ * The conversion process involves decoding the 'x' parameter of the JWK (which represent the
263
+ * x coordinate of the elliptic curve point) from base64url format into a byte array.
264
+ *
265
+ * @example
266
+ * ```ts
267
+ * const publicKey = { ... }; // An Ed25519 public key in JWK format
268
+ * const publicKeyBytes = await Ed25519.publicKeyToBytes({ publicKey });
269
+ * ```
270
+ *
271
+ * @param params - The parameters for the public key conversion.
272
+ * @param params.publicKey - The public key in JWK format.
273
+ *
274
+ * @returns A Promise that resolves to the public key as a Uint8Array.
275
+ */
276
+ static publicKeyToBytes({ publicKey }: {
277
+ publicKey: Jwk;
278
+ }): Promise<Uint8Array>;
279
+ /**
280
+ * Generates an RFC8032-compliant EdDSA signature of given data using an Ed25519 private key.
281
+ *
282
+ * @remarks
283
+ * This method signs the provided data with a specified private key using the EdDSA
284
+ * (Edwards-curve Digital Signature Algorithm) as defined in RFC8032. It
285
+ * involves converting the private key from JWK format to a byte array and then employing
286
+ * the Ed25519 algorithm to sign the data. The output is a digital signature in the form
287
+ * of a Uint8Array, uniquely corresponding to both the data and the private key used for
288
+ * signing.
289
+ *
290
+ * @example
291
+ * ```ts
292
+ * const data = new TextEncoder().encode('Messsage'); // Data to be signed
293
+ * const privateKey = { ... }; // A Jwk object representing an Ed25519 private key
294
+ * const signature = await Ed25519.sign({ key: privateKey, data });
295
+ * ```
296
+ *
297
+ * @param params - The parameters for the signing operation.
298
+ * @param params.key - The private key to use for signing, represented in JWK format.
299
+ * @param params.data - The data to sign, represented as a Uint8Array.
300
+ *
301
+ * @returns A Promise that resolves to the signature as a Uint8Array.
302
+ */
303
+ static sign({ key, data }: SignParams): Promise<Uint8Array>;
304
+ /**
305
+ * Validates a given public key to confirm its mathematical correctness on the Edwards curve.
306
+ *
307
+ * @remarks
308
+ * This method decodes the Edwards points from the key bytes and asserts their validity on the
309
+ * Curve25519 curve in Twisted Edwards form. If the points are not valid, the method returns
310
+ * false. If the points are valid, the method returns true.
311
+ *
312
+ * Note that this validation strictly pertains to the key's format and numerical validity; it does
313
+ * not assess whether the key corresponds to a known entity or its security status (e.g., whether
314
+ * it has been compromised).
315
+ *
316
+ * @example
317
+ * ```ts
318
+ * const publicKeyBytes = new Uint8Array([...]); // A public key in byte format
319
+ * const isValid = await Ed25519.validatePublicKey({ publicKeyBytes });
320
+ * console.log(isValid); // true if the key is valid on the Edwards curve, false otherwise
321
+ * ```
322
+ *
323
+ * @param params - The parameters for the public key validation.
324
+ * @param params.publicKeyBytes - The public key to validate, represented as a Uint8Array.
325
+ *
326
+ * @returns A Promise that resolves to a boolean indicating whether the key
327
+ * corresponds to a valid point on the Edwards curve.
328
+ */
329
+ static validatePublicKey({ publicKeyBytes }: {
330
+ publicKeyBytes: Uint8Array;
331
+ }): Promise<boolean>;
332
+ /**
333
+ * Verifies an RFC8032-compliant EdDSA signature against given data using an Ed25519 public key.
334
+ *
335
+ * @remarks
336
+ * This method validates a digital signature to ensure its authenticity and integrity.
337
+ * It uses the EdDSA (Edwards-curve Digital Signature Algorithm) as specified in RFC8032.
338
+ * The verification process involves converting the public key from JWK format to a raw
339
+ * byte array and using the Ed25519 algorithm to validate the signature against the provided data.
340
+ *
341
+ * @example
342
+ * ```ts
343
+ * const data = new TextEncoder().encode('Messsage'); // Data that was signed
344
+ * const publicKey = { ... }; // A Jwk object representing an Ed25519 public key
345
+ * const signature = new Uint8Array([...]); // Signature to verify
346
+ * const isValid = await Ed25519.verify({ key: publicKey, signature, data });
347
+ * console.log(isValid); // true if the signature is valid, false otherwise
348
+ * ```
349
+ *
350
+ * @param params - The parameters for the signature verification.
351
+ * @param params.key - The public key in JWK format used for verification.
352
+ * @param params.signature - The signature to verify, represented as a Uint8Array.
353
+ * @param params.data - The data that was signed, represented as a Uint8Array.
354
+ *
355
+ * @returns A Promise that resolves to a boolean indicating whether the signature is valid.
356
+ */
357
+ static verify({ key, signature, data }: VerifyParams): Promise<boolean>;
358
+ }
359
+ //# sourceMappingURL=ed25519.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ed25519.d.ts","sourceRoot":"","sources":["../../../src/primitives/ed25519.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,gBAAgB,CAAC;AAC1C,OAAO,KAAK,EAAE,sBAAsB,EAAE,kBAAkB,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AAItH;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgDG;AACH,qBAAa,OAAO;IAClB;;;;;;;;;;;;;;;;;;;;;;;;;OAyBG;WACiB,iBAAiB,CAAC,EAAE,eAAe,EAAE,EAAE;QACzD,eAAe,EAAE,UAAU,CAAC;KAC7B,GAAG,OAAO,CAAC,GAAG,CAAC;IAkBhB;;;;;;;;;;;;;;;;;;;;;;;OAuBG;WACiB,gBAAgB,CAAC,EAAE,cAAc,EAAE,EAAE;QACvD,cAAc,EAAE,UAAU,CAAC;KAC5B,GAAG,OAAO,CAAC,GAAG,CAAC;IAchB;;;;;;;;;;;;;;;;;;;;OAoBG;WACiB,gBAAgB,CAAC,EAAE,GAAG,EAAE,EAC1C,sBAAsB,GACrB,OAAO,CAAC,GAAG,CAAC;IAoBf;;;;;;;;;;;;;;;;;;;;;OAqBG;WACiB,yBAAyB,CAAC,EAAE,UAAU,EAAE,EAAE;QAC5D,UAAU,EAAE,GAAG,CAAC;KACjB,GAAG,OAAO,CAAC,GAAG,CAAC;IAwBhB;;;;;;;;;;;;;;;;;;;;;OAqBG;WACiB,wBAAwB,CAAC,EAAE,SAAS,EAAE,EAAE;QAC1D,SAAS,EAAE,GAAG,CAAC;KAChB,GAAG,OAAO,CAAC,GAAG,CAAC;IA0BhB;;;;;;;;;;;;;;;;;;;;;;OAsBG;WACiB,WAAW,IAAI,OAAO,CAAC,GAAG,CAAC;IAa/C;;;;;;;;;;;;;;;;;;;;;;;;;;OA0BG;WACiB,YAAY,CAAC,EAAE,GAAG,EAAE,EACtC,kBAAkB,GACjB,OAAO,CAAC,GAAG,CAAC;IAef;;;;;;;;;;;;;;;;;;;;OAoBG;WACiB,iBAAiB,CAAC,EAAE,UAAU,EAAE,EAAE;QACpD,UAAU,EAAE,GAAG,CAAC;KACjB,GAAG,OAAO,CAAC,UAAU,CAAC;IAYvB;;;;;;;;;;;;;;;;;;OAkBG;WACiB,gBAAgB,CAAC,EAAE,SAAS,EAAE,EAAE;QAClD,SAAS,EAAE,GAAG,CAAC;KAChB,GAAG,OAAO,CAAC,UAAU,CAAC;IAYvB;;;;;;;;;;;;;;;;;;;;;;;OAuBG;WACiB,IAAI,CAAC,EAAE,GAAG,EAAE,IAAI,EAAE,EACpC,UAAU,GACT,OAAO,CAAC,UAAU,CAAC;IAUtB;;;;;;;;;;;;;;;;;;;;;;;;OAwBG;WACiB,iBAAiB,CAAC,EAAE,cAAc,EAAE,EAAE;QACxD,cAAc,EAAE,UAAU,CAAC;KAC5B,GAAG,OAAO,CAAC,OAAO,CAAC;IAepB;;;;;;;;;;;;;;;;;;;;;;;;OAwBG;WACiB,MAAM,CAAC,EAAE,GAAG,EAAE,SAAS,EAAE,IAAI,EAAE,EACjD,YAAY,GACX,OAAO,CAAC,OAAO,CAAC;CASpB"}
@@ -0,0 +1,94 @@
1
+ /**
2
+ * The object that should be passed into `Pbkdf2.deriveKey()`, when using the PBKDF2 algorithm.
3
+ */
4
+ export type Pbkdf2DeriveKeyParams = {
5
+ /**
6
+ * A string representing the digest algorithm to use. This may be one of:
7
+ * - 'SHA-256'
8
+ * - 'SHA-384'
9
+ * - 'SHA-512'
10
+ */
11
+ hash: 'SHA-256' | 'SHA-384' | 'SHA-512';
12
+ /**
13
+ * The password from which to derive the key, represented as a Uint8Array.
14
+ */
15
+ password: Uint8Array;
16
+ /**
17
+ * The salt value to use in the derivation process, as a Uint8Array. This should be a random or
18
+ * pseudo-random value of at least 16 bytes. Unlike the `password`, `salt` does not need to be
19
+ * kept secret.
20
+ */
21
+ salt: Uint8Array;
22
+ /**
23
+ * A `Number` representing the number of iterations the hash function will be executed in
24
+ * `deriveKey()`. This impacts the computational cost of the `deriveKey()` operation, making it
25
+ * more resistant to dictionary attacks. The higher the number, the more secure, but also slower,
26
+ * the operation. Choose a value that balances security needs and performance for your
27
+ * application.
28
+ */
29
+ iterations: number;
30
+ /**
31
+ * The desired length of the derived key in bits. To be compatible with all browsers, the number
32
+ * should be a multiple of 8.
33
+ */
34
+ length: number;
35
+ };
36
+ /**
37
+ * The `Pbkdf2` class provides a secure way to derive cryptographic keys from a password
38
+ * using the PBKDF2 (Password-Based Key Derivation Function 2) algorithm.
39
+ *
40
+ * The PBKDF2 algorithm is widely used for generating keys from passwords, as it applies
41
+ * a pseudorandom function to the input password along with a salt value and iterates the
42
+ * process multiple times to increase the key's resistance to brute-force attacks.
43
+ *
44
+ * This class offers a single static method `deriveKey` to perform key derivation.
45
+ *
46
+ * @example
47
+ * ```ts
48
+ * // Key Derivation
49
+ * const derivedKey = await Pbkdf2.deriveKey({
50
+ * hash: 'SHA-256', // The hash function to use ('SHA-256', 'SHA-384', 'SHA-512')
51
+ * password: new TextEncoder().encode('password'), // The password as a Uint8Array
52
+ * salt: new Uint8Array([...]), // The salt value
53
+ * iterations: 1000, // The number of iterations
54
+ * length: 256 // The length of the derived key in bits
55
+ * });
56
+ * ```
57
+ *
58
+ * @remarks
59
+ * This class relies on the availability of the Web Crypto API.
60
+ */
61
+ export declare class Pbkdf2 {
62
+ /**
63
+ * Derives a cryptographic key from a password using the PBKDF2 algorithm.
64
+ *
65
+ * @remarks
66
+ * This method applies the PBKDF2 algorithm to the provided password along with
67
+ * a salt value and iterates the process a specified number of times. It uses
68
+ * a cryptographic hash function to enhance security and produce a key of the
69
+ * desired length. The method is capable of utilizing either the Web Crypto API
70
+ * or the Node.js Crypto module, depending on the environment's support.
71
+ *
72
+ * @example
73
+ * ```ts
74
+ * const derivedKey = await Pbkdf2.deriveKey({
75
+ * hash: 'SHA-256',
76
+ * password: new TextEncoder().encode('password'),
77
+ * salt: new Uint8Array([...]),
78
+ * iterations: 1000,
79
+ * length: 256
80
+ * });
81
+ * ```
82
+ *
83
+ * @param params - The parameters for key derivation.
84
+ * @param params.hash - The hash function to use, such as 'SHA-256', 'SHA-384', or 'SHA-512'.
85
+ * @param params.password - The password from which to derive the key, represented as a Uint8Array.
86
+ * @param params.salt - The salt value to use in the derivation process, as a Uint8Array.
87
+ * @param params.iterations - The number of iterations to apply in the PBKDF2 algorithm.
88
+ * @param params.length - The desired length of the derived key in bits.
89
+ *
90
+ * @returns A Promise that resolves to the derived key as a Uint8Array.
91
+ */
92
+ static deriveKey({ hash, password, salt, iterations, length }: Pbkdf2DeriveKeyParams): Promise<Uint8Array>;
93
+ }
94
+ //# sourceMappingURL=pbkdf2.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pbkdf2.d.ts","sourceRoot":"","sources":["../../../src/primitives/pbkdf2.ts"],"names":[],"mappings":"AAEA;;GAEG;AACH,MAAM,MAAM,qBAAqB,GAAG;IAClC;;;;;OAKG;IACH,IAAI,EAAE,SAAS,GAAG,SAAS,GAAG,SAAS,CAAC;IAExC;;OAEG;IACH,QAAQ,EAAE,UAAU,CAAC;IAErB;;;;OAIG;IACH,IAAI,EAAE,UAAU,CAAC;IAEjB;;;;;;OAMG;IACH,UAAU,EAAE,MAAM,CAAC;IAEnB;;;OAGG;IACH,MAAM,EAAE,MAAM,CAAC;CAChB,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,qBAAa,MAAM;IACjB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA6BG;WACiB,SAAS,CAAC,EAAE,IAAI,EAAE,QAAQ,EAAE,IAAI,EAAE,UAAU,EAAE,MAAM,EAAE,EACxE,qBAAqB,GACpB,OAAO,CAAC,UAAU,CAAC;CAqBvB"}