Пример #1
0
        /// <summary>
        /// Get the hash representing the string
        /// </summary>
        /// <param name="target">The bytes array to hash</param>
        /// <param name="algorithm">The hash algortihm to use</param>
        /// <returns>The hash value</returns>
        public static string GetHash(this byte[] target, HashAlgorithms algorithm)
        {
            if (algorithm == HashAlgorithms.Md5)
            {
                var md5    = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Md5);
                var buffer = CryptographicBuffer.CreateFromByteArray(target);
                var hashed = md5.HashData(buffer);
                return(CryptographicBuffer.EncodeToHexString(hashed));
            }
            else if (algorithm == HashAlgorithms.Sha512 || algorithm == HashAlgorithms.Sha256)
            {
                var sha    = HashAlgorithmProvider.OpenAlgorithm(algorithm == HashAlgorithms.Sha512 ? HashAlgorithmNames.Sha256 : HashAlgorithmNames.Sha512);
                var buffer = CryptographicBuffer.CreateFromByteArray(target);

                var    hashed = sha.HashData(buffer);
                byte[] bytes;

                CryptographicBuffer.CopyToByteArray(hashed, out bytes);
                return(Convert.ToBase64String(bytes));
            }
            else
            {
                throw new NotSupportedException("Unsupported hash algorithm");
            }
        }
Пример #2
0
        private static string GetFileHash(HashAlgorithms hashAlgorithmSelected, FileInfo inputFile)
        {
            HashAlgorithm hashAlgorithm;

            switch (hashAlgorithmSelected)
            {
            case HashAlgorithms.md5:
                hashAlgorithm = MD5.Create();
                break;

            case HashAlgorithms.sha1:
                hashAlgorithm = SHA1.Create();
                break;

            case HashAlgorithms.sha384:
                hashAlgorithm = SHA384.Create();
                break;

            case HashAlgorithms.sha512:
                hashAlgorithm = SHA512.Create();
                break;

            default:
                hashAlgorithm = SHA256.Create();
                break;
            }
            try
            {
                // Create a fileStream for the file.
                FileStream fileStream = inputFile.Open(FileMode.Open, FileAccess.Read);
                // Be sure it's positioned to the beginning of the stream.
                fileStream.Position = 0;
                // Compute the hash of the fileStream.
                byte[] hashValue = hashAlgorithm.ComputeHash(fileStream);
                // Create a new Stringbuilder to collect the bytes
                // and create a string.
                StringBuilder sBuilder = new StringBuilder();

                // Loop through each byte of the hashed data
                // and format each one as a hexadecimal string.
                for (int i = 0; i < hashValue.Length; i++)
                {
                    sBuilder.Append(hashValue[i].ToString("x2"));
                }

                // Close the file.
                fileStream.Close();

                // Return the hexadecimal string.
                return(sBuilder.ToString());
            }
            catch (Exception e)
            {
                return(e.GetType().Name);
            }
            finally
            {
                hashAlgorithm.Dispose();
            }
        }
Пример #3
0
        public static byte[] Create(string message, HashAlgorithms hashAlgorithm, X509Certificate2 certificate)
        {
            RSACryptoServiceProvider csp = (RSACryptoServiceProvider)certificate.PrivateKey;

            if (csp == null)
            {
                throw new Exception("Valid certificate was not found");
            }

            UnicodeEncoding encoding = new UnicodeEncoding();

            byte[] data = encoding.GetBytes(message); //current message encripted in bytes
            byte[] hash = null;                       //hash result

            if (hashAlgorithm.Equals(HashAlgorithms.SHA1))
            {
                SHA1Managed sha1 = new SHA1Managed();
                hash = sha1.ComputeHash(data);
            }
            else if (hashAlgorithm.Equals(HashAlgorithms.SHA256))
            {
                SHA256Managed sha256 = new SHA256Managed();
                hash = sha256.ComputeHash(data);
            }

            return(csp.SignHash(hash, CryptoConfig.MapNameToOID(hashAlgorithm.ToString())));
        }
        private static HashAlgorithm GetHashAlgorithm(HashAlgorithms hashAlgorithm)
        {
            switch (hashAlgorithm)
            {
#if NETFRAMEWORK
            case HashAlgorithms.MD5:
                return(new MD5Cng());

            case HashAlgorithms.RIPEMD160:
                return(new RIPEMD160Managed());

            case HashAlgorithms.SHA1:
                return(new SHA1Cng());

            case HashAlgorithms.SHA256:
                return(new SHA256Cng());

            case HashAlgorithms.SHA384:
                return(new SHA384Cng());

            case HashAlgorithms.SHA512:
                return(new SHA512Cng());
#endif

            default:
                throw new InvalidOperationException(Resources.UnsupportedHashAlgorithmException);
            }
        }
