Пример #1
0
        public void MainAndTestnetAddressTest()
        {
            // Compressed
            var prvKey         = new PrivateKey(BigInteger.Pow(888, 3), false, true);
            var pubKey         = prvKey.GetPublicKey();
            var mainnetAddress = pubKey.GetAddress();

            Assert.AreEqual("148dY81A9BmdpMhvYEVznrM45kWN32vSCN", mainnetAddress);
            prvKey = new PrivateKey(BigInteger.Pow(888, 3), true, true);
            pubKey = prvKey.GetPublicKey();
            var testnetAddress = pubKey.GetAddress();

            Assert.AreEqual("mieaqB68xDCtbUBYFoUNcmZNwk74xcBfTP", testnetAddress);

            // Compressed
            var wifString = "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M8naNQh65b43SA";

            prvKey = PrivateKey.CreateFromWifString(wifString);
            Assert.AreEqual(1234567890, prvKey.Value);
            mainnetAddress = prvKey.GetPublicKey().GetAddress();
            Assert.AreEqual("17V2YVsReZMkvkQDP8Gs1yyGXi6y7jpnV7", mainnetAddress);

            // Compressed
            wifString      = "Kyd8ohVKC84wDwfmBEVkDnopsYRF9GmjRWx6VeUQqF4BpbWVUukv";
            prvKey         = PrivateKey.CreateFromWifString(wifString);
            mainnetAddress = prvKey.GetPublicKey().GetAddress();
            Assert.AreEqual("12Kk2yQEYamxySZk2wGzc8ueNuepMkbEQJ", mainnetAddress);

            // Uncompressed
            prvKey         = new PrivateKey(321, false, false);
            pubKey         = prvKey.GetPublicKey();
            mainnetAddress = pubKey.GetAddress();
            Assert.AreEqual("1S6g2xBJSED7Qr9CYZib5f4PYVhHZiVfj", mainnetAddress);
            prvKey         = new PrivateKey(321, true, false);
            pubKey         = prvKey.GetPublicKey();
            testnetAddress = pubKey.GetAddress();
            Assert.AreEqual("mfx3y63A7TfTtXKkv7Y6QzsPFY6QCBCXiP", testnetAddress);

            // Uncompressed
            prvKey         = new PrivateKey(4242424242, false, false);
            pubKey         = prvKey.GetPublicKey();
            mainnetAddress = pubKey.GetAddress();
            Assert.AreEqual("1226JSptcStqn4Yq9aAmNXdwdc2ixuH9nb", mainnetAddress);
            prvKey         = new PrivateKey(4242424242, true, false);
            pubKey         = prvKey.GetPublicKey();
            testnetAddress = pubKey.GetAddress();
            Assert.AreEqual("mgY3bVusRUL6ZB2Ss999CSrGVbdRwVpM8s", testnetAddress);

            // Uncompressed
            wifString      = "5Kb8kLf9zgWQnogidDA76MzPL6TsZZY36hWXMssSzNydYXYB9KF";
            prvKey         = PrivateKey.CreateFromWifString(wifString);
            mainnetAddress = prvKey.GetPublicKey().GetAddress();
            Assert.AreEqual("1CC3X2gu58d6wXUWMffpuzN9JAfTUWu4Kj", mainnetAddress);
        }
Пример #2
0
        public void TestSignature()
        {
            // get the private key
            foreach (var base64 in keys)
            {
                PrivateKey key = PrivateKey.FromBase64(base64);
                Assert.AreEqual(base64, key.ToBase64());

                // derive the public key
                var pub = key.GetPublicKey();

                // encode the address
                var address = pub.GetAddress();

                var text = "Guillaume is the best !";
                // sign text
                var signed = key.SignMessage(text, Network);

                // check signature against public key
                Assert.IsTrue(pub.CheckSignature(text, signed, Network));

                // check public key against address
                Assert.IsTrue(pub.CheckAddress(address.Encoded));
            }
        }
