예제 #1
0
 public static string HashBytes(byte [] bytes)
 {
     using (HashAlgorithm hashAlg = new Crc64()) {
         byte [] hash = hashAlg.ComputeHash(bytes);
         return(ToHexString(hash));
     }
 }
예제 #2
0
        public static string HashStream(Stream stream)
        {
            stream.Position = 0;

            using (HashAlgorithm hashAlg = new Crc64()) {
                byte[] hash = hashAlg.ComputeHash(stream);
                return(ToHexString(hash));
            }
        }
예제 #3
0
        protected bool FileCompare(string file1, string file2)
        {
            bool result = false;

            result = File.Exists(file1) && File.Exists(file2);

            if (result)
            {
                byte[] f1 = ReadAllBytesIgnoringLineEndings(file1);
                byte[] f2 = ReadAllBytesIgnoringLineEndings(file2);

                using (var hash = new Crc64()) {
                    var f1hash = Convert.ToBase64String(hash.ComputeHash(f1));
                    var f2hash = Convert.ToBase64String(hash.ComputeHash(f2));
                    result = string.Equals(f1hash, f2hash, StringComparison.Ordinal);
                }
            }

            return(result);
        }
예제 #4
0
 static string ToHash(byte[] data)
 {
     using (var crc = new Crc64()) {
         var hash = crc.ComputeHash(data);
         var buf  = new StringBuilder(hash.Length * 2);
         foreach (var b in hash)
         {
             buf.AppendFormat("{0:x2}", b);
         }
         return(buf.ToString());
     }
 }
예제 #5
0
        public void AllBytesAreProcessed()
        {
            // Slicing processes 8 bytes (a 64-bit word) at a time, and if any of the bytes are skipped we will have a
            // collision here.
            string[] inputs =
            {
                "obj/Debug/lp/10/jl/bin/classes.jar",
                "obj/Debug/lp/11/jl/bin/classes.jar",
                "obj/Debug/lp/12/jl/bin/classes.jar",
            };

            string[] expected =
            {
                "419a37c9bcfddf3c",
                "6ea5e242b7cc24a7",
                "74770a86f8b97020",
            };

            string[] outputs = new string[inputs.Length];

            for (int i = 0; i < inputs.Length; i++)
            {
                byte[] bytes = Encoding.UTF8.GetBytes(inputs [i]);
                using (HashAlgorithm hashAlg = new Crc64()) {
                    byte [] hash = hashAlg.ComputeHash(bytes);
                    outputs[i] = ToHash(hash);
                    Assert.AreEqual(expected[i], outputs[i], $"hash {i} differs");
                }
            }

            for (int i = 0; i < outputs.Length; i++)
            {
                for (int j = 0; j < outputs.Length; j++)
                {
                    if (j == i)
                    {
                        continue;
                    }
                    Assert.AreNotEqual(outputs[i], outputs[j], $"Outputs {i} and {j} are identical");
                }
            }
        }
예제 #6
0
        internal static string GetHashCore(KernelTransaction transaction, string fileFullPath, HashType hashType, PathFormat pathFormat)
        {
            var options    = GetFullPathOptions.RemoveTrailingDirectorySeparator | GetFullPathOptions.FullCheck;
            var fileNameLp = Path.GetExtendedLengthPathCore(transaction, fileFullPath, pathFormat, options);

            byte[] hash = null;


            using (var fs = OpenCore(transaction, fileNameLp, FileMode.Open, FileAccess.Read, FileShare.Read, ExtendedFileAttributes.Normal, null, null, PathFormat.LongFullPath))
            {
                switch (hashType)
                {
                case HashType.CRC32:
                    using (var hType = new Crc32())
                        hash = hType.ComputeHash(fs);
                    break;


                case HashType.CRC64ISO3309:
                    using (var hType = new Crc64())
                        hash = hType.ComputeHash(fs);
                    break;


                case HashType.MD5:
                    using (var hType = System.Security.Cryptography.MD5.Create())
                        hash = hType.ComputeHash(fs);
                    break;


                case HashType.RIPEMD160:
                    using (var hType = System.Security.Cryptography.RIPEMD160.Create())
                        hash = hType.ComputeHash(fs);
                    break;


                case HashType.SHA1:
                    using (var hType = System.Security.Cryptography.SHA1.Create())
                        hash = hType.ComputeHash(fs);
                    break;


                case HashType.SHA256:
                    using (var hType = System.Security.Cryptography.SHA256.Create())
                        hash = hType.ComputeHash(fs);
                    break;


                case HashType.SHA384:
                    using (var hType = System.Security.Cryptography.SHA384.Create())
                        hash = hType.ComputeHash(fs);
                    break;


                case HashType.SHA512:
                    using (var hType = System.Security.Cryptography.SHA512.Create())
                        hash = hType.ComputeHash(fs);
                    break;
                }
            }


            if (null != hash)
            {
                var sb = new StringBuilder(hash.Length);

                foreach (byte b in hash)
                {
                    sb.Append(b.ToString("X2", CultureInfo.InvariantCulture));
                }

                return(sb.ToString().ToUpperInvariant());
            }

            return(string.Empty);
        }