-
Notifications
You must be signed in to change notification settings - Fork 455
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(keystore): hash should actually be hashed o_O
- Loading branch information
Showing
3 changed files
with
26 additions
and
15 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 |
---|---|---|
@@ -1,15 +1,32 @@ | ||
import utils from 'node:util'; | ||
import crypto from 'crypto'; | ||
import { Encryption } from '@nangohq/utils'; | ||
import { envs } from './env.js'; | ||
|
||
const pbkdf2 = utils.promisify(crypto.pbkdf2); | ||
|
||
let encryption: Encryption | null = null; | ||
|
||
function getEncryptionKey(): string | undefined { | ||
return envs.NANGO_ENCRYPTION_KEY; | ||
} | ||
|
||
export function getEncryption(): Encryption { | ||
if (!encryption) { | ||
const encryptionKey = envs.NANGO_ENCRYPTION_KEY; | ||
const encryptionKey = getEncryptionKey(); | ||
if (!encryptionKey) { | ||
throw new Error('NANGO_ENCRYPTION_KEY is not set'); | ||
} | ||
encryption = new Encryption(encryptionKey); | ||
} | ||
return encryption; | ||
} | ||
|
||
export async function hashValue(val: string): Promise<string> { | ||
const encryptionKey = getEncryptionKey(); | ||
if (!encryptionKey) { | ||
throw new Error('NANGO_ENCRYPTION_KEY is not set'); | ||
} | ||
|
||
return (await pbkdf2(val, encryptionKey, 310000, 32, 'sha256')).toString('base64'); | ||
} |