Пример #3
0
        public void TestPublicPoints()
        {
            var points = new List <PubPointTestData>();

            points.Add(new PubPointTestData(7, "5cbdf0646e5db4eaa398f365f2ea7a0e3d419b7e0330e39ce92bddedcac4f9bc", "6aebca40ba255960a3178d6d861a54dba813d0b813fde7b5a5082628087264da"));
            points.Add(new PubPointTestData(1485, "c982196a7466fbbbb0e27a940b6af926c1a74d5ad07128c82824a11b5398afda", "7a91f9eae64438afb9ce6448a1c133db2d8fb9254e4546b6f001637d50901f55"));
            points.Add(new PubPointTestData(BigInteger.Pow(2, 128), "8f68b9d2f63b5f339239c1ad981f162ee88c5678723ea3351b7b444c9ec4c0da", "662a9f2dba063986de1d90c2b6be215dbbea2cfe95510bfdf23cbf79501fff82"));
            points.Add(new PubPointTestData(BigInteger.Pow(2, 240) + BigInteger.Pow(2, 31), "9577ff57c8234558f293df502ca4f09cbc65a6572c842b39b366f21717945116", "10b49c67fa9365ad7b90dab070be339a1daf9052373ec30ffae4f72d5e66d053"));

            foreach (var point in points)
            {
                var expectedX = BigIntegerUtilities.CreateFromUnsignedBigEndianHexString(point.X);
                var expectedY = BigIntegerUtilities.CreateFromUnsignedBigEndianHexString(point.Y);

                // Using Private/Public Key classes
                var prvKey = new PrivateKey(point.Secret);
                var pubKey = prvKey.GetPublicKey();
                Assert.AreEqual(expectedX, pubKey.X);
                Assert.AreEqual(expectedY, pubKey.Y);

                // Manual check
                var pubPoint = BitcoinConstants.G.Multiply(point.Secret);
                Assert.AreEqual(expectedX, pubPoint.X.Num);
                Assert.AreEqual(expectedY, pubPoint.Y.Num);
            }
        }
Пример #4
0
        public void TestSharedSecret()
        {
            string     privateKeyString = "5J7Yu8zZD5oV9Ex7npmsT3XBbpSdPZPBKBzLLQnXz5JHQVQVfNT";
            PrivateKey privateKey       = PrivateKey.FromWif(privateKeyString);
            var        sharedSecret     = privateKey.SharedSecret(privateKey.GetPublicKey());

            Console.WriteLine(Hex.BytesToHex(sharedSecret));
            Assert.AreEqual <string>(Hex.BytesToHex(sharedSecret), "53e731cf6c166a972962e776734f7afbfb320b1b26ddfc32125ecb7c6d6e37aa59f5b0e4ccad49630cc455a9830274c3e2ac7ae43f94f12449250f85e2311a1c", "Shared secret not expected");
        }
Пример #5
0
        public void TestPrivateKey()
        {
            string     privateKeyString = "5J7Yu8zZD5oV9Ex7npmsT3XBbpSdPZPBKBzLLQnXz5JHQVQVfNT";
            PrivateKey privateKey       = PrivateKey.FromWif(privateKeyString);

            Console.WriteLine(privateKey.ToString());
            Console.WriteLine(privateKey.GetPublicKey().ToString());
            Assert.AreEqual <string>(privateKeyString, privateKey.ToString(), "Private keys should be equal");
        }
Пример #6
0
        public EosWallet(IEnumerable <string> privateKeys)
        {
            foreach (var key in privateKeys)
            {
                var privateKey = new PrivateKey(key);
                var publicKey  = privateKey.GetPublicKey();

                keysStorage[publicKey] = privateKey;
            }
        }
Пример #7
0
        public void SignTest()
        {
            var  prvKey = new PrivateKey(1234567890);
            var  z      = 42424242;
            var  sig    = prvKey.Sign(z);
            var  pubKey = prvKey.GetPublicKey();
            bool valid  = pubKey.Verify(z, sig);

            Assert.IsTrue(valid);
        }
Пример #8
0
        public void SignTestString()
        {
            var message = Encoding.ASCII.GetBytes("This is a test message");
            // Generate signature using private key
            var prvKey      = new PrivateKey(1234567890); // WIF: KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M8naNQh65b43SA
            var wifString   = prvKey.GetWifString();
            var sig         = prvKey.Sign(message);
            var pubKeyBytes = prvKey.GetPublicKey().GetPublicKeyBytes();

            // Verify using only publicly shared information (public key, message and signature)
            var  pubKey = PublicKey.Parse(pubKeyBytes);
            bool valid  = pubKey.Verify(message, sig);

            Assert.IsTrue(valid);
        }
