Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save AshtonIzmev/120230aed37f2521ba411888d3ed00b2 to your computer and use it in GitHub Desktop.

Select an option

Save AshtonIzmev/120230aed37f2521ba411888d3ed00b2 to your computer and use it in GitHub Desktop.
Well kinda trying to protect apikey for front only app
const bcrypt = require('bcryptjs');
const CryptoJS = require('crypto-js');
// Generate a strong random password (16 characters, mix of letters, numbers, symbols)
const strongPassword = 'my!!!strong!!password!';
console.log('Strong password:', strongPassword);
// Generate bcrypt hash
const passwordHash = bcrypt.hashSync(strongPassword, 12);
console.log('Password hash:', passwordHash);
// API keys to encrypt
const maptilerKey = ‘XXX’;
const openaiKey = 'sk-proj-hello;
// Encrypt API keys with the password
const encryptedMaptiler = CryptoJS.AES.encrypt(maptilerKey, strongPassword).toString();
const encryptedOpenAI = CryptoJS.AES.encrypt(openaiKey, strongPassword).toString();
console.log('Encrypted MapTiler key:', encryptedMaptiler);
console.log('Encrypted OpenAI key:', encryptedOpenAI);
// Test decryption
const decryptedMaptiler = CryptoJS.AES.decrypt(encryptedMaptiler, strongPassword).toString(CryptoJS.enc.Utf8);
const decryptedOpenAI = CryptoJS.AES.decrypt(encryptedOpenAI, strongPassword).toString(CryptoJS.enc.Utf8);
console.log('Test decryption - MapTiler:', decryptedMaptiler === maptilerKey ? 'SUCCESS' : 'FAILED');
console.log('Test decryption - OpenAI:', decryptedOpenAI === openaiKey ? 'SUCCESS' : 'FAILED');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment