UNPKG

@safient/core

Version:

JavaScript SDK to manage safes and interact with Safient protocol.

48 lines (47 loc) 2.26 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports._aesDecryption = exports._aesEncryption = exports._generateCipherKey = void 0; const crypto = require('crypto'); const _generateCipherKey = () => { try { const seed = crypto.randomBytes(32).toString(); return new Promise((resolve) => { const cipherKey = crypto.createHash('sha256').update(seed).digest(); resolve(cipherKey); }); } catch (err) { console.error('Error while generating symmetric key:', err); return null; } }; exports._generateCipherKey = _generateCipherKey; const _aesEncryption = (data, cipherKey) => __awaiter(void 0, void 0, void 0, function* () { return new Promise((resolve) => { let iv = crypto.randomBytes(16); const cipher = crypto.createCipheriv('aes256', cipherKey, iv); const encryptedData = Buffer.concat([iv, cipher.update(data), cipher.final()]); resolve(encryptedData); }); }); exports._aesEncryption = _aesEncryption; const _aesDecryption = (data, cipherKey) => __awaiter(void 0, void 0, void 0, function* () { let encryptedData = Buffer.from(data, 'hex'); const iv = encryptedData.slice(0, 16); encryptedData = encryptedData.slice(16); return new Promise((resolve) => { const decipher = crypto.createDecipheriv('aes256', cipherKey, iv); const decryptedData = Buffer.concat([decipher.update(encryptedData), decipher.final()]); resolve(decryptedData); }); }); exports._aesDecryption = _aesDecryption;