3

For public key encryption and diffie-hellman in libsodium, I typically make private keys simply by generating 32 random bytes with randombytes_buf and then derive the public key (when needed) using crypto_scalarmult_base.

Is there any benefit to using crypto_box_keypair to generate a keypair (other than syntax)? Or does this function basically do exactly that?

Jeroen Ooms
  • 31,998
  • 35
  • 134
  • 207

2 Answers2

4

This is exactly what the crypto_box_keypair() function does.

The benefits of this function are clarity, and guarantee that the secret key is properly generated.

Frank Denis
  • 1,475
  • 9
  • 12
  • 1
    Once the crypto_box_keypair() function returns a public and private key - they are returned as uint8 arrays -> how do I convert them to usable strings (to send in json) sodium.to_string doesn't seem to work – Edward Sep 28 '17 at 04:38
1

https://download.libsodium.org/doc/public-key_cryptography/public-key_signatures.html

for example:

    unsigned char pk[crypto_sign_PUBLICKEYBYTES]; //Variable declarations
    unsigned char sk[crypto_sign_SECRETKEYBYTES]; Variable declarations
    crypto_sign_keypair(pk, sk);

    NSData *privateKeyData = [NSData dataWithBytes:sk length:crypto_box_SECRETKEYBYTES];
    NSData *publicKeyData = [NSData dataWithBytes:pk length:crypto_box_PUBLICKEYBYTES];

    NSLog(@"%@",privateKeyData);  // target publick key data and secret key data
    NSLog(@"%@",publicKeyData);  
    //Other 
    NSLog(@"%s\n\n=====\n\n\n%s",pk,sk); //(nullable const void *)bytes
    Byte *byte = (Byte *)[publicKeyData bytes];
    NSLog(@"%s",byte);
Qun Li
  • 1,256
  • 13
  • 13