Пример #9
0
        private PgpSignatureGenerator InitSignature(Stream outputStream)
        {
            if (PrivateKey == null)
            {
                return(null);
            }

            var signatureGenerator = new PgpSignatureGenerator(PrivateKey.GetPublicKey().Algorithm, HashAlgorithm);

            signatureGenerator.InitSign(PgpSignature.BinaryDocument, PrivateKey.GetSecretKey().ExtractPrivateKey(_password.ToCharArray()));

            var userId             = PrivateKey.GetPublicKey().GetUserIds().OfType <string>().FirstOrDefault();
            var subpacketGenerator = new PgpSignatureSubpacketGenerator();

            subpacketGenerator.SetSignerUserId(false, userId);
            signatureGenerator.SetHashedSubpackets(subpacketGenerator.Generate());
            signatureGenerator.GenerateOnePassVersion(false).Encode(outputStream);
            return(signatureGenerator);
        }
        private void Init(byte[] data)
        {
            GXAsn1Sequence seq = (GXAsn1Sequence)GXAsn1Converter.FromByteArray(data);

            if (seq.Count < 3)
            {
                throw new System.ArgumentException("Wrong number of elements in sequence.");
            }
            Version = (CertificateVersion)seq[0];
            GXAsn1Sequence tmp = (GXAsn1Sequence)seq[1];

            Algorithm = X9ObjectIdentifierConverter.FromString(tmp[0].ToString());
            if ((X9ObjectIdentifier)Algorithm == X9ObjectIdentifier.None)
            {
                Algorithm = PkcsObjectIdentifierConverter.FromString(tmp[0].ToString());
            }
            PrivateKey = GXPrivateKey.FromRawBytes((byte[])((GXAsn1Sequence)seq[2])[1]);
            if (PrivateKey == null)
            {
                throw new Exception("Invalid private key.");
            }
            PublicKey = PrivateKey.GetPublicKey();
        }
Пример #11
0
        public void PubKeyBytesTest()
        {
            var uncompressed = "049d5ca49670cbe4c3bfa84c96a8c87df086c6ea6a24ba6b809c9de234496808d56fa15cc7f3d38cda98dee2419f415b7513dde1301f8643cd9245aea7f3f911f9";
            var compressed   = "039d5ca49670cbe4c3bfa84c96a8c87df086c6ea6a24ba6b809c9de234496808d5";
            var prvKey       = new PrivateKey(BigInteger.Pow(999, 3), false, false);
            var pubKey       = prvKey.GetPublicKey();
            var bytes        = pubKey.GetPublicKeyBytes();

            Assert.AreEqual(uncompressed, HexUtilities.BytesToHexString(bytes));
            prvKey = new PrivateKey(BigInteger.Pow(999, 3), false, true);
            pubKey = prvKey.GetPublicKey();
            bytes  = pubKey.GetPublicKeyBytes();
            Assert.AreEqual(compressed, HexUtilities.BytesToHexString(bytes));

            uncompressed = "04a598a8030da6d86c6bc7f2f5144ea549d28211ea58faa70ebf4c1e665c1fe9b5204b5d6f84822c307e4b4a7140737aec23fc63b65b35f86a10026dbd2d864e6b";
            compressed   = "03a598a8030da6d86c6bc7f2f5144ea549d28211ea58faa70ebf4c1e665c1fe9b5";
            prvKey       = new PrivateKey(123, false, false);
            pubKey       = prvKey.GetPublicKey();
            bytes        = pubKey.GetPublicKeyBytes();
            Assert.AreEqual(uncompressed, HexUtilities.BytesToHexString(bytes));
            prvKey = new PrivateKey(123, false, true);
            pubKey = prvKey.GetPublicKey();
            bytes  = pubKey.GetPublicKeyBytes();
            Assert.AreEqual(compressed, HexUtilities.BytesToHexString(bytes));

            uncompressed = "04aee2e7d843f7430097859e2bc603abcc3274ff8169c1a469fee0f20614066f8e21ec53f40efac47ac1c5211b2123527e0e9b57ede790c4da1e72c91fb7da54a3";
            compressed   = "03aee2e7d843f7430097859e2bc603abcc3274ff8169c1a469fee0f20614066f8e";
            prvKey       = new PrivateKey(42424242, false, false);
            pubKey       = prvKey.GetPublicKey();
            bytes        = pubKey.GetPublicKeyBytes();
            Assert.AreEqual(uncompressed, HexUtilities.BytesToHexString(bytes));
            prvKey = new PrivateKey(42424242, false, true);
            pubKey = prvKey.GetPublicKey();
            bytes  = pubKey.GetPublicKeyBytes();
            Assert.AreEqual(compressed, HexUtilities.BytesToHexString(bytes));
        }
