Skip to content

Commit 20156fa

Browse files
fix: use bytesToPrivateKey
1 parent 7220425 commit 20156fa

File tree

1 file changed

+9
-16
lines changed

1 file changed

+9
-16
lines changed

src/utils/encryption.ts

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,19 @@ export type EciesEncryptionInput = {
2525
* Utility class for performing common, non-DWN specific encryption operations.
2626
*/
2727
export class Encryption {
28-
2928
/**
3029
* Encrypts the given plaintext stream using AES-256-CTR algorithm.
3130
*/
3231

3332
public static isEphemeralKeyCompressed: boolean = true; // Set default value
3433

35-
private static toBase64Url(buffer: Buffer): string {
36-
return buffer.toString('base64') // Convert to base64
37-
.replace(/\+/g, '-') // Replace + with -
38-
.replace(/\//g, '_') // Replace / with _
39-
.replace(/=+$/, ''); // Remove any trailing '='
40-
}
41-
4234
private static async convertToJwk(key: Uint8Array): Promise<Jwk> {
35+
// Construct the private key in JWK format using bytesToPrivateKey method.
36+
const privateKey = await AesCtr.bytesToPrivateKey({ privateKeyBytes: key });
37+
38+
// Assign the algorithm and extractability flag.
4339
return {
44-
kty : 'oct',
45-
k : this.toBase64Url(Buffer.from(key)), // Use the new base64url method
40+
...privateKey,
4641
alg : 'A256CTR',
4742
ext : 'true',
4843
};
@@ -90,7 +85,6 @@ export class Encryption {
9085
return cipherStream; // Return the cipher stream
9186
}
9287

93-
9488
/**
9589
* Decrypts the given cipher stream using AES-256-CTR algorithm.
9690
*/
@@ -120,7 +114,7 @@ export class Encryption {
120114
data : buffer,
121115
key : jwkKey,
122116
counter : initializationVector,
123-
length : 128, // FIX: Counter length must be between 1 and 128
117+
length : 128,
124118
});
125119

126120
plaintextStream.push(decryptedData);
@@ -187,14 +181,13 @@ export class Encryption {
187181
]);
188182

189183
/**
190-
* Expose eciesjs library configuration
191-
*/
184+
* Expose eciesjs library configuration
185+
*/
192186
return eciesjs.decrypt(privateKeyBuffer, eciesEncryptionOutput);
193187
}
194188
}
195189

196-
197190
export enum EncryptionAlgorithm {
198191
Aes256Ctr = 'A256CTR',
199192
EciesSecp256k1 = 'ECIES-ES256K',
200-
}
193+
}

0 commit comments

Comments
 (0)