Пример #5
0
        //=================================================================


        //=================================================
        //
        //       CRYPTOGRAPHY FACTORIES
        //
        //=================================================


        //---------------------------------------------------------------------
        public static HashAlgorithm Create_HashAlgorithm(HashAlgorithms HashAlgorithm_in)
        {
            switch (HashAlgorithm_in)
            {
            case HashAlgorithms.None:
                return(null);

            case HashAlgorithms.MD5_128:
                return(new MD5CryptoServiceProvider());

            case HashAlgorithms.SHA1_160:
                return(new SHA1CryptoServiceProvider());

            case HashAlgorithms.SHA_256:
                return(new SHA256Managed());

            case HashAlgorithms.SHA_384:
                return(new SHA384Managed());

            case HashAlgorithms.SHA_512:
                return(new SHA512Managed());

            default:
                return(null);
            }
        }
Пример #6
0
        public AsyncHasher(HashAlgorithms algorithm)
        {
            switch (algorithm)
            {
            case HashAlgorithms.MD5:
                _hashAlgorithm = MD5.Create();
                break;

            case HashAlgorithms.SHA1:
                _hashAlgorithm = SHA1.Create();
                break;

            case HashAlgorithms.SHA256:
                _hashAlgorithm = SHA256.Create();
                break;

            case HashAlgorithms.SHA512:
                _hashAlgorithm = SHA512.Create();
                break;

            case HashAlgorithms.CRC32:
                _hashAlgorithm = new Crc32();
                break;

            case HashAlgorithms.CRC64:
                _hashAlgorithm = new Crc64Iso();
                break;
            }
        }
Пример #7
0
        /// <summary>
        /// 指定哈希算法,哈希指定的。
        /// </summary>
        /// <param name="alog">哈希算法。</param>
        /// <param name="bytes">要计算其哈希代码的输入。</param>
        /// <returns>计算所得的哈希代码。</returns>
        public static byte[] Crypto(HashAlgorithms alog, byte[] bytes)
        {
            HashAlgorithm algorithm;

            switch (alog)
            {
            case HashAlgorithms.SHA1:
                algorithm = CreateSHA1();
                break;

            case HashAlgorithms.SHA256:
                algorithm = CreateSHA256();
                break;

            case HashAlgorithms.SHA384:
                algorithm = CreateSHA384();
                break;

            case HashAlgorithms.SHA512:
                algorithm = CreateSHA512();
                break;

            case HashAlgorithms.MD5:
                algorithm = CreateMD5();
                break;

            default: throw new NotSupportedException();
            }
            using (algorithm)
            {
                return(algorithm.ComputeHash(bytes));
            }
        }
Пример #8
0
        public static CalculateHash Create(HashAlgorithms algorithm = HashAlgorithms.SHA256)
        {
            HashAlgorithmName algorithmName;

            switch (algorithm)
            {
            case HashAlgorithms.MD5:
                algorithmName = HashAlgorithmName.MD5;
                break;

            case HashAlgorithms.SHA1:
                algorithmName = HashAlgorithmName.SHA1;
                break;

            default:
            case HashAlgorithms.SHA256:
                algorithmName = HashAlgorithmName.SHA256;
                break;

            case HashAlgorithms.SHA384:
                algorithmName = HashAlgorithmName.SHA384;
                break;

            case HashAlgorithms.SHA512:
                algorithmName = HashAlgorithmName.SHA512;
                break;
            }
            return(new CalculateHash(algorithmName));
        }
