Reputation: 312
Is there any way to generate a private key and public key from a string like a user's password? I want to retrieve it every time the user enters password
I use this elliptic but it randomly generates a keyPair
const EC = require('elliptic').ec;
const ec = new EC('secp256k1');
let keyPair = ec.genKeyPair();
Upvotes: 2
Views: 4830
Reputation: 14639
Disclaimer: Don't create brain wallets: especially low entropy human generated ones.
You can generate any 32-byte value (technically valid private keys on secp256k1 curve must be between 0x1 to 0xFFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFE BAAE DCE6 AF48 A03B BFD2 5E8C D036 4140
) see Bitcoin Wiki
One way to do this is to hash the string you want to generate the key from:
const EC = require('elliptic').ec;
var crypto = require('crypto');
const ec = new EC('secp256k1');
let secret = crypto.createHash('sha256').update('password').digest('hex');
let keyPair = ec.keyFromSecret(secret);
Upvotes: 2