public void FromHexString()
        {
            Random random = new Random();

            string value;

            {
                byte[] buffer = new byte[32];
                random.NextBytes(buffer);

                value = NetworkConverter.ToHexString(buffer);
            }

            Stopwatch sw1 = new Stopwatch();
            Stopwatch sw2 = new Stopwatch();

            var flags = new int[] { 0, 1 };

            for (int i = 0; i < 1024 * 1024 * 2; i++)
            {
                byte[] result1 = null;
                byte[] result2 = null;

                random.Shuffle(flags);
                foreach (var index in flags)
                {
                    if (index == 0)
                    {
                        sw1.Start();
                        result1 = NetworkConverter.FromHexString(value);
                        sw1.Stop();
                    }
                    else if (index == 1)
                    {
                        sw2.Start();
                        result2 = NetworkConverter.FromHexString_2(value);
                        sw2.Stop();
                    }
                }

                Assert.IsTrue(Unsafe.Equals(result1, result2));
            }

            StringBuilder sb = new StringBuilder();

            sb.AppendLine("FromHexString: " + sw1.Elapsed.ToString());
            sb.AppendLine("FromHexString_2: " + sw2.Elapsed.ToString());

            Console.WriteLine(sb.ToString());
        }
Esempio n. 2
0
        public void Test_NetworkConverter()
        {
            Assert.IsTrue(NetworkConverter.ToHexString(new byte[] { 0x00, 0x9e, 0x0f }) == "009e0f", "ToHexString");
            Assert.IsTrue(CollectionUtilities.Equals(NetworkConverter.FromHexString("1af4b"), new byte[] { 0x01, 0xaf, 0x4b }), "FromHexString");

            Assert.IsTrue(NetworkConverter.ToBoolean(new byte[] { 0x01 }), "ToBoolean");
            Assert.IsTrue(NetworkConverter.ToChar(new byte[] { 0x00, 0x41 }) == 'A', "ToChar");
            Assert.IsTrue(NetworkConverter.ToInt16(new byte[] { 0x74, 0xab }) == 0x74ab, "ToInt16");
            Assert.IsTrue(NetworkConverter.ToUInt16(new byte[] { 0x74, 0xab }) == 0x74ab, "ToUInt16");
            Assert.IsTrue(NetworkConverter.ToInt32(new byte[] { 0x74, 0xab, 0x05, 0xc1 }) == 0x74ab05c1, "ToInt32");
            Assert.IsTrue(NetworkConverter.ToUInt32(new byte[] { 0x74, 0xab, 0x05, 0xc1 }) == 0x74ab05c1, "ToUInt32");
            Assert.IsTrue(NetworkConverter.ToInt64(new byte[] { 0x74, 0xab, 0xa5, 0xbb, 0xf5, 0x39, 0x7b, 0x15 }) == 0x74aba5bbf5397b15, "ToInt64");
            Assert.IsTrue(NetworkConverter.ToUInt64(new byte[] { 0x74, 0xab, 0xa5, 0xbb, 0xf5, 0x39, 0x7b, 0x15 }) == 0x74aba5bbf5397b15, "ToUInt64");
            Assert.IsTrue(NetworkConverter.ToSingle(new byte[] { 0x4a, 0x8a, 0xd0, 0x64 }) == 4548658.0, "ToSingle");
            Assert.IsTrue(NetworkConverter.ToDouble(new byte[] { 0x41, 0xb8, 0xa6, 0xb9, 0x83, 0x27, 0x97, 0xa3 }) == 413579651.15465754, "ToDouble");

            Assert.IsTrue(CollectionUtilities.Equals(NetworkConverter.GetBytes(true), new byte[] { 0x01 }), "GetBytes #bool");
            Assert.IsTrue(CollectionUtilities.Equals(NetworkConverter.GetBytes((char)'A'), new byte[] { 0x00, 0x41 }), "GetBytes #char");
            Assert.IsTrue(CollectionUtilities.Equals(NetworkConverter.GetBytes((short)0x74ab), new byte[] { 0x74, 0xab }), "GetBytes #short");
            Assert.IsTrue(CollectionUtilities.Equals(NetworkConverter.GetBytes((ushort)0x74ab), new byte[] { 0x74, 0xab }), "GetBytes #ushort");
            Assert.IsTrue(CollectionUtilities.Equals(NetworkConverter.GetBytes((int)0x74ab05c1), new byte[] { 0x74, 0xab, 0x05, 0xc1 }), "GetBytes #int");
            Assert.IsTrue(CollectionUtilities.Equals(NetworkConverter.GetBytes((uint)0x74ab05c1), new byte[] { 0x74, 0xab, 0x05, 0xc1 }), "GetBytes #uint");
            Assert.IsTrue(CollectionUtilities.Equals(NetworkConverter.GetBytes((long)0x74aba5bbf5397b15), new byte[] { 0x74, 0xab, 0xa5, 0xbb, 0xf5, 0x39, 0x7b, 0x15 }), "GetBytes #long");
            Assert.IsTrue(CollectionUtilities.Equals(NetworkConverter.GetBytes((ulong)0x74aba5bbf5397b15), new byte[] { 0x74, 0xab, 0xa5, 0xbb, 0xf5, 0x39, 0x7b, 0x15 }), "GetBytes #ulong");
            Assert.IsTrue(CollectionUtilities.Equals(NetworkConverter.GetBytes((float)4548658.0), new byte[] { 0x4a, 0x8a, 0xd0, 0x64 }), "GetBytes #float");
            Assert.IsTrue(CollectionUtilities.Equals(NetworkConverter.GetBytes((double)413579651.15465754), new byte[] { 0x41, 0xb8, 0xa6, 0xb9, 0x83, 0x27, 0x97, 0xa3 }), "GetBytes #double");

            Assert.IsTrue(NetworkConverter.ToInt32(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x74, 0xab, 0x05, 0xc1 }, 4) == 0x74ab05c1, "ToInt32");

            for (int i = 0; i < 1024; i++)
            {
                byte[] buffer = new byte[_random.Next(0, 128)];
                _random.NextBytes(buffer);

                var s = NetworkConverter.ToBase64UrlString(buffer);
                Assert.IsTrue(CollectionUtilities.Equals(buffer, NetworkConverter.FromBase64UrlString(s)));
            }

            for (int i = 0; i < 1024; i++)
            {
                byte[] buffer = new byte[_random.Next(0, 128)];
                _random.NextBytes(buffer);

                var s = NetworkConverter.ToHexString(buffer);
                Assert.IsTrue(CollectionUtilities.Equals(buffer, NetworkConverter.FromHexString(s)));
            }
        }