Пример #9
0
        /// <summary>
        /// Create a new wireless communication module.
        /// </summary>
        /// <param name="HashAlgorithm">The used hash algorithm.</param>
        /// <param name="IssuerNameHash">The hashed value of the issuer distinguished name (DN).</param>
        /// <param name="IssuerKeyHash">The hashed value of the issuers public key.</param>
        /// <param name="SerialNumber">The serial number of the certificate to verify.</param>
        /// <param name="ResponderURL">The case-insensitive responder URL.</param>
        /// <param name="CustomData">An optional custom data object to allow to store any kind of customer specific data.</param>
        public OCSPRequestData(HashAlgorithms HashAlgorithm,
                               String IssuerNameHash,
                               String IssuerKeyHash,
                               String SerialNumber,
                               String ResponderURL,
                               CustomData CustomData = null)
        {
            this.HashAlgorithm  = HashAlgorithm;
            this.IssuerNameHash = IssuerNameHash?.Trim();
            this.IssuerKeyHash  = IssuerKeyHash?.Trim();
            this.SerialNumber   = SerialNumber?.Trim();
            this.ResponderURL   = ResponderURL?.Trim();
            this.CustomData     = CustomData;

            if (this.IssuerNameHash.IsNullOrEmpty())
            {
                throw new ArgumentNullException(nameof(IssuerNameHash), "The given issuer name hash must not be null or empty!");
            }

            if (this.IssuerKeyHash.IsNullOrEmpty())
            {
                throw new ArgumentNullException(nameof(IssuerKeyHash), "The given issuer key hash must not be null or empty!");
            }

            if (this.SerialNumber.IsNullOrEmpty())
            {
                throw new ArgumentNullException(nameof(SerialNumber), "The given serial number must not be null or empty!");
            }

            if (this.ResponderURL.IsNullOrEmpty())
            {
                throw new ArgumentNullException(nameof(ResponderURL), "The given responder URL must not be null or empty!");
            }
        }
Пример #10
0
        public static string HashInputString(string input, HashAlgorithms alg = HashAlgorithms.MD5)
        {
            HashAlgorithm hashAlgorithm = null;

            switch (alg)
            {
            case HashAlgorithms.MD5:
                hashAlgorithm = MD5.Create();
                break;

            case HashAlgorithms.SHA1:
                hashAlgorithm = SHA1.Create();
                break;

            case HashAlgorithms.SHA256:
                hashAlgorithm = SHA256.Create();
                break;

            case HashAlgorithms.SHA512:
                hashAlgorithm = SHA512.Create();
                break;
            }
            byte[] data   = Encoding.UTF8.GetBytes(input);
            byte[] output = hashAlgorithm.ComputeHash(data);
            return(MakeHashString(output));
        }
Пример #11
0
    public static byte[] GetHashBytes(this object obj, HashAlgorithms algoName, int saltLength)
    {
        byte[] hashBytes = obj.GetHashBytes(algoName);

        byte[] salt = new byte[saltLength];

        RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();

        rng.GetBytes(salt);

        byte[] hashAndSalt = new byte[hashBytes.Length + salt.Length];

        for (int i = 0; i < hashAndSalt.Length; i++)
        {
            if (i < hashBytes.Length)
            {
                hashAndSalt[i] = hashBytes[i];
            }
            else
            {
                int saltIndex = i - hashBytes.Length;
                hashAndSalt[i] = salt[saltIndex];
            }
        }

        return(hashAndSalt);
    }
Пример #12
0
    public static byte[] GetHashBytes(this object obj, HashAlgorithms algoName)
    {
        HashAlgorithm algo;

        switch (algoName)
        {
        case HashAlgorithms.SHA512:
            algo = new SHA512Managed();
            break;

        case HashAlgorithms.SHA256:
            algo = new SHA256Managed();
            break;

        case HashAlgorithms.SHA384:
            algo = new SHA384Managed();
            break;

        default:
            algo = new SHA512Managed();
            break;
        }
        byte[] hashBytes;

        if (obj is byte[])
        {
            hashBytes = obj as byte[];
        }
        else
        {
            byte[] bytes = obj.ToByteArray();
            hashBytes = algo.ComputeHash(bytes);
        }
        return(hashBytes);
    }
