public void MethodConsistency(string text, int offset, int tail, int split) { var checksums = new List <uint>(); var array = Encoding.ASCII.GetBytes(text); var padded = Enumerable.Repeat <byte>(17, offset).Concat(array).Concat(Enumerable.Repeat <byte>(93, tail)).ToArray(); var half1 = array.Take(split).ToArray(); var half2 = array.Skip(split).ToArray(); checksums.Add(Crc32CAlgorithm.Compute(array)); checksums.Add(Crc32CAlgorithm.Compute(padded, offset, array.Length)); checksums.Add(Crc32CAlgorithm.Append(0, array)); checksums.Add(Crc32CAlgorithm.Append(0, padded, offset, array.Length)); checksums.Add(Crc32CAlgorithm.Append(Crc32CAlgorithm.Append(0, half1), half2)); checksums.Add(Crc32CAlgorithm.Append(Crc32CAlgorithm.Append(0, padded, offset, split), padded, offset + split, array.Length - split)); using (var hash = new Crc32CAlgorithm()) checksums.Add(BitConverter.ToUInt32(hash.ComputeHash(array), 0)); using (var hash = new Crc32CAlgorithm()) checksums.Add(BitConverter.ToUInt32(hash.ComputeHash(padded, offset, array.Length), 0)); using (var stream = new MemoryStream(array)) using (var hash = new Crc32CAlgorithm()) checksums.Add(BitConverter.ToUInt32(hash.ComputeHash(stream), 0)); if (text.Length == 0) { Assert.AreEqual(0, checksums[0]); } foreach (var checksum in checksums) { Assert.AreEqual(checksums[0], checksum); } }
public static Byte[] CalculateCrc(Byte[] data) { using (var crc = new Crc32CAlgorithm()) { return(crc.ComputeHash(data)); } }
private static string CalculateCrc32c(Stream stream) { var algorithm = new Crc32CAlgorithm(); var hash = algorithm.ComputeHash(stream); stream.Seek(0, SeekOrigin.Begin); Array.Reverse(hash); return(Convert.ToBase64String(hash)); }
public static byte[] Calculate(string str) { byte[] hash; Crc32CAlgorithm crc = new Crc32CAlgorithm(); hash = crc.ComputeHash(Encoding.UTF8.GetBytes(str)); //Invert the hash, since Hal does that for some reason for (int i = 0; i < hash.Length; i++) { hash[i] = (byte)(255 - hash[i]); } return(hash); }