@open-norantec/utilities 1.0.1-alpha.13 → 1.0.1-alpha.15
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.
- package/dist/ecdh-util.class.d.ts +9 -0
- package/dist/ecdh-util.class.js +31 -0
- package/package.json +1 -1
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
/// <reference types="node" />
|
|
3
|
+
import * as crypto from 'node:crypto';
|
|
4
|
+
export declare class ECDHUtil {
|
|
5
|
+
readonly ecdh: crypto.ECDH;
|
|
6
|
+
protected readonly ALGORITHM: crypto.CipherGCMTypes;
|
|
7
|
+
encrypt(data: string, otherPublicKey: Buffer): string;
|
|
8
|
+
decrypt(encryptedData: string, otherPublicKey: Buffer): string;
|
|
9
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ECDHUtil = void 0;
|
|
4
|
+
const crypto = require("node:crypto");
|
|
5
|
+
class ECDHUtil {
|
|
6
|
+
constructor() {
|
|
7
|
+
this.ecdh = crypto.createECDH('secp256k1');
|
|
8
|
+
this.ALGORITHM = 'aes-256-gcm';
|
|
9
|
+
}
|
|
10
|
+
encrypt(data, otherPublicKey) {
|
|
11
|
+
const sharedKey = this.ecdh.computeSecret(new Uint8Array(otherPublicKey));
|
|
12
|
+
const iv = crypto.randomBytes(16);
|
|
13
|
+
const cipher = crypto.createCipheriv(this.ALGORITHM, new Uint8Array(sharedKey), new Uint8Array(iv));
|
|
14
|
+
const encrypted = Buffer.concat([new Uint8Array(cipher.update(data, 'utf-8')), new Uint8Array(cipher.final())]);
|
|
15
|
+
return Buffer.concat([new Uint8Array(iv), new Uint8Array(encrypted), new Uint8Array(cipher.getAuthTag())]).toString('base64');
|
|
16
|
+
}
|
|
17
|
+
decrypt(encryptedData, otherPublicKey) {
|
|
18
|
+
const sharedKey = this.ecdh.computeSecret(new Uint8Array(otherPublicKey));
|
|
19
|
+
const data = Buffer.from(encryptedData, 'base64');
|
|
20
|
+
const iv = data.subarray(0, 16);
|
|
21
|
+
const authTag = data.subarray(data.length - 16);
|
|
22
|
+
const encrypted = data.subarray(16, data.length - 16);
|
|
23
|
+
const decipher = crypto.createDecipheriv(this.ALGORITHM, new Uint8Array(sharedKey), new Uint8Array(iv));
|
|
24
|
+
decipher.setAuthTag(new Uint8Array(authTag));
|
|
25
|
+
return Buffer.concat([
|
|
26
|
+
new Uint8Array(decipher.update(new Uint8Array(encrypted))),
|
|
27
|
+
new Uint8Array(decipher.final()),
|
|
28
|
+
]).toString('utf-8');
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
exports.ECDHUtil = ECDHUtil;
|