Пример #13
0
        //---------------------------------------------------------------------
        public static byte[] Create_CryptographicIV(HashAlgorithms HashAlgorithm_in, int IVBitSize_in, byte[] PasswordBytes_in)
        {
            // Derive an IV by XOR-ing the Key with a predefined mask
            byte[] KeyMask_256 =
            {
                30,   2, 141,  78,  82, 33,  76, 248,  56, 182,
                254, 34,  59, 188, 225,  1, 180, 209,  15, 220,
                52,  69,   2, 188, 235, 27, 175, 116, 234, 127,
                28, 179
            };
            byte[] KeyBase     = new byte[PasswordBytes_in.Length];
            int    ndxPassword = 0;
            int    ndxMask     = 0;

            for (ndxPassword = PasswordBytes_in.GetLowerBound(0); ndxPassword <= PasswordBytes_in.GetUpperBound(0); ndxPassword++)
            {
                KeyBase[ndxPassword] = (byte)(PasswordBytes_in[ndxPassword] ^ KeyMask_256[ndxMask]);
                ndxMask += 1;
                if ((ndxMask == KeyMask_256.Length))
                {
                    ndxMask = 0;
                }
            }
            return(Create_CryptographicKey(HashAlgorithm_in, IVBitSize_in, KeyBase));
        }
Пример #14
0
        private static HashAlgorithm GetHashAlgorithm(HashAlgorithms hashAlgorithm)
        {
            switch (hashAlgorithm)
            {
            case HashAlgorithms.MD5:
                return(new MD5Cng());

            case HashAlgorithms.RIPEMD160:
                return(new RIPEMD160Managed());

            case HashAlgorithms.SHA1:
                return(new SHA1Cng());

            case HashAlgorithms.SHA256:
                return(new SHA256Cng());

            case HashAlgorithms.SHA384:
                return(new SHA384Cng());

            case HashAlgorithms.SHA512:
                return(new SHA512Cng());

            default:
                throw new InvalidOperationException();
            }
        }
Пример #15
0
        /// <summary>
        /// Term order. Comparision is pairwise using the term ordering of <see cref="HashAlgorithms.CompareValues" />.
        /// </summary>
        /// <param name="obj"></param>
        /// <returns>-1 if less than, 0 if equal, 1 if greater than</returns>
        /// <exception cref="System.ArgumentException">Thrown if <paramref name="obj"/> is not of the same type as this triple.</exception>
        /// <seealso cref="HashAlgorithms.CompareValues" />
        public int CompareTo(object obj)
        {
            if ((object)obj == null)
            {
                return(1);
            }
            if (!(obj is Triple <T, S, R>))
            {
                throw new ArgumentException("Incomparable argument");
            }
            Triple <T, S, R> other = (Triple <T, S, R>)obj;

            int f1 = HashAlgorithms.CompareValues(this.first, other.first);

            if (f1 != 0)
            {
                return(f1);
            }
            int f2 = HashAlgorithms.CompareValues(this.second, other.second);

            if (f2 != 0)
            {
                return(f2);
            }
            int f3 = HashAlgorithms.CompareValues(this.third, other.third);

            //^ assert f3 == 0 ==> Object.Equals(this, other);
            return(f3);
        }
