@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,521 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ var __rest = (this && this.__rest) || function (s, e) {
11
+ var t = {};
12
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
13
+ t[p] = s[p];
14
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
15
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
16
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
17
+ t[p[i]] = s[p[i]];
18
+ }
19
+ return t;
20
+ };
21
+ import { Convert } from '@enbox/common';
22
+ import { ed25519, edwardsToMontgomeryPub, edwardsToMontgomeryPriv, x25519 } from '@noble/curves/ed25519';
23
+ import { computeJwkThumbprint, isOkpPrivateJwk, isOkpPublicJwk } from '../jose/jwk.js';
24
+ /**
25
+ * The `Ed25519` class provides a comprehensive suite of utilities for working with the Ed25519
26
+ * elliptic curve, widely used in modern cryptographic applications. This class includes methods for
27
+ * key generation, conversion, signing, verification, and public key derivation.
28
+ *
29
+ * The class supports conversions between raw byte formats and JSON Web Key (JWK) formats. It
30
+ * follows the guidelines and specifications outlined in RFC8032 for EdDSA (Edwards-curve Digital
31
+ * Signature Algorithm) operations.
32
+ *
33
+ * Key Features:
34
+ * - Key Generation: Generate Ed25519 private keys in JWK format.
35
+ * - Key Conversion: Transform keys between raw byte arrays and JWK formats.
36
+ * - Public Key Derivation: Derive public keys from private keys.
37
+ * - Signing and Verification: Sign data and verify signatures with Ed25519 keys.
38
+ * - Key Validation: Validate the mathematical correctness of Ed25519 keys.
39
+ *
40
+ * The methods in this class are asynchronous, returning Promises to accommodate various
41
+ * JavaScript environments, and use `Uint8Array` for binary data handling.
42
+ *
43
+ * @example
44
+ * ```ts
45
+ * // Key Generation
46
+ * const privateKey = await Ed25519.generateKey();
47
+ *
48
+ * // Public Key Derivation
49
+ * const publicKey = await Ed25519.computePublicKey({ key: privateKey });
50
+ * console.log(publicKey === await Ed25519.getPublicKey({ key: privateKey })); // Output: true
51
+ *
52
+ * // EdDSA Signing
53
+ * const signature = await Ed25519.sign({
54
+ * key: privateKey,
55
+ * data: new TextEncoder().encode('Message')
56
+ * });
57
+ *
58
+ * // EdDSA Signature Verification
59
+ * const isValid = await Ed25519.verify({
60
+ * key: publicKey,
61
+ * signature: signature,
62
+ * data: new TextEncoder().encode('Message')
63
+ * });
64
+ *
65
+ * // Key Conversion
66
+ * const privateKeyBytes = await Ed25519.privateKeyToBytes({ privateKey });
67
+ * const publicKeyBytes = await Ed25519.publicKeyToBytes({ publicKey });
68
+ *
69
+ * // Key Validation
70
+ * const isPublicKeyValid = await Ed25519.validatePublicKey({ publicKeyBytes });
71
+ * ```
72
+ */
73
+ export class Ed25519 {
74
+ /**
75
+ * Converts a raw private key in bytes to its corresponding JSON Web Key (JWK) format.
76
+ *
77
+ * @remarks
78
+ * This method accepts a private key as a byte array (Uint8Array) for the Curve25519 curve in
79
+ * Twisted Edwards form and transforms it into a JWK object. The process involves first deriving
80
+ * the public key from the private key, then encoding both the private and public keys into
81
+ * base64url format.
82
+ *
83
+ * The resulting JWK object includes the following properties:
84
+ * - `kty`: Key Type, set to 'OKP' for Octet Key Pair.
85
+ * - `crv`: Curve Name, set to 'Ed25519'.
86
+ * - `d`: The private key component, base64url-encoded.
87
+ * - `x`: The computed public key, base64url-encoded.
88
+ *
89
+ * @example
90
+ * ```ts
91
+ * const privateKeyBytes = new Uint8Array([...]); // Replace with actual private key bytes
92
+ * const privateKey = await Ed25519.bytesToPrivateKey({ privateKeyBytes });
93
+ * ```
94
+ *
95
+ * @param params - The parameters for the private key conversion.
96
+ * @param params.privateKeyBytes - The raw private key as a Uint8Array.
97
+ *
98
+ * @returns A Promise that resolves to the private key in JWK format.
99
+ */
100
+ static bytesToPrivateKey(_a) {
101
+ return __awaiter(this, arguments, void 0, function* ({ privateKeyBytes }) {
102
+ // Derive the public key from the private key.
103
+ const publicKeyBytes = ed25519.getPublicKey(privateKeyBytes);
104
+ // Construct the private key in JWK format.
105
+ const privateKey = {
106
+ crv: 'Ed25519',
107
+ d: Convert.uint8Array(privateKeyBytes).toBase64Url(),
108
+ kty: 'OKP',
109
+ x: Convert.uint8Array(publicKeyBytes).toBase64Url(),
110
+ };
111
+ // Compute the JWK thumbprint and set as the key ID.
112
+ privateKey.kid = yield computeJwkThumbprint({ jwk: privateKey });
113
+ return privateKey;
114
+ });
115
+ }
116
+ /**
117
+ * Converts a raw private key in bytes to its corresponding JSON Web Key (JWK) format.
118
+ *
119
+ * @remarks
120
+ * This method accepts a public key as a byte array (Uint8Array) for the Curve25519 curve in
121
+ * Twisted Edwards form and transforms it into a JWK object. The process involves encoding the
122
+ * public key bytes into base64url format.
123
+ *
124
+ * The resulting JWK object includes the following properties:
125
+ * - `kty`: Key Type, set to 'OKP' for Octet Key Pair.
126
+ * - `crv`: Curve Name, set to 'X25519'.
127
+ * - `x`: The public key, base64url-encoded.
128
+ *
129
+ * @example
130
+ * ```ts
131
+ * const publicKeyBytes = new Uint8Array([...]); // Replace with actual public key bytes
132
+ * const publicKey = await X25519.bytesToPublicKey({ publicKeyBytes });
133
+ * ```
134
+ *
135
+ * @param params - The parameters for the public key conversion.
136
+ * @param params.publicKeyBytes - The raw public key as a `Uint8Array`.
137
+ *
138
+ * @returns A Promise that resolves to the public key in JWK format.
139
+ */
140
+ static bytesToPublicKey(_a) {
141
+ return __awaiter(this, arguments, void 0, function* ({ publicKeyBytes }) {
142
+ // Construct the public key in JWK format.
143
+ const publicKey = {
144
+ kty: 'OKP',
145
+ crv: 'Ed25519',
146
+ x: Convert.uint8Array(publicKeyBytes).toBase64Url(),
147
+ };
148
+ // Compute the JWK thumbprint and set as the key ID.
149
+ publicKey.kid = yield computeJwkThumbprint({ jwk: publicKey });
150
+ return publicKey;
151
+ });
152
+ }
153
+ /**
154
+ * Derives the public key in JWK format from a given Ed25519 private key.
155
+ *
156
+ * @remarks
157
+ * This method takes a private key in JWK format and derives its corresponding public key,
158
+ * also in JWK format. The derivation process involves converting the private key to a
159
+ * raw byte array and then computing the corresponding public key on the Curve25519 curve in
160
+ * Twisted Edwards form. The public key is then encoded into base64url format to construct
161
+ * a JWK representation.
162
+ *
163
+ * @example
164
+ * ```ts
165
+ * const privateKey = { ... }; // A Jwk object representing an Ed25519 private key
166
+ * const publicKey = await Ed25519.computePublicKey({ key: privateKey });
167
+ * ```
168
+ *
169
+ * @param params - The parameters for the public key derivation.
170
+ * @param params.key - The private key in JWK format from which to derive the public key.
171
+ *
172
+ * @returns A Promise that resolves to the computed public key in JWK format.
173
+ */
174
+ static computePublicKey(_a) {
175
+ return __awaiter(this, arguments, void 0, function* ({ key }) {
176
+ // Convert the provided private key to a byte array.
177
+ const privateKeyBytes = yield Ed25519.privateKeyToBytes({ privateKey: key });
178
+ // Derive the public key from the private key.
179
+ const publicKeyBytes = ed25519.getPublicKey(privateKeyBytes);
180
+ // Construct the public key in JWK format.
181
+ const publicKey = {
182
+ kty: 'OKP',
183
+ crv: 'Ed25519',
184
+ x: Convert.uint8Array(publicKeyBytes).toBase64Url()
185
+ };
186
+ // Compute the JWK thumbprint and set as the key ID.
187
+ publicKey.kid = yield computeJwkThumbprint({ jwk: publicKey });
188
+ return publicKey;
189
+ });
190
+ }
191
+ /**
192
+ * Converts an Ed25519 private key to its X25519 counterpart.
193
+ *
194
+ * @remarks
195
+ * This method enables the use of the same key pair for both digital signature (Ed25519)
196
+ * and key exchange (X25519) operations. It takes an Ed25519 private key and converts it
197
+ * to the corresponding X25519 format, facilitating interoperability between signing
198
+ * and encryption protocols.
199
+ *
200
+ * @example
201
+ * ```ts
202
+ * const ed25519PrivateKey = { ... }; // An Ed25519 private key in JWK format
203
+ * const x25519PrivateKey = await Ed25519.convertPrivateKeyToX25519({
204
+ * privateKey: ed25519PrivateKey
205
+ * });
206
+ * ```
207
+ *
208
+ * @param params - The parameters for the private key conversion.
209
+ * @param params.privateKey - The Ed25519 private key to convert, in JWK format.
210
+ *
211
+ * @returns A Promise that resolves to the X25519 private key in JWK format.
212
+ */
213
+ static convertPrivateKeyToX25519(_a) {
214
+ return __awaiter(this, arguments, void 0, function* ({ privateKey }) {
215
+ // Convert the provided Ed25519 private key to bytes.
216
+ const ed25519PrivateKeyBytes = yield Ed25519.privateKeyToBytes({ privateKey });
217
+ // Convert the Ed25519 private key to an X25519 private key.
218
+ const x25519PrivateKeyBytes = edwardsToMontgomeryPriv(ed25519PrivateKeyBytes);
219
+ // Derive the X25519 public key from the X25519 private key.
220
+ const x25519PublicKeyBytes = x25519.getPublicKey(x25519PrivateKeyBytes);
221
+ // Construct the X25519 private key in JWK format.
222
+ const x25519PrivateKey = {
223
+ kty: 'OKP',
224
+ crv: 'X25519',
225
+ d: Convert.uint8Array(x25519PrivateKeyBytes).toBase64Url(),
226
+ x: Convert.uint8Array(x25519PublicKeyBytes).toBase64Url(),
227
+ };
228
+ // Compute the JWK thumbprint and set as the key ID.
229
+ x25519PrivateKey.kid = yield computeJwkThumbprint({ jwk: x25519PrivateKey });
230
+ return x25519PrivateKey;
231
+ });
232
+ }
233
+ /**
234
+ * Converts an Ed25519 public key to its X25519 counterpart.
235
+ *
236
+ * @remarks
237
+ * This method enables the use of the same key pair for both digital signature (Ed25519)
238
+ * and key exchange (X25519) operations. It takes an Ed25519 public key and converts it
239
+ * to the corresponding X25519 format, facilitating interoperability between signing
240
+ * and encryption protocols.
241
+ *
242
+ * @example
243
+ * ```ts
244
+ * const ed25519PublicKey = { ... }; // An Ed25519 public key in JWK format
245
+ * const x25519PublicKey = await Ed25519.convertPublicKeyToX25519({
246
+ * publicKey: ed25519PublicKey
247
+ * });
248
+ * ```
249
+ *
250
+ * @param params - The parameters for the public key conversion.
251
+ * @param params.publicKey - The Ed25519 public key to convert, in JWK format.
252
+ *
253
+ * @returns A Promise that resolves to the X25519 public key in JWK format.
254
+ */
255
+ static convertPublicKeyToX25519(_a) {
256
+ return __awaiter(this, arguments, void 0, function* ({ publicKey }) {
257
+ // Convert the provided private key to a byte array.
258
+ const ed25519PublicKeyBytes = yield Ed25519.publicKeyToBytes({ publicKey });
259
+ // Verify Edwards public key is valid.
260
+ const isValid = yield Ed25519.validatePublicKey({ publicKeyBytes: ed25519PublicKeyBytes });
261
+ if (!isValid) {
262
+ throw new Error('Ed25519: Invalid public key.');
263
+ }
264
+ // Convert the Ed25519 public key to an X25519 private key.
265
+ const x25519PublicKeyBytes = edwardsToMontgomeryPub(ed25519PublicKeyBytes);
266
+ // Construct the X25519 private key in JWK format.
267
+ const x25519PublicKey = {
268
+ kty: 'OKP',
269
+ crv: 'X25519',
270
+ x: Convert.uint8Array(x25519PublicKeyBytes).toBase64Url(),
271
+ };
272
+ // Compute the JWK thumbprint and set as the key ID.
273
+ x25519PublicKey.kid = yield computeJwkThumbprint({ jwk: x25519PublicKey });
274
+ return x25519PublicKey;
275
+ });
276
+ }
277
+ /**
278
+ * Generates an Ed25519 private key in JSON Web Key (JWK) format.
279
+ *
280
+ * @remarks
281
+ * This method creates a new private key suitable for use with the Curve25519 elliptic curve in
282
+ * Twisted Edwards form. The key generation process involves using cryptographically secure
283
+ * random number generation to ensure the uniqueness and security of the key. The resulting
284
+ * private key adheres to the JWK format making it compatible with common cryptographic
285
+ * standards and easy to use in various cryptographic processes.
286
+ *
287
+ * The generated private key in JWK format includes the following components:
288
+ * - `kty`: Key Type, set to 'OKP' for Octet Key Pair.
289
+ * - `crv`: Curve Name, set to 'Ed25519'.
290
+ * - `d`: The private key component, base64url-encoded.
291
+ * - `x`: The derived public key, base64url-encoded.
292
+ *
293
+ * @example
294
+ * ```ts
295
+ * const privateKey = await Ed25519.generateKey();
296
+ * ```
297
+ *
298
+ * @returns A Promise that resolves to the generated private key in JWK format.
299
+ */
300
+ static generateKey() {
301
+ return __awaiter(this, void 0, void 0, function* () {
302
+ // Generate a random private key.
303
+ const privateKeyBytes = ed25519.utils.randomPrivateKey();
304
+ // Convert private key from bytes to JWK format.
305
+ const privateKey = yield Ed25519.bytesToPrivateKey({ privateKeyBytes });
306
+ // Compute the JWK thumbprint and set as the key ID.
307
+ privateKey.kid = yield computeJwkThumbprint({ jwk: privateKey });
308
+ return privateKey;
309
+ });
310
+ }
311
+ /**
312
+ * Retrieves the public key properties from a given private key in JWK format.
313
+ *
314
+ * @remarks
315
+ * This method extracts the public key portion from an Ed25519 private key in JWK format. It does
316
+ * so by removing the private key property 'd' and making a shallow copy, effectively yielding the
317
+ * public key. The method sets the 'kid' (key ID) property using the JWK thumbprint if it is not
318
+ * already defined. This approach is used under the assumption that a private key in JWK format
319
+ * always contains the corresponding public key properties.
320
+ *
321
+ * Note: This method offers a significant performance advantage, being about 100 times faster
322
+ * than `computePublicKey()`. However, it does not mathematically validate the private key, nor
323
+ * does it derive the public key from the private key. It simply extracts existing public key
324
+ * properties from the private key object. This makes it suitable for scenarios where speed is
325
+ * critical and the private key's integrity is already assured.
326
+ *
327
+ * @example
328
+ * ```ts
329
+ * const privateKey = { ... }; // A Jwk object representing an Ed25519 private key
330
+ * const publicKey = await Ed25519.getPublicKey({ key: privateKey });
331
+ * ```
332
+ *
333
+ * @param params - The parameters for retrieving the public key properties.
334
+ * @param params.key - The private key in JWK format.
335
+ *
336
+ * @returns A Promise that resolves to the public key in JWK format.
337
+ */
338
+ static getPublicKey(_a) {
339
+ return __awaiter(this, arguments, void 0, function* ({ key }) {
340
+ var _b;
341
+ // Verify the provided JWK represents an octet key pair (OKP) Ed25519 private key.
342
+ if (!(isOkpPrivateJwk(key) && key.crv === 'Ed25519')) {
343
+ throw new Error(`Ed25519: The provided key is not an Ed25519 private JWK.`);
344
+ }
345
+ // Remove the private key property ('d') and make a shallow copy of the provided key.
346
+ let { d } = key, publicKey = __rest(key, ["d"]);
347
+ // If the key ID is undefined, set it to the JWK thumbprint.
348
+ (_b = publicKey.kid) !== null && _b !== void 0 ? _b : (publicKey.kid = yield computeJwkThumbprint({ jwk: publicKey }));
349
+ return publicKey;
350
+ });
351
+ }
352
+ /**
353
+ * Converts a private key from JSON Web Key (JWK) format to a raw byte array (Uint8Array).
354
+ *
355
+ * @remarks
356
+ * This method accepts a private key in JWK format and extracts its raw byte representation.
357
+ *
358
+ * This method accepts a public key in JWK format and converts it into its raw binary
359
+ * form. The conversion process involves decoding the 'd' parameter of the JWK
360
+ * from base64url format into a byte array.
361
+ *
362
+ * @example
363
+ * ```ts
364
+ * const privateKey = { ... }; // An Ed25519 private key in JWK format
365
+ * const privateKeyBytes = await Ed25519.privateKeyToBytes({ privateKey });
366
+ * ```
367
+ *
368
+ * @param params - The parameters for the private key conversion.
369
+ * @param params.privateKey - The private key in JWK format.
370
+ *
371
+ * @returns A Promise that resolves to the private key as a Uint8Array.
372
+ */
373
+ static privateKeyToBytes(_a) {
374
+ return __awaiter(this, arguments, void 0, function* ({ privateKey }) {
375
+ // Verify the provided JWK represents a valid OKP private key.
376
+ if (!isOkpPrivateJwk(privateKey)) {
377
+ throw new Error(`Ed25519: The provided key is not a valid OKP private key.`);
378
+ }
379
+ // Decode the provided private key to bytes.
380
+ const privateKeyBytes = Convert.base64Url(privateKey.d).toUint8Array();
381
+ return privateKeyBytes;
382
+ });
383
+ }
384
+ /**
385
+ * Converts a public key from JSON Web Key (JWK) format to a raw byte array (Uint8Array).
386
+ *
387
+ * @remarks
388
+ * This method accepts a public key in JWK format and converts it into its raw binary form.
389
+ * The conversion process involves decoding the 'x' parameter of the JWK (which represent the
390
+ * x coordinate of the elliptic curve point) from base64url format into a byte array.
391
+ *
392
+ * @example
393
+ * ```ts
394
+ * const publicKey = { ... }; // An Ed25519 public key in JWK format
395
+ * const publicKeyBytes = await Ed25519.publicKeyToBytes({ publicKey });
396
+ * ```
397
+ *
398
+ * @param params - The parameters for the public key conversion.
399
+ * @param params.publicKey - The public key in JWK format.
400
+ *
401
+ * @returns A Promise that resolves to the public key as a Uint8Array.
402
+ */
403
+ static publicKeyToBytes(_a) {
404
+ return __awaiter(this, arguments, void 0, function* ({ publicKey }) {
405
+ // Verify the provided JWK represents a valid OKP public key.
406
+ if (!isOkpPublicJwk(publicKey)) {
407
+ throw new Error(`Ed25519: The provided key is not a valid OKP public key.`);
408
+ }
409
+ // Decode the provided public key to bytes.
410
+ const publicKeyBytes = Convert.base64Url(publicKey.x).toUint8Array();
411
+ return publicKeyBytes;
412
+ });
413
+ }
414
+ /**
415
+ * Generates an RFC8032-compliant EdDSA signature of given data using an Ed25519 private key.
416
+ *
417
+ * @remarks
418
+ * This method signs the provided data with a specified private key using the EdDSA
419
+ * (Edwards-curve Digital Signature Algorithm) as defined in RFC8032. It
420
+ * involves converting the private key from JWK format to a byte array and then employing
421
+ * the Ed25519 algorithm to sign the data. The output is a digital signature in the form
422
+ * of a Uint8Array, uniquely corresponding to both the data and the private key used for
423
+ * signing.
424
+ *
425
+ * @example
426
+ * ```ts
427
+ * const data = new TextEncoder().encode('Messsage'); // Data to be signed
428
+ * const privateKey = { ... }; // A Jwk object representing an Ed25519 private key
429
+ * const signature = await Ed25519.sign({ key: privateKey, data });
430
+ * ```
431
+ *
432
+ * @param params - The parameters for the signing operation.
433
+ * @param params.key - The private key to use for signing, represented in JWK format.
434
+ * @param params.data - The data to sign, represented as a Uint8Array.
435
+ *
436
+ * @returns A Promise that resolves to the signature as a Uint8Array.
437
+ */
438
+ static sign(_a) {
439
+ return __awaiter(this, arguments, void 0, function* ({ key, data }) {
440
+ // Convert the private key from JWK format to bytes.
441
+ const privateKeyBytes = yield Ed25519.privateKeyToBytes({ privateKey: key });
442
+ // Sign the provided data using the EdDSA algorithm.
443
+ const signature = ed25519.sign(data, privateKeyBytes);
444
+ return signature;
445
+ });
446
+ }
447
+ /**
448
+ * Validates a given public key to confirm its mathematical correctness on the Edwards curve.
449
+ *
450
+ * @remarks
451
+ * This method decodes the Edwards points from the key bytes and asserts their validity on the
452
+ * Curve25519 curve in Twisted Edwards form. If the points are not valid, the method returns
453
+ * false. If the points are valid, the method returns true.
454
+ *
455
+ * Note that this validation strictly pertains to the key's format and numerical validity; it does
456
+ * not assess whether the key corresponds to a known entity or its security status (e.g., whether
457
+ * it has been compromised).
458
+ *
459
+ * @example
460
+ * ```ts
461
+ * const publicKeyBytes = new Uint8Array([...]); // A public key in byte format
462
+ * const isValid = await Ed25519.validatePublicKey({ publicKeyBytes });
463
+ * console.log(isValid); // true if the key is valid on the Edwards curve, false otherwise
464
+ * ```
465
+ *
466
+ * @param params - The parameters for the public key validation.
467
+ * @param params.publicKeyBytes - The public key to validate, represented as a Uint8Array.
468
+ *
469
+ * @returns A Promise that resolves to a boolean indicating whether the key
470
+ * corresponds to a valid point on the Edwards curve.
471
+ */
472
+ static validatePublicKey(_a) {
473
+ return __awaiter(this, arguments, void 0, function* ({ publicKeyBytes }) {
474
+ try {
475
+ // Decode Edwards points from key bytes.
476
+ const point = ed25519.ExtendedPoint.fromHex(publicKeyBytes);
477
+ // Check if points are on the Twisted Edwards curve.
478
+ point.assertValidity();
479
+ }
480
+ catch (error) {
481
+ return false;
482
+ }
483
+ return true;
484
+ });
485
+ }
486
+ /**
487
+ * Verifies an RFC8032-compliant EdDSA signature against given data using an Ed25519 public key.
488
+ *
489
+ * @remarks
490
+ * This method validates a digital signature to ensure its authenticity and integrity.
491
+ * It uses the EdDSA (Edwards-curve Digital Signature Algorithm) as specified in RFC8032.
492
+ * The verification process involves converting the public key from JWK format to a raw
493
+ * byte array and using the Ed25519 algorithm to validate the signature against the provided data.
494
+ *
495
+ * @example
496
+ * ```ts
497
+ * const data = new TextEncoder().encode('Messsage'); // Data that was signed
498
+ * const publicKey = { ... }; // A Jwk object representing an Ed25519 public key
499
+ * const signature = new Uint8Array([...]); // Signature to verify
500
+ * const isValid = await Ed25519.verify({ key: publicKey, signature, data });
501
+ * console.log(isValid); // true if the signature is valid, false otherwise
502
+ * ```
503
+ *
504
+ * @param params - The parameters for the signature verification.
505
+ * @param params.key - The public key in JWK format used for verification.
506
+ * @param params.signature - The signature to verify, represented as a Uint8Array.
507
+ * @param params.data - The data that was signed, represented as a Uint8Array.
508
+ *
509
+ * @returns A Promise that resolves to a boolean indicating whether the signature is valid.
510
+ */
511
+ static verify(_a) {
512
+ return __awaiter(this, arguments, void 0, function* ({ key, signature, data }) {
513
+ // Convert the public key from JWK format to bytes.
514
+ const publicKeyBytes = yield Ed25519.publicKeyToBytes({ publicKey: key });
515
+ // Perform the verification of the signature.
516
+ const isValid = ed25519.verify(signature, data, publicKeyBytes);
517
+ return isValid;
518
+ });
519
+ }
520
+ }
521
+ //# sourceMappingURL=ed25519.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ed25519.js","sourceRoot":"","sources":["../../../src/primitives/ed25519.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AACxC,OAAO,EAAE,OAAO,EAAE,sBAAsB,EAAE,uBAAuB,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAC;AAKzG,OAAO,EAAE,oBAAoB,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAEvF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgDG;AACH,MAAM,OAAO,OAAO;IAClB;;;;;;;;;;;;;;;;;;;;;;;;;OAyBG;IACI,MAAM,CAAO,iBAAiB;6DAAC,EAAE,eAAe,EAEtD;YACC,8CAA8C;YAC9C,MAAM,cAAc,GAAI,OAAO,CAAC,YAAY,CAAC,eAAe,CAAC,CAAC;YAE9D,2CAA2C;YAC3C,MAAM,UAAU,GAAQ;gBACtB,GAAG,EAAG,SAAS;gBACf,CAAC,EAAK,OAAO,CAAC,UAAU,CAAC,eAAe,CAAC,CAAC,WAAW,EAAE;gBACvD,GAAG,EAAG,KAAK;gBACX,CAAC,EAAK,OAAO,CAAC,UAAU,CAAC,cAAc,CAAC,CAAC,WAAW,EAAE;aACvD,CAAC;YAEF,oDAAoD;YACpD,UAAU,CAAC,GAAG,GAAG,MAAM,oBAAoB,CAAC,EAAE,GAAG,EAAE,UAAU,EAAE,CAAC,CAAC;YAEjE,OAAO,UAAU,CAAC;QACpB,CAAC;KAAA;IAED;;;;;;;;;;;;;;;;;;;;;;;OAuBG;IACI,MAAM,CAAO,gBAAgB;6DAAC,EAAE,cAA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IAAI,CAAC;QACd,CAAC;KAAA;IAED;;;;;;;;;;;;;;;;;;;;;;;;OAwBG;IACI,MAAM,CAAO,MAAM;6DAAC,EAAE,GAAG,EAAE,SAAS,EAAE,IAAI,EACnC;YAEZ,mDAAmD;YACnD,MAAM,cAAc,GAAG,MAAM,OAAO,CAAC,gBAAgB,CAAC,EAAE,SAAS,EAAE,GAAG,EAAE,CAAC,CAAC;YAE1E,6CAA6C;YAC7C,MAAM,OAAO,GAAG,OAAO,CAAC,MAAM,CAAC,SAAS,EAAE,IAAI,EAAE,cAAc,CAAC,CAAC;YAEhE,OAAO,OAAO,CAAC;QACjB,CAAC;KAAA;CACF"}
@@ -0,0 +1,78 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import { crypto } from '@noble/hashes/crypto';
11
+ /**
12
+ * The `Pbkdf2` class provides a secure way to derive cryptographic keys from a password
13
+ * using the PBKDF2 (Password-Based Key Derivation Function 2) algorithm.
14
+ *
15
+ * The PBKDF2 algorithm is widely used for generating keys from passwords, as it applies
16
+ * a pseudorandom function to the input password along with a salt value and iterates the
17
+ * process multiple times to increase the key's resistance to brute-force attacks.
18
+ *
19
+ * This class offers a single static method `deriveKey` to perform key derivation.
20
+ *
21
+ * @example
22
+ * ```ts
23
+ * // Key Derivation
24
+ * const derivedKey = await Pbkdf2.deriveKey({
25
+ * hash: 'SHA-256', // The hash function to use ('SHA-256', 'SHA-384', 'SHA-512')
26
+ * password: new TextEncoder().encode('password'), // The password as a Uint8Array
27
+ * salt: new Uint8Array([...]), // The salt value
28
+ * iterations: 1000, // The number of iterations
29
+ * length: 256 // The length of the derived key in bits
30
+ * });
31
+ * ```
32
+ *
33
+ * @remarks
34
+ * This class relies on the availability of the Web Crypto API.
35
+ */
36
+ export class Pbkdf2 {
37
+ /**
38
+ * Derives a cryptographic key from a password using the PBKDF2 algorithm.
39
+ *
40
+ * @remarks
41
+ * This method applies the PBKDF2 algorithm to the provided password along with
42
+ * a salt value and iterates the process a specified number of times. It uses
43
+ * a cryptographic hash function to enhance security and produce a key of the
44
+ * desired length. The method is capable of utilizing either the Web Crypto API
45
+ * or the Node.js Crypto module, depending on the environment's support.
46
+ *
47
+ * @example
48
+ * ```ts
49
+ * const derivedKey = await Pbkdf2.deriveKey({
50
+ * hash: 'SHA-256',
51
+ * password: new TextEncoder().encode('password'),
52
+ * salt: new Uint8Array([...]),
53
+ * iterations: 1000,
54
+ * length: 256
55
+ * });
56
+ * ```
57
+ *
58
+ * @param params - The parameters for key derivation.
59
+ * @param params.hash - The hash function to use, such as 'SHA-256', 'SHA-384', or 'SHA-512'.
60
+ * @param params.password - The password from which to derive the key, represented as a Uint8Array.
61
+ * @param params.salt - The salt value to use in the derivation process, as a Uint8Array.
62
+ * @param params.iterations - The number of iterations to apply in the PBKDF2 algorithm.
63
+ * @param params.length - The desired length of the derived key in bits.
64
+ *
65
+ * @returns A Promise that resolves to the derived key as a Uint8Array.
66
+ */
67
+ static deriveKey(_a) {
68
+ return __awaiter(this, arguments, void 0, function* ({ hash, password, salt, iterations, length }) {
69
+ // Import the password as a raw key for use with the Web Crypto API.
70
+ const webCryptoKey = yield crypto.subtle.importKey('raw', password, { name: 'PBKDF2' }, false, ['deriveBits']);
71
+ const derivedKeyBuffer = yield crypto.subtle.deriveBits({ name: 'PBKDF2', hash, salt, iterations }, webCryptoKey, length);
72
+ // Convert from ArrayBuffer to Uint8Array.
73
+ const derivedKey = new Uint8Array(derivedKeyBuffer);
74
+ return derivedKey;
75
+ });
76
+ }
77
+ }
78
+ //# sourceMappingURL=pbkdf2.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pbkdf2.js","sourceRoot":"","sources":["../../../src/primitives/pbkdf2.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,sBAAsB,CAAC;AA0C9C;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,MAAM,OAAO,MAAM;IACjB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA6BG;IACI,MAAM,CAAO,SAAS;6DAAC,EAAE,IAAI,EAAE,QAAQ,EAAE,IAAI,EAAE,UAAU,EAAE,MAAM,EACjD;YAErB,oEAAoE;YACpE,MAAM,YAAY,GAAG,MAAM,MAAM,CAAC,MAAM,CAAC,SAAS,CAChD,KAAK,EACL,QAAQ,EACR,EAAE,IAAI,EAAE,QAAQ,EAAE,EAClB,KAAK,EACL,CAAC,YAAY,CAAC,CACf,CAAC;YAEF,MAAM,gBAAgB,GAAG,MAAM,MAAM,CAAC,MAAM,CAAC,UAAU,CACrD,EAAE,IAAI,EAAE,QAAQ,EAAE,IAAI,EAAE,IAAI,EAAE,UAAU,EAAE,EAC1C,YAAY,EACZ,MAAM,CACP,CAAC;YAEF,0CAA0C;YAC1C,MAAM,UAAU,GAAG,IAAI,UAAU,CAAC,gBAAgB,CAAC,CAAC;YAEpD,OAAO,UAAU,CAAC;QACpB,CAAC;KAAA;CACF"}