-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #707 from mynaparrot/encrypt_chat
feat: end-to-end encryption (E2EE) for chat + whiteboard
- Loading branch information
Showing
9 changed files
with
245 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
const IV_LENGTH = 12, | ||
algorithm = 'AES-GCM'; | ||
let importedKey: null | CryptoKey = null; | ||
|
||
const arrayBufferToBase64 = (buffer: ArrayBuffer) => { | ||
let binary = ''; | ||
const bytes = new Uint8Array(buffer); | ||
const len = bytes.byteLength; | ||
for (let i = 0; i < len; i++) { | ||
binary += String.fromCharCode(bytes[i]); | ||
} | ||
return window.btoa(binary); | ||
}; | ||
|
||
const base64ToArrayBuffer = (base64: string) => { | ||
const binaryString = atob(base64); | ||
const bytes = new Uint8Array(binaryString.length); | ||
for (let i = 0; i < binaryString.length; i++) { | ||
bytes[i] = binaryString.charCodeAt(i); | ||
} | ||
return bytes.buffer; | ||
}; | ||
|
||
const importSecretKey = async (secret: string) => { | ||
if (importedKey) { | ||
return importedKey; | ||
} | ||
const rawKey = new TextEncoder().encode(secret); | ||
|
||
importedKey = await window.crypto.subtle.importKey( | ||
'raw', | ||
rawKey, | ||
algorithm, | ||
true, | ||
['encrypt', 'decrypt'], | ||
); | ||
|
||
return importedKey; | ||
}; | ||
|
||
const encryptMessage = async (secret: string, message: string) => { | ||
const key = await importSecretKey(secret); | ||
const encoded = new TextEncoder().encode(message); | ||
|
||
// Generate a new IV for each encryption to ensure security | ||
const iv = window.crypto.getRandomValues(new Uint8Array(IV_LENGTH)); | ||
const cipherText = await window.crypto.subtle.encrypt( | ||
{ name: algorithm, iv: iv }, | ||
key, | ||
encoded, | ||
); | ||
|
||
const arrayView = new Uint8Array(iv.byteLength + cipherText.byteLength); | ||
arrayView.set(iv); | ||
arrayView.set(new Uint8Array(cipherText), iv.byteLength); | ||
|
||
return arrayBufferToBase64(arrayView.buffer); | ||
}; | ||
|
||
const decryptMessage = async (secret: string, cipherData: string) => { | ||
const key = await importSecretKey(secret); | ||
const data = base64ToArrayBuffer(cipherData); | ||
|
||
const iv = data.slice(0, IV_LENGTH); | ||
const cipherText = data.slice(IV_LENGTH); | ||
|
||
const textData = await window.crypto.subtle.decrypt( | ||
{ name: algorithm, iv }, | ||
key, | ||
cipherText, | ||
); | ||
|
||
return new TextDecoder().decode(textData); | ||
}; | ||
|
||
export { importSecretKey, encryptMessage, decryptMessage }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.