Ejemplo n.º 1
0
        /// <summary>
        /// Encrypts the text.
        /// </summary>
        /// <param name="recipient">The <see cref="VirgilCard"/> recipient.</param>
        /// <param name="text">The text.</param>
        /// <returns>The encrypted data</returns>
        /// <exception cref="ArgumentException"></exception>
        public static byte[] EncryptText(this VirgilCard recipient, string text)
        {
            if (string.IsNullOrWhiteSpace(text))
            {
                throw new ArgumentException(Localization.ExceptionArgumentIsNullOrWhitespace, nameof(text));
            }

            return(recipient.Encrypt(Encoding.UTF8.GetBytes(text)));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Verifies that a digital signature is valid for specified text.
        /// </summary>
        /// <param name="recipient">The <see cref="VirgilCard"/> recipient.</param>
        /// <param name="text">The text.</param>
        /// <param name="signature">The signature.</param>
        /// <returns><c>true</c> if the signature is valid; otherwise, <c>false</c>.</returns>
        /// <exception cref="ArgumentException"></exception>
        public static bool VerifyText(this VirgilCard recipient, string text, byte[] signature)
        {
            if (string.IsNullOrWhiteSpace(text))
            {
                throw new ArgumentException(Localization.ExceptionArgumentIsNullOrWhitespace, nameof(text));
            }

            return(Verify(recipient, Encoding.UTF8.GetBytes(text), signature));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Decrypts and verifies the data.
        /// </summary>
        /// <param name="cipherData">The data to be decrypted.</param>
        /// <param name="signer">The signer's <see cref="VirgilCard"/>.</param>
        /// <returns>The decrypted data, which is the original plain text before encryption.</returns>
        /// <exception cref="ArgumentNullException"></exception>
        public byte[] DecryptThenVerify(byte[] cipherData, VirgilCard signer)
        {
            var crypto    = VirgilConfig.GetService <Crypto>();
            var publicKey = crypto.ImportPublicKey(signer.PublicKey);

            var cipherdata = crypto.DecryptThenVerify(cipherData, this.KeyPair.PrivateKey, publicKey);

            return(cipherdata);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Verifies that a digital signature is valid for specified text.
        /// </summary>
        /// <param name="recipient">The <see cref="VirgilCard"/> recipient.</param>
        /// <param name="data">The text.</param>
        /// <param name="signature">The signature.</param>
        /// <returns><c>true</c> if the signature is valid; otherwise, <c>false</c>.</returns>
        /// <exception cref="ArgumentException"></exception>
        public static bool Verify(this VirgilCard recipient, byte[] data, byte[] signature)
        {
            if (recipient == null)
            {
                throw new ArgumentNullException(nameof(recipient));
            }

            var crypto    = VirgilConfig.GetService <Crypto>();
            var publicKey = crypto.ImportPublicKey(recipient.PublicKey);
            var isValid   = crypto.Verify(data, signature, publicKey);

            return(isValid);
        }