internal void TestAgainstBaseImplementationArrayType(IHash a_baseHashFunction, IHash a_hash) { for(int j = 0; j < 2; j++) { for(int i = 9; i <= (a_hash.BlockSize * 3 + 1); i++) { byte[] v = m_random.NextBytes(i); HashResult h1 = a_baseHashFunction.ComputeBytes(v); HashResult h2 = a_hash.ComputeBytes(v); Assert.AreEqual(h1, h2, String.Format("{0}, {1}", a_hash.Name, i)); } } }
} // end function TestHMACCloneIsCorrect public static void TestActualAndExpectedData(object actual, object expected, IHash i_hash) { string ActualString, name = actual.GetType().Name; if (name == "String") { ActualString = i_hash?.ComputeString((string)actual, Encoding.UTF8).ToString(); } else if (actual.GetType().Name == "Byte[]") { ActualString = i_hash?.ComputeBytes((byte[])actual).ToString(); } else { throw new NotImplementedException("Kindly implement new type."); } Assert.AreEqual(expected, ActualString, String.Format("Expected {0} but got {1}.", expected, ActualString)); }
public override byte[] Hash(byte[] input, dynamic config) { return(_hasher.ComputeBytes(input).GetBytes()); }
private void TestFastHash32(IHash a_hash) { IFastHash32 fh = a_hash as IFastHash32; if (fh == null) { return; } for (int i = 0; i < 10; i++) { { var data = m_random.NextBytes((i == 0) ? 0 : (int)(m_random.NextUInt() % 200)); var h1 = fh.ComputeBytesFast(data); var h2 = a_hash.ComputeBytes(data); Assert.AreEqual(h1, h2.GetInt()); } { var data = m_random.NextInt(); var h1 = fh.ComputeIntFast(data); var h2 = a_hash.ComputeInt(data); Assert.AreEqual(h1, h2.GetInt()); } { var data = m_random.NextUInt(); var h1 = fh.ComputeUIntFast(data); var h2 = a_hash.ComputeUInt(data); Assert.AreEqual(h1, h2.GetInt()); } { var data = m_random.NextByte(); var h1 = fh.ComputeByteFast(data); var h2 = a_hash.ComputeByte(data); Assert.AreEqual(h1, h2.GetInt()); } { var data = m_random.NextChar(); var h1 = fh.ComputeCharFast(data); var h2 = a_hash.ComputeChar(data); Assert.AreEqual(h1, h2.GetInt()); } { var data = m_random.NextShort(); var h1 = fh.ComputeShortFast(data); var h2 = a_hash.ComputeShort(data); Assert.AreEqual(h1, h2.GetInt()); } { var data = m_random.NextUShort(); var h1 = fh.ComputeUShortFast(data); var h2 = a_hash.ComputeUShort(data); Assert.AreEqual(h1, h2.GetInt()); } { var data = m_random.NextDoubleFull(); var h1 = fh.ComputeDoubleFast(data); var h2 = a_hash.ComputeDouble(data); Assert.AreEqual(h1, h2.GetInt()); } { var data = m_random.NextFloatFull(); var h1 = fh.ComputeFloatFast(data); var h2 = a_hash.ComputeFloat(data); Assert.AreEqual(h1, h2.GetInt()); } { var data = m_random.NextLong(); var h1 = fh.ComputeLongFast(data); var h2 = a_hash.ComputeLong(data); Assert.AreEqual(h1, h2.GetInt()); } { var data = m_random.NextULong(); var h1 = fh.ComputeULongFast(data); var h2 = a_hash.ComputeULong(data); Assert.AreEqual(h1, h2.GetInt()); } { var data = m_random.NextUInts((i == 0) ? 0 : (int)(m_random.NextUInt() % 200)); var h1 = fh.ComputeUIntsFast(data); var h2 = a_hash.ComputeUInts(data); Assert.AreEqual(h1, h2.GetInt()); } { var data = m_random.NextInts((i == 0) ? 0 : (int)(m_random.NextUInt() % 200)); var h1 = fh.ComputeIntsFast(data); var h2 = a_hash.ComputeInts(data); Assert.AreEqual(h1, h2.GetInt()); } { var data = m_random.NextLongs((i == 0) ? 0 : (int)(m_random.NextUInt() % 200)); var h1 = fh.ComputeLongsFast(data); var h2 = a_hash.ComputeLongs(data); Assert.AreEqual(h1, h2.GetInt()); } { var data = m_random.NextULongs((i == 0) ? 0 : (int)(m_random.NextUInt() % 200)); var h1 = fh.ComputeULongsFast(data); var h2 = a_hash.ComputeULongs(data); Assert.AreEqual(h1, h2.GetInt()); } { var data = m_random.NextDoublesFull((i == 0) ? 0 : (int)(m_random.NextUInt() % 200)); var h1 = fh.ComputeDoublesFast(data); var h2 = a_hash.ComputeDoubles(data); Assert.AreEqual(h1, h2.GetInt()); } { var data = m_random.NextFloatsFull((i == 0) ? 0 : (int)(m_random.NextUInt() % 200)); var h1 = fh.ComputeFloatsFast(data); var h2 = a_hash.ComputeFloats(data); Assert.AreEqual(h1, h2.GetInt()); } { var data = m_random.NextChars((i == 0) ? 0 : (int)(m_random.NextUInt() % 200)); var h1 = fh.ComputeCharsFast(data); var h2 = a_hash.ComputeChars(data); Assert.AreEqual(h1, h2.GetInt()); } { var data = m_random.NextUShorts((i == 0) ? 0 : (int)(m_random.NextUInt() % 200)); var h1 = fh.ComputeUShortsFast(data); var h2 = a_hash.ComputeUShorts(data); Assert.AreEqual(h1, h2.GetInt()); } { var data = m_random.NextString((i == 0) ? 0 : (int)(m_random.NextUInt() % 200)); var h1 = fh.ComputeStringFast(data); var h2 = a_hash.ComputeString(data); Assert.AreEqual(h1, h2.GetInt()); } { var data = m_random.NextShorts((i == 0) ? 0 : (int)(m_random.NextUInt() % 200)); var h1 = fh.ComputeShortsFast(data); var h2 = a_hash.ComputeShorts(data); Assert.AreEqual(h1, h2.GetInt()); } } }
protected void TestHashFile(IHash a_hash, int a_block_size) { string file_name = System.IO.Path.GetTempFileName(); int old = Hash.BUFFER_SIZE; Hash.BUFFER_SIZE = a_block_size; try { for (int i = 1; i < 10; i++) { byte[] v = new byte[13 * i]; new Random().NextBytes(v); using (FileStream fs = new FileStream(file_name, FileMode.Truncate)) { BinaryWriter bw = new BinaryWriter(fs); bw.Write(v); } HashResult h1 = a_hash.ComputeBytes(v); HashResult h2 = a_hash.ComputeFile(file_name); Assert.AreEqual(h1, h2); h1 = a_hash.ComputeBytes(HashLib.ArrayExtensions.SubArray(v, i, i * 7)); h2 = a_hash.ComputeFile(file_name, i, i * 7); Assert.AreEqual(h1, h2); h1 = a_hash.ComputeBytes(v); a_hash.Initialize(); a_hash.TransformBytes(v, 0, i * 3); a_hash.TransformFile(file_name, i * 3, i * 2); a_hash.TransformBytes(v, i * 5); h2 = a_hash.TransformFinal(); Assert.AreEqual(h1, h2); } int[] sizes = { 1, 11, 63, 64, 65, 127, 128, 129, 255, 256, 257, 511, 512, 513, 1023, 1024, 1025, 4011, 64000, 66000, 250000, Hash.BUFFER_SIZE * 20 + 511 }; foreach (int size in sizes) { byte[] v = new byte[size]; new Random().NextBytes(v); using (FileStream fs = new FileStream(file_name, FileMode.Truncate)) { BinaryWriter bw = new BinaryWriter(fs); bw.Write(v); } HashResult h1 = a_hash.ComputeBytes(v); HashResult h2 = a_hash.ComputeFile(file_name); Assert.AreEqual(h1, h2); } { byte[] v = new byte[1011]; new Random().NextBytes(v); using (FileStream fs = new FileStream(file_name, FileMode.Truncate)) { BinaryWriter bw = new BinaryWriter(fs); bw.Write(v); } bool ex = false; try { a_hash.ComputeFile(file_name, 1011, -1); } catch { ex = true; } Assert.IsFalse(ex); ex = false; try { a_hash.ComputeFile(file_name, 1010, 2); } catch { ex = true; } Assert.IsTrue(ex); ex = false; try { a_hash.ComputeFile(file_name, 0, 1012); } catch { ex = true; } Assert.IsTrue(ex); Assert.IsTrue(ex); } } finally { Hash.BUFFER_SIZE = old; new FileInfo(file_name).Delete(); } }
protected void TestHashSize(IHash a_hash) { Assert.AreEqual(a_hash.HashSize, a_hash.ComputeBytes(new byte[] { }).GetBytes().Length); }
public static byte[] Compute(byte[] input) { return(Hash.ComputeBytes(input).GetBytes()); }
protected void TestAgainstBaseImplementationValueType(IHash a_baseHashFunction, IHash a_hash) { for(int j = 0; j < 2; j++) { for(int i = 0; i <= (a_hash.BlockSize * 3 + 1); i++) { byte[] v = m_random.NextBytes(i); Assert.AreEqual(a_baseHashFunction.ComputeBytes(v), a_hash.ComputeBytes(v), String.Format("{0}, {1}", a_hash.Name, i)); } } }
protected void TestMultipleTransformsValueType(IHash a_multi, IHash a_hash) { TestMultipleTransformsValueType(a_multi, a_hash, new List<int> {0, 0}); TestMultipleTransformsValueType(a_multi, a_hash, new List<int> {1, 0}); TestMultipleTransformsValueType(a_multi, a_hash, new List<int> {0, 1}); for(int tries = 0; tries < 10; tries++) { int parts = m_random.Next(20) + 1; var list = new List<int>(parts); for(int i = 0; i < parts; i++) list.Add(m_random.Next(a_multi.BlockSize * 3 + 1)); TestMultipleTransformsValueType(a_multi, a_hash, list); } List<object> objects; byte[] bytes; for(int i = 0; i < 50; i++) { CreateListForDataTest(i, out objects, out bytes); a_hash.TransformByte(0x55); HashResult h1 = a_hash.ComputeBytes(bytes); a_multi.TransformByte(0x55); a_multi.Initialize(); foreach(object o in objects) a_multi.TransformObject(o); HashResult h2 = a_multi.TransformFinal(); Assert.AreEqual(h1, h2, String.Format("{0}, {1}", a_hash.Name, i)); } }
public void MemPool_AddedNonUniqueTransactions_NotAllContained() { SynchronizationDescriptor synchronizationDescriptor = new SynchronizationDescriptor(1, new byte[Globals.DEFAULT_HASH_SIZE], DateTime.Now, DateTime.Now, 1); IHash transactionKeyHash = HashFactory.Hash128.CreateMurmur3_128(); ILogger logger = Substitute.For <ILogger>(); ILoggerService loggerService = Substitute.For <ILoggerService>(); IIdentityKeyProvider identityKeyProvider = new TransactionRegistryKeyProvider(); IIdentityKeyProvidersRegistry identityKeyProvidersRegistry = Substitute.For <IIdentityKeyProvidersRegistry>(); IHashCalculationsRepository hashCalculationsRepository = Substitute.For <IHashCalculationsRepository>(); ICryptoService cryptoService = Substitute.For <ICryptoService>(); SynchronizationContext synchronizationContext = new SynchronizationContext(loggerService); synchronizationContext.UpdateLastSyncBlockDescriptor(synchronizationDescriptor); IStatesRepository statesRepository = Substitute.For <IStatesRepository>(); hashCalculationsRepository.Create(HashType.MurMur).Returns(new MurMurHashCalculation()); logger.WhenForAnyArgs(l => l.Error(null)).DoNotCallBase(); logger.WhenForAnyArgs(l => l.Warning(null)).DoNotCallBase(); logger.WhenForAnyArgs(l => l.Info(null)).DoNotCallBase(); loggerService.GetLogger(null).Returns(logger); identityKeyProvidersRegistry.GetTransactionsIdenityKeyProvider().ReturnsForAnyArgs(identityKeyProvider); //identityKeyProvider.GetKey(null).ReturnsForAnyArgs(c => new Key16() { Value = c.Arg<Memory<byte>>() }); statesRepository.GetInstance <ISynchronizationContext>().Returns(synchronizationContext); byte[] privateKey = CryptoHelper.GetRandomSeed(); Ed25519.KeyPairFromSeed(out byte[] publicKey, out byte[] expandedPrivateKey, privateKey); cryptoService.ComputeTransactionKey(new Memory <byte>()).ReturnsForAnyArgs(c => transactionKeyHash.ComputeBytes(c.Arg <Memory <byte> >().ToArray()).GetBytes()); cryptoService.ComputeTransactionKey(new byte[0]).ReturnsForAnyArgs(c => transactionKeyHash.ComputeBytes(c.Arg <byte[]>()).GetBytes()); cryptoService.Sign(null).ReturnsForAnyArgs(c => Ed25519.Sign(c.Arg <byte[]>(), expandedPrivateKey)); cryptoService.PublicKey.ReturnsForAnyArgs(new Key32(publicKey)); TransactionRegistryMemPool transactionRegistryMemPool = new TransactionRegistryMemPool(loggerService, identityKeyProvidersRegistry, cryptoService, statesRepository, new TransactionsRegistryHelper(cryptoService, identityKeyProvidersRegistry), hashCalculationsRepository); SortedList <ushort, RegistryRegisterBlock> expectedBlocks = new SortedList <ushort, RegistryRegisterBlock>(); ulong[] heights = new ulong[] { 1, 2, 2, 5, 4, 3, 4, 3, 4, 5, 3 }; HashSet <ulong> addedHeights = new HashSet <ulong>(); ushort order = 0; for (ulong i = 0; i < (ulong)heights.Length; i++) { RegistryRegisterBlock transactionRegisterBlock = PacketsBuilder.GetTransactionRegisterBlock(synchronizationContext.LastBlockDescriptor.BlockHeight, 1, null, heights[i], PacketType.Transactional, BlockTypes.Transaction_TransferFunds, new byte[Globals.POW_HASH_SIZE], new byte[Globals.DEFAULT_HASH_SIZE], privateKey); RegistryRegisterBlockSerializer serializer = new RegistryRegisterBlockSerializer(cryptoService, identityKeyProvidersRegistry, hashCalculationsRepository); serializer.Initialize(transactionRegisterBlock); serializer.FillBodyAndRowBytes(); if (!addedHeights.Contains(heights[i])) { expectedBlocks.Add(order++, transactionRegisterBlock); addedHeights.Add(heights[i]); } transactionRegistryMemPool.EnqueueTransactionRegisterBlock(transactionRegisterBlock); } SortedList <ushort, ITransactionRegistryBlock> actualBlocks = transactionRegistryMemPool.DequeueBulk(-1); Assert.Equal(expectedBlocks.Count, actualBlocks.Count); for (ushort i = 0; i < (ushort)expectedBlocks.Count; i++) { Assert.Equal(expectedBlocks[i].BlockHeight, ((RegistryRegisterBlock)actualBlocks[i]).BlockHeight); } }
private void TestFastHash32(IHash a_hash) { IFastHash32 fh = a_hash as IFastHash32; if (fh == null) return; for (int i=0; i<10; i++) { { var data = m_random.NextBytes((i == 0) ? 0 : (int)(m_random.NextUInt() % 200)); var h1 = fh.ComputeBytesFast(data); var h2 = a_hash.ComputeBytes(data); Assert.AreEqual(h1, h2.GetInt()); } { var data = m_random.NextInt(); var h1 = fh.ComputeIntFast(data); var h2 = a_hash.ComputeInt(data); Assert.AreEqual(h1, h2.GetInt()); } { var data = m_random.NextUInt(); var h1 = fh.ComputeUIntFast(data); var h2 = a_hash.ComputeUInt(data); Assert.AreEqual(h1, h2.GetInt()); } { var data = m_random.NextByte(); var h1 = fh.ComputeByteFast(data); var h2 = a_hash.ComputeByte(data); Assert.AreEqual(h1, h2.GetInt()); } { var data = m_random.NextChar(); var h1 = fh.ComputeCharFast(data); var h2 = a_hash.ComputeChar(data); Assert.AreEqual(h1, h2.GetInt()); } { var data = m_random.NextShort(); var h1 = fh.ComputeShortFast(data); var h2 = a_hash.ComputeShort(data); Assert.AreEqual(h1, h2.GetInt()); } { var data = m_random.NextUShort(); var h1 = fh.ComputeUShortFast(data); var h2 = a_hash.ComputeUShort(data); Assert.AreEqual(h1, h2.GetInt()); } { var data = m_random.NextDoubleFull(); var h1 = fh.ComputeDoubleFast(data); var h2 = a_hash.ComputeDouble(data); Assert.AreEqual(h1, h2.GetInt()); } { var data = m_random.NextFloatFull(); var h1 = fh.ComputeFloatFast(data); var h2 = a_hash.ComputeFloat(data); Assert.AreEqual(h1, h2.GetInt()); } { var data = m_random.NextLong(); var h1 = fh.ComputeLongFast(data); var h2 = a_hash.ComputeLong(data); Assert.AreEqual(h1, h2.GetInt()); } { var data = m_random.NextULong(); var h1 = fh.ComputeULongFast(data); var h2 = a_hash.ComputeULong(data); Assert.AreEqual(h1, h2.GetInt()); } { var data = m_random.NextUInts((i == 0) ? 0 : (int)(m_random.NextUInt() % 200)); var h1 = fh.ComputeUIntsFast(data); var h2 = a_hash.ComputeUInts(data); Assert.AreEqual(h1, h2.GetInt()); } { var data = m_random.NextInts((i == 0) ? 0 : (int)(m_random.NextUInt() % 200)); var h1 = fh.ComputeIntsFast(data); var h2 = a_hash.ComputeInts(data); Assert.AreEqual(h1, h2.GetInt()); } { var data = m_random.NextLongs((i == 0) ? 0 : (int)(m_random.NextUInt() % 200)); var h1 = fh.ComputeLongsFast(data); var h2 = a_hash.ComputeLongs(data); Assert.AreEqual(h1, h2.GetInt()); } { var data = m_random.NextULongs((i == 0) ? 0 : (int)(m_random.NextUInt() % 200)); var h1 = fh.ComputeULongsFast(data); var h2 = a_hash.ComputeULongs(data); Assert.AreEqual(h1, h2.GetInt()); } { var data = m_random.NextDoublesFull((i == 0) ? 0 : (int)(m_random.NextUInt() % 200)); var h1 = fh.ComputeDoublesFast(data); var h2 = a_hash.ComputeDoubles(data); Assert.AreEqual(h1, h2.GetInt()); } { var data = m_random.NextFloatsFull((i == 0) ? 0 : (int)(m_random.NextUInt() % 200)); var h1 = fh.ComputeFloatsFast(data); var h2 = a_hash.ComputeFloats(data); Assert.AreEqual(h1, h2.GetInt()); } { var data = m_random.NextChars((i == 0) ? 0 : (int)(m_random.NextUInt() % 200)); var h1 = fh.ComputeCharsFast(data); var h2 = a_hash.ComputeChars(data); Assert.AreEqual(h1, h2.GetInt()); } { var data = m_random.NextUShorts((i == 0) ? 0 : (int)(m_random.NextUInt() % 200)); var h1 = fh.ComputeUShortsFast(data); var h2 = a_hash.ComputeUShorts(data); Assert.AreEqual(h1, h2.GetInt()); } { var data = m_random.NextString((i == 0) ? 0 : (int)(m_random.NextUInt() % 200)); var h1 = fh.ComputeStringFast(data); var h2 = a_hash.ComputeString(data); Assert.AreEqual(h1, h2.GetInt()); } { var data = m_random.NextShorts((i == 0) ? 0 : (int)(m_random.NextUInt() % 200)); var h1 = fh.ComputeShortsFast(data); var h2 = a_hash.ComputeShorts(data); Assert.AreEqual(h1, h2.GetInt()); } } }
protected void TestAgainstTestFile(IHash a_hash, TestData a_test_data = null) { if (a_test_data == null) a_test_data = TestData.Load(a_hash); for (int i = 0; i < a_test_data.Count; i++) { string output_array = Converters.ConvertBytesToHexString(a_test_data.GetHash(i)); if (a_test_data.GetRepeat(i) != 1) continue; if (a_hash is IWithKey) (a_hash as IWithKey).Key = a_test_data.GetKey(i); Assert.AreEqual(output_array, a_hash.ComputeBytes(a_test_data.GetData(i)).ToString(), String.Format("{0}, {1}", a_hash.Name, i)); } }
protected void TestMultipleTransforms(IHash a_hash) { TestMultipleTransforms(a_hash, a_hash, new List<int>() { 0, 0 }); TestMultipleTransforms(a_hash, a_hash, new List<int>() { 1, 0 }); TestMultipleTransforms(a_hash, a_hash, new List<int>() { 0, 1 }); for (int tries = 0; tries < 10; tries++) { int parts = m_random.Next(20) + 1; List<int> list = new List<int>(parts); for (int i = 0; i < parts; i++) list.Add(m_random.Next(a_hash.BlockSize * 3 + 1)); TestMultipleTransforms(a_hash, a_hash, list); } List<object> objects; byte[] bytes; for (int i = 0; i < 10; i++) { CreateListForDataTest(out objects, out bytes); HashResult h1 = a_hash.ComputeBytes(bytes); a_hash.Initialize(); foreach (object o in objects) a_hash.TransformObject(o); HashResult h2 = a_hash.TransformFinal(); Assert.AreEqual(h1, h2, String.Format("{0}, {1}", a_hash.Name, i)); } { a_hash.Initialize(); a_hash.TransformString("rwffasfger4536552▰Ḑ"); var h3 = a_hash.TransformFinal(); a_hash.Initialize(); a_hash.TransformString("rwffasfger4536552▰Ḑ", Encoding.Unicode); var h4 = a_hash.TransformFinal(); Assert.AreEqual(h3, h4); } }
protected void TestInitialization(IHash a_hash) { for (int i = 0; i <= (a_hash.BlockSize * 3 + 1); i++) { IHash hash2 = ((IHash)Activator.CreateInstance(a_hash.GetType())); byte[] v = m_random.NextBytes(i); HashResult h1 = a_hash.ComputeBytes(v); HashResult h2 = hash2.ComputeBytes(v); Assert.AreEqual(h1, h2, String.Format("{0}, {1}", a_hash.Name, i)); } a_hash.TransformByte(0x55); HashResult r1 = a_hash.ComputeBytes(new byte[] { 0x55, 0x55 }); a_hash.TransformByte(0x55); a_hash.Initialize(); HashResult r2 = a_hash.ComputeBytes(new byte[] { 0x55, 0x55 }); a_hash.ComputeBytes(new byte[] { 0x55, 0x55 }); a_hash.TransformByte(0x55); a_hash.TransformByte(0x55); HashResult r3 = a_hash.TransformFinal(); a_hash.ComputeBytes(new byte[] { 0x55, 0x55 }); a_hash.Initialize(); a_hash.TransformByte(0x55); a_hash.TransformByte(0x55); HashResult r4 = a_hash.TransformFinal(); a_hash.TransformByte(0x55); a_hash.Initialize(); a_hash.TransformByte(0x55); a_hash.TransformByte(0x55); HashResult r5 = a_hash.TransformFinal(); Assert.AreEqual(r1, r2); Assert.AreEqual(r2, r3); Assert.AreEqual(r3, r4); Assert.AreEqual(r4, r5); a_hash.Initialize(); HashResult r6 = a_hash.TransformFinal(); HashResult r7 = a_hash.TransformFinal(); a_hash.ComputeBytes(new byte[] { 0x55, 0x55 }); HashResult r8 = a_hash.TransformFinal(); a_hash = (IHash)Activator.CreateInstance(a_hash.GetType()); HashResult r9 = a_hash.TransformFinal(); Assert.AreEqual(r6, r7); Assert.AreEqual(r7, r8); Assert.AreEqual(r9, r9); { a_hash.Initialize(); a_hash.TransformByte(0x55); HashResult h1 = a_hash.TransformFinal(); a_hash.Initialize(); a_hash.TransformByte(0x55); a_hash.ComputeBytes(new byte[] { 0x55, 0x55 }); a_hash.TransformByte(0x55); HashResult h2 = a_hash.TransformFinal(); Assert.AreEqual(h1, h2); } { a_hash.Initialize(); a_hash.TransformByte(0x55); HashResult h1 = a_hash.TransformFinal(); a_hash.Initialize(); a_hash.TransformByte(0x55); a_hash.ComputeBytes(new byte[] { 0x55, 0x55 }); a_hash.TransformByte(0xA3); HashResult h2 = a_hash.TransformFinal(); Assert.AreNotEqual(h1, h2); } if (a_hash is IFastHash32) { IFastHash32 fast_hash = a_hash as IFastHash32; List<Action> fast_list = new List<Action>() { () => fast_hash.ComputeByteFast(55), () => fast_hash.ComputeBytesFast(new byte[] { 0x55, 0x55 }), () => fast_hash.ComputeCharFast('c'), () => fast_hash.ComputeCharsFast(new char[] { 'c', 'c' }), () => fast_hash.ComputeDoubleFast(3.456489566e156), () => fast_hash.ComputeDoublesFast(new double[] { 3.456489566e156, 3.456489566e156 }), () => fast_hash.ComputeFloatFast(3.45698986e16f), () => fast_hash.ComputeFloatsFast(new float[] { 3.45698986e16f, 3.45698986e16f }), () => fast_hash.ComputeIntFast(1234567456), () => fast_hash.ComputeIntsFast(new int[] { 1234567456, 1234567456 }), () => fast_hash.ComputeLongFast(7632345678765765765), () => fast_hash.ComputeLongsFast(new long[] { 7632345678765765765, 7632345678765765765 }), () => fast_hash.ComputeShortFast(22345), () => fast_hash.ComputeShortsFast(new short[] { 22345, 22345 }), () => fast_hash.ComputeStringFast("test"), () => fast_hash.ComputeUIntFast(3234567456), () => fast_hash.ComputeUIntsFast(new uint[] { 3234567456, 3234567456 }), () => fast_hash.ComputeULongFast(9632345678765765765), () => fast_hash.ComputeULongsFast(new ulong[] { 9632345678765765765, 9632345678765765765 }), () => fast_hash.ComputeUShortFast(42345), () => fast_hash.ComputeUShortsFast(new ushort[] { 42345, 42345 }), }; foreach (var fast in fast_list) { { a_hash.Initialize(); a_hash.TransformByte(0x55); HashResult h1 = a_hash.TransformFinal(); a_hash.Initialize(); a_hash.TransformByte(0x55); fast(); a_hash.TransformByte(0x55); HashResult h2 = a_hash.TransformFinal(); Assert.AreEqual(h1, h2); } { a_hash.Initialize(); a_hash.TransformByte(0x55); HashResult h1 = a_hash.TransformFinal(); a_hash.Initialize(); a_hash.TransformByte(0x55); fast(); a_hash.TransformByte(0xA3); HashResult h2 = a_hash.TransformFinal(); Assert.AreNotEqual(h1, h2); } } } }
protected void TestHashStream(IHash a_hash, int a_block_size) { int old = Hash.BUFFER_SIZE; Hash.BUFFER_SIZE = a_block_size; try { for (int i = 1; i < 10; i++) { byte[] data = new byte[13 * i]; new Random().NextBytes(data); using (MemoryStream ms = new MemoryStream(data)) { HashResult h1 = a_hash.ComputeBytes(data); HashResult h2 = a_hash.ComputeStream(ms); Assert.AreEqual(h1, h2); h1 = a_hash.ComputeBytes(HashLib.ArrayExtensions.SubArray(data, i, i * 7)); ms.Seek(i, SeekOrigin.Begin); h2 = a_hash.ComputeStream(ms, i * 7); Assert.AreEqual(h1, h2); h1 = a_hash.ComputeBytes(data); a_hash.Initialize(); a_hash.TransformBytes(data, 0, i * 3); ms.Seek(i * 3, SeekOrigin.Begin); a_hash.TransformStream(ms, i * 2); a_hash.TransformBytes(data, i * 5); h2 = a_hash.TransformFinal(); Assert.AreEqual(h1, h2); } } int[] sizes = { 1, 11, 63, 64, 65, 127, 128, 129, 255, 256, 257, 511, 512, 513, 1023, 1024, 1025, 4011, 64000, 66000, 250000, Hash.BUFFER_SIZE * 20 + 511}; foreach (int size in sizes) { byte[] data = new byte[size]; new Random().NextBytes(data); using (MemoryStream ms = new MemoryStream(data)) { HashResult h1 = a_hash.ComputeBytes(data); HashResult h2 = a_hash.ComputeStream(ms); Assert.AreEqual(h1, h2); } } { byte[] data = new byte[1011]; new Random().NextBytes(data); using (MemoryStream ms = new MemoryStream(data)) { bool ex = false; try { ms.Position = 1011; a_hash.ComputeStream(ms, -1); } catch { ex = true; } Assert.IsFalse(ex); ex = false; try { ms.Position = 1010; a_hash.ComputeStream(ms, 2); } catch { ex = true; } Assert.IsTrue(ex); try { ms.Position = 0; a_hash.ComputeStream(ms, 1012); } catch { ex = true; } Assert.IsTrue(ex); } } } finally { Hash.BUFFER_SIZE = old; } }
protected void TestHashResult(IHash a_hash) { a_hash.Initialize(); a_hash.TransformBytes(m_random.NextBytes(64)); HashResult r1 = a_hash.TransformFinal(); byte[] r2 = (byte[])r1.GetBytes().Clone(); HashResult r3 = a_hash.ComputeBytes(m_random.NextBytes(64)); byte[] r4 = (byte[])r3.GetBytes().Clone(); Assert.AreNotSame(r1, r2); Assert.AreNotSame(r1.GetBytes(), r3.GetBytes()); CollectionAssert.AreEqual(r1.GetBytes(), r2); CollectionAssert.AreEqual(r3.GetBytes(), r4); Assert.AreNotEqual(r1, r3); CollectionAssert.AreNotEqual(r2, r4); CollectionAssert.AreNotEqual(r1.GetBytes(), r3.GetBytes()); Assert.AreEqual(Converters.ConvertBytesToHexString(Converters.ConvertHexStringToBytes("A1B1C2D34567890F")), "A1B1C2D3-4567890F"); }
protected void TestHashFile(IHash a_hash, int a_block_size) { string file_name = System.IO.Path.GetTempFileName(); int old = Hash.BUFFER_SIZE; Hash.BUFFER_SIZE = a_block_size; try { for (int i = 1; i < 10; i++) { byte[] v = new byte[13 * i]; new Random().NextBytes(v); using (FileStream fs = new FileStream(file_name, FileMode.Truncate)) { BinaryWriter bw = new BinaryWriter(fs); bw.Write(v); } HashResult h1 = a_hash.ComputeBytes(v); HashResult h2 = a_hash.ComputeFile(file_name); Assert.AreEqual(h1, h2); h1 = a_hash.ComputeBytes(HashLib.ArrayExtensions.SubArray(v, i, i * 7)); h2 = a_hash.ComputeFile(file_name, i, i * 7); Assert.AreEqual(h1, h2); h1 = a_hash.ComputeBytes(v); a_hash.Initialize(); a_hash.TransformBytes(v, 0, i * 3); a_hash.TransformFile(file_name, i * 3, i * 2); a_hash.TransformBytes(v, i * 5); h2 = a_hash.TransformFinal(); Assert.AreEqual(h1, h2); } int[] sizes = { 1, 11, 63, 64, 65, 127, 128, 129, 255, 256, 257, 511, 512, 513, 1023, 1024, 1025, 4011, 64000, 66000, 250000, Hash.BUFFER_SIZE * 20 + 511}; foreach (int size in sizes) { byte[] v = new byte[size]; new Random().NextBytes(v); using (FileStream fs = new FileStream(file_name, FileMode.Truncate)) { BinaryWriter bw = new BinaryWriter(fs); bw.Write(v); } HashResult h1 = a_hash.ComputeBytes(v); HashResult h2 = a_hash.ComputeFile(file_name); Assert.AreEqual(h1, h2); } { byte[] v = new byte[1011]; new Random().NextBytes(v); using (FileStream fs = new FileStream(file_name, FileMode.Truncate)) { BinaryWriter bw = new BinaryWriter(fs); bw.Write(v); } bool ex = false; try { a_hash.ComputeFile(file_name, 1011, -1); } catch { ex = true; } Assert.IsFalse(ex); ex = false; try { a_hash.ComputeFile(file_name, 1010, 2); } catch { ex = true; } Assert.IsTrue(ex); ex = false; try { a_hash.ComputeFile(file_name, 0, 1012); } catch { ex = true; } Assert.IsTrue(ex); Assert.IsTrue(ex); } } finally { Hash.BUFFER_SIZE = old; new FileInfo(file_name).Delete(); } }
public byte[] ComputeBytes(byte[] input) { var skeinResult = skein512.ComputeBytes(input).GetBytes(); return(Hashes.SHA256(skeinResult)); }
protected void TestResult(IHash a_hash) { a_hash.Initialize(); a_hash.TransformBytes(m_random.NextBytes(64)); HashResult r1 = a_hash.TransformFinal(); var r2 = (byte[])r1.GetBytes().Clone(); HashResult r3 = a_hash.ComputeBytes(m_random.NextBytes(64)); var r4 = (byte[])r3.GetBytes().Clone(); Assert.AreNotSame(r1, r2); Assert.AreNotSame(r1.GetBytes(), r3.GetBytes()); CollectionAssert.AreEqual(r1.GetBytes(), r2); CollectionAssert.AreEqual(r3.GetBytes(), r4); Assert.AreNotEqual(r1, r3); CollectionAssert.AreNotEqual(r2, r4); CollectionAssert.AreNotEqual(r1.GetBytes(), r3.GetBytes()); }
public static byte[] ComputeBytes(ReadOnlySpan <byte> input) { return(Hash.ComputeBytes(input).GetBytes()); }
/// <summary> /// Returns a 32-byte Keccak256 hash of the given bytes. /// </summary> /// <param name="input"></param> /// <returns></returns> public static byte[] Keccak256(byte[] input) { return(Keccak.ComputeBytes(input).GetBytes()); }
protected void TestAgainstTestFile(IHash a_hash, TestData a_testData) { a_testData.Load(); for(int i = 0; i < a_testData.Count; i++) { byte[] output_array = a_testData.GetHash(i); CollectionAssert.AreEqual(output_array, a_hash.ComputeBytes(a_testData.GetData(i)).GetBytes(), String.Format("{0}, {1}", a_hash.Name, i)); CollectionAssert.AreEqual(output_array, a_testData.GetHash(i), a_hash.Name); } }
protected void TestInitialization(IHash a_hash) { for (int i = 0; i <= (a_hash.BlockSize * 3 + 1); i++) { IHash hash2 = ((IHash)Activator.CreateInstance(a_hash.GetType())); byte[] v = m_random.NextBytes(i); HashResult h1 = a_hash.ComputeBytes(v); HashResult h2 = hash2.ComputeBytes(v); Assert.AreEqual(h1, h2, String.Format("{0}, {1}", a_hash.Name, i)); } a_hash.TransformByte(0x55); HashResult r1 = a_hash.ComputeBytes(new byte[] { 0x55, 0x55 }); a_hash.TransformByte(0x55); a_hash.Initialize(); HashResult r2 = a_hash.ComputeBytes(new byte[] { 0x55, 0x55 }); a_hash.ComputeBytes(new byte[] { 0x55, 0x55 }); a_hash.TransformByte(0x55); a_hash.TransformByte(0x55); HashResult r3 = a_hash.TransformFinal(); a_hash.ComputeBytes(new byte[] { 0x55, 0x55 }); a_hash.Initialize(); a_hash.TransformByte(0x55); a_hash.TransformByte(0x55); HashResult r4 = a_hash.TransformFinal(); a_hash.TransformByte(0x55); a_hash.Initialize(); a_hash.TransformByte(0x55); a_hash.TransformByte(0x55); HashResult r5 = a_hash.TransformFinal(); Assert.AreEqual(r1, r2); Assert.AreEqual(r2, r3); Assert.AreEqual(r3, r4); Assert.AreEqual(r4, r5); a_hash.Initialize(); HashResult r6 = a_hash.TransformFinal(); HashResult r7 = a_hash.TransformFinal(); a_hash.ComputeBytes(new byte[] { 0x55, 0x55 }); HashResult r8 = a_hash.TransformFinal(); a_hash = (IHash)Activator.CreateInstance(a_hash.GetType()); HashResult r9 = a_hash.TransformFinal(); Assert.AreEqual(r6, r7); Assert.AreEqual(r7, r8); Assert.AreEqual(r9, r9); { a_hash.Initialize(); a_hash.TransformByte(0x55); HashResult h1 = a_hash.TransformFinal(); a_hash.Initialize(); a_hash.TransformByte(0x55); a_hash.ComputeBytes(new byte[] { 0x55, 0x55 }); a_hash.TransformByte(0x55); HashResult h2 = a_hash.TransformFinal(); Assert.AreEqual(h1, h2); } { a_hash.Initialize(); a_hash.TransformByte(0x55); HashResult h1 = a_hash.TransformFinal(); a_hash.Initialize(); a_hash.TransformByte(0x55); a_hash.ComputeBytes(new byte[] { 0x55, 0x55 }); a_hash.TransformByte(0xA3); HashResult h2 = a_hash.TransformFinal(); Assert.AreNotEqual(h1, h2); } if (a_hash is IFastHash32) { IFastHash32 fast_hash = a_hash as IFastHash32; List <Action> fast_list = new List <Action>() { () => fast_hash.ComputeByteFast(55), () => fast_hash.ComputeBytesFast(new byte[] { 0x55, 0x55 }), () => fast_hash.ComputeCharFast('c'), () => fast_hash.ComputeCharsFast(new char[] { 'c', 'c' }), () => fast_hash.ComputeDoubleFast(3.456489566e156), () => fast_hash.ComputeDoublesFast(new double[] { 3.456489566e156, 3.456489566e156 }), () => fast_hash.ComputeFloatFast(3.45698986e16f), () => fast_hash.ComputeFloatsFast(new float[] { 3.45698986e16f, 3.45698986e16f }), () => fast_hash.ComputeIntFast(1234567456), () => fast_hash.ComputeIntsFast(new int[] { 1234567456, 1234567456 }), () => fast_hash.ComputeLongFast(7632345678765765765), () => fast_hash.ComputeLongsFast(new long[] { 7632345678765765765, 7632345678765765765 }), () => fast_hash.ComputeShortFast(22345), () => fast_hash.ComputeShortsFast(new short[] { 22345, 22345 }), () => fast_hash.ComputeStringFast("test"), () => fast_hash.ComputeUIntFast(3234567456), () => fast_hash.ComputeUIntsFast(new uint[] { 3234567456, 3234567456 }), () => fast_hash.ComputeULongFast(9632345678765765765), () => fast_hash.ComputeULongsFast(new ulong[] { 9632345678765765765, 9632345678765765765 }), () => fast_hash.ComputeUShortFast(42345), () => fast_hash.ComputeUShortsFast(new ushort[] { 42345, 42345 }), }; foreach (var fast in fast_list) { { a_hash.Initialize(); a_hash.TransformByte(0x55); HashResult h1 = a_hash.TransformFinal(); a_hash.Initialize(); a_hash.TransformByte(0x55); fast(); a_hash.TransformByte(0x55); HashResult h2 = a_hash.TransformFinal(); Assert.AreEqual(h1, h2); } { a_hash.Initialize(); a_hash.TransformByte(0x55); HashResult h1 = a_hash.TransformFinal(); a_hash.Initialize(); a_hash.TransformByte(0x55); fast(); a_hash.TransformByte(0xA3); HashResult h2 = a_hash.TransformFinal(); Assert.AreNotEqual(h1, h2); } } } }
protected void TestHashImplementationValueType(IHash a_baseHashFunction, IHash a_hash) { for(int j = 0; j < 8; j++) { { byte v = m_random.NextByte(); Assert.AreEqual(a_baseHashFunction.ComputeByte(v), a_hash.ComputeByte(v), a_hash.Name); } { char v = m_random.NextChar(); Assert.AreEqual(a_baseHashFunction.ComputeChar(v), a_hash.ComputeChar(v), a_hash.Name); } { short v = m_random.NextShort(); Assert.AreEqual(a_baseHashFunction.ComputeShort(v), a_hash.ComputeShort(v), a_hash.Name); } { ushort v = m_random.NextUShort(); Assert.AreEqual(a_baseHashFunction.ComputeUShort(v), a_hash.ComputeUShort(v), a_hash.Name); } { int v = m_random.Next(); Assert.AreEqual(a_baseHashFunction.ComputeInt(v), a_hash.ComputeInt(v), a_hash.Name); } { uint v = m_random.NextUInt(); Assert.AreEqual(a_baseHashFunction.ComputeUInt(v), a_hash.ComputeUInt(v), a_hash.Name); } { long v = m_random.NextLong(); Assert.AreEqual(a_baseHashFunction.ComputeLong(v), a_hash.ComputeLong(v), a_hash.Name); } { ulong v = m_random.NextULong(); Assert.AreEqual(a_baseHashFunction.ComputeULong(v), a_hash.ComputeULong(v), a_hash.Name); } { double v = m_random.NextDoubleFull(); Assert.AreEqual(a_baseHashFunction.ComputeDouble(v), a_hash.ComputeDouble(v), a_hash.Name); } { float v = m_random.NextFloatFull(); Assert.AreEqual(a_baseHashFunction.ComputeFloat(v), a_hash.ComputeFloat(v), a_hash.Name); } } for(int j = 0; j < 2; j++) { for(int i = 2050; i <= (a_hash.BlockSize * 3 + 1); i++) { byte[] v = m_random.NextBytes(i); Assert.AreEqual(a_baseHashFunction.ComputeBytes(v), a_hash.ComputeBytes(v), String.Format("{0}, {1}", a_hash.Name, i)); } for(int i = 0; i <= (a_hash.BlockSize * 3 + 1) / 2; i++) { char[] v = m_random.NextChars(i); Assert.AreEqual(a_baseHashFunction.ComputeChars(v), a_hash.ComputeChars(v), String.Format("{0}, {1}", a_hash.Name, i)); } for(int i = 0; i <= (a_hash.BlockSize * 3 + 1) / 2; i++) { short[] v = m_random.NextShorts(i); Assert.AreEqual(a_baseHashFunction.ComputeShorts(v), a_hash.ComputeShorts(v), String.Format("{0}, {1}", a_hash.Name, i)); } for(int i = 0; i <= (a_hash.BlockSize * 3 + 1) / 2; i++) { ushort[] v = (from n in Enumerable.Range(0, i) select m_random.NextUShort()).ToArray(); Assert.AreEqual(a_baseHashFunction.ComputeUShorts(v), a_hash.ComputeUShorts(v), String.Format("{0}, {1}", a_hash.Name, i)); } for(int i = 0; i <= (a_hash.BlockSize * 3 + 1) / 4; i++) { int[] v = m_random.NextInts(i); Assert.AreEqual(a_baseHashFunction.ComputeInts(v), a_hash.ComputeInts(v), String.Format("{0}, {1}", a_hash.Name, i)); } for(int i = 0; i <= (a_hash.BlockSize * 3 + 1) / 4; i++) { uint[] v = m_random.NextUInts(i); Assert.AreEqual(a_baseHashFunction.ComputeUInts(v), a_hash.ComputeUInts(v), String.Format("{0}, {1}", a_hash.Name, i)); } for(int i = 1; i <= (a_hash.BlockSize * 3 + 1) / 8; i++) { long[] v = m_random.NextLongs(i); Assert.AreEqual(a_baseHashFunction.ComputeLongs(v), a_hash.ComputeLongs(v), String.Format("{0}, {1}", a_hash.Name, i)); } for(int i = 0; i <= (a_hash.BlockSize * 3 + 1) / 8; i++) { ulong[] v = m_random.NextULongs(i); Assert.AreEqual(a_baseHashFunction.ComputeULongs(v), a_hash.ComputeULongs(v), String.Format("{0}, {1}", a_hash.Name, i)); } for(int i = 0; i <= (a_hash.BlockSize * 3 + 1) / 8; i++) { double[] v = m_random.NextDoublesFull(i); Assert.AreEqual(a_baseHashFunction.ComputeDoubles(v), a_hash.ComputeDoubles(v), String.Format("{0}, {1}", a_hash.Name, i)); } for(int i = 0; i <= (a_hash.BlockSize * 3 + 1) / 4; i++) { float[] v = m_random.NextFloatsFull(i); Assert.AreEqual(a_baseHashFunction.ComputeFloats(v), a_hash.ComputeFloats(v), String.Format("{0}, {1}", a_hash.Name, i)); } for(int i = 0; i <= (a_hash.BlockSize * 3 + 1) / 2; i++) { string v = m_random.NextString(i); Assert.AreEqual(a_baseHashFunction.ComputeString(v), a_hash.ComputeString(v), String.Format("{0}, {1}", a_hash.Name, i)); } } }
protected void TestMultipleTransforms(IHash a_hash) { TestMultipleTransforms(a_hash, a_hash, new List <int>() { 0, 0 }); TestMultipleTransforms(a_hash, a_hash, new List <int>() { 1, 0 }); TestMultipleTransforms(a_hash, a_hash, new List <int>() { 0, 1 }); for (int tries = 0; tries < 10; tries++) { int parts = m_random.Next(20) + 1; List <int> list = new List <int>(parts); for (int i = 0; i < parts; i++) { list.Add(m_random.Next(a_hash.BlockSize * 3 + 1)); } TestMultipleTransforms(a_hash, a_hash, list); } List <object> objects; byte[] bytes; for (int i = 0; i < 10; i++) { CreateListForDataTest(out objects, out bytes); HashResult h1 = a_hash.ComputeBytes(bytes); a_hash.Initialize(); foreach (object o in objects) { a_hash.TransformObject(o); } HashResult h2 = a_hash.TransformFinal(); Assert.AreEqual(h1, h2, String.Format("{0}, {1}", a_hash.Name, i)); } { a_hash.Initialize(); a_hash.TransformString("rwffasfger4536552▰Ḑ"); var h3 = a_hash.TransformFinal(); a_hash.Initialize(); a_hash.TransformString("rwffasfger4536552▰Ḑ", Encoding.Unicode); var h4 = a_hash.TransformFinal(); Assert.AreEqual(h3, h4); } }
protected void TestHashSize(IHash a_hash) { Assert.AreEqual(a_hash.HashSize, a_hash.ComputeBytes(new byte[] {}).GetBytes().Length); }
protected void TestHashStream(IHash a_hash, int a_block_size) { int old = Hash.BUFFER_SIZE; Hash.BUFFER_SIZE = a_block_size; try { for (int i = 1; i < 10; i++) { byte[] data = new byte[13 * i]; new Random().NextBytes(data); using (MemoryStream ms = new MemoryStream(data)) { HashResult h1 = a_hash.ComputeBytes(data); HashResult h2 = a_hash.ComputeStream(ms); Assert.AreEqual(h1, h2); h1 = a_hash.ComputeBytes(HashLib.ArrayExtensions.SubArray(data, i, i * 7)); ms.Seek(i, SeekOrigin.Begin); h2 = a_hash.ComputeStream(ms, i * 7); Assert.AreEqual(h1, h2); h1 = a_hash.ComputeBytes(data); a_hash.Initialize(); a_hash.TransformBytes(data, 0, i * 3); ms.Seek(i * 3, SeekOrigin.Begin); a_hash.TransformStream(ms, i * 2); a_hash.TransformBytes(data, i * 5); h2 = a_hash.TransformFinal(); Assert.AreEqual(h1, h2); } } int[] sizes = { 1, 11, 63, 64, 65, 127, 128, 129, 255, 256, 257, 511, 512, 513, 1023, 1024, 1025, 4011, 64000, 66000, 250000, Hash.BUFFER_SIZE * 20 + 511 }; foreach (int size in sizes) { byte[] data = new byte[size]; new Random().NextBytes(data); using (MemoryStream ms = new MemoryStream(data)) { HashResult h1 = a_hash.ComputeBytes(data); HashResult h2 = a_hash.ComputeStream(ms); Assert.AreEqual(h1, h2); } } { byte[] data = new byte[1011]; new Random().NextBytes(data); using (MemoryStream ms = new MemoryStream(data)) { bool ex = false; try { ms.Position = 1011; a_hash.ComputeStream(ms, -1); } catch { ex = true; } Assert.IsFalse(ex); ex = false; try { ms.Position = 1010; a_hash.ComputeStream(ms, 2); } catch { ex = true; } Assert.IsTrue(ex); try { ms.Position = 0; a_hash.ComputeStream(ms, 1012); } catch { ex = true; } Assert.IsTrue(ex); } } } finally { Hash.BUFFER_SIZE = old; } }
protected void TestHashStreamValueType(IHash a_hash_stream, IHash a_hash) { string file_name = Path.GetTempFileName(); int ratio = (TEST_BLOCK_SIZE * 3 / 2) / 9; try { for(int i = 0; i < 10; i++) { var data = new byte[ratio * i]; m_random.NextBytes(data); using(var stream = new FileStream(file_name, FileMode.Truncate)) { new BinaryWriter(stream).Write(data); stream.Seek(0, SeekOrigin.Begin); HashResult h1 = a_hash_stream.ComputeStream(stream); HashResult h2 = a_hash.ComputeBytes(data); Assert.AreEqual(h2, h1, String.Format("{0}, {1}", a_hash.Name, data.Length)); } } } finally { new FileInfo(file_name).Delete(); } }
public void Test(IHash a_hash, SpeedTestDataSourceRow a_row, bool a_bFast) { Random r = new Random(); int length = 10000000; { byte[] ar = new byte[length]; r.NextBytes(ar); if (a_bFast) { a_hash.ComputeByte(ar[0]); a_row.ByteSpeed = Measure(ar.Length, () => { for (int i = 0; i < ar.Length; i++) a_hash.ComputeByte(ar[i]); }); } a_hash.ComputeBytes(ar.Take(100).ToArray()); a_row.BytesSpeed = Measure(ar.Length, () => { a_hash.ComputeBytes(ar); }); } { if (a_bFast) { char[] ar = (from n in Enumerable.Range(0, length / 2) select (char)r.Next()).ToArray(); a_hash.ComputeChar(ar[0]); a_row.CharSpeed = Measure(ar.Length * 2, () => { for (int i = 0; i < ar.Length; i++) a_hash.ComputeChar(ar[i]); }); a_hash.ComputeChars(ar.Take(100).ToArray()); a_row.CharsSpeed = Measure(ar.Length * 2, () => { a_hash.ComputeChars(ar); }); } } { if (a_bFast) { short[] ar = (from n in Enumerable.Range(0, length / 2) select (short)r.Next()).ToArray(); a_hash.ComputeShort(ar[0]); a_row.ShortSpeed = Measure(ar.Length * 2, () => { for (int i = 0; i < ar.Length; i++) a_hash.ComputeShort(ar[i]); }); a_hash.ComputeShorts(ar.Take(100).ToArray()); a_row.ShortsSpeed = Measure(ar.Length * 2, () => { a_hash.ComputeShorts(ar); }); } } { if (a_bFast) { ushort[] ar = (from n in Enumerable.Range(0, length / 2) select (ushort)r.Next()).ToArray(); a_hash.ComputeUShort(ar[0]); a_row.UShortSpeed = Measure(ar.Length * 2, () => { for (int i = 0; i < ar.Length; i++) a_hash.ComputeUShort(ar[i]); }); a_hash.ComputeUShorts(ar.Take(100).ToArray()); a_row.UShortsSpeed = Measure(ar.Length * 2, () => { a_hash.ComputeUShorts(ar); }); } } { if (a_bFast) { int[] ar = (from n in Enumerable.Range(0, length / 4) select r.Next()).ToArray(); a_hash.ComputeInt(ar[0]); a_row.IntSpeed = Measure(ar.Length * 4, () => { for (int i = 0; i < ar.Length; i++) a_hash.ComputeInt(ar[i]); }); a_hash.ComputeInts(ar.Take(100).ToArray()); a_row.IntsSpeed = Measure(ar.Length * 4, () => { a_hash.ComputeInts(ar); }); } } { if (a_bFast) { uint[] ar = (from n in Enumerable.Range(0, length / 4) select (uint)r.Next()).ToArray(); a_hash.ComputeUInt(ar[0]); a_row.UIntSpeed = Measure(ar.Length * 4, () => { for (int i = 0; i < ar.Length; i++) a_hash.ComputeUInt(ar[i]); }); a_hash.ComputeUInts(ar.Take(100).ToArray()); a_row.UIntsSpeed = Measure(ar.Length * 4, () => { a_hash.ComputeUInts(ar); }); } } { if (a_bFast) { long[] ar = (from n in Enumerable.Range(0, length / 8) select (long)(((ulong)r.Next() + (ulong)r.Next() * (ulong)Math.Pow(2, 32)))).ToArray(); a_hash.ComputeLong(ar[0]); a_row.LongSpeed = Measure(ar.Length * 8, () => { for (int i = 0; i < ar.Length; i++) a_hash.ComputeLong(ar[i]); }); a_hash.ComputeLongs(ar.Take(100).ToArray()); a_row.LongsSpeed = Measure(ar.Length * 8, () => { a_hash.ComputeLongs(ar); }); } } { if (a_bFast) { ulong[] ar = (from n in Enumerable.Range(0, length / 8) select ((ulong)r.Next() + (ulong)r.Next() * (ulong)Math.Pow(2, 32))).ToArray(); a_hash.ComputeULong(ar[0]); a_row.ULongSpeed = Measure(ar.Length * 8, () => { for (int i = 0; i < ar.Length; i++) a_hash.ComputeULong(ar[i]); }); a_row.ULongsSpeed = Measure(ar.Length * 8, () => { a_hash.ComputeULongs(ar); }); } } { if (a_bFast) { float[] ar = (from n in Enumerable.Range(0, length / 8) select (float)(r.NextDouble() * r.Next())).ToArray(); a_hash.ComputeFloat(ar[0]); a_row.FloatSpeed = Measure(ar.Length * 4, () => { for (int i = 0; i < ar.Length; i++) a_hash.ComputeFloat(ar[i]); }); a_hash.ComputeFloats(ar.Take(100).ToArray()); a_row.FloatsSpeed = Measure(ar.Length * 4, () => { a_hash.ComputeFloats(ar); }); } } { if (a_bFast) { double[] ar = (from n in Enumerable.Range(0, length / 8) select r.NextDouble() * r.Next()).ToArray(); a_hash.ComputeDouble(ar[0]); a_row.DoubleSpeed = Measure(ar.Length * 8, () => { for (int i = 0; i < ar.Length; i++) a_hash.ComputeDouble(ar[i]); }); a_hash.ComputeDoubles(ar.Take(100).ToArray()); a_row.DoublesSpeed = Measure(ar.Length * 8, () => { a_hash.ComputeDoubles(ar); }); } } { if (a_bFast) { byte[] a = new byte[length]; r.NextBytes(a); string ar = new String((from b in a select (char)(b / 2 + 32)).ToArray()); a_hash.ComputeString(ar.Substring(0, 100)); a_row.StringSpeed = Measure(ar.Length * 2, () => { a_hash.ComputeString(ar); }); } } }
protected void TestInitialization(IHash a_hash) { for(int i = 0; i <= (a_hash.BlockSize * 3 + 1); i++) { var hash2 = ((IHash)Activator.CreateInstance(a_hash.GetType())); byte[] v = m_random.NextBytes(i); HashResult h1 = a_hash.ComputeBytes(v); HashResult h2 = hash2.ComputeBytes(v); Assert.AreEqual(h1, h2, String.Format("{0}, {1}", a_hash.Name, i)); } }
public String GetMixHash(UInt64 nonce) { IHash hash = HashFactory.Crypto.SHA3.CreateKeccak512(); byte[] data; System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); sw.Start(); // def hashimoto(header, nonce, full_size, dataset_lookup): int epoch = Epoch; byte[] seedhashArray = Utilities.StringToByteArray(Seedhash); byte[] headerhashArray = Utilities.StringToByteArray(Headerhash); DAGCache cache = new DAGCache(epoch, Seedhash); data = cache.GetData(); long fullSize = Utilities.GetDAGSize(epoch); // n = full_size / HASH_BYTES int n = (int)(fullSize / HASH_BYTES); // w = MIX_BYTES // WORD_BYTES int w = MIX_BYTES / WORD_BYTES; // mixhashes = MIX_BYTES / HASH_BYTES int mixhashes = MIX_BYTES / HASH_BYTES; // # combine header+nonce into a 64 byte seed // s = sha3_512(header + nonce[::-1]) byte[] combined = new byte[headerhashArray.Length + sizeof(UInt64)]; Buffer.BlockCopy(headerhashArray, 0, combined, 0, headerhashArray.Length); for (int i = 0; i < 8; ++i) { combined[headerhashArray.Length + i] = (byte)((nonce >> (i * 8)) & 0xff); } byte[] s = hash.ComputeBytes(combined).GetBytes(); // # start the mix with replicated s // mix = [] // for _ in range(MIX_BYTES / HASH_BYTES): // mix.extend(s) byte[] mix = new byte[s.Length * mixhashes]; for (int i = 0; i < mixhashes; ++i) { Buffer.BlockCopy(s, 0, mix, i * s.Length, s.Length); } // # mix in random dataset nodes // for i in range(ACCESSES): for (int i = 0; i < ACCESSES; ++i) { // p = fnv(i ^ s[0], mix[i % w]) % (n // mixhashes) * mixhashes UInt32 v1 = ((UInt32)s[0] ^ (UInt32)i) | ((UInt32)s[1] << 8) | ((UInt32)s[2] << 16) | ((UInt32)s[3] << 24); int mixIndex = (i % w) * 4; UInt32 v2 = ((UInt32)mix[mixIndex + 0] << 0) | ((UInt32)mix[mixIndex + 1] << 8) | ((UInt32)mix[mixIndex + 2] << 16) | ((UInt32)mix[mixIndex + 3] << 24); int p = (int)(FNV(v1, v2) % (n / mixhashes) * mixhashes); // newdata = [] // for j in range(MIX_BYTES / HASH_BYTES): // newdata.extend(dataset_lookup(p + j)) byte[] newData = new byte[s.Length * mixhashes]; for (int j = 0; j < mixhashes; ++j) { Buffer.BlockCopy(CalculateDatasetItem(data, p + j), 0, newData, j * (int)s.Length, (int)s.Length); } // mix = map(fnv, mix, newdata) for (int j = 0; j < s.Length * mixhashes; j += 4) { v1 = ((UInt32)mix[j + 0] << 0) | ((UInt32)mix[j + 1] << 8) | ((UInt32)mix[j + 2] << 16) | ((UInt32)mix[j + 3] << 24); v2 = ((UInt32)newData[j + 0] << 0) | ((UInt32)newData[j + 1] << 8) | ((UInt32)newData[j + 2] << 16) | ((UInt32)newData[j + 3] << 24); UInt32 result = FNV(v1, v2); for (int k = 0; k < 4; ++k) { mix[j + k] = (byte)((result >> (k * 8)) & 0xff); } } } // # compress mix // cmix = [] // for i in range(0, len(mix), 4): // cmix.append(fnv(fnv(fnv(mix[i], mix[i+1]), mix[i+2]), mix[i+3])) byte[] cmix = new byte[mix.Length / 4]; for (int i = 0; i < mix.Length / 4; i += 4) { UInt32 v1 = ((UInt32)mix[i * 4 + 0] << 0) | ((UInt32)mix[i * 4 + 1] << 8) | ((UInt32)mix[i * 4 + 2] << 16) | ((UInt32)mix[i * 4 + 3] << 24); for (int j = 1; j < 4; ++j) { UInt32 v2 = ((UInt32)mix[(i + j) * 4 + 0] << 0) | ((UInt32)mix[(i + j) * 4 + 1] << 8) | ((UInt32)mix[(i + j) * 4 + 2] << 16) | ((UInt32)mix[(i + j) * 4 + 3] << 24); v1 = FNV(v1, v2); } for (int j = 0; j < 4; ++j) { cmix[i + j] = (byte)((v1 >> (j * 8)) & 0xff); } } // return { // "mix digest": serialize_hash(cmix), // "result": serialize_hash(sha3_256(s+cmix)) // } sw.Stop(); MainForm.Logger("Generated mix hash (" + (long)sw.Elapsed.TotalMilliseconds + "ms)."); return(Utilities.ByteArrayToString(cmix)); }
protected void TestMultipleTransformsValueType(IHash a_multi, IHash a_hash, IList<int> a_list) { a_multi.Initialize(); a_hash.Initialize(); var v1 = new List<byte[]>(a_list.Count); foreach(int length in a_list) { var ar = new byte[length]; for(int i = 0; i < ar.Length; i++) ar[i] = (byte)m_random.Next(Byte.MaxValue); v1.Add(ar); } int len = 0; foreach(var ar in v1) len += ar.Length; var v2 = new byte[len]; int index = 0; foreach(var ar in v1) { Array.Copy(ar, 0, v2, index, ar.Length); index += ar.Length; } for(int i = 0; i < v1.Count; i++) a_multi.TransformBytes(v1[i]); HashResult h1 = a_multi.TransformFinal(); HashResult h2 = a_hash.ComputeBytes(v2); Assert.AreEqual(h2, h1); index = 0; a_multi.Initialize(); for(int i = 0; i < v1.Count; i++) { a_multi.TransformBytes(v2, index, a_list[i]); index += a_list[i]; } h1 = a_multi.TransformFinal(); Assert.AreEqual(h2, h1); }
public byte[] Hash(byte[] input) { return(_hasher.ComputeBytes(input).GetBytes()); }
protected void TestComputeTransforms(IHash a_hash) { foreach (var creator in m_creators) { for (int i = 0; i < 10; i++) { object v = creator(); byte[] bytes = Converters.ConvertToBytes(v); var h1 = a_hash.ComputeObject(v); var h2 = a_hash.ComputeBytes(bytes); Assert.AreEqual(h1, h2, String.Format("{0}, {1}", a_hash.Name, i)); } } { var h3 = a_hash.ComputeString("rwffasfger4536552▰Ḑ"); var h4 = a_hash.ComputeString("rwffasfger4536552▰Ḑ", Encoding.Unicode); Assert.AreEqual(h3, h4); } }