libsodium如何生成密钥对

How does libsodium generate a keypair

对于 libsodium 中的 public key encryption and diffie-hellman,我通常通过使用 randombytes_buf 生成 32 个随机字节来制作私钥,然后使用 crypto_scalarmult_base 导出 public 密钥(需要时) ].

使用 crypto_box_keypair 生成密钥对(语法除外)有什么好处吗?或者这个功能基本上就是这样做的吗?

这正是 crypto_box_keypair() 函数的作用。

这个函数的好处是清晰明了,保证秘钥正确生成。

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

例如:

    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);