cryptography icon indicating copy to clipboard operation
cryptography copied to clipboard

KeyPair PublicKey to bytes

Open apoleo88 opened this issue 4 years ago • 2 comments

From version 2 I can't find a way to extract the bytes from the public and private keys generated with Keypair:

KeyPair keyPair = await X25519().newKeyPair();

In v1.4.1 I was using:

keyPair.privateKey.extractSync()
keyPair.publicKey.bytes

But now in v2:

  • PublicKey has no longer the getter .bytes

  • Private key is no longer extractable(?). There is the method extract() that returns KeyPairData, but the documentation is not clear on what is that class and how to retrieve the data.

apoleo88 avatar Apr 02 '21 10:04 apoleo88

I guess I figured it out.

// First generate KeyPair
KeyPair keyPair = await X25519().newKeyPair();

// To get the public key bytes you have to use SimplePublicKey
SimplePublicKey publicKey = await keyPair.extractPublicKey();
List<int> publicKeyBytes = publicKey.bytes; // now this works


// To extract the private key do the following:
// extract SimpleKeyPairData
SimpleKeyPairData simpleKeyPairData = await keyPair.extract();

// then you can extract the private key bytes
List<int> privateKeyBytes = await simpleKeyPairData.extractPrivateKeyBytes();

But I agree with you that this is not very clearly documented and not very intuitive to use either. I hope this helps you anyways.

Update:

It appears that after updating the min-sdk version to 2.12 this doesn't work anymore.

maxfornacon avatar Apr 10 '21 02:04 maxfornacon

For sdk: ">=2.12.0-0 <3.0.0" you need to add as SimplePublicKey. Like this: SimplePublicKey publicKey = await keyPair.extractPublicKey() as SimplePublicKey; Same goes for SimpleKeyPairData.

maxfornacon avatar Apr 19 '21 19:04 maxfornacon