Пример #12
0
        public void AddressTest()
        {
            // Uncompressed Public Key
            var ecdsaPrivateKey = "18E14A7B6A307F426A94F8114701E7C8E774E7F9A47E2C2035DB29A206321725";
            var expectedPubXVal = "50863AD64A87AE8A2FE83C1AF1A8403CB53F53E486D8511DAD8A04887E5B2352";
            var expectedPubYVal = "2CD470243453A299FA9E77237716103ABC11A1DF38855ED6F2EE187E9C582BA6";
            var expectedPubKey  = "0450863AD64A87AE8A2FE83C1AF1A8403CB53F53E486D8511DAD8A04887E5B23522CD470243453A299FA9E77237716103ABC11A1DF38855ED6F2EE187E9C582BA6";
            var expectedAddress = "16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM";

            var prvKeyVal = BigIntegerUtilities.CreateFromUnsignedBigEndianHexString(ecdsaPrivateKey);
            var prvKey    = new PrivateKey(prvKeyVal, false, false);
            var pubKey    = prvKey.GetPublicKey();

            Assert.AreEqual(expectedPubXVal, pubKey.X.PositiveValToHexString(false));
            Assert.AreEqual(expectedPubYVal, pubKey.Y.PositiveValToHexString(false));

            var pubKeyBytes  = pubKey.GetPublicKeyBytes();
            var pubKeyString = HexUtilities.BytesToHexString(pubKeyBytes, false);

            Assert.AreEqual(expectedPubKey, pubKeyString);
            var address = pubKey.GetAddress();

            Assert.AreEqual(expectedAddress, address);
        }
Пример #13
0
        protected void SendPunchPackets(NetConnection conn)
        {
            var cParams = conn.GetConnectionParameters();

            var stream = new PacketStream();

            stream.Write((byte)PacketType.Punch);

            if (cParams.IsInitiator)
            {
                cParams.Nonce.Write(stream);
            }
            else
            {
                cParams.ServerNonce.Write(stream);
            }

            var encryptPos = stream.GetBytePosition();

            stream.SetBytePosition(encryptPos);

            if (cParams.IsInitiator)
            {
                cParams.ServerNonce.Write(stream);
            }
            else
            {
                cParams.Nonce.Write(stream);

                if (stream.WriteFlag(RequiresKeyExchange || (cParams.RequestKeyExchange && PrivateKey != null)))
                {
                    stream.Write(stream.WriteFlag(cParams.RequestCertificate && Certificate != null) ? Certificate : PrivateKey.GetPublicKey());
                }
            }

            var theCipher = new SymmetricCipher(cParams.ArrangedSecret);

            stream.HashAndEncrypt(NetConnection.MessageSignatureBytes, encryptPos, theCipher);

            foreach (var ep in cParams.PossibleAddresses)
            {
                stream.SendTo(Socket, ep);

                Console.WriteLine("Sending punch packet ({0}, {1}) to {2}", Convert.ToBase64String(cParams.Nonce.Data), Convert.ToBase64String(cParams.ServerNonce.Data), ep);
            }

            ++conn.ConnectSendCount;
            conn.ConnectLastSendTime = GetCurrentTime();
        }
Пример #14
0
        protected void SendConnectChallengeResponse(IPEndPoint addr, Nonce clientNonce, bool wantsKeyExchange, bool wantsCertificate)
        {
            var stream = new PacketStream();

            stream.Write((byte)PacketType.ConnectChallengeResponse);

            clientNonce.Write(stream);

            var identityToken = ComputeClientIdentityToken(addr, clientNonce);

            stream.Write(identityToken);

            PuzzleManager.CurrentNonce.Write(stream);
            stream.Write(PuzzleManager.CurrentDifficulty);

            // ReSharper disable PossibleNullReferenceException
            if (stream.WriteFlag(RequiresKeyExchange || (wantsKeyExchange && PrivateKey != null)))
            {
                stream.Write(stream.WriteFlag(wantsCertificate && Certificate != null) ? Certificate : PrivateKey.GetPublicKey());
            }
            // ReSharper restore PossibleNullReferenceException

            stream.SendTo(Socket, addr);
        }
 private PrivateKeyNotWallet(PrivateKey key)
 {
     PrivateKey = key;
     PublicKey  = key.GetPublicKey();
     Address    = PublicKey.GetAddress();
 }