-
Notifications
You must be signed in to change notification settings - Fork 2
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 #53 from fullstackedorg/p2p
Peer-to-Peer Connectivity
- Loading branch information
Showing
81 changed files
with
4,474 additions
and
1,479 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
Submodule editor-sample-demo
updated
3 files
+0 −4 | utils/random.ts | |
+ − | webview/.DS_Store | |
+38 −49 | webview/counter.tsx |
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export enum CONFIG_TYPE { | ||
PROJECTS = "projects", | ||
GIT = "git" | ||
GIT = "git", | ||
CONNECTIVITY = "connectivity" | ||
} |
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,82 @@ | ||
// for large strings, use this from https://stackoverflow.com/a/49124600 | ||
export const toBase64 = (data: ArrayBufferLike) => | ||
btoa( | ||
new Uint8Array(data).reduce( | ||
(data, byte) => data + String.fromCharCode(byte), | ||
"" | ||
) | ||
); | ||
|
||
export const fromBase64 = (data: string) => | ||
Uint8Array.from(atob(data), (c) => c.charCodeAt(null)); | ||
|
||
export const generateHash = (byteLength: number) => | ||
toBase64(crypto.getRandomValues(new Uint8Array(byteLength))); | ||
|
||
const textEncoder = new TextEncoder(); | ||
const textDecoder = new TextDecoder(); | ||
|
||
export async function encrypt(data: string, key: string) { | ||
const salt = crypto.getRandomValues(new Uint8Array(16)); | ||
const iv = crypto.getRandomValues(new Uint8Array(12)); | ||
const cryptoKey = await importKey(key); | ||
const derivedKey = await deriveKey(cryptoKey, salt, "encrypt"); | ||
const encryptedContent = await crypto.subtle.encrypt( | ||
{ | ||
name: "AES-GCM", | ||
iv | ||
}, | ||
derivedKey, | ||
textEncoder.encode(data) | ||
); | ||
|
||
const encryptedContentArr = new Uint8Array(encryptedContent); | ||
let buff = new Uint8Array( | ||
salt.byteLength + iv.byteLength + encryptedContentArr.byteLength | ||
); | ||
buff.set(salt, 0); | ||
buff.set(iv, salt.byteLength); | ||
buff.set(encryptedContentArr, salt.byteLength + iv.byteLength); | ||
return toBase64(buff); | ||
} | ||
|
||
export async function decrypt(base64: string, key: string) { | ||
const encryptedDataBuff = fromBase64(base64); | ||
const salt = encryptedDataBuff.slice(0, 16); | ||
const iv = encryptedDataBuff.slice(16, 16 + 12); | ||
const data = encryptedDataBuff.slice(16 + 12); | ||
const cryptoKey = await importKey(key); | ||
const derivedKey = await deriveKey(cryptoKey, salt, "decrypt"); | ||
const decryptedContent = await crypto.subtle.decrypt( | ||
{ | ||
name: "AES-GCM", | ||
iv | ||
}, | ||
derivedKey, | ||
data | ||
); | ||
return textDecoder.decode(decryptedContent); | ||
} | ||
|
||
const importKey = (key: string) => | ||
crypto.subtle.importKey("raw", textEncoder.encode(key), "PBKDF2", false, [ | ||
"deriveKey" | ||
]); | ||
|
||
const deriveKey = ( | ||
cryptoKey: CryptoKey, | ||
salt: ArrayBufferLike, | ||
keyUsage: "encrypt" | "decrypt" | ||
) => | ||
crypto.subtle.deriveKey( | ||
{ | ||
name: "PBKDF2", | ||
salt, | ||
iterations: 250000, | ||
hash: "SHA-256" | ||
}, | ||
cryptoKey, | ||
{ name: "AES-GCM", length: 256 }, | ||
false, | ||
[keyUsage] | ||
); |
Oops, something went wrong.