/// <summary> /// Generate a KeyPair from a given Private Key /// </summary> /// <param name="privKey">Secret Private Key</param> public KeyPair(IPrivateSecretKey privKey) { if (privKey == null) { throw new ArgumentNullException(nameof(privKey)); } var sodiumKeyPair = Sodium.PublicKeyBox.GenerateKeyPair(privKey.Bytes); Secret = new PrivateSecretKey(sodiumKeyPair.PrivateKey); Public = new PublicKey(sodiumKeyPair.PublicKey); }
/// <summary> /// Generate a KeyPair from a given Private Key /// </summary> /// <param name="privKey">Secret Private Key</param> public SigningKeyPair(IPrivateSecretKey privKey) { if (privKey == null) { throw new ArgumentNullException(nameof(privKey)); } var pubKey = Sodium.PublicKeyAuth.ExtractEd25519PublicKeyFromEd25519SecretKey(privKey.Bytes); Secret = privKey; Public = new PublicKey(pubKey); }
/// <summary> /// Unlock the Data /// </summary> /// <param name="senderPubKey">The Public Key of the Sender</param> /// <param name="recieverPrivKey">The Private Key of the Reciever</param> /// <returns>The unlocked Data</returns> public byte[] UnlockBytes(IPublicKey senderPubKey, IPrivateSecretKey recieverPrivKey) { if (senderPubKey == null) { throw new ArgumentNullException(nameof(senderPubKey)); } if (recieverPrivKey == null) { throw new ArgumentNullException(nameof(recieverPrivKey)); } return(PublicKeyBox.Open(Cipher, Nonce.Bytes, secretKey: recieverPrivKey.Bytes, publicKey: senderPubKey.Bytes)); }
/// <summary> /// Create Signer /// /// Primitive: Ed25519 (libsodium crypto_sign) /// </summary> /// <param name="privKey">Private Key used for Signing</param> /// <example> /// <code> /// // Create KeyPair /// var AliceKeys = new SigningKeyPair(); /// /// // Create Sigend with the Private Key /// var pen = new Signer(AliceKeys.Secret); /// /// // create signature /// var signature = pen.Sign(MESSAGE); /// /// // Validate with the Public Key /// if(signature.Verify(MESSAGE, AliceKeys.Public)) /// // Singature is Valid! /// /// pen.Clear(); /// </code> /// </example> public Signer(IPrivateSecretKey privKey) { PrivKey = privKey ?? throw new ArgumentNullException(nameof(privKey)); }
/// <summary> /// Creates a Locker with Asymmertic/Public Keys /// /// Primitive: X25519 + XSalsa20 + Poly1305 MAC (libsodium crypto_box) /// </summary> /// <param name="recieverPubKey">The Public Key of the Reciever the Data are locked for</param> /// <param name="senderPrivKey">The Private Key of the Sender</param> public SharedLocker(IPublicKey recieverPubKey, IPrivateSecretKey senderPrivKey) { publickKey = recieverPubKey ?? throw new ArgumentNullException(nameof(recieverPubKey)); privateKey = senderPrivKey ?? throw new ArgumentNullException(nameof(senderPrivKey)); }
/// <summary> /// Unlock the String /// </summary> /// <param name="senderPubKey">The Public Key of the Sender</param> /// <param name="recieverPrivKey">The Private Key of the Reciever</param> /// <returns>The unlocked String</returns> public string UnlockString(IPublicKey senderPubKey, IPrivateSecretKey recieverPrivKey) => Secure.Encode(UnlockBytes(senderPubKey, recieverPrivKey));
/// <summary> /// Load a KeyPair /// </summary> /// <param name="privKey">Secret Private Key</param> /// <param name="pubKey">Public Key</param> public KeyPair(IPrivateSecretKey privKey, IPublicKey pubKey) { Secret = privKey ?? throw new ArgumentNullException(nameof(privKey)); Public = pubKey ?? throw new ArgumentNullException(nameof(pubKey)); }