Esempio n. 1
0
                public void AddActual(string name, byte[] storedActualHash, HashStyle hashStyle = HashStyle.Binary)
                {
                    var item = new HashedFile(name, storedActualHash, hashStyle, storedActualHash);

                    Data.items.Add(item);
                    item.IsMatch = storedActualHash == null? (bool?)null : true;
                }
Esempio n. 2
0
 private HashedFile(string name, byte[] storedHash, HashStyle style, byte[] actualHash = null)
 {
     this.storedHash = storedHash;
     this.actualHash = actualHash;
     this.FileName   = name;
     this.IsMatch    = null;
     this.IsFound    = null;
     this.IsRelative = !Path.IsPathRooted(name);
     this.Style      = style;
 }
Esempio n. 3
0
 public void Add(string name, byte[] storedHash, HashStyle hashStyle)
 => Data.items.Add(new HashedFile(name, storedHash, hashStyle));
Esempio n. 4
0
        public async Task <string> HashFileAsync(
            HashStyle hashStyle,
            string fullPath)
        {
            switch (hashStyle)
            {
            case HashStyle.DropboxSHA256:
            {
                StorageFile file = await StorageFile.GetFileFromPathAsync(fullPath);

                using (IRandomAccessStream input = await file.OpenReadAsync())
                {
                    List <byte[]> hashes = new List <byte[]>();

                    using (SHA256 crypto = SHA256.Create())
                    {
                        bool moreData = true;
                        while (moreData)
                        {
                            byte[]  bytes = new byte[4 * 1024 * 1024];
                            IBuffer read  = await input.ReadAsync(bytes.AsBuffer(), (uint)input.Size, InputStreamOptions.None);

                            byte[] blockHash = crypto.ComputeHash(read.ToArray());
                            hashes.Add(blockHash);

                            moreData = !(read.Length < bytes.Length);
                        }

                        byte[] joined    = hashes.SelectMany(byteArr => byteArr).ToArray();
                        byte[] totalHash = crypto.ComputeHash(joined);
                        string hex       = BitConverter.ToString(totalHash).ToLower().Replace("-", String.Empty);
                        return(hex);
                    }
                }
            }

            case HashStyle.OneDriveSHA1:
            {
                string      shaHash = String.Empty;
                StorageFile file    = await StorageFile.GetFileFromPathAsync(fullPath);

                using (SHA1Managed shaForStream = new SHA1Managed())
                    using (Stream sourceFileStream = await file.OpenStreamForReadAsync())
                        using (Stream sourceStream = new CryptoStream(sourceFileStream, shaForStream, CryptoStreamMode.Read))
                        {
                            //!!!Could this be optimised?
                            while (sourceStream.ReadByte() != -1)
                            {
                                ;
                            }
                            shaHash = shaForStream.Hash.ToHexString().ToUpper();
                        }
                return(shaHash);
            }

            case HashStyle.AmazonS3MD5:
            {
                string      md5Hash = String.Empty;
                StorageFile file    = await StorageFile.GetFileFromPathAsync(fullPath);

                using (MD5 md5ForStream = MD5.Create())
                    using (Stream sourceFileStream = await file.OpenStreamForReadAsync())
                        using (Stream sourceStream = new CryptoStream(sourceFileStream, md5ForStream, CryptoStreamMode.Read))
                        {
                            //!!!Could this be optimised?
                            while (sourceStream.ReadByte() != -1)
                            {
                                ;
                            }
                            md5Hash = md5ForStream.Hash.ToHexString().ToUpper();
                        }
                return(md5Hash);
            }

            default:
            {
                throw new NotImplementedException();
            }
            }
        }
Esempio n. 5
0
 public static char GetStyleChar(HashStyle hashStyle) => styleChar[(int)hashStyle];
Esempio n. 6
0
        public async Task <string> HashFileAsync(
            HashStyle hashStyle,
            string fullPath)
        {
            switch (hashStyle)
            {
            case HashStyle.DropboxSHA256:
            {
                using (Stream input = File.OpenRead(fullPath))
                {
                    List <byte[]> hashes = new List <byte[]>();

                    using (SHA256 crypto = SHA256.Create())
                    {
                        bool moreData = true;
                        while (moreData)
                        {
                            byte[] bytes = new byte[4 * 1024 * 1024];
                            int    read  = await input.ReadAsync(bytes, 0, bytes.Length);

                            byte[] blockHash = crypto.ComputeHash(bytes, 0, read);
                            hashes.Add(blockHash);

                            moreData = !(read < bytes.Length);
                        }

                        byte[] joined    = hashes.SelectMany(byteArr => byteArr).ToArray();
                        byte[] totalHash = crypto.ComputeHash(joined);
                        string hex       = BitConverter.ToString(totalHash).ToLower().Replace("-", String.Empty);
                        return(hex);
                    }
                }
            }

            case HashStyle.OneDriveSHA1:
            {
                string shaHash = String.Empty;
                using (SHA1Managed shaForStream = new SHA1Managed())
                    using (Stream sourceFileStream = File.Open(fullPath, FileMode.Open))
                        using (Stream sourceStream = new CryptoStream(sourceFileStream, shaForStream, CryptoStreamMode.Read))
                        {
                            //!!!Could this be optimised?
                            while (sourceStream.ReadByte() != -1)
                            {
                                ;
                            }
                            shaHash = shaForStream.Hash.ToHexString().ToUpper();
                        }
                return(shaHash);
            }

            case HashStyle.AmazonS3MD5:
            {
                string md5Hash = String.Empty;
                using (MD5 md5ForStream = MD5.Create())
                    using (Stream sourceFileStream = File.Open(fullPath, FileMode.Open))
                        using (Stream sourceStream = new CryptoStream(sourceFileStream, md5ForStream, CryptoStreamMode.Read))
                        {
                            //!!!Could this be optimised?
                            while (sourceStream.ReadByte() != -1)
                            {
                                ;
                            }
                            md5Hash = md5ForStream.Hash.ToHexString().ToUpper();
                        }
                return(md5Hash);
            }

            default:
            {
                throw new NotImplementedException(String.Format("Hash style '{0}' is not supported by the native file handler.", hashStyle.ToString()));
            }
            }
        }