Page MenuHomePhabricator

D6995.id23513.diff
No OneTemporary

D6995.id23513.diff

diff --git a/web/database/utils/worker-crypto-utils.js b/web/database/utils/worker-crypto-utils.js
new file mode 100644
--- /dev/null
+++ b/web/database/utils/worker-crypto-utils.js
@@ -0,0 +1,60 @@
+// @flow
+
+import type { AesGcmParams, Crypto, CryptoKey } from './crypto-types.js';
+
+declare var crypto: Crypto;
+
+const ENCRYPTION_ALGORITHM = 'AES-GCM';
+
+type EncryptionResult = {
+ iv: BufferSource,
+ cipher: ArrayBuffer,
+};
+
+function generateCryptoKey(): Promise<CryptoKey> {
+ return crypto.subtle.generateKey(
+ {
+ name: ENCRYPTION_ALGORITHM,
+ length: 256,
+ },
+ false,
+ ['encrypt', 'decrypt'],
+ );
+}
+
+function generateIv(): BufferSource {
+ return crypto.getRandomValues(new Uint8Array(12));
+}
+
+async function encrypt(
+ data: ArrayBuffer,
+ key: CryptoKey,
+): Promise<EncryptionResult> {
+ const iv = generateIv();
+ const params: AesGcmParams = {
+ name: ENCRYPTION_ALGORITHM,
+ iv: iv,
+ };
+ const cipher = await crypto.subtle.encrypt(params, key, data);
+ return {
+ cipher,
+ iv,
+ };
+}
+
+async function decrypt(
+ cipher: ArrayBuffer,
+ key: CryptoKey,
+ iv: BufferSource,
+): Promise<ArrayBuffer> {
+ return crypto.subtle.decrypt(
+ {
+ name: ENCRYPTION_ALGORITHM,
+ iv,
+ },
+ key,
+ cipher,
+ );
+}
+
+export { generateCryptoKey, encrypt, decrypt };

File Metadata

Mime Type
text/plain
Expires
Wed, Dec 25, 10:11 AM (9 h, 49 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2702499
Default Alt Text
D6995.id23513.diff (1 KB)

Event Timeline