コード例 #1
0
        /// <summary>
        /// Hashes the specified <paramref name="data"/>.
        /// </summary>
        /// <param name="hasher">The hasher.</param>
        /// <param name="data">The data.</param>
        /// <returns>The hash value as a byte array.</returns>
        public static byte[] Hash(
            this IHasher hasher,
            Guid data)
        {
            if (hasher == null)
            {
                throw new ArgumentNullException(nameof(hasher));
            }

            return(hasher.Hash(ToByteArray.Convert(data)));
        }
コード例 #2
0
        public static byte[] Encrypt(
            this ICipher cipher,
            ushort data)
        {
            if (cipher == null)
            {
                throw new ArgumentNullException(nameof(cipher));
            }

            return(cipher.Encrypt(ToByteArray.Convert(data)));
        }
コード例 #3
0
        /// <summary>
        /// Verifies that the passed <paramref name="hash"/> was produced from the same <paramref name="data"/>.
        /// </summary>
        /// <param name="hasher">The hasher.</param>
        /// <param name="data">The data, which hash must be verified.</param>
        /// <param name="hash">The hash to be tested.</param>
        /// <exception cref="System.Security.Cryptography.CryptographicException">Thrown when the hash is not valid.</exception>
        public static void VerifyHash(
            this IHasher hasher,
            Guid[] data,
            byte[] hash)
        {
            if (hasher == null)
            {
                throw new ArgumentNullException(nameof(hasher));
            }
            if (data == null && hash != null)
            {
                throw new CryptographicException(Resources.InvalidHash);
            }

            hasher.VerifyHash(ToByteArray.Convert(data), hash);
        }
コード例 #4
0
        /// <summary>
        /// Verifies that the passed <paramref name="hash"/> was produced from the same <paramref name="data"/>.
        /// </summary>
        /// <param name="hasher">The hasher.</param>
        /// <param name="data">The data, which hash must be verified.</param>
        /// <param name="hash">The hash to be tested.</param>
        /// <exception cref="System.Security.Cryptography.CryptographicException">Thrown when the hash is not valid.</exception>
        public static void VerifyHash(
            this IHasher hasher,
            Guid data,
            byte[] hash)
        {
            if (hasher == null)
            {
                throw new ArgumentNullException(nameof(hasher));
            }
            if (hash == null)
            {
                throw new ArgumentNullException(nameof(hash));
            }

            hasher.VerifyHash(ToByteArray.Convert(data), hash);
        }
コード例 #5
0
        /// <summary>
        /// Verifies that the passed <paramref name="hash"/> was produced from the same <paramref name="data"/>.
        /// </summary>
        /// <param name="hasher">The hasher.</param>
        /// <param name="data">The data, which hash must be verified.</param>
        /// <param name="hash">The hash to be tested.</param>
        /// <returns><c>true</c> if the hash has a value that was produced rom the <paramref name="data"/>; <c>false</c> otherwise.</returns>
        public static bool TryVerifyHash(
            this IHasher hasher,
            DateTime[] data,
            byte[] hash)
        {
            if (hasher == null)
            {
                throw new ArgumentNullException(nameof(hasher));
            }
            if (data == null && hash != null)
            {
                throw new CryptographicException(Resources.InvalidHash);
            }

            return(hasher.TryVerifyHash(ToByteArray.Convert(data), hash));
        }
コード例 #6
0
        /// <summary>
        /// Verifies that the passed <paramref name="hash"/> was produced from the same <paramref name="data"/>.
        /// </summary>
        /// <param name="hasher">The hasher.</param>
        /// <param name="data">The data, which hash must be verified.</param>
        /// <param name="hash">The hash to be tested.</param>
        /// <returns><c>true</c> if the hash has a value that was produced rom the <paramref name="data"/>; <c>false</c> otherwise.</returns>
        public static bool TryVerifyHash(
            this IHasher hasher,
            DateTime data,
            byte[] hash)
        {
            if (hasher == null)
            {
                throw new ArgumentNullException(nameof(hasher));
            }
            if (hash == null)
            {
                throw new ArgumentNullException(nameof(hash));
            }

            return(hasher.TryVerifyHash(ToByteArray.Convert(data), hash));
        }
コード例 #7
0
        /// <summary>
        /// Encrypts the <paramref name="data"/> with the specified <paramref name="cipher"/>.
        /// </summary>
        /// <typeparam name="T">The type of the data to be encrypted.</typeparam>
        /// <param name="cipher">The cipher.</param>
        /// <param name="data">The data to be encrypted.</param>
        /// <returns>The encrypted data.</returns>
        public static byte[] EncryptNullable <T>(
            this ICipher cipher,
            T?data) where T : struct
        {
            if (cipher == null)
            {
                throw new ArgumentNullException(nameof(cipher));
            }

            if (!EncryptTypedData.ContainsKey(typeof(T)))
            {
                throw new ArgumentException("The specified data type cannot be converted.", nameof(data));
            }

            return(cipher.Encrypt(ToByteArray.ConvertNullable(data)));
        }