37 lines
1.3 KiB
JavaScript
37 lines
1.3 KiB
JavaScript
async function generateDeterministicPassword(
|
|
email,
|
|
userId,
|
|
) {
|
|
// Use the user ID or email as the base for deterministic generation
|
|
const baseString = userId || email;
|
|
const secret = 'ODEwMGNiMmUtOGMxYS0xMWYwLWJlZDYtYTM3YzYyMWY0NGEzCg==';
|
|
|
|
// Create a deterministic password using HMAC
|
|
const encoder = new TextEncoder();
|
|
const keyData = encoder.encode(secret);
|
|
const messageData = encoder.encode(baseString);
|
|
|
|
// Import key for HMAC
|
|
const key = await crypto.subtle.importKey(
|
|
'raw',
|
|
keyData,
|
|
{ name: 'HMAC', hash: 'SHA-256' },
|
|
false,
|
|
['sign'],
|
|
);
|
|
// Generate HMAC
|
|
const signature = await crypto.subtle.sign('HMAC', key, messageData);
|
|
// Convert to base64 and make it a valid password
|
|
const hashArray = Array.from(new Uint8Array(signature));
|
|
const hashHex = hashArray
|
|
.map((b) => b.toString(16).padStart(2, '0'))
|
|
.join('');
|
|
// Take first 24 characters and add some complexity
|
|
const basePassword = hashHex.substring(0, 24);
|
|
// Add some required complexity for Medusa (uppercase, lowercase, numbers, symbols)
|
|
return `Mk${basePassword}9!`;
|
|
}
|
|
|
|
generateDeterministicPassword('', '3835d1f2-10a8-44be-85fe-d6d8d3c31848').then((result) => console.log('one:', result));
|
|
generateDeterministicPassword('ee37408020410@medreport.ee', '').then((result) => console.log('two:', result));
|