예제 #1
0
        internal GenericHashResult ComputeHash(Enums.HashAlgorithm hashAlgorithm, byte[] bytesToComputeHash,
                                               int offset = 0, int count = 0)
        {
            if (bytesToComputeHash == null || bytesToComputeHash.Length <= 0)
            {
                return(new GenericHashResult()
                {
                    Success = false,
                    Message = MessageDictionary.Instance["Hash.InputRequired"]
                });
            }

            GenericHashResult result = null;

            try
            {
                using (var hashAlg = (HashAlgorithm)CryptoConfig.CreateFromName(hashAlgorithm.ToString()))
                {
                    //offset = (offset == 0 ? 0 : offset);
                    count = (count == 0 ? bytesToComputeHash.Length : count);

                    byte[] hash = hashAlg.ComputeHash(bytesToComputeHash, offset, count);

                    result = new GenericHashResult()
                    {
                        Success    = true,
                        Message    = MessageDictionary.Instance["Hash.ComputeSuccess"],
                        HashBytes  = hash,
                        HashString = Encoding.Hexadecimal.ToHexString(hash)
                    };
                }
            }
            catch (Exception ex)
            {
                return(new GenericHashResult()
                {
                    Success = false,
                    Message = ex.ToString()
                });
            }

            return(result);
        }
예제 #2
0
        internal GenericHashResult ComputeFileHash(Enums.HashAlgorithm hashAlgorithm, string filePathToComputeHash,
                                                   long offset = 0, long count = 0)
        {
            if (!File.Exists(filePathToComputeHash))
            {
                return(new GenericHashResult()
                {
                    Success = false,
                    Message = $"{MessageDictionary.Instance["Common.FileNotFound"]} \"{filePathToComputeHash}\"."
                });
            }

            GenericHashResult result = null;

            try
            {
                byte[] hash = null;

                using (var fStream = new FileStream(filePathToComputeHash, FileMode.Open, FileAccess.Read, FileShare.None))
                {
                    //offset = (offset == 0 ? 0 : offset);
                    count            = (count == 0 ? fStream.Length : count);
                    fStream.Position = offset;
                    byte[] buffer = new byte[(1024 * 4)];
                    long   amount = (count - offset);

                    using (var hashAlg = (HashAlgorithm)CryptoConfig.CreateFromName(hashAlgorithm.ToString()))
                    {
                        int percentageDone = 0;

                        while (amount > 0)
                        {
                            int bytesRead = fStream.Read(buffer, 0, (int)Math.Min(buffer.Length, amount));

                            if (bytesRead > 0)
                            {
                                amount -= bytesRead;

                                if (amount > 0)
                                {
                                    hashAlg.TransformBlock(buffer, 0, bytesRead, buffer, 0);
                                }
                                else
                                {
                                    hashAlg.TransformFinalBlock(buffer, 0, bytesRead);
                                }

                                var tmpPercentageDone = (int)(fStream.Position * 100 / count);

                                if (tmpPercentageDone != percentageDone)
                                {
                                    percentageDone = tmpPercentageDone;

                                    RaiseOnHashProgress(percentageDone, (percentageDone != 100 ? $"Computing hash ({percentageDone}%)..." : $"Hash computed ({percentageDone}%)."));
                                }
                            }
                            else
                            {
                                throw new InvalidOperationException();
                            }
                        }

                        hash = hashAlg.Hash;
                    }
                }

                result = new GenericHashResult()
                {
                    Success    = true,
                    Message    = MessageDictionary.Instance["Hash.ComputeSuccess"],
                    HashString = Encoding.Hexadecimal.ToHexString(hash),
                    HashBytes  = hash
                };
            }
            catch (Exception ex)
            {
                result = new GenericHashResult()
                {
                    Success = false,
                    Message = ex.ToString()
                };
            }

            return(result);
        }
