23
lib/hash.ts
23
lib/hash.ts
@@ -1,5 +1,4 @@
|
||||
import crypto from 'crypto';
|
||||
import bcrypt from 'bcrypt';
|
||||
|
||||
export interface HashResult {
|
||||
plaintext: string;
|
||||
@@ -7,22 +6,18 @@ export interface HashResult {
|
||||
sha1: string;
|
||||
sha256: string;
|
||||
sha512: string;
|
||||
bcrypt: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate all common hashes for a given plaintext
|
||||
*/
|
||||
export async function generateHashes(plaintext: string): Promise<HashResult> {
|
||||
const bcryptHash = await bcrypt.hash(plaintext, 10);
|
||||
|
||||
export function generateHashes(plaintext: string): HashResult {
|
||||
return {
|
||||
plaintext,
|
||||
md5: crypto.createHash('md5').update(plaintext).digest('hex'),
|
||||
sha1: crypto.createHash('sha1').update(plaintext).digest('hex'),
|
||||
sha256: crypto.createHash('sha256').update(plaintext).digest('hex'),
|
||||
sha512: crypto.createHash('sha512').update(plaintext).digest('hex'),
|
||||
bcrypt: bcryptHash,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -52,11 +47,6 @@ export function detectHashType(hash: string): string | null {
|
||||
return 'sha512';
|
||||
}
|
||||
|
||||
// BCrypt: starts with $2a$, $2b$, $2x$, or $2y$
|
||||
if (/^\$2[abxy]\$/.test(cleanHash)) {
|
||||
return 'bcrypt';
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -66,14 +56,3 @@ export function detectHashType(hash: string): string | null {
|
||||
export function isHash(input: string): boolean {
|
||||
return detectHashType(input) !== null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify a plaintext against a bcrypt hash
|
||||
*/
|
||||
export async function verifyBcrypt(plaintext: string, hash: string): Promise<boolean> {
|
||||
try {
|
||||
return await bcrypt.compare(plaintext, hash);
|
||||
} catch (_error) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Referencia en una nueva incidencia
Block a user