@git-stunts/git-cas 1.6.2 → 3.0.0

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.
@@ -1,4 +1,5 @@
1
- import { createHash, createCipheriv, createDecipheriv, randomBytes } from 'node:crypto';
1
+ import { createHash, createCipheriv, createDecipheriv, randomBytes, pbkdf2, scrypt } from 'node:crypto';
2
+ import { promisify } from 'node:util';
2
3
  import CryptoPort from '../../ports/CryptoPort.js';
3
4
  import CasError from '../../domain/errors/CasError.js';
4
5
 
@@ -65,6 +66,44 @@ export default class NodeCryptoAdapter extends CryptoPort {
65
66
  return { encrypt, finalize };
66
67
  }
67
68
 
69
+ /** @override */
70
+ async deriveKey({
71
+ passphrase,
72
+ salt,
73
+ algorithm = 'pbkdf2',
74
+ iterations = 100_000,
75
+ cost = 16384,
76
+ blockSize = 8,
77
+ parallelization = 1,
78
+ keyLength = 32,
79
+ }) {
80
+ const saltBuf = salt || randomBytes(32);
81
+ let key;
82
+ const params = {
83
+ algorithm,
84
+ salt: Buffer.from(saltBuf).toString('base64'),
85
+ keyLength,
86
+ };
87
+
88
+ if (algorithm === 'pbkdf2') {
89
+ key = await promisify(pbkdf2)(passphrase, saltBuf, iterations, keyLength, 'sha512');
90
+ params.iterations = iterations;
91
+ } else if (algorithm === 'scrypt') {
92
+ key = await promisify(scrypt)(passphrase, saltBuf, keyLength, {
93
+ N: cost,
94
+ r: blockSize,
95
+ p: parallelization,
96
+ });
97
+ params.cost = cost;
98
+ params.blockSize = blockSize;
99
+ params.parallelization = parallelization;
100
+ } else {
101
+ throw new Error(`Unsupported KDF algorithm: ${algorithm}`);
102
+ }
103
+
104
+ return { key, salt: Buffer.from(saltBuf), params };
105
+ }
106
+
68
107
  /**
69
108
  * Validates that a key is a 32-byte Buffer.
70
109
  * @param {Buffer} key
@@ -117,6 +117,64 @@ export default class WebCryptoAdapter extends CryptoPort {
117
117
  return { encrypt, finalize };
118
118
  }
119
119
 
120
+ /** @override */
121
+ async deriveKey({
122
+ passphrase,
123
+ salt,
124
+ algorithm = 'pbkdf2',
125
+ iterations = 100_000,
126
+ cost = 16384,
127
+ blockSize = 8,
128
+ parallelization = 1,
129
+ keyLength = 32,
130
+ }) {
131
+ const saltBuf = salt || this.randomBytes(32);
132
+ const params = { algorithm, salt: this.#toBase64(saltBuf), keyLength };
133
+
134
+ const opts = { passphrase, saltBuf, iterations, cost, blockSize, parallelization, keyLength, params };
135
+ let key;
136
+ if (algorithm === 'pbkdf2') {
137
+ key = await this.#derivePbkdf2(opts);
138
+ } else if (algorithm === 'scrypt') {
139
+ key = await this.#deriveScrypt(opts);
140
+ } else {
141
+ throw new Error(`Unsupported KDF algorithm: ${algorithm}`);
142
+ }
143
+
144
+ return { key: Buffer.from(key), salt: Buffer.from(saltBuf), params };
145
+ }
146
+
147
+ async #derivePbkdf2({ passphrase, saltBuf, iterations, keyLength, params }) {
148
+ const enc = new globalThis.TextEncoder();
149
+ const baseKey = await globalThis.crypto.subtle.importKey(
150
+ 'raw', enc.encode(passphrase), 'PBKDF2', false, ['deriveBits'],
151
+ );
152
+ const bits = await globalThis.crypto.subtle.deriveBits(
153
+ { name: 'PBKDF2', salt: saltBuf, iterations, hash: 'SHA-512' },
154
+ baseKey, keyLength * 8,
155
+ );
156
+ params.iterations = iterations;
157
+ return Buffer.from(bits);
158
+ }
159
+
160
+ async #deriveScrypt({ passphrase, saltBuf, cost, blockSize, parallelization, keyLength, params }) {
161
+ let scryptCb;
162
+ let promisifyFn;
163
+ try {
164
+ ({ scrypt: scryptCb } = await import('node:crypto'));
165
+ ({ promisify: promisifyFn } = await import('node:util'));
166
+ } catch {
167
+ throw new Error('scrypt KDF requires a Node.js-compatible runtime (node:crypto unavailable)');
168
+ }
169
+ const key = await promisifyFn(scryptCb)(passphrase, saltBuf, keyLength, {
170
+ N: cost, r: blockSize, p: parallelization,
171
+ });
172
+ params.cost = cost;
173
+ params.blockSize = blockSize;
174
+ params.parallelization = parallelization;
175
+ return key;
176
+ }
177
+
120
178
  /**
121
179
  * Imports a raw key for use with Web Crypto AES-GCM operations.
122
180
  * @param {Buffer|Uint8Array} rawKey - 32-byte raw key material.
@@ -51,4 +51,21 @@ export default class CryptoPort {
51
51
  createEncryptionStream(_key) {
52
52
  throw new Error('Not implemented');
53
53
  }
54
+
55
+ /**
56
+ * Derives an encryption key from a passphrase using a KDF.
57
+ * @param {Object} options
58
+ * @param {string} options.passphrase - The passphrase to derive a key from.
59
+ * @param {Buffer} [options.salt] - Salt for the KDF (random if omitted).
60
+ * @param {'pbkdf2'|'scrypt'} [options.algorithm='pbkdf2'] - KDF algorithm.
61
+ * @param {number} [options.iterations=100000] - PBKDF2 iteration count.
62
+ * @param {number} [options.cost=16384] - scrypt cost parameter (N).
63
+ * @param {number} [options.blockSize=8] - scrypt block size (r).
64
+ * @param {number} [options.parallelization=1] - scrypt parallelization (p).
65
+ * @param {number} [options.keyLength=32] - Derived key length in bytes.
66
+ * @returns {Promise<{ key: Buffer, salt: Buffer, params: { algorithm: string, salt: string, iterations?: number, cost?: number, blockSize?: number, parallelization?: number, keyLength: number } }>}
67
+ */
68
+ deriveKey(_options) {
69
+ throw new Error('Not implemented');
70
+ }
54
71
  }
