public static string GetPackageName(Type type) { string assemblyName = type.Assembly.GetName().Name; if (IsPackageNamePreservedForAssembly(assemblyName)) { return(type.Namespace.ToLowerInvariant()); } switch (PackageNamingPolicy) { case PackageNamingPolicy.Lowercase: return(type.Namespace.ToLowerInvariant()); case PackageNamingPolicy.LowercaseWithAssemblyName: return("assembly_" + (assemblyName.Replace('.', '_') + "." + type.Namespace).ToLowerInvariant()); case PackageNamingPolicy.LowercaseCrc64: using (var crc = new Crc64()) return("crc64" + ToHash(type.Namespace + ":" + assemblyName, crc)); case PackageNamingPolicy.LowercaseMD5: default: using (var md5 = MD5.Create()) return("md5" + ToHash(type.Namespace + ":" + assemblyName, md5)); } }
public void UrlHashKeyAreNotTheSame() { var urlHash1 = Crc64.ComputeHashString("http://www.optipess.com/wp-content/uploads/2010/08/02_Bad-Comics6-10.png?a=bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbasdasdasdasdasasdasdasdasdasd"); var urlHash2 = Crc64.ComputeHashString("http://www.optipess.com/wp-content/uploads/2010/08/02_Bad-Comics6-10.png?a=bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbasdasdasdasdasasdasda"); Assert.IsTrue(urlHash1 != urlHash2); }
private bool CompareCrc64(string localFile, string crc64ecma) { Crc64.InitECMA(); String hash = String.Empty; using (FileStream fs = File.Open(localFile, FileMode.Open)) { byte[] buffer = new byte[2048]; int bytesRead; ulong crc = 0; while ((bytesRead = fs.Read(buffer, 0, buffer.Length)) > 0) { ulong partCrc = Crc64.Compute(buffer, 0, bytesRead); if (crc == 0) { crc = partCrc; } else { crc = Crc64.Combine(crc, partCrc, bytesRead); } } localFileCrc64 = crc.ToString(); return(localFileCrc64 == crc64ecma); } }
public static string HashBytes(byte [] bytes) { using (HashAlgorithm hashAlg = new Crc64()) { byte [] hash = hashAlg.ComputeHash(bytes); return(ToHexString(hash)); } }
public Scheme(string title) { Title = title; var key = Encodings.cp932.GetBytes(title); Hash = Crc64.Compute(key, 0, key.Length); }
public override void Handle(Communication.ServiceResponse response) { if (response.Headers.ContainsKey(HttpHeaders.HashCrc64Ecma)) { ulong crc64 = 0; bool checkCrc = true; foreach (var part in _request.PartETags) { if (!String.IsNullOrEmpty(part.Crc64) && part.Length != 0) { crc64 = Crc64.Combine(crc64, ulong.Parse(part.Crc64), part.Length); } else { checkCrc = false; } } if (!checkCrc) // the request does not have CRC64 for at least one part, skip the check { return; } var ossCalculatedHashStr = response.Headers[HttpHeaders.HashCrc64Ecma]; if (!crc64.ToString().Equals(ossCalculatedHashStr)) { response.Dispose(); throw new ClientException("Crc64 validation failed. Expected hash not equal to calculated hash"); } } }
public void Crc64Test() { byte[] hash; foreach (var item in Data) { hash = Crc64.Compute(item, item.Length); } }
public void TestAddByteRange() { Crc64 all = new Crc64(new byte[] { 0x2, 0x3, 0x4, 0x5, 0x6 }); Crc64 crc = new Crc64(); Assert.AreEqual(0, crc.Value); crc.Add(new byte[] { 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8 }, 1, 5); Assert.AreEqual(all.Value, crc.Value); }
private void ReadRootElement(XElement element) { dicElements = new Dictionary <ulong, int>(); dicAttributes = new Dictionary <ulong, int>(); dicVariants = new Dictionary <ulong, int>(); strings = new Dictionary <string, StringEntry>(); crc = new Crc64(); rootElementIndex = ReadElement(element); }
public static string HashStream(Stream stream) { stream.Position = 0; using (HashAlgorithm hashAlg = new Crc64()) { byte[] hash = hashAlg.ComputeHash(stream); return(ToHexString(hash)); } }
public void TestInitialise() { _Snapshot = Factory.TakeSnapshot(); _FileSystem = new MockFileSystemProvider(); Factory.RegisterInstance <IFileSystemProvider>(_FileSystem); _ChecksumGenerator = new Crc64(); _Instance = new ChecksumFileEntry(); }
public void TestCrcCore() { string testStr = "This is a test."; byte[] content = Encoding.ASCII.GetBytes(testStr); Crc64.InitECMA(); ulong crc = Crc64.Compute(content, 0, content.Length, 0); Assert.AreEqual(crc, 2186167744391481992); }
/// <summary> /// Calculates an on-going hash using the input byte array. /// </summary> /// <param name="input">The input array used for calculating the hash.</param> /// <param name="offset">The offset in the input buffer to calculate from.</param> /// <param name="count">The number of bytes to use from input.</param> internal void UpdateHash(byte[] input, int offset, int count) { if (offset != 0) { throw new NotImplementedException("non-zero offset for Crc64Wrapper update not supported"); } if (count > 0) { this.uCRC = Crc64.ComputeSlicedSafe(input, count, this.uCRC); } }
public void TestOperatorPlusString() { Crc64 all = new Crc64("hello there world"); Crc64 crc = new Crc64(); Assert.AreEqual(0, crc.Value); crc += "hello "; crc += "there "; crc += "world"; Assert.AreEqual(all.Value, crc.Value); }
public void TestCrcCore() { string testStr = "This is a test."; byte[] content = Encoding.ASCII.GetBytes(testStr); Crc64 crc64 = new Crc64(); Assert.Equal(2186167744391481992UL, crc64.Compute(content, 0, content.Length, 0)); crc64 = new Crc64(); Assert.Equal(2186167744391481992UL, crc64.Compute(content, 0, content.Length)); }
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()); } }
public void TestHashValue() { Crc64 crc = new Crc64(); Assert.AreEqual(0, crc.Value); Assert.AreEqual(0, crc.GetHashCode()); crc.Add(0x1b); Assert.AreNotEqual(0, crc.Value); Assert.AreEqual((int)crc.Value ^ (int)(crc.Value >> 32), crc.GetHashCode()); }
public bool AddFile(ArchiveFileInfo archiveFileInfo) { var hash = Crc64.Compute(archiveFileInfo.FileName); files.Add(new FileEntry() { Hash = hash, FileData = archiveFileInfo.FileData, FileName = archiveFileInfo.FileName, }); return(true); }
public void Execute() { unsafe { var ptr = Translations.GetUnsafeReadOnlyPtr(); var lengthByte = FixTranslation.BYTE_SIZE * Translations.Length; Hash[0] = Crc64.Compute(ptr, lengthByte); ptr = Entities.GetUnsafeReadOnlyPtr(); lengthByte = sizeof(int) * 2 * Entities.Length; Hash[0] ^= Crc64.Compute(ptr, lengthByte); } }
public void TestOperatorPlusBytes() { Crc64 all = new Crc64(new byte[] { 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8 }); Crc64 crc = new Crc64(); Assert.AreEqual(0, crc.Value); crc += new byte[] { 0x1, 0x2, 0x3, 0x4 }; crc += 0x5; crc += 0x6; crc += 0x7; crc += 0x8; Assert.AreEqual(all.Value, crc.Value); }
public void TestCrc64Equals() { Crc64 empty = new Crc64(); Crc64 value = new Crc64("Hello"); Crc64 copy = new Crc64(value.Value); Assert.IsTrue(value.Equals(copy)); Assert.IsFalse(value.Equals(empty)); Assert.IsTrue(value.Equals(copy.Value)); Assert.IsFalse(value.Equals(empty.Value)); Assert.IsTrue(value.Equals((object)copy)); Assert.IsFalse(value.Equals((object)empty)); Assert.IsTrue(value.Equals((object)copy.Value)); Assert.IsFalse(value.Equals((object)empty.Value)); }
public void TestOperatorEquality() { Crc64 empty = new Crc64(); Crc64 value = new Crc64("Hello"); Crc64 copy = new Crc64(value.Value); Assert.IsTrue(value == copy); Assert.IsFalse(value == empty); Assert.IsTrue(value == copy.Value); Assert.IsFalse(value == empty.Value); Assert.IsFalse(value != copy); Assert.IsTrue(value != empty); Assert.IsFalse(value != copy.Value); Assert.IsTrue(value != empty.Value); }
private static void AssertCrc64(byte[] input, ulong expectedu) { long expected = unchecked((long) expectedu); Crc64 crc = new Crc64(input); Assert.AreEqual(expected, crc.Value); crc = new Crc64(0); crc.Add(input); Assert.AreEqual(expected, crc.Value); crc = new Crc64(); crc.Add(input, 0, input.Length); Assert.AreEqual(expected, crc.Value); crc = new Crc64(); foreach (byte b in input) crc.Add(b); Assert.AreEqual(expected, crc.Value); }
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"); } } }
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); }
public void TestCrcCompute() { ulong[] crcVals = { 0x0 /*0*/, 0x330284772E652B05, 0xBC6573200E84B046, 0x2CD8094A1A277627, 0x3C9D28596E5960BA, 0x40BDF58FB0895F2 /*5*/, 0xD08E9F8545A700F4, 0xEC20A3A8CC710E66, 0x67B4F30A647A0C59, 0x9966F6C89D56EF8E, 0x32093A2ECD5773F4 /*10*/, 0x8A0825223EA6D221, 0x8562C0AC2AB9A00D, 0x3EE2A39C083F38B4, 0x1F603830353E518A, 0x2FD681D7B2421FD /*15*/, 0x790EF2B16A745A41, 0x3EF8F06DACCDCDDF, 0x49E41B2660B106D, 0x561CC0CFA235AC68, 0xD4FE9EF082E69F59 /*20*/, 0xE3B5E46CD8D63A4D, 0x865AAF6B94F2A051, 0x7ECA10D2F8136EB4, 0xD7DD118C98E98727, 0x70FB33C119C29318 /*25*/, 0x57C891E39A97D9B7, 0xA1F46BA20AD06EB7, 0x7AD25FAFA1710407, 0x73CEF1666185C13F, 0xB41858F73C389602 /*30*/ }; string[] textVals = { "" /*0*/, "a", "ab", "abc", "abcd", "abcde" /*5*/, "abcdef", "abcdefg", "abcdefgh", "abcdefghi", "abcdefghij" /*10*/, "Discard medicine more than two years old.", "He who has a shady past knows that nice guys finish last.", "I wouldn't marry him with a ten foot pole.", "Free! Free!/A trip/to Mars/for 900/empty jars/Burma Shave", "The days of the digital watch are numbered. -Tom Stoppard" /*15*/, "Nepal premier won't resign.", "For every action there is an equal and opposite government program.", "His money is twice tainted: 'taint yours and 'taint mine.", "There is no reason for any individual to have a computer in their home. -Ken Olsen, 1977", "It's a tiny change to the code and not completely disgusting. - Bob Manchek" /*20*/, "size: a.out: bad magic", "The major problem is with sendmail. -Mark Horton", "Give me a rock, paper and scissors and I will move the world. CCFestoon", "If the enemy is within range, then so are you.", "It's well we cannot hear the screams/That we create in others' dreams." /*25*/, "You remind me of a TV show, but that's all right: I watch it anyway.", "C is as portable as Stonehedge!!", "Even if I could be Shakespeare, I think I should still choose to be Faraday. - A. Huxley", "The fugacity of a constituent in a mixture of gases at a given temperature is proportional to its mole fraction. Lewis-Randall Rule", "How can you write a big system without C++? -Paul Glick" /*30*/ }; Crc64.InitECMA(); for (int i = 0; i < crcVals.Length; i++) { byte[] content = Encoding.ASCII.GetBytes(textVals[i]); ulong crc = Crc64.Compute(content, 0, content.Length, 0); System.Console.WriteLine("{0:X}", crc); Assert.AreEqual(crc, crcVals[i]); } }
public void TestCombine() { string str1 = "this is a test."; string str2 = "hello world."; string str = str1 + str2; byte[] content1 = Encoding.ASCII.GetBytes(str1); byte[] content2 = Encoding.ASCII.GetBytes(str2); byte[] content = Encoding.ASCII.GetBytes(str); Crc64.InitECMA(); ulong crc1 = Crc64.Compute(content1, 0, content1.Length); ulong crc2 = Crc64.Compute(content2, 0, content2.Length); ulong crc = Crc64.Compute(content, 0, content.Length); Assert.AreEqual(crc, Crc64.Combine(crc1, crc2, content2.Length)); ulong crc3 = Crc64.Compute(content2, 0, content2.Length, crc1); Assert.AreEqual(crc3, crc); }
static ulong GetHashFromNetMessageTypes(Type[] netMessageTypes) { StringBuilder concatenatedNamesBuilder = new StringBuilder(); foreach (var t in netMessageTypes) { concatenatedNamesBuilder.Append(t.FullName); } string concatenatedNames = concatenatedNamesBuilder.ToString(); byte[] crcData = new byte[concatenatedNames.Length * 2]; // each char is 2 bytes BitStreamWriter writer = new BitStreamWriter(crcData); for (int i = 0; i < concatenatedNames.Length; i++) { writer.WriteChar(concatenatedNames[i]); } return(Crc64.Compute(crcData)); }
private static void AssertCrc64(byte[] input, ulong expectedu) { long expected = unchecked ((long)expectedu); Crc64 crc = new Crc64(input); Assert.AreEqual(expected, crc.Value); crc = new Crc64(0); crc.Add(input); Assert.AreEqual(expected, crc.Value); crc = new Crc64(); crc.Add(input, 0, input.Length); Assert.AreEqual(expected, crc.Value); crc = new Crc64(); foreach (byte b in input) { crc.Add(b); } Assert.AreEqual(expected, crc.Value); }
public static string GetPackageName(TypeDefinition type) { if (IsPackageNamePreservedForAssembly(type.GetPartialAssemblyName())) { return(type.Namespace.ToLowerInvariant()); } switch (PackageNamingPolicy) { case PackageNamingPolicy.Lowercase: return(type.Namespace.ToLowerInvariant()); case PackageNamingPolicy.LowercaseWithAssemblyName: return("assembly_" + (type.GetPartialAssemblyName().Replace('.', '_') + "." + type.Namespace).ToLowerInvariant()); case PackageNamingPolicy.LowercaseCrc64: using (var crc = new Crc64()) return("crc64" + ToHash(type.Namespace + ":" + type.GetPartialAssemblyName(), crc)); default: throw new NotSupportedException($"PackageNamingPolicy.{PackageNamingPolicy} is no longer supported."); } }
public void TestCrcCombine() { string text = "Even if I could be Shakespeare, I think I should still choose to be Faraday. - A. Huxley"; byte[] content = Encoding.ASCII.GetBytes(text); Crc64.InitECMA(); ulong crc = Crc64.Compute(content, 0, content.Length); for (int i = 1; i < text.Length; i++) { string before = text.Substring(0, i); string after = text.Substring(i); byte[] content1 = Encoding.ASCII.GetBytes(before); byte[] content2 = Encoding.ASCII.GetBytes(after); ulong crc1 = Crc64.Compute(content1, 0, content1.Length); ulong crc2 = Crc64.Compute(content2, 0, content2.Length); Assert.AreEqual(crc, Crc64.Combine(crc1, crc2, content2.Length)); ulong crc3 = Crc64.Compute(content2, 0, content2.Length, crc1); Assert.AreEqual(crc3, crc); } }