This repository has been archived by the owner on Jul 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add used nonce to data submission request
- Loading branch information
1 parent
7d01f33
commit 936fb10
Showing
3 changed files
with
15 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,18 @@ | ||
import * as crypto from 'crypto'; | ||
|
||
export function encryptData(keyOfHealthDepartment: string, data): { dataToTransport: string; keyToTransport: string } { | ||
const publicKey = crypto.createPublicKey(keyOfHealthDepartment); | ||
const iv = crypto.randomBytes(16); | ||
export function encryptData( | ||
keyOfHealthDepartment: string, | ||
data, | ||
): { dataToTransport: string; keyToTransport: string; nonce: string } { | ||
const nonce = crypto.randomBytes(16); | ||
const key = crypto.randomBytes(32); | ||
const cipher = crypto.createCipheriv('aes-256', key, iv); | ||
const encryptedData = Buffer.concat([cipher.update(JSON.stringify(data), 'utf8'), cipher.final()]); | ||
const encryptedKey = crypto.publicEncrypt( | ||
{ key: publicKey, padding: crypto.constants.RSA_PKCS1_OAEP_PADDING, oaepHash: 'sha3' }, | ||
key, | ||
); | ||
const cipher = crypto.createCipheriv('AES-256-CBC', key, nonce); | ||
const dataString = JSON.stringify(data); | ||
const encryptedData = Buffer.concat([cipher.update(dataString, 'utf8'), cipher.final()]); | ||
const encryptedKey = crypto.publicEncrypt({ key: keyOfHealthDepartment }, key); | ||
return { | ||
dataToTransport: encryptedData.toString('base64'), | ||
keyToTransport: encryptedKey.toString('base64'), | ||
nonce: nonce.toString('base64'), | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters