| 1 | import { AESECBCipher, AESEAXCipher } from "./aes.js"; |
| 2 | import { DESCBCCipher, DESECBCipher } from "./des.js"; |
| 3 | import { RSACipher } from "./rsa.js"; |
| 4 | import { DHCipher } from "./dh.js"; |
| 5 | import { MD5 } from "./md5.js"; |
| 6 | |
| 7 | // A single interface for the cryptographic algorithms not supported by SubtleCrypto. |
| 8 | // Both synchronous and asynchronous implmentations are allowed. |
| 9 | class 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 | } |
| 21 | |
| 22 | 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 | } |
| 31 | |
| 32 | 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 | } |
| 41 | |
| 42 | 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 | } |
| 52 | |
| 53 | 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 | } |
| 60 | |
| 61 | 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 | } |
| 70 | |
| 71 | 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 | } |
| 78 | |
| 79 | 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 | } |
| 89 | |
| 90 | export default new LegacyCrypto; |