-
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.
- Loading branch information
Showing
3 changed files
with
35 additions
and
0 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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './errors'; | ||
export * from './locales'; | ||
export * from './plugins'; | ||
export * from './utils'; |
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,33 @@ | ||
import { randomBytes, scrypt, timingSafeEqual } from 'node:crypto'; | ||
import { promisify } from 'node:util'; | ||
|
||
const scryptAsync = promisify(scrypt); | ||
|
||
/** | ||
* Function to hash any plain string | ||
* @param text Plain string to convert into a hash | ||
* @returns Hashed version of the string | ||
*/ | ||
export async function hashText(text: string) { | ||
const salt = randomBytes(10).toString('hex'); | ||
const buf = (await scryptAsync(text, salt, 64)) as Buffer; | ||
|
||
return `${buf.toString('hex')}.${salt}`; | ||
} | ||
|
||
/** | ||
* Function to check if a plain string is equal to a hashed string | ||
* @param storedHash The hashed version of a string | ||
* @param supplied Plain string to compare against the hash | ||
* @returns Whether or not both strings match | ||
*/ | ||
export async function compareHash(storedHash: string, supplied: string) { | ||
const [hashedText, salt] = storedHash.split('.'); | ||
if (!hashedText || !salt) return false; | ||
|
||
const buf = (await scryptAsync(supplied, salt, 64)) as Buffer; | ||
const keyBuffer = Buffer.from(hashedText, 'hex'); | ||
const match = timingSafeEqual(buf, keyBuffer); | ||
|
||
return match; | ||
} |
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 @@ | ||
export * from './hash'; |