-
-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
28 changed files
with
106 additions
and
61 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"tasks": { | ||
"dev": "deno run --watch main.ts" | ||
}, | ||
"imports": { | ||
"@std/assert": "jsr:@std/assert@1", | ||
"@internal/": "./src/internal/", | ||
"@storage/": "./src/storage/", | ||
"postgres-migrations/": "npm:postgres-migrations/", | ||
"postgres-migrations/dist/": "npm:postgres-migrations/dist/", | ||
"ajv": "npm:ajv" | ||
} | ||
} |
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
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
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 +1 @@ | ||
import './start/server' | ||
import './start/server.ts' |
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,21 +1,53 @@ | ||
import AES from 'crypto-js/aes' | ||
import Utf8 from 'crypto-js/enc-utf8' | ||
import { createCipheriv, createDecipheriv, createHash, randomBytes } from 'node:crypto' | ||
import { getConfig } from '../../config' | ||
|
||
const { encryptionKey } = getConfig() | ||
|
||
/** | ||
* Decrypts a text with the configured encryption key via ENCRYPTION_KEY env | ||
* @param ciphertext | ||
*/ | ||
* Generate CryptoJs.AES key from passphrase | ||
* https://github.com/brix/crypto-js/issues/468 | ||
* */ | ||
function convertPassphraseToAesKeyBuffer(key: string, salt: Buffer): Buffer { | ||
const password = Buffer.concat([Buffer.from(key, 'binary'), salt]) | ||
const hash: Buffer[] = [] | ||
let digest = password | ||
for (let i = 0; i < 3; i++) { | ||
hash[i] = createHash('md5').update(digest).digest() | ||
digest = Buffer.concat([hash[i]!, password]) | ||
} | ||
return Buffer.concat(hash) | ||
} | ||
|
||
/** | ||
* Replicate CryptoJs.AES.decrypt method | ||
* */ | ||
export function decrypt(ciphertext: string): string { | ||
return AES.decrypt(ciphertext, encryptionKey).toString(Utf8) | ||
try { | ||
const cipherBuffer = Buffer.from(ciphertext, 'base64') | ||
const salt = cipherBuffer.subarray(8, 16) | ||
const keyDerivation = convertPassphraseToAesKeyBuffer(encryptionKey, salt) | ||
const [key, iv] = [keyDerivation.subarray(0, 32), keyDerivation.subarray(32)] | ||
const contents = cipherBuffer.subarray(16) | ||
const decipher = createDecipheriv('aes-256-cbc', key, iv) | ||
const decrypted = Buffer.concat([decipher.update(contents), decipher.final()]) | ||
return decrypted.toString('utf8') | ||
} catch (e) { | ||
throw e | ||
} | ||
} | ||
|
||
/** | ||
* Encrypts a text with the configured encryption key via ENCRYPTION_KEY env | ||
* @param plaintext | ||
*/ | ||
* Replicate CryptoJs.AES.encrypt method | ||
* */ | ||
export function encrypt(plaintext: string): string { | ||
return AES.encrypt(plaintext, encryptionKey).toString() | ||
try { | ||
const salt = randomBytes(8) | ||
const keyDerivation = convertPassphraseToAesKeyBuffer(encryptionKey, salt) | ||
const [key, iv] = [keyDerivation.subarray(0, 32), keyDerivation.subarray(32)] | ||
const cipher = createCipheriv('aes-256-cbc', key, iv) | ||
const contents = Buffer.concat([cipher.update(plaintext), cipher.final()]) | ||
const encrypted = Buffer.concat([Buffer.from('Salted__', 'utf8'), salt, contents]) | ||
return encrypted.toString('base64') | ||
} catch (e) { | ||
throw e | ||
} | ||
} |
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
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
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
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
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