@@ -0,0 +1,48 @@
1
+ /**
2
+ * Abstract port for Git ref and commit operations.
3
+ * @abstract
4
+ */
5
+ export default class GitRefPort {
6
+ /**
7
+ * Resolves a Git ref to its commit OID.
8
+ * @param {string} ref - Git ref (e.g. 'refs/cas/vault').
9
+ * @returns {Promise<string>} The commit OID.
10
+ * @throws If the ref does not exist.
11
+ */
12
+ async resolveRef(_ref) {
13
+ throw new Error('Not implemented');
14
+ }
15
+
16
+ /**
17
+ * Resolves the tree OID from a commit OID.
18
+ * @param {string} commitOid - Git commit OID.
19
+ * @returns {Promise<string>} The tree OID.
20
+ */
21
+ async resolveTree(_commitOid) {
22
+ throw new Error('Not implemented');
23
+ }
24
+
25
+ /**
26
+ * Creates a Git commit object.
27
+ * @param {Object} options
28
+ * @param {string} options.treeOid - Tree OID for the commit.
29
+ * @param {string|null} [options.parentOid] - Parent commit OID (null for root commit).
30
+ * @param {string} options.message - Commit message.
31
+ * @returns {Promise<string>} The new commit OID.
32
+ */
33
+ async createCommit(_options) {
34
+ throw new Error('Not implemented');
35
+ }
36
+
37
+ /**
38
+ * Atomically updates a Git ref with optional CAS (compare-and-swap) semantics.
39
+ * @param {Object} options
40
+ * @param {string} options.ref - Git ref to update.
41
+ * @param {string} options.newOid - New OID to set.
42
+ * @param {string|null} [options.expectedOldOid] - Expected current OID for CAS. If provided and mismatched, throws.
43
+ * @returns {Promise<void>}
44
+ */
45
+ async updateRef(_options) {
46
+ throw new Error('Not implemented');
47
+ }
48
+ }