Skip to content
Snippets Groups Projects
CryptoServiceWeb.js 3.5 KiB
Newer Older
  • Learn to ignore specific revisions
  • Igor Markin's avatar
    Igor Markin committed
    "use strict";
    
    Igor Markin's avatar
    Igor Markin committed
    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());
        });
    };
    
    Igor Markin's avatar
    Igor Markin committed
    Object.defineProperty(exports, "__esModule", { value: true });
    
    exports.verifyRSASignature = void 0;
    const common_1 = require("../../utils/common");
    
    Igor Markin's avatar
    Igor Markin committed
    function exportKey(key) {
        return __awaiter(this, void 0, void 0, function* () {
            return crypto.subtle.exportKey("raw", key);
        });
    }
    
    const convertPemToBinary = (pem) => {
        const lines = pem.split("\n");
        let encoded = "";
        for (let i = 0; i < lines.length; i++) {
            if (lines[i].trim().length > 0 &&
                lines[i].indexOf("-BEGIN PRIVATE KEY-") < 0 &&
                lines[i].indexOf("-BEGIN PUBLIC KEY-") < 0 &&
                lines[i].indexOf("-END PRIVATE KEY-") < 0 &&
                lines[i].indexOf("-END PUBLIC KEY-") < 0) {
                encoded += lines[i].trim();
            }
        }
        return common_1.base64ToArrayBuffer(encoded);
    };
    exports.verifyRSASignature = (publicKeyPEM, data, signature) => __awaiter(void 0, void 0, void 0, function* () {
        const publicKey = yield crypto.subtle.importKey("spki", convertPemToBinary(publicKeyPEM), {
            name: "RSASSA-PKCS1-v1_5",
            hash: "SHA-256",
        }, true, ["verify"]);
        return yield crypto.subtle.verify({
            name: "RSASSA-PKCS1-v1_5",
            hash: "SHA-256",
        }, publicKey, signature, data);
    });
    
    Igor Markin's avatar
    Igor Markin committed
    const encryptAESGCM = (data) => __awaiter(void 0, void 0, void 0, function* () {
        const key = yield crypto.subtle.generateKey({
            name: "AES-GCM",
            length: 256,
        }, true, ["encrypt", "decrypt"]);
        const encoded = new TextEncoder().encode(data);
        const iv = crypto.getRandomValues(new Buffer(12));
        const encrypted = yield crypto.subtle.encrypt({ name: "AES-GCM", iv: iv }, key, encoded);
        return { data: encrypted, key: yield exportKey(key), iv };
    });
    const decryptAESGCM = (data, key, iv) => __awaiter(void 0, void 0, void 0, function* () {
        const importedKey = yield crypto.subtle.importKey("raw", key, {
            name: "AES-GCM",
            length: 256,
        }, true, ["encrypt", "decrypt"]);
        const decrypted = yield crypto.subtle.decrypt({ name: "AES-GCM", iv: iv }, importedKey, data);
        return new TextDecoder().decode(decrypted);
    });
    
    Igor Markin's avatar
    Igor Markin committed
    const SHA256 = (string) => __awaiter(void 0, void 0, void 0, function* () {
        const encoder = new TextEncoder();
        return yield crypto.subtle.digest("SHA-256", encoder.encode(string));
    });
    
    Igor Markin's avatar
    Igor Markin committed
    const implementation = {
    
    Igor Markin's avatar
    Igor Markin committed
        encryptAESGCM,
        decryptAESGCM,
    
        verifyRSASignature: exports.verifyRSASignature,
    
    Igor Markin's avatar
    Igor Markin committed
        generateRSAKeys() {
    
    Igor Markin's avatar
    Igor Markin committed
            throw new Error("Surprise. Not implemented yet :)");
    
    Igor Markin's avatar
    Igor Markin committed
        },
        encryptRSA(publicKeyPEM, data) {
    
    Igor Markin's avatar
    Igor Markin committed
            throw new Error("Surprise. Not implemented yet :)");
    
    Igor Markin's avatar
    Igor Markin committed
        },
        decryptRSA(privateKeyPEM, data) {
    
    Igor Markin's avatar
    Igor Markin committed
            throw new Error("Surprise. Not implemented yet :)");
    
    Igor Markin's avatar
    Igor Markin committed
        },
    
    Igor Markin's avatar
    Igor Markin committed
        signRSA(privateKeyPEM, data) {
            throw new Error("Surprise. Not implemented yet :)");
        },
        SHA256,
    
    Igor Markin's avatar
    Igor Markin committed
    };
    exports.default = implementation;