ruby_ecdsa icon indicating copy to clipboard operation
ruby_ecdsa copied to clipboard

How to read from PEM file to sign ECDSA

Open kyisoethin opened this issue 6 years ago • 2 comments

I have private key in pem format. I want to sign with following code.

signature = ECDSA.sign($group, $private_key, digest, temp_key) I want to know is how to read from pem file.

kyisoethin avatar Dec 10 '18 10:12 kyisoethin

This library treats private keys as normal Ruby integers, so if you can figure out how to extract the actual integer value of your private key then you would be able to use it with this library. I don't know how to do that at the moment, but there is probably a way to do it using Ruby's OpenSSL bindings.

DavidEGrayson avatar Dec 10 '18 18:12 DavidEGrayson

It's quite easy (granted, all the lifting is provided by OpenSSL 😄 )

pem_private_key = File.read(private_key_path)
ec_private_key = OpenSSL::PKey::EC.new(pem_private_key)
int_private_key = ec_private_key.private_key.to_i

The int_private_key can then be plugged into ECDSA::sign

aleksandrs-ledovskis avatar Feb 15 '20 16:02 aleksandrs-ledovskis