SPB Git forge

spb/admin-ka

Public
41commits 1branches 0releases
172.9 MBsize
maindefault branch
20 days agolast push
JavaScript 65.5% Python 17.8% CSS 13% HTML 3.7%
3.0 KB · 91 lines javascript
Raw Blame History
1import { AESECBCipher, AESEAXCipher } from "./aes.js";2import { DESCBCCipher, DESECBCipher } from "./des.js";3import { RSACipher } from "./rsa.js";4import { DHCipher } from "./dh.js";5import { MD5 } from "./md5.js";67// A single interface for the cryptographic algorithms not supported by SubtleCrypto.8// Both synchronous and asynchronous implmentations are allowed.9class LegacyCrypto {10    constructor() {11        this._algorithms = {12            "AES-ECB": AESECBCipher,13            "AES-EAX": AESEAXCipher,14            "DES-ECB": DESECBCipher,15            "DES-CBC": DESCBCCipher,16            "RSA-PKCS1-v1_5": RSACipher,17            "DH": DHCipher,18            "MD5": MD5,19        };20    }2122    encrypt(algorithm, key, data) {23        if (key.algorithm.name !== algorithm.name) {24            throw new Error("algorithm does not match");25        }26        if (typeof key.encrypt !== "function") {27            throw new Error("key does not support encryption");28        }29        return key.encrypt(algorithm, data);30    }3132    decrypt(algorithm, key, data) {33        if (key.algorithm.name !== algorithm.name) {34            throw new Error("algorithm does not match");35        }36        if (typeof key.decrypt !== "function") {37            throw new Error("key does not support encryption");38        }39        return key.decrypt(algorithm, data);40    }4142    importKey(format, keyData, algorithm, extractable, keyUsages) {43        if (format !== "raw") {44            throw new Error("key format is not supported");45        }46        const alg = this._algorithms[algorithm.name];47        if (typeof alg === "undefined" || typeof alg.importKey !== "function") {48            throw new Error("algorithm is not supported");49        }50        return alg.importKey(keyData, algorithm, extractable, keyUsages);51    }5253    generateKey(algorithm, extractable, keyUsages) {54        const alg = this._algorithms[algorithm.name];55        if (typeof alg === "undefined" || typeof alg.generateKey !== "function") {56            throw new Error("algorithm is not supported");57        }58        return alg.generateKey(algorithm, extractable, keyUsages);59    }6061    exportKey(format, key) {62        if (format !== "raw") {63            throw new Error("key format is not supported");64        }65        if (typeof key.exportKey !== "function") {66            throw new Error("key does not support exportKey");67        }68        return key.exportKey();69    }7071    digest(algorithm, data) {72        const alg = this._algorithms[algorithm];73        if (typeof alg !== "function") {74            throw new Error("algorithm is not supported");75        }76        return alg(data);77    }7879    deriveBits(algorithm, key, length) {80        if (key.algorithm.name !== algorithm.name) {81            throw new Error("algorithm does not match");82        }83        if (typeof key.deriveBits !== "function") {84            throw new Error("key does not support deriveBits");85        }86        return key.deriveBits(algorithm, length);87    }88}8990export default new LegacyCrypto;91