public NextBytes ( Byte buffer ) : void | ||
buffer | Byte | The buffer to fill. |
Résultat | void |
protected HashesTestBase() { m_creators = new List <Func <object> > { () => { return(m_random.NextByte()); }, () => { return(m_random.NextChar()); }, () => { return(m_random.NextShort()); }, () => { return(m_random.NextUShort()); }, () => { return(m_random.NextInt()); }, () => { return(m_random.NextUInt()); }, () => { return(m_random.NextLong()); }, () => { return(m_random.NextULong()); }, () => { return(m_random.NextFloatFull()); }, () => { return(m_random.NextDoubleFull()); }, () => { return(m_random.NextString(m_random.Next(0, 200))); }, () => { return(m_random.NextBytes(m_random.Next(0, 200))); }, () => { return(m_random.NextChars(m_random.Next(0, 200))); }, () => { return(m_random.NextShorts(m_random.Next(0, 200))); }, () => { return(m_random.NextUShorts(m_random.Next(0, 200))); }, () => { return(m_random.NextInts(m_random.Next(0, 200))); }, () => { return(m_random.NextUInts(m_random.Next(0, 200))); }, () => { return(m_random.NextLongs(m_random.Next(0, 200))); }, () => { return(m_random.NextULongs(m_random.Next(0, 200))); }, () => { return(m_random.NextFloatsFull(m_random.Next(0, 200))); }, () => { return(m_random.NextDoublesFull(m_random.Next(0, 200))); }, }.AsReadOnly(); }
private void Test(IHMAC a_base_hmac, IHMAC a_hmac) { Assert.AreEqual(a_base_hmac.HashSize, a_hmac.HashSize); Assert.AreEqual(a_base_hmac.BlockSize, a_hmac.BlockSize); List <int> keys_length = new List <int>() { 0, 1, 7, 51, 121, 512, 1023 }; keys_length.Add(a_hmac.BlockSize - 1); keys_length.Add(a_hmac.BlockSize); keys_length.Add(a_hmac.BlockSize + 1); List <int> msgs_length = new List <int>(); msgs_length.AddRange(keys_length); msgs_length.Add(a_hmac.BlockSize * 4 - 1); msgs_length.Add(a_hmac.BlockSize * 4); msgs_length.Add(a_hmac.BlockSize * 4 + 1); foreach (int key_length in keys_length) { byte[] key = m_random.NextBytes(key_length); a_base_hmac.Key = key; a_hmac.Key = key; foreach (int msg_length in msgs_length) { byte[] msg = m_random.NextBytes(msg_length); a_base_hmac.Initialize(); a_base_hmac.TransformBytes(msg); HashResult h1 = a_base_hmac.TransformFinal(); a_hmac.Initialize(); a_hmac.TransformBytes(msg); HashResult h2 = a_hmac.TransformFinal(); Assert.AreEqual(h1, h2, a_hmac.Name); h1 = a_base_hmac.ComputeString(BitConverter.ToString(msg)); h2 = a_hmac.ComputeString(BitConverter.ToString(msg)); Assert.AreEqual(h1, h2, a_hmac.Name); } } }
public static void QuickStart_MersenneTwister() { // MersenneTwister is a pseudo-random number generator. var mt = new MersenneTwister(42); var ul = mt.NextUInt64(); // [0, 2^64-1] var d = mt.NextDouble(); // [0,1) var bytes = new byte[10]; mt.NextBytes(bytes); }
public static void QuickStart_RandomVault() { // RandomVault is a random number pool. // It's thread-safe and faster than lock in most cases. var mt = new MersenneTwister(); // Create a random generator. var rv = new RandomVault(() => mt.NextUInt64(), x => mt.NextBytes(x)); // Specify NextULong() or NextBytes() or both delegates, and forget about mt. Console.WriteLine("RandomVault:"); Console.WriteLine(rv.NextInt64()); Console.WriteLine(rv.NextDouble()); }
public static void Main(string[] args) { CommandLineParser cmd = CommandLineParser.Parse(args); Log.CreateInstance(true); string keyText = null; if (cmd["key"] != null) { keyText = cmd["key"].Value; } else { keyText = PasswordPrompt.Get("Please enter the encryption key"); string keyText2 = PasswordPrompt.Get("Please confirm the encryption key"); if (keyText != keyText2) { Log.Instance.Write(Log_Severity.Fatal, "Keys did not match"); } } if (cmd["to"] == null || cmd["from"] == null) { Log.Instance.Write(Log_Severity.Fatal, "Need arguments 'to' and 'from'"); } ulong sender = ulong.Parse(cmd["from"].Value); ulong recipient = ulong.Parse(cmd["to"].Value); var mt = new MersenneTwister((uint)Guid.NewGuid().GetHashCode()); var key = Encoding.UTF8.GetBytes(keyText); if (sender == 0) { sender = mt.NextULong(); } if (recipient == 0) { recipient = mt.NextULong(); } var iv = mt.NextBytes(BLOCK_SIZE); var data = BitShifter.ToByte(sender).Concat(BitShifter.ToByte(recipient)).ToArray(); BufferedBlockCipher cipher = new CtsBlockCipher(new CbcBlockCipher(new AesEngine())); ICipherParameters keyParam = new ParametersWithIV(new KeyParameter(key), iv); cipher.Init(true, keyParam); Log.Instance.Write(iv.Concat(cipher.DoFinal(data, 0, data.Length)).ToArray().ToHex()); }
public void TestNextBytes() { MersenneTwister m = new MersenneTwister(42); try { m.NextBytes(new byte[] { 0x00 }); } catch (Exception e) { Assert.Fail(e.ToString()); } }
private void SaveLine(StreamWriter a_sw, int a_data_length) { byte[] data = m_random.NextBytes(a_data_length); byte[] hash = m_hash.ComputeBytes(data).GetBytes(); if (m_hash is IWithKey) { a_sw.WriteLine("Key: {0}", Converters.ConvertBytesToHexString((m_hash as IWithKey).Key, false)); } a_sw.WriteLine("Repeat: {0}", 1); a_sw.WriteLine("Messsage: {0}", Converters.ConvertBytesToHexString(data, false)); a_sw.WriteLine("Hash: {0}", Converters.ConvertBytesToHexString(hash, false)); a_sw.WriteLine(""); }
private void MeasureBytesPerCycle(SpeedTestDataSourceRow a_row) { Stopwatch sw = new Stopwatch(); IHash hash = a_row.HashFunction; MersenneTwister r = new MersenneTwister(); hash.Initialize(); var msg = r.NextBytes(BYTES_ARRAY_LENGTH_CYCLES_PER_SECOND); RDTSC.rdtsc(); hash.ComputeBytes(msg); int repeats = 0; ulong min_delta = UInt64.MaxValue; for (; ;) { HighPriority(); try { sw.Start(); ulong before = RDTSC.rdtsc(); hash.ComputeBytes(msg); ulong after = RDTSC.rdtsc(); sw.Stop(); ulong delta = after - before; min_delta = Math.Min(min_delta, delta); } finally { NormalPriority(); } repeats++; if (sw.ElapsedMilliseconds > MEASURE_TIME) { if (repeats > MIN_REPEATS) { break; } } } a_row.BytesPerCycle = ((double)min_delta / BYTES_ARRAY_LENGTH_CYCLES_PER_SECOND).ToString("F2"); }
public static void Test1() { var mt2 = new MersenneTwister(new ulong[] { 0x12345UL, 0x23456UL, 0x34567UL, 0x45678UL }); var poolSliding = new RandomVault(() => mt2.NextUInt64(), x => mt2.NextBytes(x)); Debug.Assert(poolSliding.NextUInt64() == 7266447313870364031UL); for (var i = 0; i < 20000; i++) { poolSliding.NextUInt64(); } var mt3 = new MersenneTwister(new ulong[] { 0x12345UL, 0x23456UL, 0x34567UL, 0x45678UL }); var poolConcurrentQueue = new RandomPoolConcurrentQueue(() => mt3.NextUInt64(), x => mt3.NextBytes(x)); Debug.Assert(poolConcurrentQueue.NextULong() == 7266447313870364031UL); for (var i = 0; i < 20000; i++) { poolConcurrentQueue.NextULong(); } }
public static string Encrypt(ulong sender, ulong recipient) { var mt = new MersenneTwister((uint)Guid.NewGuid().GetHashCode()); var key = Encoding.UTF8.GetBytes(((FusionBotConfig)Globals.Bot.Config).DonationPaymentIdKey); if (sender == 0) { sender = mt.NextULong(); } if (recipient == 0) { recipient = mt.NextULong(); } var iv = mt.NextBytes(BLOCK_SIZE); var data = BitShifter.ToByte(sender).Concat(BitShifter.ToByte(recipient)).ToArray(); BufferedBlockCipher cipher = new CtsBlockCipher(new CbcBlockCipher(new AesEngine())); ICipherParameters keyParam = new ParametersWithIV(new KeyParameter(key), iv); cipher.Init(true, keyParam); return(iv.Concat(cipher.DoFinal(data, 0, data.Length)).ToArray().ToHex()); }
private void Measure(SpeedTestDataSourceRow a_row) { MersenneTwister r = new MersenneTwister(); { var ar = r.NextBytes(BYTES_ARRAY_LENGTH); a_row.BytesSpeed = Measure(ar.Length, () => { a_row.HashFunction.ComputeBytes(ar); }); } IFastHash32 hash = a_row.HashFunction as IFastHash32; if (hash == null) return; { var ar = r.NextBytes(BYTES_ARRAY_LENGTH / SINGLE_ELEMENT_RATIO); a_row.ByteSpeed = Measure(ar.Length, () => { for (int i = 0; i < ar.Length; i++) hash.ComputeByteFast(ar[i]); }); } { var ar = r.NextChars(BYTES_ARRAY_LENGTH / sizeof(char)); a_row.CharsSpeed = Measure(ar.Length * sizeof(char), () => { hash.ComputeCharsFast(ar); }); ar = ArrayExtensions.SubArray(ar, 0, ar.Length / SINGLE_ELEMENT_RATIO); a_row.CharSpeed = Measure(ar.Length * sizeof(char), () => { for (int i = 0; i < ar.Length; i++) hash.ComputeCharFast(ar[i]); }); } { var ar = r.NextShorts(BYTES_ARRAY_LENGTH / sizeof(short)); a_row.ShortsSpeed = Measure(ar.Length * sizeof(short), () => { hash.ComputeShortsFast(ar); }); ar = ArrayExtensions.SubArray(ar, 0, ar.Length / SINGLE_ELEMENT_RATIO); a_row.ShortSpeed = Measure(ar.Length * sizeof(short), () => { for (int i = 0; i < ar.Length; i++) hash.ComputeShortFast(ar[i]); }); } { var ar = r.NextUShorts(BYTES_ARRAY_LENGTH / sizeof(ushort)); a_row.UShortsSpeed = Measure(ar.Length * sizeof(ushort), () => { hash.ComputeUShortsFast(ar); }); ar = ArrayExtensions.SubArray(ar, 0, ar.Length / SINGLE_ELEMENT_RATIO); a_row.UShortSpeed = Measure(ar.Length * sizeof(ushort), () => { for (int i = 0; i < ar.Length; i++) hash.ComputeUShortFast(ar[i]); }); } { var ar = r.NextInts(BYTES_ARRAY_LENGTH / sizeof(int)); a_row.IntsSpeed = Measure(ar.Length * sizeof(int), () => { hash.ComputeIntsFast(ar); }); ar = ArrayExtensions.SubArray(ar, 0, ar.Length / SINGLE_ELEMENT_RATIO); a_row.IntSpeed = Measure(ar.Length * sizeof(int), () => { for (int i = 0; i < ar.Length; i++) hash.ComputeIntFast(ar[i]); }); } { var ar = r.NextUInts(BYTES_ARRAY_LENGTH / sizeof(uint)); a_row.UIntsSpeed = Measure(ar.Length * sizeof(uint), () => { hash.ComputeUIntsFast(ar); }); ar = ArrayExtensions.SubArray(ar, 0, ar.Length / SINGLE_ELEMENT_RATIO); a_row.UIntSpeed = Measure(ar.Length * sizeof(uint), () => { for (int i = 0; i < ar.Length; i++) hash.ComputeUIntFast(ar[i]); }); } { var ar = r.NextLongs(BYTES_ARRAY_LENGTH / sizeof(long)); a_row.LongsSpeed = Measure(ar.Length * sizeof(long), () => { hash.ComputeLongsFast(ar); }); ar = ArrayExtensions.SubArray(ar, 0, ar.Length / SINGLE_ELEMENT_RATIO); a_row.LongSpeed = Measure(ar.Length * sizeof(long), () => { for (int i = 0; i < ar.Length; i++) hash.ComputeLongFast(ar[i]); }); } { var ar = r.NextULongs(BYTES_ARRAY_LENGTH / sizeof(ulong)); a_row.ULongsSpeed = Measure(ar.Length * sizeof(ulong), () => { hash.ComputeULongsFast(ar); }); ar = ArrayExtensions.SubArray(ar, 0, ar.Length / SINGLE_ELEMENT_RATIO); a_row.ULongSpeed = Measure(ar.Length * sizeof(ulong), () => { for (int i = 0; i < ar.Length; i++) hash.ComputeULongFast(ar[i]); }); } { var ar = r.NextFloatsFullSafe(BYTES_ARRAY_LENGTH / sizeof(float)); a_row.FloatsSpeed = Measure(ar.Length * sizeof(float), () => { hash.ComputeFloatsFast(ar); }); ar = ArrayExtensions.SubArray(ar, 0, ar.Length / SINGLE_ELEMENT_RATIO); a_row.FloatSpeed = Measure(ar.Length * sizeof(float), () => { for (int i = 0; i < ar.Length; i++) hash.ComputeFloatFast(ar[i]); }); } { var ar = r.NextDoublesFullSafe(BYTES_ARRAY_LENGTH / sizeof(double)); a_row.DoublesSpeed = Measure(ar.Length * sizeof(double), () => { hash.ComputeDoublesFast(ar); }); ar = ArrayExtensions.SubArray(ar, 0, ar.Length / SINGLE_ELEMENT_RATIO); a_row.DoubleSpeed = Measure(ar.Length * sizeof(double), () => { for (int i = 0; i < ar.Length; i++) hash.ComputeDoubleFast(ar[i]); }); } { var ar = r.NextString(BYTES_ARRAY_LENGTH / sizeof(char)); a_row.StringSpeed = Measure(ar.Length * sizeof(char), () => { hash.ComputeStringFast(ar); }); } }
private void MeasureBytesPerCycle(SpeedTestDataSourceRow a_row) { Stopwatch sw = new Stopwatch(); IHash hash = a_row.HashFunction; MersenneTwister r = new MersenneTwister(); hash.Initialize(); var msg = r.NextBytes(BYTES_ARRAY_LENGTH_CYCLES_PER_SECOND); RDTSC.rdtsc(); hash.ComputeBytes(msg); int repeats = 0; ulong min_delta = UInt64.MaxValue; for (; ; ) { HighPriority(); try { sw.Start(); ulong before = RDTSC.rdtsc(); hash.ComputeBytes(msg); ulong after = RDTSC.rdtsc(); sw.Stop(); ulong delta = after - before; min_delta = Math.Min(min_delta, delta); } finally { NormalPriority(); } repeats++; if (sw.ElapsedMilliseconds > MEASURE_TIME) if (repeats > MIN_REPEATS) break; } a_row.BytesPerCycle = ((double)min_delta / BYTES_ARRAY_LENGTH_CYCLES_PER_SECOND).ToString("F2"); }
public void Test1() { var init = new ulong[] { 0x12345UL, 0x23456UL, 0x34567UL, 0x45678UL }; var mt = new MersenneTwister(init); // NextULong test mt.NextUInt64().Is(7266447313870364031UL); mt.NextUInt64().Is(4946485549665804864UL); mt.NextUInt64().Is(16945909448695747420UL); mt.NextUInt64().Is(16394063075524226720UL); mt.NextUInt64().Is(4873882236456199058UL); for (var i = 0; i < 990; i++) { mt.NextUInt64(); } mt.NextUInt64().Is(10197035660403006684UL); mt.NextUInt64().Is(13004818533162292132UL); mt.NextUInt64().Is(9831652587047067687UL); mt.NextUInt64().Is(7619315254749630976UL); mt.NextUInt64().Is(994412663058993407UL); // NextDouble test DoubleToString(mt.NextDouble()).Is("0.35252031"); DoubleToString(mt.NextDouble()).Is("0.51052342"); DoubleToString(mt.NextDouble()).Is("0.79771733"); DoubleToString(mt.NextDouble()).Is("0.39300273"); DoubleToString(mt.NextDouble()).Is("0.27216673"); string DoubleToString(double d) => d.ToString("F8"); // Reset(byte[]) test var size = init.Length * sizeof(ulong); byte[] seed = new byte[size]; Buffer.BlockCopy(init, 0, seed, 0, size); mt.Reset(init); mt.NextUInt64().Is(7266447313870364031UL); mt.NextUInt64().Is(4946485549665804864UL); mt.NextUInt64().Is(16945909448695747420UL); mt.NextUInt64().Is(16394063075524226720UL); mt.NextUInt64().Is(4873882236456199058UL); // NextBytes test var bytes = new byte[24]; var bytes2 = new byte[24]; var span = bytes.AsSpan(); BitConverter.TryWriteBytes(span, 7266447313870364031UL); span = span.Slice(8); BitConverter.TryWriteBytes(span, 4946485549665804864UL); span = span.Slice(8); BitConverter.TryWriteBytes(span, 16945909448695747420UL); for (var i = 0; i <= 8; i++) { mt.Reset(init); var span2 = bytes2.AsSpan(0, 16 + i); mt.NextBytes(span2); span2.SequenceEqual(bytes.AsSpan().Slice(0, 16 + i)).IsTrue(); } }
public void AdaptersTest_All() { { IHash md5 = HashFactory.Crypto.CreateMD5(); for(int i = 0; i < 5; i++) { var v = new byte[16]; new Random().NextBytes(v); Assert.IsFalse(!md5.ComputeBytes(v.Take(1).ToArray()).GetBytes().SequenceEqual(md5.ComputeByte(v[0]).GetBytes())); Assert.IsFalse(!md5.ComputeBytes(v.Take(2).ToArray()).GetBytes().SequenceEqual(md5.ComputeChar(BitConverter.ToChar(v, 0)).GetBytes())); Assert.IsFalse(!md5.ComputeBytes(v.Take(2).ToArray()).GetBytes().SequenceEqual(md5.ComputeShort(BitConverter.ToInt16(v, 0)).GetBytes())); Assert.IsFalse(!md5.ComputeBytes(v.Take(2).ToArray()).GetBytes().SequenceEqual(md5.ComputeUShort(BitConverter.ToUInt16(v, 0)).GetBytes())); Assert.IsFalse(!md5.ComputeBytes(v.Take(4).ToArray()).GetBytes().SequenceEqual(md5.ComputeInt(BitConverter.ToInt32(v, 0)).GetBytes())); Assert.IsFalse(!md5.ComputeBytes(v.Take(4).ToArray()).GetBytes().SequenceEqual(md5.ComputeUInt(BitConverter.ToUInt32(v, 0)).GetBytes())); Assert.IsFalse(!md5.ComputeBytes(v.Take(8).ToArray()).GetBytes().SequenceEqual(md5.ComputeLong(BitConverter.ToInt64(v, 0)).GetBytes())); Assert.IsFalse(!md5.ComputeBytes(v.Take(8).ToArray()).GetBytes().SequenceEqual(md5.ComputeULong(BitConverter.ToUInt64(v, 0)).GetBytes())); Assert.IsFalse(!md5.ComputeBytes(v.Take(8).ToArray()).GetBytes().SequenceEqual(md5.ComputeDouble(BitConverter.ToDouble(v, 0)).GetBytes())); Assert.IsFalse(!md5.ComputeBytes(v.Take(4).ToArray()).GetBytes().SequenceEqual(md5.ComputeFloat(Converters.ConvertBytesToFloat(v, 0)).GetBytes())); { string s = BitConverter.ToString(v); Assert.IsFalse(!md5.ComputeBytes(Converters.ConvertStringToBytes(s)).GetBytes().SequenceEqual(md5.ComputeString(s).GetBytes())); } Assert.IsFalse(!md5.ComputeBytes(v).GetBytes().SequenceEqual(md5.ComputeChars(Converters.ConvertBytesToChars(v)).GetBytes())); Assert.IsFalse(!md5.ComputeBytes(v).GetBytes().SequenceEqual(md5.ComputeShorts(Converters.ConvertBytesToShorts(v)).GetBytes())); Assert.IsFalse(!md5.ComputeBytes(v).GetBytes().SequenceEqual(md5.ComputeUShorts(Converters.ConvertBytesToUShorts(v)).GetBytes())); Assert.IsFalse(!md5.ComputeBytes(v).GetBytes().SequenceEqual(md5.ComputeInts(Converters.ConvertBytesToInts(v)).GetBytes())); Assert.IsFalse(!md5.ComputeBytes(v).GetBytes().SequenceEqual(md5.ComputeUInts(Converters.ConvertBytesToUInts(v)).GetBytes())); Assert.IsFalse(!md5.ComputeBytes(v).GetBytes().SequenceEqual(md5.ComputeLongs(Converters.ConvertBytesToLongs(v)).GetBytes())); Assert.IsFalse(!md5.ComputeBytes(v).GetBytes().SequenceEqual(md5.ComputeULongs(Converters.ConvertBytesToULongs(v)).GetBytes())); Assert.IsFalse(!md5.ComputeBytes(v).GetBytes().SequenceEqual(md5.ComputeFloats(Converters.ConvertBytesToFloats(v)).GetBytes())); Assert.IsFalse(!md5.ComputeBytes(v).GetBytes().SequenceEqual(md5.ComputeDoubles(Converters.ConvertBytesToDoubles(v)).GetBytes())); } } { IHash murmur2 = HashFactory.Hash32.CreateMurmur2(); for(int i = 0; i < 5; i++) { var v = new byte[16]; new Random().NextBytes(v); Assert.IsFalse(murmur2.ComputeBytes(v.Take(1).ToArray()) != murmur2.ComputeByte(v[0])); Assert.IsFalse(murmur2.ComputeBytes(v.Take(2).ToArray()) != murmur2.ComputeChar(BitConverter.ToChar(v, 0))); Assert.IsFalse(murmur2.ComputeBytes(v.Take(2).ToArray()) != murmur2.ComputeShort(BitConverter.ToInt16(v, 0))); Assert.IsFalse(murmur2.ComputeBytes(v.Take(2).ToArray()) != murmur2.ComputeUShort(BitConverter.ToUInt16(v, 0))); Assert.IsFalse(murmur2.ComputeBytes(v.Take(4).ToArray()) != murmur2.ComputeInt(BitConverter.ToInt32(v, 0))); Assert.IsFalse(murmur2.ComputeBytes(v.Take(4).ToArray()) != murmur2.ComputeUInt(BitConverter.ToUInt32(v, 0))); Assert.IsFalse(murmur2.ComputeBytes(v.Take(8).ToArray()) != murmur2.ComputeLong(BitConverter.ToInt64(v, 0))); Assert.IsFalse(murmur2.ComputeBytes(v.Take(8).ToArray()) != murmur2.ComputeULong(BitConverter.ToUInt64(v, 0))); Assert.IsFalse(murmur2.ComputeBytes(v.Take(8).ToArray()) != murmur2.ComputeDouble(BitConverter.ToDouble(v, 0))); Assert.IsFalse(murmur2.ComputeBytes(v.Take(4).ToArray()) != murmur2.ComputeFloat(Converters.ConvertBytesToFloat(v, 0))); { string s = BitConverter.ToString(v); Assert.IsFalse(murmur2.ComputeBytes(Converters.ConvertStringToBytes(s)) != murmur2.ComputeString(s)); } Assert.IsFalse(murmur2.ComputeBytes(v) != murmur2.ComputeChars(Converters.ConvertBytesToChars(v))); Assert.IsFalse(murmur2.ComputeBytes(v) != murmur2.ComputeShorts(Converters.ConvertBytesToShorts(v))); Assert.IsFalse(murmur2.ComputeBytes(v) != murmur2.ComputeUShorts(Converters.ConvertBytesToUShorts(v))); Assert.IsFalse(murmur2.ComputeBytes(v) != murmur2.ComputeInts(Converters.ConvertBytesToInts(v))); Assert.IsFalse(murmur2.ComputeBytes(v) != murmur2.ComputeUInts(Converters.ConvertBytesToUInts(v))); Assert.IsFalse(murmur2.ComputeBytes(v) != murmur2.ComputeLongs(Converters.ConvertBytesToLongs(v))); Assert.IsFalse(murmur2.ComputeBytes(v) != murmur2.ComputeULongs(Converters.ConvertBytesToULongs(v))); Assert.IsFalse(murmur2.ComputeBytes(v) != murmur2.ComputeFloats(Converters.ConvertBytesToFloats(v))); Assert.IsFalse(murmur2.ComputeBytes(v) != murmur2.ComputeDoubles(Converters.ConvertBytesToDoubles(v))); } } { IHash murmur2 = HashFactory.Hash64.CreateMurmur2(); for(int i = 0; i < 5; i++) { var v = new byte[16]; new Random().NextBytes(v); Assert.IsFalse(murmur2.ComputeBytes(v.Take(1).ToArray()) != murmur2.ComputeByte(v[0])); Assert.IsFalse(murmur2.ComputeBytes(v.Take(2).ToArray()) != murmur2.ComputeChar(BitConverter.ToChar(v, 0))); Assert.IsFalse(murmur2.ComputeBytes(v.Take(2).ToArray()) != murmur2.ComputeShort(BitConverter.ToInt16(v, 0))); Assert.IsFalse(murmur2.ComputeBytes(v.Take(2).ToArray()) != murmur2.ComputeUShort(BitConverter.ToUInt16(v, 0))); Assert.IsFalse(murmur2.ComputeBytes(v.Take(4).ToArray()) != murmur2.ComputeInt(BitConverter.ToInt32(v, 0))); Assert.IsFalse(murmur2.ComputeBytes(v.Take(4).ToArray()) != murmur2.ComputeUInt(BitConverter.ToUInt32(v, 0))); Assert.IsFalse(murmur2.ComputeBytes(v.Take(8).ToArray()) != murmur2.ComputeLong(BitConverter.ToInt64(v, 0))); Assert.IsFalse(murmur2.ComputeBytes(v.Take(8).ToArray()) != murmur2.ComputeULong(BitConverter.ToUInt64(v, 0))); Assert.IsFalse(murmur2.ComputeBytes(v.Take(8).ToArray()) != murmur2.ComputeDouble(BitConverter.ToDouble(v, 0))); Assert.IsFalse(murmur2.ComputeBytes(v.Take(4).ToArray()) != murmur2.ComputeFloat(Converters.ConvertBytesToFloat(v, 0))); { string s = BitConverter.ToString(v); Assert.IsFalse(murmur2.ComputeBytes(Converters.ConvertStringToBytes(s)) != murmur2.ComputeString(s)); } Assert.IsFalse(murmur2.ComputeBytes(v) != murmur2.ComputeChars(Converters.ConvertBytesToChars(v))); Assert.IsFalse(murmur2.ComputeBytes(v) != murmur2.ComputeShorts(Converters.ConvertBytesToShorts(v))); Assert.IsFalse(murmur2.ComputeBytes(v) != murmur2.ComputeUShorts(Converters.ConvertBytesToUShorts(v))); Assert.IsFalse(murmur2.ComputeBytes(v) != murmur2.ComputeInts(Converters.ConvertBytesToInts(v))); Assert.IsFalse(murmur2.ComputeBytes(v) != murmur2.ComputeUInts(Converters.ConvertBytesToUInts(v))); Assert.IsFalse(murmur2.ComputeBytes(v) != murmur2.ComputeLongs(Converters.ConvertBytesToLongs(v))); Assert.IsFalse(murmur2.ComputeBytes(v) != murmur2.ComputeULongs(Converters.ConvertBytesToULongs(v))); Assert.IsFalse(murmur2.ComputeBytes(v) != murmur2.ComputeFloats(Converters.ConvertBytesToFloats(v))); Assert.IsFalse(murmur2.ComputeBytes(v) != murmur2.ComputeDoubles(Converters.ConvertBytesToDoubles(v))); } } { // Adapters.TransformObjectHash() test IHash md5 = HashFactory.Crypto.CreateMD5(); var ms1 = new MersenneTwister(5566); var ms2 = new MersenneTwister(5566); Assert.AreEqual(md5.ComputeByte(ms1.NextByte()), md5.ComputeObject(ms2.NextByte())); Assert.AreEqual(md5.ComputeShort(ms1.NextShort()), md5.ComputeObject(ms2.NextShort())); Assert.AreEqual(md5.ComputeUShort(ms1.NextUShort()), md5.ComputeObject(ms2.NextUShort())); Assert.AreEqual(md5.ComputeChar(ms1.NextChar()), md5.ComputeObject(ms2.NextChar())); Assert.AreEqual(md5.ComputeInt(ms1.NextInt()), md5.ComputeObject(ms2.NextInt())); Assert.AreEqual(md5.ComputeUInt(ms1.NextUInt()), md5.ComputeObject(ms2.NextUInt())); Assert.AreEqual(md5.ComputeLong(ms1.NextLong()), md5.ComputeObject(ms2.NextLong())); Assert.AreEqual(md5.ComputeULong(ms1.NextULong()), md5.ComputeObject(ms2.NextULong())); Assert.AreEqual(md5.ComputeDouble(ms1.NextDoubleFull()), md5.ComputeObject(ms2.NextDoubleFull())); Assert.AreEqual(md5.ComputeFloat(ms1.NextFloatFull()), md5.ComputeObject(ms2.NextFloatFull())); Assert.AreEqual(md5.ComputeString(ms1.NextString(20)), md5.ComputeObject(ms2.NextString(20))); Assert.AreEqual(md5.ComputeBytes(ms1.NextBytes(20)), md5.ComputeObject(ms2.NextBytes(20))); Assert.AreEqual(md5.ComputeShorts(ms1.NextShorts(20)), md5.ComputeObject(ms2.NextShorts(20))); Assert.AreEqual(md5.ComputeUShorts(ms1.NextUShorts(20)), md5.ComputeObject(ms2.NextUShorts(20))); Assert.AreEqual(md5.ComputeChars(ms1.NextChars(20)), md5.ComputeObject(ms2.NextChars(20))); Assert.AreEqual(md5.ComputeInts(ms1.NextInts(20)), md5.ComputeObject(ms2.NextInts(20))); Assert.AreEqual(md5.ComputeUInts(ms1.NextUInts(20)), md5.ComputeObject(ms2.NextUInts(20))); Assert.AreEqual(md5.ComputeLongs(ms1.NextLongs(20)), md5.ComputeObject(ms2.NextLongs(20))); Assert.AreEqual(md5.ComputeULongs(ms1.NextULongs(20)), md5.ComputeObject(ms2.NextULongs(20))); Assert.AreEqual(md5.ComputeDoubles(ms1.NextDoublesFull(20)), md5.ComputeObject(ms2.NextDoublesFull(20))); Assert.AreEqual(md5.ComputeFloats(ms1.NextFloatsFull(20)), md5.ComputeObject(ms2.NextFloatsFull(20))); } { // Adapters.ComputeObjectHash() test IHash murmur2 = HashFactory.Hash32.CreateMurmur2(); var ms1 = new MersenneTwister(5566); var ms2 = new MersenneTwister(5566); Assert.AreEqual(murmur2.ComputeByte(ms1.NextByte()), murmur2.ComputeObject(ms2.NextByte())); Assert.AreEqual(murmur2.ComputeShort(ms1.NextShort()), murmur2.ComputeObject(ms2.NextShort())); Assert.AreEqual(murmur2.ComputeUShort(ms1.NextUShort()), murmur2.ComputeObject(ms2.NextUShort())); Assert.AreEqual(murmur2.ComputeChar(ms1.NextChar()), murmur2.ComputeObject(ms2.NextChar())); Assert.AreEqual(murmur2.ComputeInt(ms1.NextInt()), murmur2.ComputeObject(ms2.NextInt())); Assert.AreEqual(murmur2.ComputeUInt(ms1.NextUInt()), murmur2.ComputeObject(ms2.NextUInt())); Assert.AreEqual(murmur2.ComputeLong(ms1.NextLong()), murmur2.ComputeObject(ms2.NextLong())); Assert.AreEqual(murmur2.ComputeULong(ms1.NextULong()), murmur2.ComputeObject(ms2.NextULong())); Assert.AreEqual(murmur2.ComputeDouble(ms1.NextDoubleFull()), murmur2.ComputeObject(ms2.NextDoubleFull())); Assert.AreEqual(murmur2.ComputeFloat(ms1.NextFloatFull()), murmur2.ComputeObject(ms2.NextFloatFull())); Assert.AreEqual(murmur2.ComputeString(ms1.NextString(20)), murmur2.ComputeObject(ms2.NextString(20))); Assert.AreEqual(murmur2.ComputeBytes(ms1.NextBytes(20)), murmur2.ComputeObject(ms2.NextBytes(20))); Assert.AreEqual(murmur2.ComputeShorts(ms1.NextShorts(20)), murmur2.ComputeObject(ms2.NextShorts(20))); Assert.AreEqual(murmur2.ComputeUShorts(ms1.NextUShorts(20)), murmur2.ComputeObject(ms2.NextUShorts(20))); Assert.AreEqual(murmur2.ComputeChars(ms1.NextChars(20)), murmur2.ComputeObject(ms2.NextChars(20))); Assert.AreEqual(murmur2.ComputeInts(ms1.NextInts(20)), murmur2.ComputeObject(ms2.NextInts(20))); Assert.AreEqual(murmur2.ComputeUInts(ms1.NextUInts(20)), murmur2.ComputeObject(ms2.NextUInts(20))); Assert.AreEqual(murmur2.ComputeLongs(ms1.NextLongs(20)), murmur2.ComputeObject(ms2.NextLongs(20))); Assert.AreEqual(murmur2.ComputeULongs(ms1.NextULongs(20)), murmur2.ComputeObject(ms2.NextULongs(20))); Assert.AreEqual(murmur2.ComputeDoubles(ms1.NextDoublesFull(20)), murmur2.ComputeObject(ms2.NextDoublesFull(20))); Assert.AreEqual(murmur2.ComputeFloats(ms1.NextFloatsFull(20)), murmur2.ComputeObject(ms2.NextFloatsFull(20))); } int old = Hash.BUFFER_SIZE; Hash.BUFFER_SIZE = 26; { IHash md5 = HashFactory.Crypto.CreateMD5(); for(int i = 0; i < 10; i++) { var v = new byte[13 * i]; new Random().NextBytes(v); using(var ms = new MemoryStream(v)) { HashResult h1 = md5.ComputeBytes(v); HashResult h2 = md5.ComputeStream(ms); Assert.AreEqual(h1, h2); h1 = md5.ComputeBytes(v.SubArray(i, i * 7)); ms.Seek(i, SeekOrigin.Begin); h2 = md5.ComputeStream(ms, i * 7); Assert.AreEqual(h1, h2); h1 = md5.ComputeBytes(v); md5.Initialize(); md5.TransformBytes(v, 0, i * 3); ms.Seek(i * 3, SeekOrigin.Begin); md5.TransformStream(ms, i * 2); md5.TransformBytes(v, i * 5); h2 = md5.TransformFinal(); Assert.AreEqual(h1, h2); } } } { IHash murmur2 = HashFactory.Hash32.CreateMurmur2(); for(int i = 0; i < 10; i++) { var v = new byte[13 * i]; new Random().NextBytes(v); using(var ms = new MemoryStream(v)) { HashResult h1 = murmur2.ComputeBytes(v); HashResult h2 = murmur2.ComputeStream(ms); Assert.AreEqual(h1, h2); h1 = murmur2.ComputeBytes(v.SubArray(i, i * 7)); ms.Seek(i, SeekOrigin.Begin); h2 = murmur2.ComputeStream(ms, i * 7); Assert.AreEqual(h1, h2); h1 = murmur2.ComputeBytes(v); murmur2.Initialize(); murmur2.TransformBytes(v, 0, i * 3); ms.Seek(i * 3, SeekOrigin.Begin); murmur2.TransformStream(ms, i * 2); murmur2.TransformBytes(v, i * 5); h2 = murmur2.TransformFinal(); Assert.AreEqual(h1, h2); } } } { IHash murmur2 = HashFactory.Hash64.CreateMurmur2(); for(int i = 0; i < 10; i++) { var v = new byte[13 * i]; new Random().NextBytes(v); using(var ms = new MemoryStream(v)) { HashResult h1 = murmur2.ComputeBytes(v); HashResult h2 = murmur2.ComputeStream(ms); Assert.AreEqual(h1, h2); h1 = murmur2.ComputeBytes(v.SubArray(i, i * 7)); ms.Seek(i, SeekOrigin.Begin); h2 = murmur2.ComputeStream(ms, i * 7); Assert.AreEqual(h1, h2); h1 = murmur2.ComputeBytes(v); murmur2.Initialize(); murmur2.TransformBytes(v, 0, i * 3); ms.Seek(i * 3, SeekOrigin.Begin); murmur2.TransformStream(ms, i * 2); murmur2.TransformBytes(v, i * 5); h2 = murmur2.TransformFinal(); Assert.AreEqual(h1, h2); } } } Hash.BUFFER_SIZE = old; { IHash md5 = HashFactory.Crypto.CreateMD5(); for(int i = 0; i < 10; i++) { var v = new byte[13 * i]; new Random().NextBytes(v); string file_name = Path.GetTempFileName(); using(var fs = new FileStream(file_name, FileMode.Truncate)) { var bw = new BinaryWriter(fs); bw.Write(v); } HashResult h1 = md5.ComputeBytes(v); HashResult h2 = md5.ComputeFile(file_name); Assert.AreEqual(h1, h2); h1 = md5.ComputeBytes(v.SubArray(i, i * 7)); h2 = md5.ComputeFile(file_name, i, i * 7); Assert.AreEqual(h1, h2); h1 = md5.ComputeBytes(v); md5.Initialize(); md5.TransformBytes(v, 0, i * 3); md5.TransformFile(file_name, i * 3, i * 2); md5.TransformBytes(v, i * 5); h2 = md5.TransformFinal(); Assert.AreEqual(h1, h2); } } { IHash murmur2 = HashFactory.Hash32.CreateMurmur2(); for(int i = 0; i < 10; i++) { var v = new byte[13 * i]; new Random().NextBytes(v); string file_name = Path.GetTempFileName(); using(var fs = new FileStream(file_name, FileMode.Truncate)) { var bw = new BinaryWriter(fs); bw.Write(v); } HashResult h1 = murmur2.ComputeBytes(v); HashResult h2 = murmur2.ComputeFile(file_name); Assert.AreEqual(h1, h2); h1 = murmur2.ComputeBytes(v.SubArray(i, i * 7)); h2 = murmur2.ComputeFile(file_name, i, i * 7); Assert.AreEqual(h1, h2); h1 = murmur2.ComputeBytes(v); murmur2.Initialize(); murmur2.TransformBytes(v, 0, i * 3); murmur2.TransformFile(file_name, i * 3, i * 2); murmur2.TransformBytes(v, i * 5); h2 = murmur2.TransformFinal(); Assert.AreEqual(h1, h2); } } { IHash murmur2 = HashFactory.Hash64.CreateMurmur2(); for(int i = 0; i < 10; i++) { var v = new byte[13 * i]; new Random().NextBytes(v); string file_name = Path.GetTempFileName(); using(var fs = new FileStream(file_name, FileMode.Truncate)) { var bw = new BinaryWriter(fs); bw.Write(v); } HashResult h1 = murmur2.ComputeBytes(v); HashResult h2 = murmur2.ComputeFile(file_name); Assert.AreEqual(h1, h2); h1 = murmur2.ComputeBytes(v.SubArray(i, i * 7)); h2 = murmur2.ComputeFile(file_name, i, i * 7); Assert.AreEqual(h1, h2); h1 = murmur2.ComputeBytes(v); murmur2.Initialize(); murmur2.TransformBytes(v, 0, i * 3); murmur2.TransformFile(file_name, i * 3, i * 2); murmur2.TransformBytes(v, i * 5); h2 = murmur2.TransformFinal(); Assert.AreEqual(h1, h2); } } { new HashesTestBase().TestAgainstBaseImplementationArrayType( HashFactory.Crypto.CreateSHA1(), HashFactory.Wrappers.HashAlgorithmToHash( HashFactory.Wrappers.HashToHashAlgorithm( HashFactory.Crypto.CreateSHA1()), HashFactory.Crypto.CreateSHA1().BlockSize ) ); } }
private void Measure(SpeedTestDataSourceRow a_row) { MersenneTwister r = new MersenneTwister(); { var ar = r.NextBytes(BYTES_ARRAY_LENGTH); a_row.BytesSpeed = Measure(ar.Length, () => { a_row.HashFunction.ComputeBytes(ar); }); } IFastHash32 hash = a_row.HashFunction as IFastHash32; if (hash == null) { return; } { var ar = r.NextBytes(BYTES_ARRAY_LENGTH / SINGLE_ELEMENT_RATIO); a_row.ByteSpeed = Measure(ar.Length, () => { for (int i = 0; i < ar.Length; i++) { hash.ComputeByteFast(ar[i]); } }); } { var ar = r.NextChars(BYTES_ARRAY_LENGTH / sizeof(char)); a_row.CharsSpeed = Measure(ar.Length * sizeof(char), () => { hash.ComputeCharsFast(ar); }); ar = ArrayExtensions.SubArray(ar, 0, ar.Length / SINGLE_ELEMENT_RATIO); a_row.CharSpeed = Measure(ar.Length * sizeof(char), () => { for (int i = 0; i < ar.Length; i++) { hash.ComputeCharFast(ar[i]); } }); } { var ar = r.NextShorts(BYTES_ARRAY_LENGTH / sizeof(short)); a_row.ShortsSpeed = Measure(ar.Length * sizeof(short), () => { hash.ComputeShortsFast(ar); }); ar = ArrayExtensions.SubArray(ar, 0, ar.Length / SINGLE_ELEMENT_RATIO); a_row.ShortSpeed = Measure(ar.Length * sizeof(short), () => { for (int i = 0; i < ar.Length; i++) { hash.ComputeShortFast(ar[i]); } }); } { var ar = r.NextUShorts(BYTES_ARRAY_LENGTH / sizeof(ushort)); a_row.UShortsSpeed = Measure(ar.Length * sizeof(ushort), () => { hash.ComputeUShortsFast(ar); }); ar = ArrayExtensions.SubArray(ar, 0, ar.Length / SINGLE_ELEMENT_RATIO); a_row.UShortSpeed = Measure(ar.Length * sizeof(ushort), () => { for (int i = 0; i < ar.Length; i++) { hash.ComputeUShortFast(ar[i]); } }); } { var ar = r.NextInts(BYTES_ARRAY_LENGTH / sizeof(int)); a_row.IntsSpeed = Measure(ar.Length * sizeof(int), () => { hash.ComputeIntsFast(ar); }); ar = ArrayExtensions.SubArray(ar, 0, ar.Length / SINGLE_ELEMENT_RATIO); a_row.IntSpeed = Measure(ar.Length * sizeof(int), () => { for (int i = 0; i < ar.Length; i++) { hash.ComputeIntFast(ar[i]); } }); } { var ar = r.NextUInts(BYTES_ARRAY_LENGTH / sizeof(uint)); a_row.UIntsSpeed = Measure(ar.Length * sizeof(uint), () => { hash.ComputeUIntsFast(ar); }); ar = ArrayExtensions.SubArray(ar, 0, ar.Length / SINGLE_ELEMENT_RATIO); a_row.UIntSpeed = Measure(ar.Length * sizeof(uint), () => { for (int i = 0; i < ar.Length; i++) { hash.ComputeUIntFast(ar[i]); } }); } { var ar = r.NextLongs(BYTES_ARRAY_LENGTH / sizeof(long)); a_row.LongsSpeed = Measure(ar.Length * sizeof(long), () => { hash.ComputeLongsFast(ar); }); ar = ArrayExtensions.SubArray(ar, 0, ar.Length / SINGLE_ELEMENT_RATIO); a_row.LongSpeed = Measure(ar.Length * sizeof(long), () => { for (int i = 0; i < ar.Length; i++) { hash.ComputeLongFast(ar[i]); } }); } { var ar = r.NextULongs(BYTES_ARRAY_LENGTH / sizeof(ulong)); a_row.ULongsSpeed = Measure(ar.Length * sizeof(ulong), () => { hash.ComputeULongsFast(ar); }); ar = ArrayExtensions.SubArray(ar, 0, ar.Length / SINGLE_ELEMENT_RATIO); a_row.ULongSpeed = Measure(ar.Length * sizeof(ulong), () => { for (int i = 0; i < ar.Length; i++) { hash.ComputeULongFast(ar[i]); } }); } { var ar = r.NextFloatsFullSafe(BYTES_ARRAY_LENGTH / sizeof(float)); a_row.FloatsSpeed = Measure(ar.Length * sizeof(float), () => { hash.ComputeFloatsFast(ar); }); ar = ArrayExtensions.SubArray(ar, 0, ar.Length / SINGLE_ELEMENT_RATIO); a_row.FloatSpeed = Measure(ar.Length * sizeof(float), () => { for (int i = 0; i < ar.Length; i++) { hash.ComputeFloatFast(ar[i]); } }); } { var ar = r.NextDoublesFullSafe(BYTES_ARRAY_LENGTH / sizeof(double)); a_row.DoublesSpeed = Measure(ar.Length * sizeof(double), () => { hash.ComputeDoublesFast(ar); }); ar = ArrayExtensions.SubArray(ar, 0, ar.Length / SINGLE_ELEMENT_RATIO); a_row.DoubleSpeed = Measure(ar.Length * sizeof(double), () => { for (int i = 0; i < ar.Length; i++) { hash.ComputeDoubleFast(ar[i]); } }); } { var ar = r.NextString(BYTES_ARRAY_LENGTH / sizeof(char)); a_row.StringSpeed = Measure(ar.Length * sizeof(char), () => { hash.ComputeStringFast(ar); }); } }
public void Save() { var fs = new FileStream(GetFileName(), FileMode.Create); using(var sw = new StreamWriter(fs)) { var random = new MersenneTwister(4563487); for(int i = 0; i <= m_hash.BlockSize * 3 + 1; i++) { byte[] data = random.NextBytes(i); sw.Write(Convert.ToBase64String(data)); sw.Write("|"); sw.WriteLine(Convert.ToBase64String(m_hash.ComputeBytes(data).GetBytes())); } } }