Esempio n. 1
0
        /// <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);
        }
Esempio n. 2
0
        /// <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);
        }
Esempio n. 3
0
        /// <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));
        }
Esempio n. 4
0
 /// <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));
 }
Esempio n. 5
0
 /// <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));
 }
Esempio n. 6
0
 /// <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));
Esempio n. 7
0
 /// <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));
 }