dint-dev / cryptography

Cryptography for Flutter developers: encryption, digital signatures, key agreement, etc.
https://pub.dev/packages/cryptography
Apache License 2.0
155 stars 75 forks source link

How to export/import private keys #148

Closed HerrMuellerluedenscheid closed 1 year ago

HerrMuellerluedenscheid commented 1 year ago

Hi, Disclaimer: I'm relatively new to dart/flutter and I may have missed the obvious way. I use the following to export the private key and store it to a file for re-load after re-opening the app

  final algorithm = Ed25519();
  final keyPair = await algorithm.newKeyPair();
  final privateKey = await keyPair.extractPrivateKeyBytes();
  final privateKeyBase64 = await base64.encode(privateKey);
// ... write base64 encoded string to file

How can I instantiate an Ed25519 key pair from the private key which I dumped to the file? Thanks in advance!

HerrMuellerluedenscheid commented 1 year ago

Ah, I have to instantiate a SimpleKeyPairData:

final keyPairLoaded = SimpleKeyPairData(privateKeyBase64Decoded, publicKey: publicKey, type: KeyPairType.ed25519);
muhdlaziem commented 1 year ago

@HerrMuellerluedenscheid what about constructing public key from encoded string?

muhdlaziem commented 1 year ago

@HerrMuellerluedenscheid what about constructing public key from encoded string?

SimplePublicKey( base64Decode("kvtIPK3Ze8Dzsv9y1R1vAgASaiOPEPTQ0fojai2RTyE=") will do. thanks !