Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F3528763
D6995.id23513.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
1 KB
Referenced Files
None
Subscribers
None
D6995.id23513.diff
View Options
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
Details
Attached
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)
Attached To
Mode
D6995: [web-db] implement crypto functions
Attached
Detach File
Event Timeline
Log In to Comment