I generate an ED25519 key-pair:
$ openssl genpkey -algorithm ed25519 | openssl pkey -text
-----BEGIN PRIVATE KEY-----
MC4CAQAwBQYDK2VwBCIEIEp53xeY8qoDU5mi2h7O//pJnI5CXWHWI9SVhgjKq1kt
-----END PRIVATE KEY-----
ED25519 Private-Key:
priv:
4a:79:df:17:98:f2:aa:03:53:99:a2:da:1e:ce:ff:
fa:49:9c:8e:42:5d:61:d6:23:d4:95:86:08:ca:ab:
59:2d
pub:
c8:af:94:9f:f9:a2:10:95:2d:54:34:75:be:94:2d:
e7:9a:ef:9c:4e:7c:6d:80:ae:fd:25:e3:a4:d5:b0:
e3:30
Then I store the priv key in HEX for later use.
$ hex=4a:79:df:17:98:f2:aa:03:53:99:a2:da:1e:ce:ff:fa:49:9c:8e:42:5d:61:d6:23:d4:95:86:08:ca:ab:59:2d
According to another answer, the following should get me the original data, but instead I get an error:
$ echo $hex | xxd -r -ps > ed25519.der
$ cat ed25519.der | openssl pkey -inform der -noout -text
Could not read key from <stdin>
How can I correctly regenerate the ED25519 public key from the HEX private key?
How can I correctly regenerate the ED25519 public key from the HEX private key?
As explained in Topaco's comment,
hexis the encoded raw private key, i.e. only the 32-bytes of the ED25519 key. See this answer for more details.However
opensslexpects an input in the OpenSSL DER or PEM formats.So there will be two steps:
Recreate an OpenSSL private key from the 32-byte hex key.
In order to do that, you just need to add the following extra
headerbytes before the 32-bytehexkey:You can now check the contents of the created DER file as follows:
Output:
Optionally, you can also create a private key in the PEM format as follows:
Contents of ed25519.pem:
Create an OpenSSL public key from the OpenSSL private key.
To create a public key in the DER format:
To create a public key in the PEM format:
Contents of ed25519.pub.pem:
Optional: Printing out the 32-byte hex keys of the generated DER or PEM files for checking
Output: