crypto-attachements.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. "use strict";
  2. /*
  3. Copyright 2021-2022 The Matrix.org Foundation C.I.C.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  15. if (k2 === undefined) k2 = k;
  16. Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
  17. }) : (function(o, m, k, k2) {
  18. if (k2 === undefined) k2 = k;
  19. o[k2] = m[k];
  20. }));
  21. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  22. Object.defineProperty(o, "default", { enumerable: true, value: v });
  23. }) : function(o, v) {
  24. o["default"] = v;
  25. });
  26. var __importStar = (this && this.__importStar) || function (mod) {
  27. if (mod && mod.__esModule) return mod;
  28. var result = {};
  29. if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  30. __setModuleDefault(result, mod);
  31. return result;
  32. };
  33. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  34. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  35. return new (P || (P = Promise))(function (resolve, reject) {
  36. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  37. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  38. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  39. step((generator = generator.apply(thisArg, _arguments || [])).next());
  40. });
  41. };
  42. var __generator = (this && this.__generator) || function (thisArg, body) {
  43. var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
  44. return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
  45. function verb(n) { return function (v) { return step([n, v]); }; }
  46. function step(op) {
  47. if (f) throw new TypeError("Generator is already executing.");
  48. while (_) try {
  49. if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
  50. if (y = 0, t) op = [op[0] & 2, t.value];
  51. switch (op[0]) {
  52. case 0: case 1: t = op; break;
  53. case 4: _.label++; return { value: op[1], done: false };
  54. case 5: _.label++; y = op[1]; op = [0]; continue;
  55. case 7: op = _.ops.pop(); _.trys.pop(); continue;
  56. default:
  57. if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
  58. if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
  59. if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
  60. if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
  61. if (t[2]) _.ops.pop();
  62. _.trys.pop(); continue;
  63. }
  64. op = body.call(thisArg, _);
  65. } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
  66. if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
  67. }
  68. };
  69. var _a;
  70. Object.defineProperty(exports, "__esModule", { value: true });
  71. exports.decodeBase64 = exports.encodeBase64 = exports.decryptAttachment = exports.encryptAttachment = void 0;
  72. var webcrypto = __importStar(require("./webcrypto"));
  73. var nodejs = __importStar(require("./nodejs"));
  74. var hasWebcrypto = !!(typeof window !== 'undefined' && ((_a = window.crypto) === null || _a === void 0 ? void 0 : _a.subtle));
  75. /**
  76. * Encrypt an attachment to latest protocol specification (currently v2).
  77. * @param {ArrayBuffer} plaintextBuffer The attachment data buffer.
  78. * @return {Promise} A promise that resolves with an object when the attachment is encrypted.
  79. * The object has a "data" key with an ArrayBuffer of encrypted data and an "info" key
  80. * with an object containing the info needed to decrypt the data.
  81. */
  82. function encryptAttachment(plaintextBuffer) {
  83. return __awaiter(this, void 0, void 0, function () {
  84. return __generator(this, function (_a) {
  85. return [2 /*return*/, hasWebcrypto ? webcrypto.encryptAttachment(plaintextBuffer)
  86. : nodejs.encryptAttachment(Buffer.from(plaintextBuffer))];
  87. });
  88. });
  89. }
  90. exports.encryptAttachment = encryptAttachment;
  91. /**
  92. * Decrypt an attachment that has been encrypted with v1 or v2.
  93. * @param {ArrayBuffer} ciphertextBuffer The encrypted attachment data buffer.
  94. * @param {IEncryptedFile} info The information needed to decrypt the attachment.
  95. * @return {Promise<ArrayBuffer>} A promise that resolves with an ArrayBuffer when the attachment is decrypted.
  96. */
  97. function decryptAttachment(ciphertextBuffer, info) {
  98. return __awaiter(this, void 0, void 0, function () {
  99. return __generator(this, function (_a) {
  100. return [2 /*return*/, hasWebcrypto ? webcrypto.decryptAttachment(ciphertextBuffer, info)
  101. : nodejs.decryptAttachment(Buffer.from(ciphertextBuffer), info)];
  102. });
  103. });
  104. }
  105. exports.decryptAttachment = decryptAttachment;
  106. /**
  107. * Encode a typed array of uint8 as unpadded base64.
  108. * @param {Uint8Array} uint8Array The data to encode.
  109. * @return {string} The base64 without padding.
  110. */
  111. function encodeBase64(uint8Array) {
  112. return hasWebcrypto ? webcrypto.encodeBase64(uint8Array)
  113. : nodejs.encodeBase64(uint8Array);
  114. }
  115. exports.encodeBase64 = encodeBase64;
  116. /**
  117. * Decode a base64 string to a typed array of uint8.
  118. * This will decode unpadded base64, but will also accept base64 with padding.
  119. * @param {string} base64 The unpadded base64 to decode.
  120. * @return {Uint8Array} The decoded data.
  121. */
  122. function decodeBase64(base64) {
  123. return hasWebcrypto ? webcrypto.decodeBase64(base64)
  124. : nodejs.decodeBase64(base64);
  125. }
  126. exports.decodeBase64 = decodeBase64;
  127. //# sourceMappingURL=index.js.map