test-numbers-generator
Version:
Generate and validate European test phone numbers (mobile and landline) in safe, non-existent ranges.
30 lines (27 loc) • 1.12 kB
text/typescript
import crypto from 'crypto';
/**
* Example usage:
*
* import { generateDeterministicPassword } from './passwordGenerator';
*
* const password = generateDeterministicPassword('verzorger1kind', 'myAppSecret');
* console.log(password); // Always the same for same secret+appSecret
*/
/**
* Generates a deterministic password based on a user secret and an app secret (pepper).
* The same combination will always produce the same password.
* @param userSecret - A user-provided secret string
* @param appSecret - An application-wide secret (pepper, should be kept private)
* @returns Deterministic password string (12 chars)
*/
export function generateDeterministicPassword(userSecret: string, appSecret: string): string {
const length = 12;
// Combine user secret and app secret
const input = `${userSecret}:${appSecret}`;
// Create a SHA-256 hash
const hash = crypto.createHash('sha256').update(input).digest('base64');
// Remove non-alphanumeric characters for password safety
const safe = hash.replace(/[^a-zA-Z0-9]/g, '');
// Return the requested length
return safe.slice(0, length);
}