Esempio n. 3
0
        public void Test_HmacSha256()
        {
            // http://tools.ietf.org/html/rfc4868#section-2.7.1
            {
                var key   = NetworkConverter.FromHexString("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b");
                var value = NetworkConverter.FromHexString("4869205468657265");

                using (MemoryStream stream = new MemoryStream(value))
                {
                    var s = NetworkConverter.ToHexString(HmacSha256.ComputeHash(stream, key));
                }

                using (HMACSHA256 hmacSha256 = new HMACSHA256(key))
                {
                    var s = NetworkConverter.ToHexString(hmacSha256.ComputeHash(value));
                }
            }

            var list = new List <int>();

            list.Add(1);
            list.Add(64);
            list.Add(128);

            byte[] buffer = new byte[1024 * 32];
            _random.NextBytes(buffer);

            for (int i = 0; i < list.Count; i++)
            {
                byte[] key = new byte[list[i]];
                _random.NextBytes(key);

                using (MemoryStream stream1 = new MemoryStream(buffer))
                    using (MemoryStream stream2 = new MemoryStream(buffer))
                    {
                        using (HMACSHA256 hmacSha256 = new HMACSHA256(key))
                        {
                            Assert.IsTrue(Unsafe.Equals(hmacSha256.ComputeHash(stream1), HmacSha256.ComputeHash(stream2, key)));
                        }
                    }
            }
        }