Пример #16
0
        /*
         * Supported Hash Algorithms
         * ---------------------------
         * MD5 (MD5-128)
         * SHA0 (SHA0-160)
         * SHA1 (SHA1-160)
         * SHA2-224 (SHA-224)
         * SHA2-256 (SHA-256)
         * SHA2-384 (SHA-384)
         * SHA2-512 (SHA-512)
         * SHA2-512/224 (SHA-512/224)
         * SHA2-512/256 (SHA-512/256)
         * SHA3-224
         * SHA3-256
         * SHA3-384
         * SHA3-512
         */

        #region Methods

        public string Hash(string message, HashAlgorithms algorithm, Encoding encoding)
        {
            HashAlgorithm hashAlgorithm;

            switch (algorithm)
            {
            case HashAlgorithms.MD5:
                hashAlgorithm = new MD5CryptoServiceProvider();
                break;

            case HashAlgorithms.SHA0:
                throw new NotSupportedException("Not supported hash algorithm :(");

            case HashAlgorithms.SHA1:
                hashAlgorithm = new SHA1Managed();
                break;

            case HashAlgorithms.SHA2_224:
                throw new NotSupportedException("Not supported hash algorithm :(");

            case HashAlgorithms.SHA2_256:
                hashAlgorithm = new SHA256Managed();
                break;

            case HashAlgorithms.SHA2_384:
                hashAlgorithm = new SHA384Managed();
                break;

            case HashAlgorithms.SHA2_512:
                hashAlgorithm = new SHA512Managed();
                break;

            case HashAlgorithms.SHA2_512_224:
                throw new NotSupportedException("Not supported hash algorithm :(");

            case HashAlgorithms.SHA2_512_256:
                throw new NotSupportedException("Not supported hash algorithm :(");

            case HashAlgorithms.SHA3_224:
                throw new NotSupportedException("Not supported hash algorithm :(");

            case HashAlgorithms.SHA3_256:
                throw new NotSupportedException("Not supported hash algorithm :(");

            case HashAlgorithms.SHA3_384:
                throw new NotSupportedException("Not supported hash algorithm :(");

            case HashAlgorithms.SHA3_512:
                throw new NotSupportedException("Not supported hash algorithm :(");

            default:
                throw new NotSupportedException("Not supported hash algorithm :(");
            }

            byte[] bytes = hashAlgorithm.ComputeHash(encoding.GetBytes(message));
            string hash  = BitConverter.ToString(bytes).Replace("-", string.Empty).ToLower();

            return(hash);
        }
        /// <summary>
        /// Get the hash representing the string
        /// </summary>
        /// <param name="target">The string to hash</param>
        /// <param name="algorithm">The hash algortihm to use</param>
        /// <returns>The hash value</returns>
        public static string GetHash(this string target, HashAlgorithms algorithm)
        {
#if NETCORE
            return(UTF8Encoding.UTF8.GetBytes(target).GetHash(algorithm));
#else
            return(UTF8Encoding.UTF8.GetBytes(target).GetHash(algorithm));
#endif
        }
Пример #18
0
 internal static string HashBytes(byte[] val, HashAlgorithms hashAlgorithm)
 {
     using (HashAlgorithm hasher = Crypt.Hash.GetHashAlgorithm(hashAlgorithm))
     {
         byte[] hash = hasher.ComputeHash(val, 0, val.Length);
         return(Convert.ToBase64String(hash));
     }
 }
Пример #19
0
        /// <summary>
        /// Get the first checksum algorithm mutually supported by both servers.
        /// </summary>
        private FtpHashAlgorithm GetFirstMutualChecksum(FtpClient destination)
        {
            // special handling for HASH command which is a meta-command supporting all hash types
            if (HasFeature(FtpCapability.HASH) && destination.HasFeature(FtpCapability.HASH))
            {
                if (HashAlgorithms.HasFlag(FtpHashAlgorithm.MD5) && destination.HashAlgorithms.HasFlag(FtpHashAlgorithm.MD5))
                {
                    return(FtpHashAlgorithm.MD5);
                }
                if (HashAlgorithms.HasFlag(FtpHashAlgorithm.SHA1) && destination.HashAlgorithms.HasFlag(FtpHashAlgorithm.SHA1))
                {
                    return(FtpHashAlgorithm.SHA1);
                }
                if (HashAlgorithms.HasFlag(FtpHashAlgorithm.SHA256) && destination.HashAlgorithms.HasFlag(FtpHashAlgorithm.SHA256))
                {
                    return(FtpHashAlgorithm.SHA256);
                }
                if (HashAlgorithms.HasFlag(FtpHashAlgorithm.SHA512) && destination.HashAlgorithms.HasFlag(FtpHashAlgorithm.SHA512))
                {
                    return(FtpHashAlgorithm.SHA512);
                }
                if (HashAlgorithms.HasFlag(FtpHashAlgorithm.CRC) && destination.HashAlgorithms.HasFlag(FtpHashAlgorithm.CRC))
                {
                    return(FtpHashAlgorithm.CRC);
                }
            }

            // handling for non-standard specific hashing commands
            if (HasFeature(FtpCapability.MD5) && destination.HasFeature(FtpCapability.MD5))
            {
                return(FtpHashAlgorithm.MD5);
            }
            if (HasFeature(FtpCapability.XMD5) && destination.HasFeature(FtpCapability.XMD5))
            {
                return(FtpHashAlgorithm.MD5);
            }
            if (HasFeature(FtpCapability.MMD5) && destination.HasFeature(FtpCapability.MMD5))
            {
                return(FtpHashAlgorithm.MD5);
            }
            if (HasFeature(FtpCapability.XSHA1) && destination.HasFeature(FtpCapability.XSHA1))
            {
                return(FtpHashAlgorithm.SHA1);
            }
            if (HasFeature(FtpCapability.XSHA256) && destination.HasFeature(FtpCapability.XSHA256))
            {
                return(FtpHashAlgorithm.SHA256);
            }
            if (HasFeature(FtpCapability.XSHA512) && destination.HasFeature(FtpCapability.XSHA512))
            {
                return(FtpHashAlgorithm.SHA512);
            }
            if (HasFeature(FtpCapability.XCRC) && destination.HasFeature(FtpCapability.XCRC))
            {
                return(FtpHashAlgorithm.CRC);
            }
            return(FtpHashAlgorithm.NONE);
        }