예제 #3
0
        internal GenericHashResult ComputeHash(Enums.HashAlgorithm hashAlgorithm, byte[] bytesToComputeHash,
                                               int offset = 0, int count = 0)
        {
            if (bytesToComputeHash == null || bytesToComputeHash.Length <= 0)
            {
                return(new GenericHashResult()
                {
                    Success = false,
                    Message = MessageDictionary.Instance["Hash.InputRequired"]
                });
            }

            GenericHashResult result = null;

            try
            {
                HashAlgorithm hashAlg = null;

#if CORERT
                switch (hashAlgorithm)
                {
                case Enums.HashAlgorithm.MD5:
                    hashAlg = MD5.Create();
                    break;

                case Enums.HashAlgorithm.SHA1:
                    hashAlg = SHA1.Create();
                    break;

                case Enums.HashAlgorithm.SHA256:
                    hashAlg = SHA256.Create();
                    break;

                case Enums.HashAlgorithm.SHA384:
                    hashAlg = SHA384.Create();
                    break;

                case Enums.HashAlgorithm.SHA512:
                    hashAlg = SHA512.Create();
                    break;

                case Enums.HashAlgorithm.BCrypt:
                default:
                    break;
                }
#else
                hashAlg = (HashAlgorithm)CryptoConfig.CreateFromName(hashAlgorithm.ToString());
#endif

                using (hashAlg)
                {
                    //offset = (offset == 0 ? 0 : offset);
                    count = (count == 0 ? bytesToComputeHash.Length : count);

                    byte[] hash = hashAlg.ComputeHash(bytesToComputeHash, offset, count);

                    result = new GenericHashResult()
                    {
                        Success    = true,
                        Message    = MessageDictionary.Instance["Hash.ComputeSuccess"],
                        HashBytes  = hash,
                        HashString = Encoding.HighPerformanceHexadecimal.ToHexString(hash)
                    };
                }
            }
            catch (Exception ex)
            {
                return(new GenericHashResult()
                {
                    Success = false,
                    Message = ex.ToString()
                });
            }

            return(result);
        }
예제 #4
0
        internal GenericHashResult ComputeFileHash(Enums.HashAlgorithm hashAlgorithm, string filePathToComputeHash,
                                                   long offset = 0, long count = 0)
        {
            if (!File.Exists(filePathToComputeHash))
            {
                return(new GenericHashResult()
                {
                    Success = false,
                    Message = $"{MessageStrings.Common_FileNotFound} \"{filePathToComputeHash}\"."
                });
            }

            GenericHashResult result  = null;
            HashAlgorithm     hashAlg = null;

#if CORERT
            switch (hashAlgorithm)
            {
            case Enums.HashAlgorithm.MD5:
                hashAlg = MD5.Create();
                break;

            case Enums.HashAlgorithm.SHA1:
                hashAlg = SHA1.Create();
                break;

            case Enums.HashAlgorithm.SHA256:
                hashAlg = SHA256.Create();
                break;

            case Enums.HashAlgorithm.SHA384:
                hashAlg = SHA384.Create();
                break;

            case Enums.HashAlgorithm.SHA512:
                hashAlg = SHA512.Create();
                break;

            case Enums.HashAlgorithm.BCrypt:
            default:
                break;
            }
#else
            hashAlg = (HashAlgorithm)CryptoConfig.CreateFromName(hashAlgorithm.ToString());
#endif

            try
            {
                byte[] hash = null;

                using (var fStream = new FileStream(filePathToComputeHash, FileMode.Open, FileAccess.Read, FileShare.None))
                {
                    //offset = (offset == 0 ? 0 : offset);
                    count            = (count == 0 ? fStream.Length : count);
                    fStream.Position = offset;
                    var buffer = new byte[(1024 * 4)];
                    var amount = (count - offset);

                    using (hashAlg)
                    {
                        var percentageDone = 0;

                        while (amount > 0)
                        {
                            var bytesRead = fStream.Read(buffer, 0, (int)Math.Min(buffer.Length, amount));

                            if (bytesRead > 0)
                            {
                                amount -= bytesRead;

                                if (amount > 0)
                                {
                                    hashAlg.TransformBlock(buffer, 0, bytesRead, buffer, 0);
                                }
                                else
                                {
                                    hashAlg.TransformFinalBlock(buffer, 0, bytesRead);
                                }

                                var tmpPercentageDone = (int)(fStream.Position * 100 / count);

                                if (tmpPercentageDone != percentageDone)
                                {
                                    percentageDone = tmpPercentageDone;

                                    RaiseOnHashProgress(percentageDone, (percentageDone != 100 ? $"Computing hash ({percentageDone}%)..." : $"Hash computed ({percentageDone}%)."));
                                }
                            }
                            else
                            {
                                throw new InvalidOperationException();
                            }
                        }

                        hash = hashAlg.Hash;
                    }
                }

                result = new GenericHashResult()
                {
                    Success    = true,
                    Message    = MessageStrings.Hash_ComputeSuccess,
                    HashString = Encoding.HighPerformanceHexadecimal.ToHexString(hash),
                    HashBytes  = hash
                };
            }
            catch (Exception ex)
            {
                result = new GenericHashResult()
                {
                    Success = false,
                    Message = ex.ToString()
                };
            }

            return(result);
        }