Esempio n. 4
0
            public byte[] Create_1(byte[] value, int limit, TimeSpan computationTime)
            {
                if (value == null)
                {
                    throw new ArgumentNullException("value");
                }
                if (value.Length != 32)
                {
                    throw new ArgumentOutOfRangeException("value");
                }

                var info = new ProcessStartInfo(_path);

                info.CreateNoWindow         = true;
                info.UseShellExecute        = false;
                info.RedirectStandardOutput = true;

                {
                    if (limit < 0)
                    {
                        limit = -1;
                    }

                    int timeout;

                    if (computationTime < TimeSpan.Zero)
                    {
                        timeout = -1;
                    }
                    else
                    {
                        timeout = (int)computationTime.TotalSeconds;
                    }

                    info.Arguments = string.Format(
                        "hashcash1 create {0} {1} {2}",
                        NetworkConverter.ToHexString(value),
                        limit,
                        timeout);
                }

                using (var process = Process.Start(info))
                {
                    _processes.Add(process);

                    try
                    {
                        process.PriorityClass = ProcessPriorityClass.Idle;

                        try
                        {
                            var result = process.StandardOutput.ReadLine();

                            process.WaitForExit();
                            if (process.ExitCode != 0)
                            {
                                throw new MinerException();
                            }

                            return(NetworkConverter.FromHexString(result));
                        }
                        catch (MinerException)
                        {
                            throw;
                        }
                        catch (Exception e)
                        {
                            throw new MinerException(e.Message, e);
                        }
                    }
                    finally
                    {
                        _processes.Remove(process);
                    }
                }
            }
Esempio n. 5
0
        public void Test_Miner()
        {
            //{
            //    var key = NetworkConverter.FromHexString("e0ee19d617ee6ea9ea592afbdf71bafba6eecde2beba0d3cdc51419522fe5dbdf18f6830081be1615969b1fe43344fac3c312cd86a487cb1bd04f2c44cddca11");
            //    var value = NetworkConverter.FromHexString("01010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101");

            //    var count = Verify_1(key, value);
            //}

            {
                Miner miner = new Miner(CashAlgorithm.Version1, -1, new TimeSpan(0, 0, 1));

                Cash cash = null;

                Stopwatch sw = new Stopwatch();
                sw.Start();

                using (MemoryStream stream = new MemoryStream(NetworkConverter.FromHexString("0101010101010101")))
                {
                    cash = miner.Create(stream);
                }

                sw.Stop();
                Assert.IsTrue(sw.ElapsedMilliseconds < 1000 * 30);
            }

            {
                Miner miner = new Miner(CashAlgorithm.Version1, 20, new TimeSpan(1, 0, 0));

                Cash cash = null;

                using (MemoryStream stream = new MemoryStream(NetworkConverter.FromHexString("0101010101010101")))
                {
                    cash = miner.Create(stream);

                    stream.Seek(0, SeekOrigin.Begin);
                    Assert.IsTrue(Miner.Verify(cash, stream) >= 20);
                }
            }

            {
                Miner miner = new Miner(CashAlgorithm.Version1, 0, TimeSpan.Zero);

                Cash cash = null;

                using (MemoryStream stream = new MemoryStream(NetworkConverter.FromHexString("0101010101010101")))
                {
                    cash = miner.Create(stream);
                }

                Assert.IsTrue(cash == null);
            }

            {
                Stopwatch sw = new Stopwatch();
                sw.Start();

                Assert.Throws <AggregateException>(() =>
                {
                    Miner miner = new Miner(CashAlgorithm.Version1, -1, new TimeSpan(1, 0, 0));

                    var task = Task.Factory.StartNew(() =>
                    {
                        Cash cash = null;

                        using (MemoryStream stream = new MemoryStream(NetworkConverter.FromHexString("0101010101010101")))
                        {
                            cash = miner.Create(stream);
                        }
                    });

                    Thread.Sleep(1000);

                    miner.Cancel();

                    task.Wait();
                });

                sw.Stop();
                Assert.IsTrue(sw.ElapsedMilliseconds < 1000 * 30);
            }
        }