js-databox
Version:
databox & metabox
59 lines (58 loc) • 1.99 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.EncryptApi = void 0;
const RSAEncrypt_1 = require("./RSAEncrypt");
const AESEncrypt_1 = require("./AESEncrypt");
class Encrypt {
async aesKeyGen() {
let aesKey = "";
const array = new BigUint64Array(16);
window.crypto.getRandomValues(array);
array.forEach(v => {
aesKey += v.toString(16);
});
while (aesKey.length < 256)
aesKey += "0";
return aesKey;
}
async genRSAKey() {
try {
const keyPair = await RSAEncrypt_1.RSAEncryptApi.generateKey();
const pemPrivateKey = await RSAEncrypt_1.RSAEncryptApi.exportCryptoKey(keyPair.privateKey);
const pemPublicKey = await RSAEncrypt_1.RSAEncryptApi.publicExportCryptoKey(keyPair.publicKey);
return {
privateKey: pemPrivateKey,
publicKey: pemPublicKey
};
}
catch (e) {
throw e;
}
}
async encryptPrivateKey(privateKey, passwordHash, Iv) {
try {
const u8 = await AESEncrypt_1.AESEncryptApi.AESEncData(privateKey, passwordHash, Iv);
return String.fromCharCode.apply(null,
//@ts-ignore
new Uint8Array(u8));
}
catch (e) {
throw e;
}
}
async decryptPrivateKey(prePrivateKey, passwordHash, Iv) {
try {
const buf = new ArrayBuffer(prePrivateKey.length);
const bufView = new Uint8Array(buf);
for (let i = 0, strLen = prePrivateKey.length; i < strLen; i++) {
bufView[i] = prePrivateKey.charCodeAt(i);
}
const plainText = AESEncrypt_1.AESEncryptApi.AESDecData(bufView.buffer, passwordHash, Iv);
return new TextDecoder().decode(plainText);
}
catch (e) {
throw e;
}
}
}
exports.EncryptApi = new Encrypt();