Пример #20
0
        byte[] ComputeHash(string str, HashAlgorithms algorithm)
        {
            HashAlgorithm hasher = GetHasher(algorithm);

            byte[] buffer = Encoding.UTF8.GetBytes(str);
            byte[] hash   = hasher.ComputeHash(buffer);
            //hasher.Dispose();
            return(hash);
        }
Пример #21
0
        public HashProvider(HashAlgorithms HashAlgorithm)
        {
            switch (HashAlgorithm)
            {
            case HashAlgorithms.MD5: _Hash = MD5.Create(); break;

            case HashAlgorithms.SHA512: _Hash = SHA512.Create(); break;

            case HashAlgorithms.SHA256: _Hash = SHA256.Create(); break;
            }
        }
Пример #22
0
        public static bool VerifyHash(string plainText, HashAlgorithms hashAlgorithm, string hashValue)
        {
            // Base64-encoded hash
            byte[] hashWithSaltBytes = Convert.FromBase64String(hashValue);

            // Hash without Salt
            int hashSizeInBits, hashSizeInBytes;

            // Size of hash is based on the specified algorithm.
            switch (hashAlgorithm)
            {
            case HashAlgorithms.SHA1:
                hashSizeInBits = 160;
                break;

            case HashAlgorithms.SHA256:
                hashSizeInBits = 256;
                break;

            case HashAlgorithms.SHA384:
                hashSizeInBits = 384;
                break;

            case HashAlgorithms.SHA512:
                hashSizeInBits = 512;
                break;

            default:     // MD5
                hashSizeInBits = 128;
                break;
            }

            // Convert to bytes.
            hashSizeInBytes = hashSizeInBits / 8;

            // Verify Hash Length
            if (hashWithSaltBytes.Length < hashSizeInBytes)
            {
                return(false);
            }

            // Array to hold Salt
            byte[] saltBytes = new byte[hashWithSaltBytes.Length - hashSizeInBytes];

            // Salt to New Array
            for (int i = 0; i < saltBytes.Length; i++)
            {
                saltBytes[i] = hashWithSaltBytes[hashSizeInBytes + i];
            }

            string expectedHashString = ComputeHash(plainText, hashAlgorithm, saltBytes);

            return(hashValue == expectedHashString);
        }
