Example #1
0
        public void TestHashOfTextInput()
        {
            var textTestVectors = new Dictionary <string, string>
            {
                { string.Empty, "9c1185a5c5e9fc54612808977ee8f548b2258d31" },
                { "a", "0bdc9d2d256b3ee9daae347be6f4dc835a467ffe" },
                { "abc", "8eb208f7e05d987a9b044a8e98c6b087f15a0bfc" },
                { "abcdefghijklmnopqrstuvwxyz", "f71c27109c692c1b56bbdceb5b9d2865b3708dbc" },
                { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", "12a053384a9c0c88e405a06c27dcf49ada62eb2b" },
                { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", "b0e20b6e3116640286ed3a87a5713079b21f5189" },
                { "The quick brown fox jumps over the lazy dog", "37f332f68db77bd9d7edd4969571ad671cf9dd3b" },
                { "The quick brown fox jumps over the lazy dog.", "fc850169b1f2ce72e3f8aa0aeb5ca87d6f8519c6" }
            };

            //test hex string overload.  RIPEMD160.Hash(string hexData)
            foreach (var testString in textTestVectors)
            {
                var hex       = Hex.AsciiToHex(testString.Key);
                var hashBytes = RIPEMD160.Hash(hex);
                Assert.AreEqual(testString.Value, hashBytes.ToHex());
            }

            //test byte array overload.  RIPEMD160.Hash(byte[] data)
            foreach (var testString in textTestVectors)
            {
                var bytes     = Encoding.UTF8.GetBytes(testString.Key);
                var hashBytes = RIPEMD160.Hash(bytes);
                Assert.AreEqual(testString.Value, hashBytes.ToHex());
            }
        }
Example #2
0
        public void SingleDigest()
        {
            Assert.AreEqual(
                SHA256.Hash(new byte[] { 0x01, 0x02, 0x03 }),
                new byte[] { 0x03, 0x90, 0x58, 0xC6, 0xF2, 0xC0, 0xCB, 0x49, 0x2C, 0x53, 0x3B, 0x0A, 0x4D, 0x14, 0xEF, 0x77, 0xCC, 0x0F, 0x78, 0xAB, 0xCC, 0xCE, 0xD5, 0x28, 0x7D, 0x84, 0xA1, 0xA2, 0x01, 0x1C, 0xFB, 0x81 }
                );

            Assert.AreEqual(
                SHA1.Hash(new byte[] { 0x01, 0x02, 0x03 }),
                new byte[] { 0x70, 0x37, 0x80, 0x71, 0x98, 0xC2, 0x2A, 0x7D, 0x2B, 0x08, 0x07, 0x37, 0x1D, 0x76, 0x37, 0x79, 0xA8, 0x4F, 0xDF, 0xCF }
                );

            Assert.AreEqual(
                RIPEMD160.Hash(new byte[] { 0x01, 0x02, 0x03 }),
                new byte[] { 0x79, 0xF9, 0x01, 0xDA, 0x26, 0x09, 0xF0, 0x20, 0xAD, 0xAD, 0xBF, 0x2E, 0x5F, 0x68, 0xA1, 0x6C, 0x8C, 0x3F, 0x7D, 0x57 }
                );
        }
Example #3
0
        public void TestHashOfHexInput()
        {
            var hexTestVectors = new Dictionary <string, string>
            {
                { string.Empty, "9c1185a5c5e9fc54612808977ee8f548b2258d31" },                                                                                                                   //empty string
                { "61", "0bdc9d2d256b3ee9daae347be6f4dc835a467ffe" },                                                                                                                           //"a"
                { "616263", "8eb208f7e05d987a9b044a8e98c6b087f15a0bfc" },                                                                                                                       //"abc"
                { "6162636465666768696a6b6c6d6e6f707172737475767778797a", "f71c27109c692c1b56bbdceb5b9d2865b3708dbc" },                                                                         //"abcdefghijklmnopqrstuvwxyz"
                { "6162636462636465636465666465666765666768666768696768696a68696a6b696a6b6c6a6b6c6d6b6c6d6e6c6d6e6f6d6e6f706e6f7071", "12a053384a9c0c88e405a06c27dcf49ada62eb2b" },             //"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
                { "4142434445464748494a4b4c4d4e4f505152535455565758595a6162636465666768696a6b6c6d6e6f707172737475767778797a30313233343536373839", "b0e20b6e3116640286ed3a87a5713079b21f5189" }, //"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
                { "54686520717569636b2062726f776e20666f78206a756d7073206f76657220746865206c617a7920646f67", "37f332f68db77bd9d7edd4969571ad671cf9dd3b" },                                       //"The quick brown fox jumps over the lazy dog"
                { "54686520717569636b2062726f776e20666f78206a756d7073206f76657220746865206c617a7920646f672e", "fc850169b1f2ce72e3f8aa0aeb5ca87d6f8519c6" } //"The quick brown fox jumps over the lazy dog."
            };

            foreach (var testString in hexTestVectors)
            {
                var hashBytes = RIPEMD160.Hash(testString.Key);
                Assert.AreEqual(testString.Value, hashBytes.ToHex());
            }
        }
Example #4
0
 public static byte[] Hash(byte[] data)
 {
     return(RIPEMD160.Hash(SHA256.Hash(data)));
 }
Example #5
0
        public NetworkCommand ProcessCommand()
        {
            NetworkCommand command = this.ReceiveCommand();

            this.LastReceivedCommand = command;
            switch (command)
            {
            case NetworkCommand.Ping:
            {
                long time = this.ReceiveLong();
                this.SendCommand(NetworkCommand.PingBack, time);
            }
            break;

            case NetworkCommand.PingBack:
            {
                long     time    = this.ReceiveLong();
                TimeSpan elapsed = TimeSpan.FromTicks(DateTime.Now.Ticks - time);
                this.LogString(this, "Returning ping time: " + elapsed.ToString() + "\r\n");
            }
            break;

            case NetworkCommand.ComputeECPoint:
            {
                var factor         = ecc.GetRandomFactorModN();
                var multiplication = ecc.ECMultiplication(factor);
                var factorBytes    = factor.ToByteArray();
                Array.Resize(ref factorBytes, ecc.BytesCount);
                var eccBytes = multiplication.UncompressedBytes;
                this.SendCommand(NetworkCommand.Response_ComputeECPoint, factorBytes.Concat(eccBytes));
            }
            break;

            case NetworkCommand.Response_ComputeECPoint:
            {
                byte[] data         = this.ReceiveBytes(ecc.BytesCount * 3);
                var    factor       = data.Take(ecc.BytesCount).ToBigInteger();
                var    verification = ecc.ECMultiplication(factor).UncompressedBytes;
                bool   ok           = Enumerable.Range(0, verification.Length).All(idx => data[ecc.BytesCount + idx] == verification[idx]);
                if (!ok)
                {
                    throw new InvalidOperationException("ECC verification failed!!");
                }
            }
            break;

            case NetworkCommand.RegisterClient:
            {
                var    point             = new ECC521Point(this.ReceiveBytes(ecc.CompressedBytesCount), true);
                string clientIdentifiers = this.ReadString();
                this.serverChallenge = ecc.GetRandomFactorModN();
                var    clientChallengePoint = point.Multiply(this.serverChallenge);
                string userName             = ConfigurationFile.LoadFromFile().GetUserName(clientIdentifiers);
                var    currentTime          = DateTime.Now.Ticks;
                this.SendCommand(NetworkCommand.SolveServerChallenge,
                                 clientChallengePoint.UncompressedBytes.
                                 Concat(BitConverter.GetBytes(currentTime)).
                                 Concat(userName.StringToVariableLengthBytes()));
            }
            break;

            case NetworkCommand.SolveServerChallenge:
            {
                var point           = new ECC521Point(this.ReceiveBytes(ecc.BytesCount * 2), false);
                var serverTimeBytes = this.ReceiveBytes(8);
                this.RegisteredUserName = this.ReadString();
                var secretPoint = point.Multiply(this.clientSecretKey.ModInverse(ecc.N));
                var solution    = RIPEMD160.Hash(secretPoint.CompressedBytes);
                this.SendCommand(NetworkCommand.FollowsServerChallengeSolution, solution.Concat(serverTimeBytes));
                this.SetSharedKey(SHA256.Hash(secretPoint.UncompressedBytes), solution.Take(128 / 8).ToArray());
            }
            break;

            case NetworkCommand.FollowsServerChallengeSolution:
            {
                byte[] ripemd160Solution = this.ReceiveBytes(160 / 8);
                var    challengeDuration = TimeSpan.FromTicks(DateTime.Now.Ticks - BitConverter.ToInt64(this.ReceiveBytes(8), 0));
                var    secretPoint       = ecc.G.Multiply(serverChallenge);
                byte[] verification      = RIPEMD160.Hash(secretPoint.CompressedBytes);
                if (!ripemd160Solution.IsSameAs(verification))
                {
                    throw new InvalidOperationException("Authentication failed.");
                }
                this.SetSharedKey(SHA256.Hash(secretPoint.UncompressedBytes), verification.Take(128 / 8).ToArray());
                this.LogString(this, "Client challenge duration: " + challengeDuration.ToString() + "\r\n");
            }
            break;

            case NetworkCommand.IncomingMessageReplyToAll:
            case NetworkCommand.IncomingMessageNoReply:
            {
                string incomingMessage = this.DecodeIncomingMessage();
                this.ProcessStringCommand(this, incomingMessage);
            }
            break;

            case NetworkCommand.RegisterNewUserName:
            {
                string          clientIdentifiers = this.ReadString();
                string          userName          = this.ReadString();
                UserInformation userInformation   = new UserInformation()
                {
                    ClientIdentifiers = clientIdentifiers,
                    UserName          = userName
                };
                var configurationFile = ConfigurationFile.LoadFromFile();
                configurationFile.AddUser(userInformation);
                configurationFile.SaveToFile();
            }
            break;
            }
            return(command);
        }
Example #6
0
        public void Step()
        {
            byte next            = Execution.Pop();
            bool branchIsRunning = RunBranch.Items.All(b => b);

            if (branchIsRunning)
            {
                if (Opcodes.IsFastPush(next))
                {
                    Main.Push(Execution.Pop(next));
                }
                else
                {
                    switch ((Op)next)
                    {
                    case Op.False:
                        Main.Push(false);
                        break;

                    case Op.PushData1:
                        int i = new SignedInt(Execution.Pop()).Value;
                        Main.Push(Execution.Pop(i));
                        break;

                    case Op.PushData2:
                        Main.Push(Execution.Pop(new SignedInt(Execution.Pop(2)).Value));
                        break;

                    case Op.PushData4:
                        Main.Push(Execution.Pop(new SignedInt(Execution.Pop(4)).Value));
                        break;

                    case Op.Negate1:
                        Main.Push(-1);
                        break;

                    case Op.True:
                        Main.Push(true);
                        break;

                    case Op.Num2:
                        Main.Push(2);
                        break;

                    case Op.Num3:
                        Main.Push(3);
                        break;

                    case Op.Num4:
                        Main.Push(4);
                        break;

                    case Op.Num5:
                        Main.Push(5);
                        break;

                    case Op.Num6:
                        Main.Push(6);
                        break;

                    case Op.Num7:
                        Main.Push(7);
                        break;

                    case Op.Num8:
                        Main.Push(8);
                        break;

                    case Op.Num9:
                        Main.Push(9);
                        break;

                    case Op.Num10:
                        Main.Push(10);
                        break;

                    case Op.Num11:
                        Main.Push(11);
                        break;

                    case Op.Num12:
                        Main.Push(12);
                        break;

                    case Op.Num13:
                        Main.Push(13);
                        break;

                    case Op.Num14:
                        Main.Push(14);
                        break;

                    case Op.Num15:
                        Main.Push(15);
                        break;

                    case Op.Num16:
                        Main.Push(16);
                        break;

                    case Op.Verify:
                        if (!Main.PopTruth())
                        {
                            throw new Exception();
                        }
                        break;

                    case Op.Return:
                        throw new Exception();

                    case Op.ToAltStack:
                        Alt.Push(Main.Pop());
                        break;

                    case Op.FromAltStack:
                        Main.Push(Alt.Pop());
                        break;

                    case Op.IfDup:
                        if (Main.IsTrue)
                        {
                            Main.Push(Main[0]);
                        }
                        break;

                    case Op.Depth:
                        Main.Push(Main.Count);
                        break;

                    case Op.Drop:
                        Main.Pop();
                        break;

                    case Op.Dup:
                        Main.Push(Main[0]);
                        break;

                    case Op.Nip:
                        Main.RemoveAt(1);
                        break;

                    case Op.Over:
                        Main.Push(Main[1]);
                        break;

                    case Op.Pick:
                        Main.Push(Main[Main.PopInt()]);
                        break;

                    case Op.Roll:
                        int    index = Main.PopInt();
                        byte[] roll  = Main[index];
                        Main.RemoveAt(index);
                        Main.Push(roll);
                        break;

                    case Op.Rot:
                        byte[][] rot = Main.Pop(3);
                        Main.Push(new byte[][] { rot[0], rot[2], rot[1] });
                        break;

                    case Op.Swap:
                        byte[][] swap = Main.Pop(2);
                        Main.Push(new byte[][] { swap[0], swap[1] });
                        break;

                    case Op.Tuck:
                        byte[][] tuck = Main.Pop(2);
                        Main.Push(new byte[][] { tuck[0], tuck[1], tuck[0] });
                        break;

                    case Op.Drop2:
                        Main.Pop(2);
                        break;

                    case Op.Dup2:
                        Main.Push(new byte[][] { Main[1], Main[0] });
                        break;

                    case Op.Dup3:
                        Main.Push(new byte[][] { Main[2], Main[1], Main[0] });
                        break;

                    case Op.Over2:
                        Main.Push(new byte[][] { Main[3], Main[2] });
                        break;

                    case Op.Rot2:
                        byte[][] rot2 = Main.Pop(6);
                        Main.Push(new byte[][] { rot2[1], rot2[0], rot2[5], rot2[4], rot2[3], rot2[2] });
                        break;

                    case Op.Swap2:
                        byte[][] swap2 = Main.Pop(4);
                        Main.Push(new byte[][] { swap2[2], swap2[3], swap2[0], swap2[1] });
                        break;

                    case Op.Size:
                        throw new NotImplementedException();

                    case Op.Equal:
                        Main.Push(Main[0].SequenceEqual(Main[1]));
                        break;

                    case Op.EqualVerify:
                        Main.Push(Main[0] == Main[1]);
                        goto case Op.Verify;

                    case Op.Add1:
                        Main.Push(Main.PopInt() + 1);
                        break;

                    case Op.Sub1:
                        Main.Push(Main.PopInt() - 1);
                        break;

                    case Op.Negate:
                        Main.Push(-Main.PopInt());
                        break;

                    case Op.Abs:
                        Main.Push(Math.Abs(Main.PopInt()));
                        break;

                    case Op.Not:
                        Main.Push(Main.PopInt() == 0 ? 1 : 0);
                        break;

                    case Op.NotEqual0:
                        Main.Push(Main.PopInt() == 0 ? 0 : 1);
                        break;

                    case Op.Add:
                        Main.Push(Main.PopInt() + Main.PopInt());
                        break;

                    case Op.Sub:
                        int b = Main.PopInt();
                        Main.Push(Main.PopInt() - b);
                        break;

                    case Op.BoolAnd:
                        Main.Push(Main.PopBool() && Main.PopBool());
                        break;

                    case Op.BoolOr:
                        Main.Push(Main.PopBool() || Main.PopBool());
                        break;

                    case Op.NumEqual:
                        Main.Push(Main.PopInt() == Main.PopInt());
                        break;

                    case Op.NumEqualVerify:
                        Main.Push(Main.PopInt() == Main.PopInt());
                        goto case Op.Verify;

                    case Op.NumNotEqual:
                        Main.Push(Main.PopInt() != Main.PopInt());
                        break;

                    case Op.LessThan:
                        Main.Push(Main.PopInt() < Main.PopInt());
                        break;

                    case Op.GreaterThan:
                        Main.Push(Main.PopInt() > Main.PopInt());
                        break;

                    case Op.LessThanOrEqual:
                        Main.Push(Main.PopInt() <= Main.PopInt());
                        break;

                    case Op.GreaterThanOrEqual:
                        Main.Push(Main.PopInt() >= Main.PopInt());
                        break;

                    case Op.Min:
                        Main.Push(Math.Min(Main.PopInt(), Main.PopInt()));
                        break;

                    case Op.Max:
                        Main.Push(Math.Max(Main.PopInt(), Main.PopInt()));
                        break;

                    case Op.Within:
                        int max = Main.PopInt();
                        int min = Main.PopInt();
                        int x   = Main.PopInt();
                        Main.Push(min <= x && x < max);
                        break;

                    case Op.RIPEMD160:
                        Main.Push(RIPEMD160.Hash(Main.Pop()));
                        break;

                    case Op.SHA1:
                        Main.Push(SHA1.Hash(Main.Pop()));
                        break;

                    case Op.SHA256:
                        Main.Push(SHA256.Hash(Main.Pop()));
                        break;

                    case Op.Hash160:
                        Main.Push(Digest.Hash <RIPEMD160, SHA256>(Main.Pop()));
                        break;

                    case Op.Hash256:
                        Main.Push(Digest.Hash <SHA256, SHA256>(Main.Pop()));
                        break;

                    case Op.CodeSeparator:
                        lastSeparatorIndex = _start.Count - Execution.Count;
                        break;

                    case Op.CheckSig:
//						Main.Push(Transaction.SigIsValid(Main.Pop(), Main.Pop(), SubScript, InputIndex));
                        throw new NotImplementedException();

                    case Op.CheckSigVerify:
                        throw new NotImplementedException();

                    case Op.CheckMultiSig:
                        throw new NotImplementedException();

                    case Op.CheckMultiSigVerify:
                        throw new NotImplementedException();

                    case Op.Reserved:
                    case Op.Ver:
                    case Op.Reserved1:
                    case Op.Reserved2:
                        throw new Exception(String.Format("Invalid script! (opcode {0} used)", (Op)next));


                    case Op.If:
                    case Op.NotIf:
                    case Op.Else:
                    case Op.EndIf:
                        goto case Op.Nop;

                    case Op.Nop:
                    case Op.Nop1:
                    case Op.Nop2:
                    case Op.Nop3:
                    case Op.Nop4:
                    case Op.Nop5:
                    case Op.Nop6:
                    case Op.Nop7:
                    case Op.Nop8:
                    case Op.Nop9:
                    case Op.Nop10:
                        break;

                    default:
                        throw new Exception(String.Format("{0} is an invalid opcode", next));
                    }
                }
            }

            switch ((Op)next)
            {
            case Op.If:
                RunBranch.Push(branchIsRunning && Main.PopBool());
                break;

            case Op.NotIf:
                RunBranch.Push(branchIsRunning && !Main.PopBool());
                break;

            case Op.Else:
                RunBranch.Push(!RunBranch.Pop() && !branchIsRunning);
                break;

            case Op.EndIf:
                RunBranch.Pop();
                break;

            case Op.VerIf:
            case Op.VerNotIf:
                throw new Exception(String.Format("Invalid script! (opcode {0} included)", (Op)next));
            }
        }