internal static (int[] randomData, int[] sortedData, string reproContext) GenerateData( int size, int seed, int forcedValue = -1, double forcedValueRate = double.NaN, int modulo = int.MaxValue, bool dontSort = false) { var r = new Random(seed); var data = new int[size]; for (var i = 0; i < size; ++i) { data[i] = double.IsNaN(forcedValueRate) ? r.Next() % modulo : r.NextDouble() > forcedValueRate ? forcedValue : (r.Next() % modulo); } int[] sorted = null; if (!dontSort) { sorted = new int[size]; data.CopyTo(sorted, 0); Array.Sort(sorted); } var reproContext = ""; using (var sha1 = new SHA1CryptoServiceProvider()) { Span <byte> hash = stackalloc byte[20]; sha1.TryComputeHash(MemoryMarshal.Cast <int, byte>(new ReadOnlySpan <int>(data)), hash, out _); var dataHash = Convert.ToBase64String(hash); sha1.TryComputeHash(MemoryMarshal.Cast <int, byte>(new ReadOnlySpan <int>(sorted)), hash, out _); var sortedHash = Convert.ToBase64String(hash); reproContext = $"[{size},{seed}] -> [{dataHash},{sortedHash}]"; } return(data, sorted, reproContext); }
private void hashSHA1() { uint actual_in_size = sha1.in_size + 0x20; byte[] mem = new byte[actual_in_size]; for (int i = 0; i < (actual_in_size >> 2); i += 4) { Console.WriteLine("Read: 0x" + (sha1.in_addr + i).ToString("X")); //mem[i] = this.device.Memory.ReadUInt8((uint)(sha1.in_addr + i)); } System.Security.Cryptography.SHA1 sha = new SHA1CryptoServiceProvider(); int bytesWritten; bool result = sha.TryComputeHash(mem, sha1.hash_out, out bytesWritten); if (result) { Console.WriteLine("SHA1: hashSHA1() successfully computed " + bytesWritten + " bytes!"); } else { Console.WriteLine("SHA1: hashSHA1() failed!"); } }
public static (int[] data, int stepSize, string reproContext) GenerateEx03TestArray(int stepSize, int size) { var seed = (int)DateTime.UtcNow.Ticks; var r = new Random(seed); var data = Enumerable.Repeat(0, size).Select(_ => r.Next(0, stepSize)).ToArray(); var reproContext = ""; using (var sha1 = new SHA1CryptoServiceProvider()) { Span <byte> hash = stackalloc byte[20]; sha1.TryComputeHash(MemoryMarshal.Cast <int, byte>(new ReadOnlySpan <int>(data)), hash, out _); var dataHash = Convert.ToBase64String(hash); reproContext = $"[{seed},{size},{stepSize}] -> [{dataHash}]"; } return(data, stepSize, reproContext); }
public static string Create() { Span <char> guid = stackalloc char[36]; Guid.NewGuid().TryFormat(guid, out _, "D"); Span <char> guidHex = stackalloc char[72]; Span <byte> guidBytes = stackalloc byte[100]; for (int i = 0; i < guid.Length; i++) { ((int)guid[i]).TryFormat(guidHex.Slice(i * 2), out _, "X2"); } Encoding.ASCII.GetBytes(guidHex, guidBytes); Encoding.ASCII.GetBytes(":MsOIJ39Q28:PTDc3H8a)Vi=UYap", guidBytes.Slice(72)); using SHA1 sha = new SHA1CryptoServiceProvider(); Span <byte> hash = stackalloc byte[20]; sha.TryComputeHash(guidBytes, hash, out _); Span <char> hashHex = stackalloc char[40]; for (int i = 0; i < hash.Length; i++) { hash[i].TryFormat(hashHex.Slice(i * 2), out _, "x2"); } Span <byte> result = stackalloc byte[124]; guidBytes.Slice(0, 72).CopyTo(result); Encoding.ASCII.GetBytes("_MsOIJ39Q28:", result.Slice(72)); Encoding.ASCII.GetBytes(hashHex, result.Slice(84)); return(Convert.ToBase64String(result)); }