59 líneas
1.3 KiB
TypeScript
59 líneas
1.3 KiB
TypeScript
import crypto from 'crypto';
|
|
|
|
export interface HashResult {
|
|
plaintext: string;
|
|
md5: string;
|
|
sha1: string;
|
|
sha256: string;
|
|
sha512: string;
|
|
}
|
|
|
|
/**
|
|
* Generate all common hashes for a given plaintext
|
|
*/
|
|
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'),
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Detect hash type based on length and format
|
|
*/
|
|
export function detectHashType(hash: string): string | null {
|
|
const cleanHash = hash.trim().toLowerCase();
|
|
|
|
// MD5: 32 hex characters
|
|
if (/^[a-f0-9]{32}$/i.test(cleanHash)) {
|
|
return 'md5';
|
|
}
|
|
|
|
// SHA1: 40 hex characters
|
|
if (/^[a-f0-9]{40}$/i.test(cleanHash)) {
|
|
return 'sha1';
|
|
}
|
|
|
|
// SHA256: 64 hex characters
|
|
if (/^[a-f0-9]{64}$/i.test(cleanHash)) {
|
|
return 'sha256';
|
|
}
|
|
|
|
// SHA512: 128 hex characters
|
|
if (/^[a-f0-9]{128}$/i.test(cleanHash)) {
|
|
return 'sha512';
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Check if a string is a valid hash
|
|
*/
|
|
export function isHash(input: string): boolean {
|
|
return detectHashType(input) !== null;
|
|
}
|