Пример #23
0
        /// <summary>
        /// Computes a Hash of the Data using the specified Algorithm
        /// </summary>
        /// <param name="Data">The Data to Hash</param>
        /// <param name="Algorithm">The Hash Algorithm to use</param>
        /// <param name="Base64Encode">True to return a Base64 Encoded string (for use in Xml files).</param>
        /// <returns></returns>
        public static string Hash(string Data, HashAlgorithms Algorithm, bool Base64Encode)
        {
            // Determine the Hash Algorithm
            HashAlgorithm hashAlgorithm = null;

            switch (Algorithm)
            {
            case HashAlgorithms.MD5:
                hashAlgorithm = new MD5CryptoServiceProvider();
                break;

            case HashAlgorithms.SHA1:
                hashAlgorithm = new SHA1Managed();
                break;

            case HashAlgorithms.SHA256:
                hashAlgorithm = new SHA256Managed();
                break;

            case HashAlgorithms.SHA384:
                hashAlgorithm = new SHA384Managed();
                break;

            case HashAlgorithms.SHA512:
                hashAlgorithm = new SHA512Managed();
                break;
            }

            try
            {
                byte[] hashData = System.Text.Encoding.UTF8.GetBytes(Data);
                hashData = hashAlgorithm.ComputeHash(hashData);
                if (Base64Encode)
                {
                    return(Convert.ToBase64String(hashData));
                }
                else
                {
                    return(System.Text.Encoding.UTF8.GetString(hashData));
                }
            }
            catch
            {
                return(string.Empty);
            }
            finally
            {
                if (hashAlgorithm != null)
                {
                    hashAlgorithm.Clear();
                    hashAlgorithm = null;
                }
            }
        }
Пример #24
0
        private void ValidateHashAlgorithm(FtpHashAlgorithm algorithm)
        {
            // if NO hashing algos or commands supported, throw here
            if (!HasFeature(FtpCapability.HASH) &&
                !HasFeature(FtpCapability.MD5) &&
                !HasFeature(FtpCapability.XMD5) &&
                !HasFeature(FtpCapability.MMD5) &&
                !HasFeature(FtpCapability.XSHA1) &&
                !HasFeature(FtpCapability.XSHA256) &&
                !HasFeature(FtpCapability.XSHA512) &&
                !HasFeature(FtpCapability.XCRC))
            {
                throw new FtpHashUnsupportedException();
            }

            // only if the user has specified a certain hash algorithm
            var useFirst = (algorithm == FtpHashAlgorithm.NONE);

            if (!useFirst)
            {
                // first check if the HASH command supports the required algo
                if (HasFeature(FtpCapability.HASH) && HashAlgorithms.HasFlag(algorithm))
                {
                    // we are good
                }
                else
                {
                    // second check if the special FTP command is supported based on the algo
                    if (algorithm == FtpHashAlgorithm.MD5 && !HasFeature(FtpCapability.MD5) &&
                        !HasFeature(FtpCapability.XMD5) && !HasFeature(FtpCapability.MMD5))
                    {
                        throw new FtpHashUnsupportedException(FtpHashAlgorithm.MD5, "MD5, XMD5, MMD5");
                    }
                    if (algorithm == FtpHashAlgorithm.SHA1 && !HasFeature(FtpCapability.XSHA1))
                    {
                        throw new FtpHashUnsupportedException(FtpHashAlgorithm.SHA1, "XSHA1");
                    }
                    if (algorithm == FtpHashAlgorithm.SHA256 && !HasFeature(FtpCapability.XSHA256))
                    {
                        throw new FtpHashUnsupportedException(FtpHashAlgorithm.SHA256, "XSHA256");
                    }
                    if (algorithm == FtpHashAlgorithm.SHA512 && !HasFeature(FtpCapability.XSHA512))
                    {
                        throw new FtpHashUnsupportedException(FtpHashAlgorithm.SHA512, "XSHA512");
                    }
                    if (algorithm == FtpHashAlgorithm.CRC && !HasFeature(FtpCapability.XCRC))
                    {
                        throw new FtpHashUnsupportedException(FtpHashAlgorithm.CRC, "XCRC");
                    }

                    // we are good
                }
            }
        }
