From 4f84dd3248798778f17cbdab2d3d3d1990ea1bd7 Mon Sep 17 00:00:00 2001 From: HorizonCode Date: Tue, 18 Oct 2022 01:45:17 +0200 Subject: [PATCH] add encrypt test --- test/encrypt.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 test/encrypt.js diff --git a/test/encrypt.js b/test/encrypt.js new file mode 100644 index 0000000..ce9b690 --- /dev/null +++ b/test/encrypt.js @@ -0,0 +1,26 @@ +const dpapi = require('wincrypt'); +const entropyBuffer = Buffer.from('cu24180ncjeiu0ci1nwui', 'utf-8'); + +const run = async () => { + const stringToEncrypt = "Test123456!"; + + + const encrypted = await encryptString(stringToEncrypt) + console.log(encrypted); + const decrypted = await decryptString(encrypted); + console.log(decrypted); + +} + +async function encryptString(value) { + const encryptedString = await dpapi.protect(Buffer.from(value, 'utf-8'), entropyBuffer, 'CurrentUser'); + const encodedBase64 = Buffer.from(encryptedString, 'utf-8').toString('base64'); + return encodedBase64; +} + +async function decryptString(value) { + const decrypted = await dpapi.unprotect(Buffer.from(value, 'base64'), entropyBuffer, 'CurrentUser'); + return decrypted.toString(); +} + +run(); \ No newline at end of file