Enhance Credential Generation Security Using crypto.getRandomValues() #6380
scottnzuk
started this conversation in
Feature Requests & Suggestions
Replies: 3 comments 5 replies
-
This looks completely unrelated to the project. Is this spam/auto/AI-generated? |
Beta Was this translation helpful? Give feedback.
4 replies
-
@scottnzuk can you explain what the problem is exactly? |
Beta Was this translation helpful? Give feedback.
1 reply
-
#6473 🥇 |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
What features would you like to see added?
Enhance Credential Generation Security Using crypto.getRandomValues()
Description:
Currently, credential generation relies on an external API. To improve security and reduce dependency on external systems, I propose using crypto.getRandomValues() or crypto.subtle.generateKey() for in-browser random key generation.
Proposed Solution:
• Replace Math.random() (if used) with crypto.getRandomValues().
• Consider crypto.subtle.generateKey() for AES-based secure key generation.
• Ensure the output meets entropy requirements for secrets.
Benefits:
✅ Eliminates reliance on an external API.
✅ Improves randomness and security.
✅ Works across all modern browsers.
Im far from developer so mostly this is code review via AI but it said the generator is predictable and its not crypto secure.
More details
If the credential generation happens in the browser and relies on an external API, you have a few alternatives:
⸻
1️⃣ Use crypto.subtle.generateKey (More Secure)
If you want strong random keys without an external API, use crypto.subtle.generateKey().
Example:
async function generateKey() {
const key = await crypto.subtle.generateKey(
{ name: "AES-GCM", length: 256 },
true,
["encrypt", "decrypt"]
);
const exportedKey = await crypto.subtle.exportKey("jwk", key);
return exportedKey.k; // Returns a secure random key
}
📌 Use Case: Generates AES 256-bit keys securely in the browser.
⸻
2️⃣ Use crypto.getRandomValues() (Fast & Simple)
If you just need random strings, you can use crypto.getRandomValues():
function generateSecureRandomString(length = 32) {
const array = new Uint8Array(length);
window.crypto.getRandomValues(array);
return Array.from(array, (byte) => byte.toString(16).padStart(2, '0')).join('').slice(0, length);
}
console.log(generateSecureRandomString(32)); // Example output: "a3f5e2d6c9b847..."
📌 Use Case: Creates random hex strings (good for secrets, API keys, etc.).
⸻
3️⃣ Use WebAssembly (If You Need High Performance)
If you need ultra-fast cryptography, you can use WebAssembly libraries like libsodium:
<script src="https://cdnjs.cloudflare.com/ajax/libs/libsodium-wrappers/0.7.9/libsodium-wrappers.min.js"></script> <script> async function generateSodiumKey() { await sodium.ready; return sodium.to_hex(sodium.randombytes_buf(32)); } generateSodiumKey().then(console.log); </script>📌 Use Case: More advanced crypto functions (HMAC, Argon2 hashing, etc.).
⸻
4️⃣ Alternative: Use IndexedDB for Secure Storage
If the credentials need storage, avoid localStorage (which is insecure).
Instead, use IndexedDB:
const db = indexedDB.open("secure-store", 1);
db.onupgradeneeded = (event) => {
let store = event.target.result.createObjectStore("keys", { keyPath: "id" });
store.transaction.oncomplete = () => console.log("DB Ready");
};
📌 Use Case: Stores secrets securely in the browser.
⸻
🔹 What’s Best for You?
• For simple randomness → crypto.getRandomValues()
• For AES encryption → crypto.subtle.generateKey()
• For high-performance crypto → WebAssembly (libsodium)
• For secure storage → IndexedDB
The security and randomness of your credentials generator depend heavily on how the generateCredentials function (from useCredentialsGenerator) is implemented. Here are the key factors to consider:
Security & Randomness Concerns
1. Source of Randomness:
• If generateCredentials uses Math.random(), it’s not cryptographically secure and can be predictable.
• Instead, use crypto APIs like:
crypto.getRandomValues(new Uint8Array(16))
Or, for Node.js:
require('crypto').randomBytes(32).toString('hex')
setTimeout(() => navigator.clipboard.writeText(''), 10000) // Clears after 10s
Recommendations to Improve Security
✅ Use crypto.getRandomValues() for better randomness.
✅ Ensure secrets are at least 32-64 characters.
✅ Hide credentials by default, allow users to reveal them.
✅ Auto-clear clipboard after a short delay.
✅ Avoid storing secrets in localStorage.
Which components are impacted by your request?
Other, General
Pictures
No response
Code of Conduct
Beta Was this translation helpful? Give feedback.
All reactions