@sebastienrousseau/crypto-lib 0.0.2 → 0.0.8

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 (243) hide show
  1. package/README.md +334 -185
  2. package/dist/accel/index.d.ts +11 -0
  3. package/dist/accel/index.d.ts.map +1 -0
  4. package/dist/accel/index.js +31 -0
  5. package/dist/accel/native-pqc-bridge.d.ts +18 -0
  6. package/dist/accel/native-pqc-bridge.d.ts.map +1 -0
  7. package/dist/accel/native-pqc-bridge.js +100 -0
  8. package/dist/accel/wasm-bridge.d.ts +35 -0
  9. package/dist/accel/wasm-bridge.d.ts.map +1 -0
  10. package/dist/accel/wasm-bridge.js +161 -0
  11. package/dist/accel/webcrypto-bridge.d.ts +32 -0
  12. package/dist/accel/webcrypto-bridge.d.ts.map +1 -0
  13. package/dist/accel/webcrypto-bridge.js +179 -0
  14. package/dist/accel/webcrypto-modern.d.ts +40 -0
  15. package/dist/accel/webcrypto-modern.d.ts.map +1 -0
  16. package/dist/accel/webcrypto-modern.js +213 -0
  17. package/dist/accel/worker-pool.d.ts +23 -0
  18. package/dist/accel/worker-pool.d.ts.map +1 -0
  19. package/dist/accel/worker-pool.js +119 -0
  20. package/dist/bin/cryptolib.d.ts +25 -0
  21. package/dist/bin/cryptolib.d.ts.map +1 -0
  22. package/dist/bin/cryptolib.js +87 -0
  23. package/dist/config/config.d.ts +8 -0
  24. package/dist/config/config.d.ts.map +1 -0
  25. package/dist/config/config.js +13 -0
  26. package/dist/crypto.d.ts +43 -0
  27. package/dist/crypto.d.ts.map +1 -0
  28. package/dist/crypto.js +107 -0
  29. package/dist/enums.d.ts +81 -0
  30. package/dist/enums.d.ts.map +1 -0
  31. package/dist/enums.js +82 -0
  32. package/dist/errors.d.ts +14 -0
  33. package/dist/errors.d.ts.map +1 -0
  34. package/dist/errors.js +22 -0
  35. package/dist/high-level/index.d.ts +11 -0
  36. package/dist/high-level/index.d.ts.map +1 -0
  37. package/dist/high-level/index.js +53 -0
  38. package/dist/high-level/key-wrap.d.ts +16 -0
  39. package/dist/high-level/key-wrap.d.ts.map +1 -0
  40. package/dist/high-level/key-wrap.js +93 -0
  41. package/dist/high-level/multi-recipient.d.ts +25 -0
  42. package/dist/high-level/multi-recipient.d.ts.map +1 -0
  43. package/dist/high-level/multi-recipient.js +122 -0
  44. package/dist/high-level/password-encrypt.d.ts +14 -0
  45. package/dist/high-level/password-encrypt.d.ts.map +1 -0
  46. package/dist/high-level/password-encrypt.js +93 -0
  47. package/dist/high-level/sealedbox.d.ts +13 -0
  48. package/dist/high-level/sealedbox.d.ts.map +1 -0
  49. package/dist/high-level/sealedbox.js +119 -0
  50. package/dist/high-level/secretbox.d.ts +7 -0
  51. package/dist/high-level/secretbox.d.ts.map +1 -0
  52. package/dist/high-level/secretbox.js +54 -0
  53. package/dist/index.d.ts +16 -2
  54. package/dist/index.d.ts.map +1 -1
  55. package/dist/index.js +61 -2
  56. package/dist/key/key.d.ts +10 -0
  57. package/dist/key/key.d.ts.map +1 -0
  58. package/dist/key/key.js +25 -0
  59. package/dist/key/keystore.d.ts +11 -0
  60. package/dist/key/keystore.d.ts.map +1 -0
  61. package/dist/key/keystore.js +87 -0
  62. package/dist/keys/index.d.ts +7 -0
  63. package/dist/keys/index.d.ts.map +1 -0
  64. package/dist/keys/index.js +22 -0
  65. package/dist/keys/keygen.d.ts +16 -0
  66. package/dist/keys/keygen.d.ts.map +1 -0
  67. package/dist/keys/keygen.js +128 -0
  68. package/dist/keys/keyring.d.ts +35 -0
  69. package/dist/keys/keyring.d.ts.map +1 -0
  70. package/dist/keys/keyring.js +136 -0
  71. package/dist/keys/serialize.d.ts +30 -0
  72. package/dist/keys/serialize.d.ts.map +1 -0
  73. package/dist/keys/serialize.js +120 -0
  74. package/dist/lib/decrypt.d.ts +5 -5
  75. package/dist/lib/decrypt.d.ts.map +1 -1
  76. package/dist/lib/decrypt.js +49 -39
  77. package/dist/lib/encrypt.d.ts +2 -5
  78. package/dist/lib/encrypt.d.ts.map +1 -1
  79. package/dist/lib/encrypt.js +34 -39
  80. package/dist/lib/generate.d.ts +2 -11
  81. package/dist/lib/generate.d.ts.map +1 -1
  82. package/dist/lib/generate.js +57 -29
  83. package/dist/lib/index.d.ts +10 -0
  84. package/dist/lib/index.d.ts.map +1 -0
  85. package/dist/lib/index.js +53 -0
  86. package/dist/lib/key-output.d.ts +7 -0
  87. package/dist/lib/key-output.d.ts.map +1 -0
  88. package/dist/lib/key-output.js +50 -0
  89. package/dist/lib/reformat.d.ts +9 -0
  90. package/dist/lib/reformat.d.ts.map +1 -0
  91. package/dist/lib/reformat.js +67 -0
  92. package/dist/lib/revoke.d.ts +3 -3
  93. package/dist/lib/revoke.d.ts.map +1 -1
  94. package/dist/lib/revoke.js +36 -28
  95. package/dist/lib/session.d.ts +5 -0
  96. package/dist/lib/session.d.ts.map +1 -0
  97. package/dist/lib/session.js +55 -0
  98. package/dist/lib/sign.d.ts +4 -0
  99. package/dist/lib/sign.d.ts.map +1 -0
  100. package/dist/lib/sign.js +63 -0
  101. package/dist/lib/verify.d.ts +5 -0
  102. package/dist/lib/verify.d.ts.map +1 -0
  103. package/dist/lib/verify.js +52 -0
  104. package/dist/modern/aead.d.ts +17 -0
  105. package/dist/modern/aead.d.ts.map +1 -0
  106. package/dist/modern/aead.js +51 -0
  107. package/dist/modern/aes.d.ts +35 -0
  108. package/dist/modern/aes.d.ts.map +1 -0
  109. package/dist/modern/aes.js +82 -0
  110. package/dist/modern/cost-limits.d.ts +11 -0
  111. package/dist/modern/cost-limits.d.ts.map +1 -0
  112. package/dist/modern/cost-limits.js +35 -0
  113. package/dist/modern/curves.d.ts +86 -0
  114. package/dist/modern/curves.d.ts.map +1 -0
  115. package/dist/modern/curves.js +171 -0
  116. package/dist/modern/ecdh.d.ts +11 -0
  117. package/dist/modern/ecdh.d.ts.map +1 -0
  118. package/dist/modern/ecdh.js +28 -0
  119. package/dist/modern/fn-dsa.d.ts +19 -0
  120. package/dist/modern/fn-dsa.d.ts.map +1 -0
  121. package/dist/modern/fn-dsa.js +57 -0
  122. package/dist/modern/hash.d.ts +13 -0
  123. package/dist/modern/hash.d.ts.map +1 -0
  124. package/dist/modern/hash.js +45 -0
  125. package/dist/modern/hpke.d.ts +47 -0
  126. package/dist/modern/hpke.d.ts.map +1 -0
  127. package/dist/modern/hpke.js +207 -0
  128. package/dist/modern/index.d.ts +40 -0
  129. package/dist/modern/index.d.ts.map +1 -0
  130. package/dist/modern/index.js +194 -0
  131. package/dist/modern/kdf.d.ts +23 -0
  132. package/dist/modern/kdf.d.ts.map +1 -0
  133. package/dist/modern/kdf.js +67 -0
  134. package/dist/modern/mac.d.ts +51 -0
  135. package/dist/modern/mac.d.ts.map +1 -0
  136. package/dist/modern/mac.js +119 -0
  137. package/dist/modern/password.d.ts +40 -0
  138. package/dist/modern/password.d.ts.map +1 -0
  139. package/dist/modern/password.js +123 -0
  140. package/dist/modern/pq-hash-sign.d.ts +18 -0
  141. package/dist/modern/pq-hash-sign.d.ts.map +1 -0
  142. package/dist/modern/pq-hash-sign.js +73 -0
  143. package/dist/modern/pq-kem.d.ts +88 -0
  144. package/dist/modern/pq-kem.d.ts.map +1 -0
  145. package/dist/modern/pq-kem.js +270 -0
  146. package/dist/modern/pq-sign.d.ts +30 -0
  147. package/dist/modern/pq-sign.d.ts.map +1 -0
  148. package/dist/modern/pq-sign.js +94 -0
  149. package/dist/modern/pq.d.ts +38 -0
  150. package/dist/modern/pq.d.ts.map +1 -0
  151. package/dist/modern/pq.js +80 -0
  152. package/dist/modern/signing.d.ts +16 -0
  153. package/dist/modern/signing.d.ts.map +1 -0
  154. package/dist/modern/signing.js +34 -0
  155. package/dist/protocols/index.d.ts +7 -0
  156. package/dist/protocols/index.d.ts.map +1 -0
  157. package/dist/protocols/index.js +46 -0
  158. package/dist/protocols/iso20022.d.ts +51 -0
  159. package/dist/protocols/iso20022.d.ts.map +1 -0
  160. package/dist/protocols/iso20022.js +145 -0
  161. package/dist/protocols/opaque/ake.d.ts +51 -0
  162. package/dist/protocols/opaque/ake.d.ts.map +1 -0
  163. package/dist/protocols/opaque/ake.js +102 -0
  164. package/dist/protocols/opaque/client.d.ts +45 -0
  165. package/dist/protocols/opaque/client.d.ts.map +1 -0
  166. package/dist/protocols/opaque/client.js +63 -0
  167. package/dist/protocols/opaque/config.d.ts +14 -0
  168. package/dist/protocols/opaque/config.d.ts.map +1 -0
  169. package/dist/protocols/opaque/config.js +18 -0
  170. package/dist/protocols/opaque/credentials.d.ts +29 -0
  171. package/dist/protocols/opaque/credentials.d.ts.map +1 -0
  172. package/dist/protocols/opaque/credentials.js +61 -0
  173. package/dist/protocols/opaque/envelope.d.ts +36 -0
  174. package/dist/protocols/opaque/envelope.d.ts.map +1 -0
  175. package/dist/protocols/opaque/envelope.js +77 -0
  176. package/dist/protocols/opaque/ksf.d.ts +15 -0
  177. package/dist/protocols/opaque/ksf.d.ts.map +1 -0
  178. package/dist/protocols/opaque/ksf.js +27 -0
  179. package/dist/protocols/opaque/messages.d.ts +61 -0
  180. package/dist/protocols/opaque/messages.d.ts.map +1 -0
  181. package/dist/protocols/opaque/messages.js +94 -0
  182. package/dist/protocols/opaque/server.d.ts +33 -0
  183. package/dist/protocols/opaque/server.d.ts.map +1 -0
  184. package/dist/protocols/opaque/server.js +74 -0
  185. package/dist/protocols/opaque/suite.d.ts +58 -0
  186. package/dist/protocols/opaque/suite.d.ts.map +1 -0
  187. package/dist/protocols/opaque/suite.js +115 -0
  188. package/dist/protocols/pake.d.ts +14 -0
  189. package/dist/protocols/pake.d.ts.map +1 -0
  190. package/dist/protocols/pake.js +33 -0
  191. package/dist/protocols/pqxdh.d.ts +53 -0
  192. package/dist/protocols/pqxdh.d.ts.map +1 -0
  193. package/dist/protocols/pqxdh.js +143 -0
  194. package/dist/protocols/ratchet.d.ts +52 -0
  195. package/dist/protocols/ratchet.d.ts.map +1 -0
  196. package/dist/protocols/ratchet.js +187 -0
  197. package/dist/protocols/threshold.d.ts +23 -0
  198. package/dist/protocols/threshold.d.ts.map +1 -0
  199. package/dist/protocols/threshold.js +191 -0
  200. package/dist/registry.d.ts +20 -0
  201. package/dist/registry.d.ts.map +1 -0
  202. package/dist/registry.js +399 -0
  203. package/dist/streaming/index.d.ts +7 -0
  204. package/dist/streaming/index.d.ts.map +1 -0
  205. package/dist/streaming/index.js +15 -0
  206. package/dist/streaming/stream-aead.d.ts +17 -0
  207. package/dist/streaming/stream-aead.d.ts.map +1 -0
  208. package/dist/streaming/stream-aead.js +125 -0
  209. package/dist/streaming/stream-hash.d.ts +9 -0
  210. package/dist/streaming/stream-hash.d.ts.map +1 -0
  211. package/dist/streaming/stream-hash.js +52 -0
  212. package/dist/streaming/web-streams.d.ts +17 -0
  213. package/dist/streaming/web-streams.d.ts.map +1 -0
  214. package/dist/streaming/web-streams.js +114 -0
  215. package/dist/tokens/index.d.ts +5 -0
  216. package/dist/tokens/index.d.ts.map +1 -0
  217. package/dist/tokens/index.js +15 -0
  218. package/dist/tokens/key-rotation.d.ts +20 -0
  219. package/dist/tokens/key-rotation.d.ts.map +1 -0
  220. package/dist/tokens/key-rotation.js +76 -0
  221. package/dist/tokens/paseto.d.ts +46 -0
  222. package/dist/tokens/paseto.d.ts.map +1 -0
  223. package/dist/tokens/paseto.js +200 -0
  224. package/dist/types/types.d.ts +57 -0
  225. package/dist/types/types.d.ts.map +1 -1
  226. package/dist/types/types.js +1 -0
  227. package/dist/utils.d.ts +13 -0
  228. package/dist/utils.d.ts.map +1 -0
  229. package/dist/utils.js +56 -0
  230. package/package.json +77 -130
  231. package/dist/COPYRIGHT +0 -15
  232. package/dist/Report.txt +0 -72
  233. package/dist/bin/crypto-lib.d.ts +0 -13
  234. package/dist/bin/crypto-lib.d.ts.map +0 -1
  235. package/dist/bin/crypto-lib.js +0 -52
  236. package/dist/bin/crypto-lib.js.map +0 -1
  237. package/dist/index.js.map +0 -1
  238. package/dist/lib/decrypt.js.map +0 -1
  239. package/dist/lib/encrypt.js.map +0 -1
  240. package/dist/lib/generate.js.map +0 -1
  241. package/dist/lib/revoke.js.map +0 -1
  242. package/dist/package.json +0 -131
  243. package/dist/types/types.js.map +0 -1
@@ -0,0 +1,114 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.WEB_STREAM_HASH_ALGORITHMS = void 0;
4
+ exports.createEncryptStream = createEncryptStream;
5
+ exports.createDecryptStream = createDecryptStream;
6
+ exports.createHashStream = createHashStream;
7
+ const chacha_js_1 = require("@noble/ciphers/chacha.js");
8
+ const utils_js_1 = require("@noble/ciphers/utils.js");
9
+ const sha2_js_1 = require("@noble/hashes/sha2.js");
10
+ const sha3_js_1 = require("@noble/hashes/sha3.js");
11
+ const blake3_js_1 = require("@noble/hashes/blake3.js");
12
+ const TS = globalThis
13
+ .TransformStream;
14
+ const NONCE_LEN = 24;
15
+ exports.WEB_STREAM_HASH_ALGORITHMS = [
16
+ "sha256",
17
+ "sha512",
18
+ "sha3-256",
19
+ "sha3-512",
20
+ "blake3",
21
+ ];
22
+ const HEX_RE = /^[0-9a-fA-F]*$/;
23
+ function toKey(key) {
24
+ if (key instanceof Uint8Array) {
25
+ if (key.length !== 32) {
26
+ throw new Error(`Key must be 32 bytes, got ${key.length}`);
27
+ }
28
+ return key;
29
+ }
30
+ if (!HEX_RE.test(key))
31
+ throw new Error("Invalid hex key");
32
+ const buf = Buffer.from(key, "hex");
33
+ if (buf.length !== 32) {
34
+ throw new Error(`Key must be 32 bytes (256 bits), got ${buf.length}`);
35
+ }
36
+ return buf;
37
+ }
38
+ const hashFactories = {
39
+ sha256: () => sha2_js_1.sha256.create(),
40
+ sha512: () => sha2_js_1.sha512.create(),
41
+ "sha3-256": () => sha3_js_1.sha3_256.create(),
42
+ "sha3-512": () => sha3_js_1.sha3_512.create(),
43
+ blake3: () => blake3_js_1.blake3.create(),
44
+ };
45
+ function createEncryptStream(options) {
46
+ const keyBytes = toKey(options.key);
47
+ const chunks = [];
48
+ let totalLen = 0;
49
+ return new TS({
50
+ transform(chunk) {
51
+ chunks.push(chunk);
52
+ totalLen += chunk.length;
53
+ },
54
+ flush(controller) {
55
+ const plaintext = new Uint8Array(totalLen);
56
+ let offset = 0;
57
+ for (const c of chunks) {
58
+ plaintext.set(c, offset);
59
+ offset += c.length;
60
+ }
61
+ const nonce = (0, utils_js_1.randomBytes)(NONCE_LEN);
62
+ const cipher = (0, chacha_js_1.xchacha20poly1305)(keyBytes, nonce);
63
+ const encrypted = cipher.encrypt(plaintext);
64
+ const output = new Uint8Array(NONCE_LEN + encrypted.length);
65
+ output.set(nonce, 0);
66
+ output.set(encrypted, NONCE_LEN);
67
+ controller.enqueue(output);
68
+ },
69
+ });
70
+ }
71
+ function createDecryptStream(options) {
72
+ const keyBytes = toKey(options.key);
73
+ const chunks = [];
74
+ let totalLen = 0;
75
+ return new TS({
76
+ transform(chunk) {
77
+ chunks.push(chunk);
78
+ totalLen += chunk.length;
79
+ },
80
+ flush(controller) {
81
+ const ciphertext = new Uint8Array(totalLen);
82
+ let offset = 0;
83
+ for (const c of chunks) {
84
+ ciphertext.set(c, offset);
85
+ offset += c.length;
86
+ }
87
+ if (ciphertext.length < NONCE_LEN + 16) {
88
+ throw new Error("Ciphertext too short — missing nonce or auth tag");
89
+ }
90
+ const nonce = ciphertext.subarray(0, NONCE_LEN);
91
+ const encrypted = ciphertext.subarray(NONCE_LEN);
92
+ const cipher = (0, chacha_js_1.xchacha20poly1305)(keyBytes, nonce);
93
+ const plaintext = cipher.decrypt(encrypted);
94
+ controller.enqueue(plaintext);
95
+ },
96
+ });
97
+ }
98
+ function createHashStream(algorithm) {
99
+ const factory = hashFactories[algorithm];
100
+ if (!factory) {
101
+ throw new Error(`Unsupported hash algorithm: ${algorithm}`);
102
+ }
103
+ const hasher = factory();
104
+ return new TS({
105
+ transform(chunk) {
106
+ hasher.update(chunk);
107
+ },
108
+ flush(controller) {
109
+ const digest = Buffer.from(hasher.digest()).toString("hex");
110
+ controller.enqueue({ digest, algorithm });
111
+ },
112
+ });
113
+ }
114
+ //# sourceMappingURL=web-streams.js.map
@@ -0,0 +1,5 @@
1
+ export { v4local, v4public, pae } from "./paseto";
2
+ export type { PasetoLocalEncryptOptions, PasetoLocalDecryptOptions, PasetoPublicSignOptions, PasetoPublicVerifyOptions, PasetoToken, PasetoPayload, } from "./paseto";
3
+ export { createKeyRing, rotateKey, findKeyByVersion, pruneExpiredKeys, encryptWithVersion, decryptWithVersion, } from "./key-rotation";
4
+ export type { VersionedKey, KeyRing } from "./key-rotation";
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/tokens/index.ts"],"names":[],"mappings":"AAcA,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC;AAElD,YAAY,EACV,yBAAyB,EACzB,yBAAyB,EACzB,uBAAuB,EACvB,yBAAyB,EACzB,WAAW,EACX,aAAa,GACd,MAAM,UAAU,CAAC;AAElB,OAAO,EACL,aAAa,EACb,SAAS,EACT,gBAAgB,EAChB,gBAAgB,EAChB,kBAAkB,EAClB,kBAAkB,GACnB,MAAM,gBAAgB,CAAC;AAExB,YAAY,EAAE,YAAY,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC"}
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.decryptWithVersion = exports.encryptWithVersion = exports.pruneExpiredKeys = exports.findKeyByVersion = exports.rotateKey = exports.createKeyRing = exports.pae = exports.v4public = exports.v4local = void 0;
4
+ var paseto_1 = require("./paseto");
5
+ Object.defineProperty(exports, "v4local", { enumerable: true, get: function () { return paseto_1.v4local; } });
6
+ Object.defineProperty(exports, "v4public", { enumerable: true, get: function () { return paseto_1.v4public; } });
7
+ Object.defineProperty(exports, "pae", { enumerable: true, get: function () { return paseto_1.pae; } });
8
+ var key_rotation_1 = require("./key-rotation");
9
+ Object.defineProperty(exports, "createKeyRing", { enumerable: true, get: function () { return key_rotation_1.createKeyRing; } });
10
+ Object.defineProperty(exports, "rotateKey", { enumerable: true, get: function () { return key_rotation_1.rotateKey; } });
11
+ Object.defineProperty(exports, "findKeyByVersion", { enumerable: true, get: function () { return key_rotation_1.findKeyByVersion; } });
12
+ Object.defineProperty(exports, "pruneExpiredKeys", { enumerable: true, get: function () { return key_rotation_1.pruneExpiredKeys; } });
13
+ Object.defineProperty(exports, "encryptWithVersion", { enumerable: true, get: function () { return key_rotation_1.encryptWithVersion; } });
14
+ Object.defineProperty(exports, "decryptWithVersion", { enumerable: true, get: function () { return key_rotation_1.decryptWithVersion; } });
15
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,20 @@
1
+ export interface VersionedKey {
2
+ version: string;
3
+ key: string;
4
+ activatedAt: Date;
5
+ expiresAt?: Date;
6
+ }
7
+ export interface KeyRing {
8
+ current: VersionedKey;
9
+ previous: VersionedKey[];
10
+ }
11
+ export declare function createKeyRing(current: VersionedKey): KeyRing;
12
+ export declare function rotateKey(ring: KeyRing, newKey: VersionedKey): KeyRing;
13
+ export declare function findKeyByVersion(ring: KeyRing, version: string): VersionedKey | undefined;
14
+ export declare function pruneExpiredKeys(ring: KeyRing, now?: Date): KeyRing;
15
+ export declare function encryptWithVersion(ring: KeyRing, plaintext: string): {
16
+ ciphertext: string;
17
+ version: string;
18
+ };
19
+ export declare function decryptWithVersion(ring: KeyRing, ciphertext: string, version: string): string;
20
+ //# sourceMappingURL=key-rotation.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"key-rotation.d.ts","sourceRoot":"","sources":["../../src/tokens/key-rotation.ts"],"names":[],"mappings":"AA8BA,MAAM,WAAW,YAAY;IAE3B,OAAO,EAAE,MAAM,CAAC;IAEhB,GAAG,EAAE,MAAM,CAAC;IAEZ,WAAW,EAAE,IAAI,CAAC;IAElB,SAAS,CAAC,EAAE,IAAI,CAAC;CAClB;AAMD,MAAM,WAAW,OAAO;IAEtB,OAAO,EAAE,YAAY,CAAC;IAEtB,QAAQ,EAAE,YAAY,EAAE,CAAC;CAC1B;AAaD,wBAAgB,aAAa,CAAC,OAAO,EAAE,YAAY,GAAG,OAAO,CAE5D;AAeD,wBAAgB,SAAS,CAAC,IAAI,EAAE,OAAO,EAAE,MAAM,EAAE,YAAY,GAAG,OAAO,CAKtE;AAcD,wBAAgB,gBAAgB,CAC9B,IAAI,EAAE,OAAO,EACb,OAAO,EAAE,MAAM,GACd,YAAY,GAAG,SAAS,CAG1B;AAiBD,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,OAAO,EAAE,GAAG,CAAC,EAAE,IAAI,GAAG,OAAO,CAMnE;AAeD,wBAAgB,kBAAkB,CAChC,IAAI,EAAE,OAAO,EACb,SAAS,EAAE,MAAM,GAChB;IAED,UAAU,EAAE,MAAM,CAAC;IAEnB,OAAO,EAAE,MAAM,CAAC;CACjB,CAGA;AAgBD,wBAAgB,kBAAkB,CAChC,IAAI,EAAE,OAAO,EACb,UAAU,EAAE,MAAM,EAClB,OAAO,EAAE,MAAM,GACd,MAAM,CAOR"}
@@ -0,0 +1,76 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.createKeyRing = createKeyRing;
37
+ exports.rotateKey = rotateKey;
38
+ exports.findKeyByVersion = findKeyByVersion;
39
+ exports.pruneExpiredKeys = pruneExpiredKeys;
40
+ exports.encryptWithVersion = encryptWithVersion;
41
+ exports.decryptWithVersion = decryptWithVersion;
42
+ const secretbox = __importStar(require("../high-level/secretbox"));
43
+ function createKeyRing(current) {
44
+ return { current, previous: [] };
45
+ }
46
+ function rotateKey(ring, newKey) {
47
+ return {
48
+ current: newKey,
49
+ previous: [...ring.previous, ring.current],
50
+ };
51
+ }
52
+ function findKeyByVersion(ring, version) {
53
+ if (ring.current.version === version)
54
+ return ring.current;
55
+ return ring.previous.find((k) => k.version === version);
56
+ }
57
+ function pruneExpiredKeys(ring, now) {
58
+ const ref = now ?? new Date();
59
+ return {
60
+ current: ring.current,
61
+ previous: ring.previous.filter((k) => !k.expiresAt || k.expiresAt > ref),
62
+ };
63
+ }
64
+ function encryptWithVersion(ring, plaintext) {
65
+ const { sealed } = secretbox.seal(ring.current.key, plaintext);
66
+ return { ciphertext: sealed, version: ring.current.version };
67
+ }
68
+ function decryptWithVersion(ring, ciphertext, version) {
69
+ const vk = findKeyByVersion(ring, version);
70
+ if (!vk) {
71
+ throw new Error(`Key version "${version}" not found in key ring`);
72
+ }
73
+ const pt = secretbox.open(vk.key, ciphertext);
74
+ return Buffer.from(pt).toString("utf8");
75
+ }
76
+ //# sourceMappingURL=key-rotation.js.map
@@ -0,0 +1,46 @@
1
+ export interface PasetoLocalEncryptOptions {
2
+ key: string;
3
+ payload: Record<string, unknown>;
4
+ footer?: string;
5
+ implicit?: string;
6
+ }
7
+ export interface PasetoLocalDecryptOptions {
8
+ key: string;
9
+ token: string;
10
+ footer?: string;
11
+ implicit?: string;
12
+ }
13
+ export interface PasetoPublicSignOptions {
14
+ secretKey: string;
15
+ payload: Record<string, unknown>;
16
+ footer?: string;
17
+ implicit?: string;
18
+ }
19
+ export interface PasetoPublicVerifyOptions {
20
+ publicKey: string;
21
+ token: string;
22
+ footer?: string;
23
+ implicit?: string;
24
+ }
25
+ export interface PasetoToken {
26
+ token: string;
27
+ }
28
+ export interface PasetoPayload {
29
+ payload: Record<string, unknown>;
30
+ footer?: string;
31
+ }
32
+ export declare function pae(...pieces: Uint8Array[]): Uint8Array;
33
+ declare function localEncrypt(opts: PasetoLocalEncryptOptions): PasetoToken;
34
+ declare function localDecrypt(opts: PasetoLocalDecryptOptions): PasetoPayload;
35
+ declare function publicSign(opts: PasetoPublicSignOptions): PasetoToken;
36
+ declare function publicVerify(opts: PasetoPublicVerifyOptions): PasetoPayload;
37
+ export declare const v4local: {
38
+ readonly encrypt: typeof localEncrypt;
39
+ readonly decrypt: typeof localDecrypt;
40
+ };
41
+ export declare const v4public: {
42
+ readonly sign: typeof publicSign;
43
+ readonly verify: typeof publicVerify;
44
+ };
45
+ export {};
46
+ //# sourceMappingURL=paseto.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"paseto.d.ts","sourceRoot":"","sources":["../../src/tokens/paseto.ts"],"names":[],"mappings":"AAqDA,MAAM,WAAW,yBAAyB;IAExC,GAAG,EAAE,MAAM,CAAC;IAEZ,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAEjC,MAAM,CAAC,EAAE,MAAM,CAAC;IAEhB,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAGD,MAAM,WAAW,yBAAyB;IAExC,GAAG,EAAE,MAAM,CAAC;IAEZ,KAAK,EAAE,MAAM,CAAC;IAEd,MAAM,CAAC,EAAE,MAAM,CAAC;IAEhB,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAGD,MAAM,WAAW,uBAAuB;IAEtC,SAAS,EAAE,MAAM,CAAC;IAElB,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAEjC,MAAM,CAAC,EAAE,MAAM,CAAC;IAEhB,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAGD,MAAM,WAAW,yBAAyB;IAExC,SAAS,EAAE,MAAM,CAAC;IAElB,KAAK,EAAE,MAAM,CAAC;IAEd,MAAM,CAAC,EAAE,MAAM,CAAC;IAEhB,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAGD,MAAM,WAAW,WAAW;IAE1B,KAAK,EAAE,MAAM,CAAC;CACf;AAGD,MAAM,WAAW,aAAa;IAE5B,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAEjC,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAkBD,wBAAgB,GAAG,CAAC,GAAG,MAAM,EAAE,UAAU,EAAE,GAAG,UAAU,CAoBvD;AAoCD,iBAAS,YAAY,CAAC,IAAI,EAAE,yBAAyB,GAAG,WAAW,CA+ClE;AAeD,iBAAS,YAAY,CAAC,IAAI,EAAE,yBAAyB,GAAG,aAAa,CA4DpE;AAiBD,iBAAS,UAAU,CAAC,IAAI,EAAE,uBAAuB,GAAG,WAAW,CA8B9D;AAcD,iBAAS,YAAY,CAAC,IAAI,EAAE,yBAAyB,GAAG,aAAa,CAkDpE;AAaD,eAAO,MAAM,OAAO;;;CAKV,CAAC;AAWX,eAAO,MAAM,QAAQ;;;CAKX,CAAC"}
@@ -0,0 +1,200 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.v4public = exports.v4local = void 0;
4
+ exports.pae = pae;
5
+ const chacha_js_1 = require("@noble/ciphers/chacha.js");
6
+ const utils_js_1 = require("@noble/ciphers/utils.js");
7
+ const ed25519_js_1 = require("@noble/curves/ed25519.js");
8
+ const blake2_js_1 = require("@noble/hashes/blake2.js");
9
+ const V4_LOCAL_HEADER = "v4.local.";
10
+ const V4_PUBLIC_HEADER = "v4.public.";
11
+ const NONCE_LEN = 32;
12
+ const EK_LEN = 32;
13
+ const N3_LEN = 24;
14
+ const SIG_LEN = 64;
15
+ const HEX_RE = /^[0-9a-fA-F]*$/;
16
+ function pae(...pieces) {
17
+ const le64 = (n) => {
18
+ const buf = new Uint8Array(8);
19
+ const view = new DataView(buf.buffer);
20
+ view.setBigUint64(0, BigInt(n), true);
21
+ return buf;
22
+ };
23
+ const parts = [le64(pieces.length)];
24
+ for (const p of pieces) {
25
+ parts.push(le64(p.length));
26
+ parts.push(p);
27
+ }
28
+ const total = parts.reduce((s, p) => s + p.length, 0);
29
+ const result = new Uint8Array(total);
30
+ let offset = 0;
31
+ for (const p of parts) {
32
+ result.set(p, offset);
33
+ offset += p.length;
34
+ }
35
+ return result;
36
+ }
37
+ function hexToBytes(hex) {
38
+ if (!HEX_RE.test(hex))
39
+ throw new Error("Invalid hex string");
40
+ return Buffer.from(hex, "hex");
41
+ }
42
+ function toBase64url(buf) {
43
+ return Buffer.from(buf).toString("base64url");
44
+ }
45
+ function fromBase64url(s) {
46
+ const padded = s.replace(/-/g, "+").replace(/_/g, "/");
47
+ return Buffer.from(padded, "base64");
48
+ }
49
+ const encoder = new TextEncoder();
50
+ function localEncrypt(opts) {
51
+ const { key, payload, footer = "", implicit = "" } = opts;
52
+ const keyBytes = hexToBytes(key);
53
+ if (keyBytes.length !== 32) {
54
+ throw new Error(`Key must be 32 bytes, got ${keyBytes.length}`);
55
+ }
56
+ const header = encoder.encode(V4_LOCAL_HEADER);
57
+ const message = encoder.encode(JSON.stringify(payload));
58
+ const footerBytes = encoder.encode(footer);
59
+ const implicitBytes = encoder.encode(implicit);
60
+ const randomN = (0, utils_js_1.randomBytes)(NONCE_LEN);
61
+ const nonce = (0, blake2_js_1.blake2b)(message, { key: randomN, dkLen: NONCE_LEN });
62
+ const ekInput = new Uint8Array(keyBytes.length + 16);
63
+ ekInput.set(keyBytes);
64
+ ekInput.set(nonce.subarray(0, 16), keyBytes.length);
65
+ const derived = (0, blake2_js_1.blake2b)(encoder.encode("paseto-encryption-key"), {
66
+ key: ekInput,
67
+ dkLen: EK_LEN + N3_LEN,
68
+ });
69
+ const ek = derived.subarray(0, EK_LEN);
70
+ const n3 = derived.subarray(EK_LEN, EK_LEN + N3_LEN);
71
+ const aad = pae(header, nonce, footerBytes, implicitBytes);
72
+ const cipher = (0, chacha_js_1.xchacha20poly1305)(ek, n3, aad);
73
+ const ciphertext = cipher.encrypt(message);
74
+ const body = new Uint8Array(NONCE_LEN + ciphertext.length);
75
+ body.set(nonce);
76
+ body.set(ciphertext, NONCE_LEN);
77
+ let token = V4_LOCAL_HEADER + toBase64url(body);
78
+ if (footer) {
79
+ token += "." + toBase64url(footerBytes);
80
+ }
81
+ return { token };
82
+ }
83
+ function localDecrypt(opts) {
84
+ const { key, token, footer = "", implicit = "" } = opts;
85
+ const keyBytes = hexToBytes(key);
86
+ if (keyBytes.length !== 32) {
87
+ throw new Error(`Key must be 32 bytes, got ${keyBytes.length}`);
88
+ }
89
+ if (!token.startsWith(V4_LOCAL_HEADER)) {
90
+ throw new Error(`Invalid token header: expected "${V4_LOCAL_HEADER}"`);
91
+ }
92
+ const withoutHeader = token.slice(V4_LOCAL_HEADER.length);
93
+ const parts = withoutHeader.split(".");
94
+ const bodyB64 = parts[0];
95
+ const footerB64 = parts[1] ?? "";
96
+ const footerBytes = encoder.encode(footer);
97
+ if (footerB64) {
98
+ const tokenFooter = fromBase64url(footerB64);
99
+ if (Buffer.from(tokenFooter).toString("utf8") !== footer) {
100
+ throw new Error("Footer mismatch");
101
+ }
102
+ }
103
+ else if (footer) {
104
+ throw new Error("Footer mismatch");
105
+ }
106
+ const body = fromBase64url(bodyB64);
107
+ if (body.length < NONCE_LEN + 16) {
108
+ throw new Error("Token body too short");
109
+ }
110
+ const header = encoder.encode(V4_LOCAL_HEADER);
111
+ const implicitBytes = encoder.encode(implicit);
112
+ const nonce = body.subarray(0, NONCE_LEN);
113
+ const ciphertext = body.subarray(NONCE_LEN);
114
+ const ekInput = new Uint8Array(keyBytes.length + 16);
115
+ ekInput.set(keyBytes);
116
+ ekInput.set(nonce.subarray(0, 16), keyBytes.length);
117
+ const derived = (0, blake2_js_1.blake2b)(encoder.encode("paseto-encryption-key"), {
118
+ key: ekInput,
119
+ dkLen: EK_LEN + N3_LEN,
120
+ });
121
+ const ek = derived.subarray(0, EK_LEN);
122
+ const n3 = derived.subarray(EK_LEN, EK_LEN + N3_LEN);
123
+ const aad = pae(header, nonce, footerBytes, implicitBytes);
124
+ const cipher = (0, chacha_js_1.xchacha20poly1305)(ek, n3, aad);
125
+ const plaintext = cipher.decrypt(ciphertext);
126
+ const payloadStr = Buffer.from(plaintext).toString("utf8");
127
+ const payload = JSON.parse(payloadStr);
128
+ return { payload, ...(footer ? { footer } : {}) };
129
+ }
130
+ function publicSign(opts) {
131
+ const { secretKey, payload, footer = "", implicit = "" } = opts;
132
+ const skBytes = hexToBytes(secretKey);
133
+ if (skBytes.length !== 64 && skBytes.length !== 32) {
134
+ throw new Error(`Secret key must be 32 or 64 bytes, got ${skBytes.length}`);
135
+ }
136
+ const header = encoder.encode(V4_PUBLIC_HEADER);
137
+ const message = encoder.encode(JSON.stringify(payload));
138
+ const footerBytes = encoder.encode(footer);
139
+ const implicitBytes = encoder.encode(implicit);
140
+ const m2 = pae(header, message, footerBytes, implicitBytes);
141
+ const seed = skBytes.length === 64 ? skBytes.subarray(0, 32) : skBytes;
142
+ const sig = ed25519_js_1.ed25519.sign(m2, seed);
143
+ const body = new Uint8Array(message.length + sig.length);
144
+ body.set(message);
145
+ body.set(sig, message.length);
146
+ let token = V4_PUBLIC_HEADER + toBase64url(body);
147
+ if (footer) {
148
+ token += "." + toBase64url(footerBytes);
149
+ }
150
+ return { token };
151
+ }
152
+ function publicVerify(opts) {
153
+ const { publicKey, token, footer = "", implicit = "" } = opts;
154
+ const pkBytes = hexToBytes(publicKey);
155
+ if (pkBytes.length !== 32) {
156
+ throw new Error(`Public key must be 32 bytes, got ${pkBytes.length}`);
157
+ }
158
+ if (!token.startsWith(V4_PUBLIC_HEADER)) {
159
+ throw new Error(`Invalid token header: expected "${V4_PUBLIC_HEADER}"`);
160
+ }
161
+ const withoutHeader = token.slice(V4_PUBLIC_HEADER.length);
162
+ const parts = withoutHeader.split(".");
163
+ const bodyB64 = parts[0];
164
+ const footerB64 = parts[1] ?? "";
165
+ const footerBytes = encoder.encode(footer);
166
+ if (footerB64) {
167
+ const tokenFooter = fromBase64url(footerB64);
168
+ if (Buffer.from(tokenFooter).toString("utf8") !== footer) {
169
+ throw new Error("Footer mismatch");
170
+ }
171
+ }
172
+ else if (footer) {
173
+ throw new Error("Footer mismatch");
174
+ }
175
+ const body = fromBase64url(bodyB64);
176
+ if (body.length < SIG_LEN) {
177
+ throw new Error("Token body too short");
178
+ }
179
+ const message = body.subarray(0, body.length - SIG_LEN);
180
+ const sig = body.subarray(body.length - SIG_LEN);
181
+ const header = encoder.encode(V4_PUBLIC_HEADER);
182
+ const implicitBytes = encoder.encode(implicit);
183
+ const m2 = pae(header, message, footerBytes, implicitBytes);
184
+ const valid = ed25519_js_1.ed25519.verify(sig, m2, pkBytes);
185
+ if (!valid) {
186
+ throw new Error("Invalid signature");
187
+ }
188
+ const payloadStr = Buffer.from(message).toString("utf8");
189
+ const payload = JSON.parse(payloadStr);
190
+ return { payload, ...(footer ? { footer } : {}) };
191
+ }
192
+ exports.v4local = {
193
+ encrypt: localEncrypt,
194
+ decrypt: localDecrypt,
195
+ };
196
+ exports.v4public = {
197
+ sign: publicSign,
198
+ verify: publicVerify,
199
+ };
200
+ //# sourceMappingURL=paseto.js.map
@@ -1 +1,58 @@
1
+ import enums from "../enums";
2
+ export interface UserID {
3
+ name: string;
4
+ email: string;
5
+ }
6
+ export type dataGenerate = {
7
+ date: Date;
8
+ name: string;
9
+ email: string;
10
+ userIDs: UserID[];
11
+ type: keyof typeof enums.type;
12
+ passphrase: string;
13
+ rsaBits: number;
14
+ curve: keyof typeof enums.curve;
15
+ keyExpirationTime: number;
16
+ format: keyof typeof enums.format;
17
+ };
18
+ export type dataRevoke = {
19
+ passphrase: string;
20
+ flag: number;
21
+ reason: string;
22
+ };
23
+ export type dataEncrypt = {
24
+ passphrase: string;
25
+ message: string;
26
+ publicKey: string;
27
+ privateKey?: string;
28
+ };
29
+ export type dataDecrypt = {
30
+ passphrase: string;
31
+ message: string;
32
+ publicKey: string;
33
+ privateKey?: string;
34
+ };
35
+ export type dataSign = {
36
+ message: string;
37
+ detached: boolean;
38
+ passphrase: string;
39
+ };
40
+ export type dataVerify = {
41
+ date: Date;
42
+ message: string;
43
+ verificationKeys: string | string[];
44
+ };
45
+ export type dataSessionKey = {
46
+ email: string;
47
+ name: string;
48
+ publicKey: string;
49
+ };
50
+ export type dataReformat = {
51
+ date: Date;
52
+ email: string;
53
+ expiration: number;
54
+ name: string;
55
+ passphrase: string;
56
+ publicKey: string;
57
+ };
1
58
  //# sourceMappingURL=types.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types/types.ts"],"names":[],"mappings":""}
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,UAAU,CAAC;AAK7B,MAAM,WAAW,MAAM;IAErB,IAAI,EAAE,MAAM,CAAC;IAEb,KAAK,EAAE,MAAM,CAAC;CACf;AAGD,MAAM,MAAM,YAAY,GAAG;IAEzB,IAAI,EAAE,IAAI,CAAC;IAEX,IAAI,EAAE,MAAM,CAAC;IAEb,KAAK,EAAE,MAAM,CAAC;IAEd,OAAO,EAAE,MAAM,EAAE,CAAC;IAElB,IAAI,EAAE,MAAM,OAAO,KAAK,CAAC,IAAI,CAAC;IAE9B,UAAU,EAAE,MAAM,CAAC;IAEnB,OAAO,EAAE,MAAM,CAAC;IAEhB,KAAK,EAAE,MAAM,OAAO,KAAK,CAAC,KAAK,CAAC;IAEhC,iBAAiB,EAAE,MAAM,CAAC;IAE1B,MAAM,EAAE,MAAM,OAAO,KAAK,CAAC,MAAM,CAAC;CACnC,CAAC;AAGF,MAAM,MAAM,UAAU,GAAG;IAEvB,UAAU,EAAE,MAAM,CAAC;IAEnB,IAAI,EAAE,MAAM,CAAC;IAEb,MAAM,EAAE,MAAM,CAAC;CAChB,CAAC;AAGF,MAAM,MAAM,WAAW,GAAG;IAExB,UAAU,EAAE,MAAM,CAAC;IAEnB,OAAO,EAAE,MAAM,CAAC;IAEhB,SAAS,EAAE,MAAM,CAAC;IAElB,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB,CAAC;AAGF,MAAM,MAAM,WAAW,GAAG;IAExB,UAAU,EAAE,MAAM,CAAC;IAEnB,OAAO,EAAE,MAAM,CAAC;IAEhB,SAAS,EAAE,MAAM,CAAC;IAElB,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB,CAAC;AAGF,MAAM,MAAM,QAAQ,GAAG;IAErB,OAAO,EAAE,MAAM,CAAC;IAEhB,QAAQ,EAAE,OAAO,CAAC;IAElB,UAAU,EAAE,MAAM,CAAC;CACpB,CAAC;AAGF,MAAM,MAAM,UAAU,GAAG;IAEvB,IAAI,EAAE,IAAI,CAAC;IAEX,OAAO,EAAE,MAAM,CAAC;IAEhB,gBAAgB,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;CACrC,CAAC;AAGF,MAAM,MAAM,cAAc,GAAG;IAE3B,KAAK,EAAE,MAAM,CAAC;IAEd,IAAI,EAAE,MAAM,CAAC;IAEb,SAAS,EAAE,MAAM,CAAC;CACnB,CAAC;AAGF,MAAM,MAAM,YAAY,GAAG;IAEzB,IAAI,EAAE,IAAI,CAAC;IAEX,KAAK,EAAE,MAAM,CAAC;IAEd,UAAU,EAAE,MAAM,CAAC;IAEnB,IAAI,EAAE,MAAM,CAAC;IAEb,UAAU,EAAE,MAAM,CAAC;IAEnB,SAAS,EAAE,MAAM,CAAC;CACnB,CAAC"}
@@ -1,2 +1,3 @@
1
1
  "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
