Exemplo n.º 1
0
        /// <summary>
        /// Computes the hash of specified data.
        /// </summary>
        public override byte[] ComputeHash(byte[] data, HashAlgorithm algorithm)
        {
            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }

            VirgilHash hasher;

            switch (algorithm)
            {
            case HashAlgorithm.MD5:
                hasher = new VirgilHash(VirgilHash.Algorithm.MD5);
                break;

            case HashAlgorithm.SHA1:
                hasher = new VirgilHash(VirgilHash.Algorithm.SHA1);
                break;

            case HashAlgorithm.SHA224:
                hasher = new VirgilHash(VirgilHash.Algorithm.SHA224);
                break;

            case HashAlgorithm.SHA256:
                hasher = new VirgilHash(VirgilHash.Algorithm.SHA256);
                break;

            case HashAlgorithm.SHA384:
                hasher = new VirgilHash(VirgilHash.Algorithm.SHA384);
                break;

            case HashAlgorithm.SHA512:
                hasher = new VirgilHash(VirgilHash.Algorithm.SHA512);
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(algorithm), algorithm, null);
            }

            try
            {
                using (hasher)
                {
                    return(hasher.Hash(data));
                }
            }
            catch (ApplicationException ex)
            {
                throw new CryptoException(ex.Message);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Calculates the fingerprint.
        /// </summary>
        public override Fingerprint CalculateFingerprint(byte[] content)
        {
            if (content == null)
            {
                throw new ArgumentNullException(nameof(content));
            }

            try
            {
                using (var sha256 = new VirgilHash(VirgilHash.Algorithm.SHA256))
                {
                    var hash = sha256.Hash(content);
                    return(new Fingerprint(hash));
                }
            }
            catch (Exception ex)
            {
                throw new CryptoException(ex.Message);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Calculates the fingerprint.
        /// </summary>
        /// <param name="payload"> original data bytes to be hashed.</param>
        /// <returns>The hash value.</returns>
        public byte[] GenerateHash(byte[] payload)
        {
            if (payload == null)
            {
                throw new ArgumentNullException("payload");
            }

            try
            {
                using (VirgilHash sha = new VirgilHash(
                           UseSHA256Fingerprints ? VirgilHash.Algorithm.SHA256 : VirgilHash.Algorithm.SHA512))
                {
                    byte[] hash = sha.Hash(payload);
                    return(hash);
                }
            }
            catch (Exception ex)
            {
                throw new VirgilCryptoException(ex.Message);
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Computes the hash of specified data.
        /// </summary>
        public override byte[] ComputeHash(byte[] data, HashAlgorithm algorithm)
        {
            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }

            var virgilHashAlg = (VirgilHash.Algorithm)algorithm;
            var hasher        = new VirgilHash(virgilHashAlg);

            try
            {
                using (hasher)
                {
                    return(hasher.Hash(data));
                }
            }
            catch (Exception ex)
            {
                throw new CryptoException(ex.Message);
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Computes the hash of the specified data and the specified <see cref="HashAlgorithm"/>.
        /// </summary>
        /// <param name="data"> original data bytes to be hashed.</param>
        /// <param name="algorithm"> the hash algorithm.</param>
        /// <returns>The value returned by execution of the hashing algorithm.</returns>
        public byte[] GenerateHash(byte[] data, HashAlgorithm algorithm)
        {
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }

            VirgilHash.Algorithm virgilHashAlg = (VirgilHash.Algorithm)algorithm;
            VirgilHash           hasher        = new VirgilHash(virgilHashAlg);

            try
            {
                using (hasher)
                {
                    return(hasher.Hash(data));
                }
            }
            catch (Exception ex)
            {
                throw new VirgilCryptoException(ex.Message);
            }
        }