ionux / phactor

Phactor is a high-performance PHP implementation of the elliptic curve math functions needed for EC keypair and ECDSA signature generation, validating signatures, validating curve points, creating SINs and much more.
https://github.com/ionux/phactor
MIT License
50 stars 18 forks source link

generate hex from certificate #5

Closed adoley closed 7 years ago

adoley commented 7 years ago

Hi, is there a way to convert the below private key to hex(private_key_hex) so that I can use it in the function $signature = $sig->generate('my message to sign...', $info['private_key_hex'])

-----BEGIN PRIVATE KEY----- MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgVfUclWgx951sJlMk rNW7DyqHXS2wwhhPWj9I3gsHpRWhRANCAAQlXxMH0e8pXwk7J/ZxG47gpNKwduaA Q+HThh0KmT3aCc7JMQhTy/ErXdCpUMfWYuJ69XXvbb9GDUYi1FVD6mYV -----END PRIVATE KEY-----

And is there a way to load .pem file and get the public key in hex format.

Thanks.

ionux commented 7 years ago

Hey @adoley thanks for the question! You can load & decode a PEM-formatted key using the Key class. The ASN1 trait (which is used by the Key class) has a pemDecode function for this purpose. Refer to line 75 of the ASN1.php class file for the param & return value information or if you'd like to see how it works internally.

Since this trait is included in the Key class, all you would need to do is call this function from your instantiated key object, i.e.: $hexKeypair = $mykey->pemDecode('...pem key text...');

Let me know if that helps!

adoley commented 7 years ago

Hi, below are the code and exception

Exception begin

Fatal error: Uncaught exception 'Exception' with message 'Invalid or corrupt secp256k1 key provided. Cannot decode the supplied PEM data. OID is not for EC key. Value checked was "'8040410815'" in ASN1.php:155 Stack trace:

0 src/ASN1.php(94): Phactor\Key->pemOidCheck('8040410815')

1 Phactor\Key->decodePEM('-----BEGIN PRIV...')

2 {main}

thrown in phactor/src/ASN1.php on line 155

Exception ends

code begins

 require dirname(__FILE__).'/../vendor/autoload.php';
 $key = new \Phactor\Key;
 $hexKeypair = $key->decodePEM('-----BEGIN PRIVATE KEY-----

MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgVfUclWgx951sJlMk rNW7DyqHXS2wwhhPWj9I3gsHpRWhRANCAAQlXxMH0e8pXwk7J/ZxG47gpNKwduaA Q+HThh0KmT3aCc7JMQhTy/ErXdCpUMfWYuJ69XXvbb9GDUYi1FVD6mYV -----END PRIVATE KEY-----');

code ends

ionux commented 7 years ago

Ok, so I apologize for not seeing the issue before but only PEM-formatted public keys are supported currently not private keys. I'll be adding support for private key pem data in a future release!

adoley commented 7 years ago

Thank you @ionux