Пример #25
0
        public static string BuildHash(Info di, HashAlgorithms hashAlgorithm)
        {
            var ids      = di.GetIdentities().Select(did => did.Key).ToList();
            var features = di.GetFeatures().Select(df => df.Var).ToList();
            var forms    = di.GetDataForms().Select(form => form.GetField("FORM_TYPE").GetValue()).ToList();

            // sort everything now
            ids.Sort();
            features.Sort();
            forms.Sort();

            var sb = new StringBuilder();

            foreach (string s in ids)
            {
                sb.Append(s + "<");
            }

            foreach (string s in features)
            {
                sb.Append(s + "<");
            }

            foreach (string s in forms)
            {
                sb.Append(s + "<");
            }

            foreach (string formType in forms)
            {
                string ftype  = formType;
                var    data   = di.GetDataForms().FirstOrDefault(f => f.GetField("FORM_TYPE").GetValue() == ftype);
                var    fields = (
                    from field in data.GetFields()
                    where field.Var != "FORM_TYPE"
                    orderby field.Var
                    select field.Var
                    ).ToList();

                foreach (string field in fields)
                {
                    sb.Append(field + "<");
                    foreach (string val in data.GetField(field).GetValues())
                    {
                        sb.Append(val + "<");
                    }
                }
            }

            string ret = HashBytes(Encoding.UTF8.GetBytes(sb.ToString()), hashAlgorithm);

            return(ret);
        }
Пример #26
0
        public static bool IsFipsCompliant(HashAlgorithms hashAlgorithm)
        {
            switch (hashAlgorithm)
            {
            case HashAlgorithms.MD5:
            case HashAlgorithms.RIPEMD160:
                return(false);

            default:
                return(true);
            }
        }
Пример #27
0
        HashAlgorithm GetHasher(HashAlgorithms algorithm)
        {
            switch (algorithm)
            {
            case HashAlgorithms.MD5: return(MD5.Create());

            case HashAlgorithms.SHA1: return(SHA1.Create());

            case HashAlgorithms.SHA256: return(SHA256.Create());

            default: return(null);
            }
        }
        private const int PBKDF2_Iterations = 10000;            // Value recommended in literature.

        public static byte[] HashData(HashAlgorithms hashAlgorithm, byte[] inputBytes)
        {
            byte[] result;

            using (HashAlgorithm algorithm = GetHashAlgorithm(hashAlgorithm))
            {
                result = algorithm.ComputeHash(inputBytes);

                algorithm.Clear();
            }

            return(result);
        }
Пример #29
0
        void IssueComputeHash(HashAlgorithms algorithm)
        {
            lock (this.computations)
            {
                ComputationProgress computation = new ComputationProgress(algorithm);

                Thread thread = new Thread(AsyncComputeHash);
                thread.IsBackground = true;
                thread.Start(computation);

                this.computations.Add(computation);
            }
        }
        public static bool IsFipsCompliant(HashAlgorithms hashAlgorithm)
        {
            switch (hashAlgorithm)
            {
#if NETFRAMEWORK
            case HashAlgorithms.MD5:
            case HashAlgorithms.RIPEMD160:
                return(false);
#endif

            default:
                return(true);
            }
        }
 public HashAlgorithm GetHashAlgorithm(HashAlgorithms hashAlgorithm)
 {
     return HashAlgorithmProviders[hashAlgorithm].Build();
 }
Пример #32
0
        public void StartHashing(string file, HashAlgorithms method, int tb)
        {
            this.Text = "Checksum - Calculating Hash...";
            foreach (Control c in this.Controls)
            {
                if (c.GetType() != typeof(LinkLabel))
                    c.Enabled = false;
            }

            thd = new Thread(() => CalculateHash(file, method, tb));
            thd.Start();
        }
Пример #33
0
 public void CalculateHash(string file, HashAlgorithms method, int tb)
 {
     output = "";
     if (!System.IO.File.Exists(file))
         return;
     switch (method)
     {
         case HashAlgorithms.SHA1:
             output = CalculateSHA1Hash(file);
             break;
         case HashAlgorithms.SHA256:
             output = CalculateSHA256Hash(file);
             break;
         case HashAlgorithms.SHA512:
             output = CalculateSHA512Hash(file);
             break;
         case HashAlgorithms.MD5:
             output = CalculateMD5Hash(file);
             break;
     }
     EnableForm();
     if (tb == 1)
         SetText1();
     else
         SetText2();
     lastFileLocation = System.IO.Path.GetFullPath(file);
 }