-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencrypt.ts
More file actions
28 lines (23 loc) · 958 Bytes
/
encrypt.ts
File metadata and controls
28 lines (23 loc) · 958 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import { Request, Response } from 'express';
import { promises as fsPromises } from 'fs';
import path from 'path';
import * as paillierBigint from 'paillier-bigint';
export const encrypt = async (req: Request, res: Response) => {
try {
const { num, name } = req.body;
console.log("encrypt body", req.body);
const m = BigInt(num);
const pubKeyPath = path.join(__dirname, 'data', `${name}-publicKey.json`);
const publicKeyJson = await fsPromises.readFile(pubKeyPath, 'utf8');
const publicKeyObj = JSON.parse(publicKeyJson);
const publicKey = new paillierBigint.PublicKey(
BigInt(publicKeyObj.n),
BigInt(publicKeyObj.g)
);
const encrypted = publicKey.encrypt(m);
res.status(200).json({ encrypted: encrypted.toString() });
} catch (error) {
console.error(error);
res.status(500).json({ error: 'Internal Server Error' });
}
};