Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F32167621
D6995.1765052841.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
1 KB
Referenced Files
None
Subscribers
None
D6995.1765052841.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
+
+const ENCRYPTION_ALGORITHM = 'AES-GCM';
+
+type EncryptedData = {
+ +iv: BufferSource,
+ +ciphertext: Uint8Array,
+};
+
+function generateDatabaseCryptoKey(): 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 encryptDatabaseFile(
+ data: Uint8Array,
+ key: CryptoKey,
+): Promise<EncryptedData> {
+ const iv = generateIV();
+ const ciphertext = await crypto.subtle.encrypt(
+ {
+ name: ENCRYPTION_ALGORITHM,
+ iv: iv,
+ },
+ key,
+ data,
+ );
+ return {
+ ciphertext: new Uint8Array(ciphertext),
+ iv,
+ };
+}
+
+async function decryptDatabaseFile(
+ encryptedData: EncryptedData,
+ key: CryptoKey,
+): Promise<Uint8Array> {
+ const { ciphertext, iv } = encryptedData;
+ const decrypted = await crypto.subtle.decrypt(
+ {
+ name: ENCRYPTION_ALGORITHM,
+ iv,
+ },
+ key,
+ ciphertext,
+ );
+ return new Uint8Array(decrypted);
+}
+
+export { generateDatabaseCryptoKey, encryptDatabaseFile, decryptDatabaseFile };
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Sat, Dec 6, 8:27 PM (22 h, 15 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
5838018
Default Alt Text
D6995.1765052841.diff (1 KB)
Attached To
Mode
D6995: [web-db] implement crypto functions
Attached
Detach File
Event Timeline
Log In to Comment