2
3
  //# sourceMappingURL=types.js.map
@@ -0,0 +1,13 @@
1
+ export declare function timingSafeEqual(a: Uint8Array, b: Uint8Array): boolean;
2
+ export declare class SecureBuffer {
3
+ private buf;
4
+ private destroyed;
5
+ constructor(data: Uint8Array | string);
6
+ get length(): number;
7
+ get isDestroyed(): boolean;
8
+ expose(): Uint8Array;
9
+ toHex(): string;
10
+ destroy(): void;
11
+ }
12
+ export declare function wipeMemory(buffer: Uint8Array): void;
13
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AA+BA,wBAAgB,eAAe,CAAC,CAAC,EAAE,UAAU,EAAE,CAAC,EAAE,UAAU,GAAG,OAAO,CAOrE;AAqBD,qBAAa,YAAY;IACvB,OAAO,CAAC,GAAG,CAAa;IACxB,OAAO,CAAC,SAAS,CAAS;gBAYd,IAAI,EAAE,UAAU,GAAG,MAAM;IASrC,IAAI,MAAM,IAAI,MAAM,CAEnB;IAGD,IAAI,WAAW,IAAI,OAAO,CAEzB;IAcD,MAAM,IAAI,UAAU;IAsBpB,KAAK,IAAI,MAAM;IAuBf,OAAO,IAAI,IAAI;CAMhB;AAiBD,wBAAgB,UAAU,CAAC,MAAM,EAAE,UAAU,GAAG,IAAI,CAEnD"}
package/dist/utils.js ADDED
@@ -0,0 +1,56 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SecureBuffer = void 0;
4
+ exports.timingSafeEqual = timingSafeEqual;
5
+ exports.wipeMemory = wipeMemory;
6
+ const errors_1 = require("./errors");
7
+ function timingSafeEqual(a, b) {
8
+ if (a.length !== b.length)
9
+ return false;
10
+ let result = 0;
11
+ for (let i = 0; i < a.length; i++) {
12
+ result |= a[i] ^ b[i];
13
+ }
14
+ return result === 0;
15
+ }
16
+ class SecureBuffer {
17
+ buf;
18
+ destroyed = false;
19
+ constructor(data) {
20
+ if (typeof data === "string") {
21
+ this.buf = Buffer.from(data, "hex");
22
+ }
23
+ else {
24
+ this.buf = new Uint8Array(data);
25
+ }
26
+ }
27
+ get length() {
28
+ return this.buf.length;
29
+ }
30
+ get isDestroyed() {
31
+ return this.destroyed;
32
+ }
33
+ expose() {
34
+ if (this.destroyed) {
35
+ throw new errors_1.CryptoError("SecureBuffer has been destroyed", errors_1.CryptoErrorCode.BUFFER_DESTROYED);
36
+ }
37
+ return this.buf;
38
+ }
39
+ toHex() {
40
+ if (this.destroyed) {
41
+ throw new errors_1.CryptoError("SecureBuffer has been destroyed", errors_1.CryptoErrorCode.BUFFER_DESTROYED);
42
+ }
43
+ return Buffer.from(this.buf).toString("hex");
44
+ }
45
+ destroy() {
46
+ if (!this.destroyed) {
47
+ this.buf.fill(0);
48
+ this.destroyed = true;
49
+ }
50
+ }
51
+ }
52
+ exports.SecureBuffer = SecureBuffer;
53
+ function wipeMemory(buffer) {
54
+ buffer.fill(0);
55
+ }
56
+ //# sourceMappingURL=utils.js.map