-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd.ts
More file actions
29 lines (23 loc) · 979 Bytes
/
add.ts
File metadata and controls
29 lines (23 loc) · 979 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
29
import { Request, Response } from 'express';
import { promises as fsPromises } from 'fs';
import path from 'path';
import * as paillierBigint from 'paillier-bigint';
export const add = async (req: Request, res: Response) => {
try {
const { encNum1, encNum2, name } = req.body;
const c1 = BigInt(encNum1);
const c2 = BigInt(encNum2);
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 encryptedSum = publicKey.addition(c1, c2);
res.status(200).json({ encryptedSum: encryptedSum.toString() });
} catch (error) {
console.error(error);
res.status(500).json({ error: 'Internal Server Error' });
}
};