Skip to content

Commit a32d6d9

Browse files
committed
fix: add wrapped func
1 parent adcfe4a commit a32d6d9

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

src/nobleEncryption.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@ export const NobleEciesToEcies = (nobleEcies: NobleEcies): Ecies => {
3333
};
3434
};
3535

36+
export const EciesToNobleEcies = (ecies: Ecies): NobleEcies => {
37+
return {
38+
iv: new Uint8Array(ecies.iv),
39+
ephemPublicKey: new Uint8Array(ecies.ephemPublicKey),
40+
ciphertext: new Uint8Array(ecies.ciphertext),
41+
mac: new Uint8Array(ecies.mac),
42+
};
43+
};
44+
3645
export const hmacSha256Sign = (key: Uint8Array, msg: Uint8Array) => {
3746
const mac = hmac(sha256, key, msg);
3847
return mac;
@@ -46,7 +55,7 @@ export function hmacSha256Verify(key: Uint8Array, msg: Uint8Array, sig: Uint8Arr
4655
export const nobleEncrypt = async function (
4756
publicKeyTo: Uint8Array,
4857
msg: Uint8Array,
49-
opts?: { iv?: Uint8Array; ephemPrivateKey?: Uint8Array; padding?: boolean }
58+
opts?: { iv?: Uint8Array; ephemPrivateKey?: Uint8Array }
5059
): Promise<NobleEcies> {
5160
const ephemPrivateKey = opts?.ephemPrivateKey || utils.randomPrivateKey();
5261
const ephemPublicKey = getPublicKey(ephemPrivateKey, false);
@@ -106,3 +115,19 @@ export const nobleDecrypt = async function (privateKey: Uint8Array, opts: NobleE
106115

107116
return decrypted;
108117
};
118+
119+
export const encrypt = async function (
120+
publicKeyTo: Buffer,
121+
msg: Buffer,
122+
opts?: { iv?: Buffer; ephemPrivateKey?: Buffer; padding?: boolean }
123+
): Promise<Ecies> {
124+
if (opts?.padding !== undefined) throw new Error("padding opts is not supported");
125+
const nobleEcies = await nobleEncrypt(publicKeyTo, msg, opts);
126+
return NobleEciesToEcies(nobleEcies);
127+
};
128+
129+
export const decrypt = async function (privateKey: Buffer, opts: Ecies, padding?: boolean): Promise<Buffer> {
130+
const nobleEcies = EciesToNobleEcies(opts);
131+
const decrypted = await nobleDecrypt(privateKey, nobleEcies, padding);
132+
return Buffer.from(decrypted);
133+
};

test/encrypt&decrypt.spec.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
/* eslint-disable import/no-extraneous-dependencies */
22
import { bytesToUtf8 } from "@noble/ciphers/utils";
3-
import { sha512 as nobleSha512 } from "@noble/hashes/sha2";
4-
import { getPublicKey, getSharedSecret } from "@noble/secp256k1";
3+
import { getPublicKey } from "@noble/secp256k1";
54
import { beforeEach, describe, expect, it } from "vitest";
65

76
import * as eccrypto from "../src/index";
8-
import { nobleDecrypt, NobleEciesToEcies, nobleEncrypt } from "../src/nobleEncryption";
7+
import { decrypt, encrypt, nobleDecrypt, NobleEciesToEcies, nobleEncrypt } from "../src/nobleEncryption";
98

109
describe("Functions: encrypt & decrypt", () => {
1110
let ephemPublicKey: Buffer;
@@ -202,13 +201,23 @@ describe("Functions: encrypt & decrypt", () => {
202201
ephemPrivateKey: ephemPrivateKey,
203202
});
204203

204+
const wrappedEncrypted = await encrypt(publicKey, Buffer.from(message), {
205+
iv: iv,
206+
ephemPrivateKey: ephemPrivateKey,
207+
});
208+
205209
expect(convertedEcies).toEqual(encrypted);
206210

211+
expect(wrappedEncrypted).toEqual(encrypted);
212+
207213
const decrypted = await nobleDecrypt(privateKey, nobleEcies);
208214
expect(bytesToUtf8(decrypted)).toBe(message);
209215

210216
const decrypted1 = await eccrypto.decrypt(privateKey, convertedEcies);
211217
expect(bytesToUtf8(decrypted1)).toBe(message);
218+
219+
const wrappedDecrypted = await decrypt(privateKey, encrypted);
220+
expect(bytesToUtf8(wrappedDecrypted)).toBe(message);
212221
});
213222
});
214223
});

0 commit comments

Comments
 (0)