Search code examples
phplibsodium

How to export Libsoidum keys using PHP


Super noob here with Libsodium so I apologize in advance.

I'm using PHP and working through this example in PHP.

// On Alice's computer:

$alice_box_kp = sodium_crypto_box_keypair();
$alice_sign_kp = sodium_crypto_sign_keypair();

// Split the key for the crypto_box API for ease of use
$alice_box_secretkey = sodium_crypto_box_secretkey($alice_box_kp);
$alice_box_publickey = sodium_crypto_box_publickey($alice_box_kp);

// Split the key for the crypto_sign API for ease of use
$alice_sign_secretkey = sodium_crypto_sign_secretkey($alice_sign_kp);
$alice_sign_publickey = sodium_crypto_sign_publickey($alice_sign_kp);

// On Bob's computer:

$bob_box_kp = sodium_crypto_box_keypair();
$bob_sign_kp = sodium_crypto_sign_keypair();

// Split the key for the crypto_box API for ease of use
$bob_box_secretkey = sodium_crypto_box_secretkey($bob_box_kp);
$bob_box_publickey = sodium_crypto_box_publickey($bob_box_kp);

// Split the key for the crypto_sign API for ease of use
$bob_sign_secretkey = sodium_crypto_sign_secretkey($bob_sign_kp);
$bob_sign_publickey = sodium_crypto_sign_publickey($bob_sign_kp);

How do I get the keys into a file format that I can exchange out of band? On the flip, how can I import the keys or read from the keys?

One key will be on a linux server and the other in a node.js module.

Thanks in advance for your help!


Solution

  • These keys are just binary data, represented as strings in PHP. They are not opaque objects, and they are compatible with all bindings (PHP and NodeJS included).

    So, you can just save them to a file (even file_put_contents() will do), or send them through the network as you would save/store an image.