crypto.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. // A single interface for the cryptographic algorithms not supported by SubtleCrypto.
  7. // Both synchronous and asynchronous implmentations are allowed.
  8. class LegacyCrypto {
  9. constructor() {
  10. this._algorithms = {
  11. "AES-ECB": AESECBCipher,
  12. "AES-EAX": AESEAXCipher,
  13. "DES-ECB": DESECBCipher,
  14. "DES-CBC": DESCBCCipher,
  15. "RSA-PKCS1-v1_5": RSACipher,
  16. "DH": DHCipher,
  17. "MD5": MD5,
  18. };
  19. }
  20. encrypt(algorithm, key, data) {
  21. if (key.algorithm.name !== algorithm.name) {
  22. throw new Error("algorithm does not match");
  23. }
  24. if (typeof key.encrypt !== "function") {
  25. throw new Error("key does not support encryption");
  26. }
  27. return key.encrypt(algorithm, data);
  28. }
  29. decrypt(algorithm, key, data) {
  30. if (key.algorithm.name !== algorithm.name) {
  31. throw new Error("algorithm does not match");
  32. }
  33. if (typeof key.decrypt !== "function") {
  34. throw new Error("key does not support encryption");
  35. }
  36. return key.decrypt(algorithm, data);
  37. }
  38. importKey(format, keyData, algorithm, extractable, keyUsages) {
  39. if (format !== "raw") {
  40. throw new Error("key format is not supported");
  41. }
  42. const alg = this._algorithms[algorithm.name];
  43. if (typeof alg === "undefined" || typeof alg.importKey !== "function") {
  44. throw new Error("algorithm is not supported");
  45. }
  46. return alg.importKey(keyData, algorithm, extractable, keyUsages);
  47. }
  48. generateKey(algorithm, extractable, keyUsages) {
  49. const alg = this._algorithms[algorithm.name];
  50. if (typeof alg === "undefined" || typeof alg.generateKey !== "function") {
  51. throw new Error("algorithm is not supported");
  52. }
  53. return alg.generateKey(algorithm, extractable, keyUsages);
  54. }
  55. exportKey(format, key) {
  56. if (format !== "raw") {
  57. throw new Error("key format is not supported");
  58. }
  59. if (typeof key.exportKey !== "function") {
  60. throw new Error("key does not support exportKey");
  61. }
  62. return key.exportKey();
  63. }
  64. digest(algorithm, data) {
  65. const alg = this._algorithms[algorithm];
  66. if (typeof alg !== "function") {
  67. throw new Error("algorithm is not supported");
  68. }
  69. return alg(data);
  70. }
  71. deriveBits(algorithm, key, length) {
  72. if (key.algorithm.name !== algorithm.name) {
  73. throw new Error("algorithm does not match");
  74. }
  75. if (typeof key.deriveBits !== "function") {
  76. throw new Error("key does not support deriveBits");
  77. }
  78. return key.deriveBits(algorithm, length);
  79. }
  80. }
  81. export default new LegacyCrypto;