generate-key.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /**
  2. * Internal API Key Generator (Node.js version)
  3. * Generates a secure internal API key and its bcrypt hash
  4. */
  5. const crypto = require('crypto');
  6. const bcrypt = require('bcryptjs');
  7. function generateApiKey() {
  8. const characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-";
  9. const length = 48;
  10. let result = "int_shopcall_";
  11. for (let i = 0; i < length; i++) {
  12. const randomIndex = crypto.randomInt(0, characters.length);
  13. result += characters.charAt(randomIndex);
  14. }
  15. return result;
  16. }
  17. async function hashApiKey(apiKey) {
  18. const salt = await bcrypt.genSalt(12);
  19. return await bcrypt.hash(apiKey, salt);
  20. }
  21. async function main() {
  22. const keyName = process.argv[2] || "Untitled Internal Key";
  23. console.log("\n🔑 Generating Internal API Key...\n");
  24. // Generate API key
  25. const apiKey = generateApiKey();
  26. console.log("✅ API Key Generated:");
  27. console.log(` ${apiKey}`);
  28. // Generate hash
  29. console.log("\n⏳ Generating bcrypt hash...");
  30. const hash = await hashApiKey(apiKey);
  31. console.log("✅ Hash Generated:");
  32. console.log(` ${hash}`);
  33. // Output the data
  34. console.log("\n📝 Data for insertion:");
  35. console.log("━".repeat(80));
  36. console.log(JSON.stringify({
  37. key_name: keyName,
  38. api_key: apiKey,
  39. key_hash: hash,
  40. description: 'Auto-generated internal API key',
  41. created_by: 'admin'
  42. }, null, 2));
  43. console.log("━".repeat(80));
  44. }
  45. main().catch(console.error);