c0mm4nd / dart-ecdsa

Dart's ecdsa signature for ec
https://pub.dev/packages/ecdsa
MIT License
8 stars 3 forks source link

Converting generated key to pem #3

Open kabaluyot opened 2 years ago

kabaluyot commented 2 years ago

var ec = getSecp256k1(); var priv = ec.generatePrivateKey(); var pub = priv.publicKey;

How to convert to pem format?

c0mm4nd commented 2 years ago

Many cryptography standards use ASN.1 to define their data structures, and Distinguished Encoding Rules (DER) to serialize those structures.[2] Because DER produces binary output, it can be challenging to transmit the resulting files through systems, like electronic mail, that only support ASCII.

The PEM format solves this problem by encoding the binary data using base64. PEM also defines a one-line header, consisting of -----BEGIN, a label, and -----, and a one-line footer, consisting of -----END, a label, and -----. The label determines the type of message encoded. Common labels include CERTIFICATE, CERTIFICATE REQUEST, PRIVATE KEY and X509 CRL.

As this said, the PEM =

-----BEGIN PRIVATE KEY-----
`base64(key.toDER())`
-----END PRIVATE KEY-----

(pseudocode, I believe you can write this in dart easily)

ref: https://en.wikipedia.org/wiki/Privacy-Enhanced_Mail

kabaluyot commented 2 years ago

Sorry I'm not too familiar with cryptography. Any chance you can post an example?