forked from stackblitz/bolt.new
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(session): encrypt data and fix renewal (stackblitz#38)
- Loading branch information
Showing
6 changed files
with
171 additions
and
51 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
const encoder = new TextEncoder(); | ||
const decoder = new TextDecoder(); | ||
const IV_LENGTH = 16; | ||
|
||
export async function encrypt(key: string, data: string) { | ||
const iv = crypto.getRandomValues(new Uint8Array(IV_LENGTH)); | ||
const cryptoKey = await getKey(key); | ||
|
||
const ciphertext = await crypto.subtle.encrypt( | ||
{ | ||
name: 'AES-CBC', | ||
iv, | ||
}, | ||
cryptoKey, | ||
encoder.encode(data), | ||
); | ||
|
||
const bundle = new Uint8Array(IV_LENGTH + ciphertext.byteLength); | ||
|
||
bundle.set(new Uint8Array(ciphertext)); | ||
bundle.set(iv, ciphertext.byteLength); | ||
|
||
return decodeBase64(bundle); | ||
} | ||
|
||
export async function decrypt(key: string, payload: string) { | ||
const bundle = encodeBase64(payload); | ||
|
||
const iv = new Uint8Array(bundle.buffer, bundle.byteLength - IV_LENGTH); | ||
const ciphertext = new Uint8Array(bundle.buffer, 0, bundle.byteLength - IV_LENGTH); | ||
|
||
const cryptoKey = await getKey(key); | ||
|
||
const plaintext = await crypto.subtle.decrypt( | ||
{ | ||
name: 'AES-CBC', | ||
iv, | ||
}, | ||
cryptoKey, | ||
ciphertext, | ||
); | ||
|
||
return decoder.decode(plaintext); | ||
} | ||
|
||
async function getKey(key: string) { | ||
return await crypto.subtle.importKey('raw', encodeBase64(key), { name: 'AES-CBC' }, false, ['encrypt', 'decrypt']); | ||
} | ||
|
||
function decodeBase64(encoded: Uint8Array) { | ||
const byteChars = Array.from(encoded, (byte) => String.fromCodePoint(byte)); | ||
|
||
return btoa(byteChars.join('')); | ||
} | ||
|
||
function encodeBase64(data: string) { | ||
return Uint8Array.from(atob(data), (ch) => ch.codePointAt(0)!); | ||
} |
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
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,5 +1,5 @@ | ||
interface Env { | ||
ANTHROPIC_API_KEY: string; | ||
SESSION_SECRET: string; | ||
LOGIN_PASSWORD: string; | ||
PAYLOAD_SECRET: string; | ||
} |