saeid ezzati
saeid ezzati

Reputation: 891

Generating the 256bit ECDSA private key

I want to make an 256 bit ECDSA private key with secp256k1 curve by php.

I used this snippet:

$pk_Generate = openssl_pkey_new(array(
    'private_key_bits' => 256,
    'private_key_type' => OPENSSL_KEYTYPE_EC,
    'curve_name' => 'secp256k1'
));

while ($msg = openssl_error_string())
    echo $msg . "<br />\n";

openssl_pkey_export($pk_Generate, $pk_Generate_Private);
var_dump($pk_Generate_Private); // show me NULL

but it give me this error:

error:0E06D06C:configuration file routines:NCONF_get_string:no value
error:0E06D06C:configuration file routines:NCONF_get_string:no value
error:0E06D06C:configuration file routines:NCONF_get_string:no value
error:0E06D06C:configuration file routines:NCONF_get_string:no value

also if this code works fine, it shows me private key in PEM format but I want it in hex string format.

please guide me.

Upvotes: 0

Views: 2033

Answers (1)

gerteb
gerteb

Reputation: 131

Try this

$config = [
    "config" => getenv('OPENSSL_CONF'),
    'private_key_type' => OPENSSL_KEYTYPE_EC,
    'curve_name' => 'secp256k1'
];
$res = openssl_pkey_new($config);
if (!$res) {
    echo 'ERROR: Fail to generate private key. -> ' . openssl_error_string();
    exit;
}
// Generate Private Key
openssl_pkey_export($res, $priv_key, NULL, $config);
// Get The Public Key
$key_detail = openssl_pkey_get_details($res);
$pub_key = $key_detail["key"];

echo "priv_key:<br>".$priv_key;
echo "<br><br>pub_key:<br>".$pub_key;

Upvotes: 1

Related Questions