@chatereum/react-e2ee
Version:
A End-to-end encryption library for React and browser based JavaScript frameworks
100 lines (99 loc) • 4.92 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());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.base64ToUint8 = exports.uIntToBase64 = exports.getAESCryptoKey = exports.getPrivateCryptoKey = exports.getPublicCryptoKey = exports.toPublicPem = exports.toPrivatePem = exports.encodeMessage = exports.base64ToArrayBuffer = exports.arrayBufferToBase64 = void 0;
const config_json_1 = __importDefault(require("./config/config.json"));
let crypto = window.crypto.subtle;
const arrayBufferToBase64 = (arrayBuffer) => {
const byteArray = new Uint8Array(arrayBuffer);
const byteString = byteArray.reduce((prev, curr) => prev + String.fromCharCode(curr), '');
const b64 = window.btoa(byteString);
return b64;
};
exports.arrayBufferToBase64 = arrayBufferToBase64;
const base64ToArrayBuffer = (str) => {
const buf = new ArrayBuffer(str.length);
const bufView = new Uint8Array(buf);
for (let i = 0, strLen = str.length; i < strLen; i++) {
bufView[i] = str.charCodeAt(i);
}
return buf;
};
exports.base64ToArrayBuffer = base64ToArrayBuffer;
const addNewLines = (str) => {
var finalString = '';
while (str.length > 0) {
finalString += str.substring(0, 64) + '\n';
str = str.substring(64);
}
return finalString;
};
const toPrivatePem = (privateKey) => {
const b64 = addNewLines(arrayBufferToBase64(privateKey));
const pem = "-----BEGIN PRIVATE KEY-----\n" + b64 + "-----END PRIVATE KEY-----";
return pem;
};
exports.toPrivatePem = toPrivatePem;
const toPublicPem = (publicKey) => {
const b64 = addNewLines(arrayBufferToBase64(publicKey));
const pem = "-----BEGIN PUBLIC KEY-----\n" + b64 + "-----END PUBLIC KEY-----";
return pem;
};
exports.toPublicPem = toPublicPem;
const encodeMessage = (plainText) => {
let enc = new TextEncoder();
return enc.encode(plainText);
};
exports.encodeMessage = encodeMessage;
const getPublicCryptoKey = (public_key) => __awaiter(void 0, void 0, void 0, function* () {
const pemHeader = "-----BEGIN PUBLIC KEY-----";
const pemFooter = "-----END PUBLIC KEY-----";
const pemContents = public_key.substring(pemHeader.length, public_key.length - pemFooter.length);
const binaryDerString = window.atob(pemContents);
const spki = base64ToArrayBuffer(binaryDerString);
const public_key_format = config_json_1.default.main.exports.public;
const crypto_key = yield crypto.importKey(public_key_format, spki, {
name: config_json_1.default.main.name,
hash: config_json_1.default.main.hash
}, true, ["encrypt"]);
return crypto_key;
});
exports.getPublicCryptoKey = getPublicCryptoKey;
const getPrivateCryptoKey = (private_key) => __awaiter(void 0, void 0, void 0, function* () {
const pemHeader = "-----BEGIN PRIVATE KEY-----";
const pemFooter = "-----END PRIVATE KEY-----";
const pemContents = private_key.substring(pemHeader.length, private_key.length - pemFooter.length);
const binaryDerString = window.atob(pemContents);
const pkcs8 = base64ToArrayBuffer(binaryDerString);
const private_key_format = config_json_1.default.main.exports.private;
const crypto_key = yield crypto.importKey(private_key_format, pkcs8, {
name: config_json_1.default.main.name,
hash: config_json_1.default.main.hash
}, true, ["decrypt"]);
return crypto_key;
});
exports.getPrivateCryptoKey = getPrivateCryptoKey;
const getAESCryptoKey = (aes_key) => __awaiter(void 0, void 0, void 0, function* () {
const raw = base64ToArrayBuffer(window.atob(aes_key));
const pre_key_format = config_json_1.default.pre.exports;
let aes_crypto_key = yield crypto.importKey(pre_key_format, raw, {
name: config_json_1.default.pre.name
}, true, ["encrypt", "decrypt"]);
return aes_crypto_key;
});
exports.getAESCryptoKey = getAESCryptoKey;
const uIntToBase64 = (u8) => window.btoa(String.fromCharCode.apply(null, u8));
exports.uIntToBase64 = uIntToBase64;
const base64ToUint8 = (str) => new Uint8Array(window.atob(str).split('').map(function (c) { return c.charCodeAt(0); }));
exports.base64ToUint8 = base64ToUint8;