nyt-util
Version:
Neyaatek Utilies
29 lines (28 loc) • 1.55 kB
JavaScript
;
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.Decrypt = void 0;
const hexStringToArrayBuffer = (hex) => {
const hexMatches = hex.match(/.{1,2}/g) || [];
const typedArray = new Uint8Array(hexMatches.map((byte) => parseInt(byte, 16)));
return typedArray.buffer;
};
const Decrypt = (text, key) => __awaiter(void 0, void 0, void 0, function* () {
const [ivHex, dataHex] = text.split(':');
const iv = hexStringToArrayBuffer(ivHex);
const data = hexStringToArrayBuffer(dataHex);
const alg = { name: 'AES-CBC', iv: iv };
const cryptoKey = yield crypto.subtle.importKey('raw', hexStringToArrayBuffer(key), alg, false, ['decrypt']);
const decrypted = yield crypto.subtle.decrypt(alg, cryptoKey, data);
const dec = new TextDecoder();
return dec.decode(decrypted);
});
exports.Decrypt = Decrypt;