示例#1
0
        public static byte[] ShiftBitsRight([NotNull] this byte[] input,
                                            int shiftCount,
                                            out byte[] carry)
        {
            if (input == null)
            {
                throw new ArgumentNullException(nameof(input));
            }

            var bytes = ByteArrayUtilities.CreateFilledByteArray(input.Length, 0x00);

            carry = input.GetBits(input.Length * 8 - shiftCount, shiftCount); // fill carry bits

            var bitWriteIndex = shiftCount;

            for (var bitReadIndex = 0; bitReadIndex < bytes.Length * 8 - shiftCount; bitReadIndex++, bitWriteIndex++)
            {
                var bitMask = (byte)(0x80 >> bitReadIndex % 8); // bit mask built from current bit position in byte

                if ((input[bitReadIndex / 8] & bitMask) > 0)    // bit is set
                {
                    bytes[bitWriteIndex / 8] |= (byte)(0x80 >> bitWriteIndex % 8);
                }
            }

            return(bytes);
        }
示例#2
0
        public void ParseWifStringTest()
        {
            // Compressed: "1E99423A4ED27608A15A2616A2B0E9E52CED330AC530EDCC32C8FFC6A526AEDD";
            var expectedBytes = HexUtilities.HexStringToBytes("801E99423A4ED27608A15A2616A2B0E9E52CED330AC530EDCC32C8FFC6A526AEDD01");
            var input         = "KxFC1jmwwCoACiCAWZ3eXa96mBM6tb3TYzGmf6YwgdGWZgawvrtJ";
            var prvKey        = PrivateKey.CreateFromWifString(input);
            var wifBytes      = prvKey.GetWifBytes();

            Assert.IsTrue(ByteArrayUtilities.CompareByteArrays(expectedBytes, wifBytes));
            var wifString = prvKey.GetWifString();

            Assert.AreEqual(input, wifString);
            Assert.IsTrue(prvKey.WifCompressed);
            Assert.IsFalse(prvKey.TestNet);

            // Compressed (TestNet): "1cca23de92fd1862fb5b76e5f4f50eb082165e5191e116c18ed1a6b24be6a53f";
            expectedBytes = HexUtilities.HexStringToBytes("ef1cca23de92fd1862fb5b76e5f4f50eb082165e5191e116c18ed1a6b24be6a53f01");
            input         = "cNYfWuhDpbNM1JWc3c6JTrtrFVxU4AGhUKgw5f93NP2QaBqmxKkg";
            prvKey        = PrivateKey.CreateFromWifString(input);
            wifBytes      = prvKey.GetWifBytes();
            Assert.IsTrue(ByteArrayUtilities.CompareByteArrays(expectedBytes, wifBytes));
            wifString = prvKey.GetWifString();
            Assert.AreEqual(input, wifString);
            Assert.IsTrue(prvKey.WifCompressed);
            Assert.IsTrue(prvKey.TestNet);

            // Uncompressed: "1E99423A4ED27608A15A2616A2B0E9E52CED330AC530EDCC32C8FFC6A526AEDD";
            expectedBytes = HexUtilities.HexStringToBytes("801E99423A4ED27608A15A2616A2B0E9E52CED330AC530EDCC32C8FFC6A526AEDD");
            input         = "5J3mBbAH58CpQ3Y5RNJpUKPE62SQ5tfcvU2JpbnkeyhfsYB1Jcn";
            prvKey        = PrivateKey.CreateFromWifString(input);
            wifBytes      = prvKey.GetWifBytes();
            Assert.IsTrue(ByteArrayUtilities.CompareByteArrays(expectedBytes, wifBytes));
            wifString = prvKey.GetWifString();
            Assert.AreEqual(input, wifString);
            Assert.IsFalse(prvKey.WifCompressed);
            Assert.IsFalse(prvKey.TestNet);

            // Uncompressed: "0C28FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D";
            input         = "5HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ";
            expectedBytes = HexUtilities.HexStringToBytes("800C28FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D");
            prvKey        = PrivateKey.CreateFromWifString(input);
            wifBytes      = prvKey.GetWifBytes();
            Assert.IsTrue(ByteArrayUtilities.CompareByteArrays(expectedBytes, wifBytes));
            wifString = prvKey.GetWifString();
            Assert.AreEqual(input, wifString);
            Assert.IsFalse(prvKey.WifCompressed);
            Assert.IsFalse(prvKey.TestNet);

            // Uncompressed (TestNet): "93XfLeifX7Jx7n7ELGMAf1SUR6f9kgQs8Xke8WStMwUtrDucMzn";
            input  = "93XfLeifX7Jx7n7ELGMAf1SUR6f9kgQs8Xke8WStMwUtrDucMzn";
            prvKey = PrivateKey.CreateFromWifString(input);
            Assert.AreEqual((BigInteger.Pow(2, 256) - BigInteger.Pow(2, 201)), prvKey.Value);
            wifString = prvKey.GetWifString();
            Assert.AreEqual(input, wifString);
            Assert.IsFalse(prvKey.WifCompressed);
            Assert.IsTrue(prvKey.TestNet);
        }
        public void DecodeBase58CheckVersion5Test()
        {
            // Version 5 (0x05) Prefixes (Pay-to-Script-Hash Addresses)

            var data     = "3CMCRgEm8HVz3DrWaCCid3vAANE42jcEv9";
            var expected = HexUtilities.HexStringToBytes("0574E9D4CDE54B8A6DECDD997541E44508FF8BA5E8");
            var actual   = Base58Utilities.DecodeBase58Check(data);

            Assert.IsTrue(ByteArrayUtilities.CompareByteArrays(expected, actual));
        }
示例#4
0
文件: Subnet.cs 项目: nicksterx/Arcus
        /// <exception cref="ArgumentException">Routing prefix is out of range</exception>
        private void InitSubnetFromIPAndRoute([NotNull] IPAddress ipAddress,
                                              [CanBeNull] int?routingPrefix)
        {
            if (ipAddress == null)
            {
                throw new ArgumentNullException(nameof(ipAddress));
            }

            // determine address type
            var isIPv4 = ipAddress.AddressFamily == AddressFamily.InterNetwork;
            var isIPv6 = ipAddress.AddressFamily == AddressFamily.InterNetworkV6;

            if (!isIPv4 &&
                !isIPv6)
            {
                throw new ArgumentException("IP Address must be IPv4 or IPv6", nameof(ipAddress));
            }

            this.RoutingPrefix = routingPrefix ?? (isIPv4
                                                       ? IPv4BitCount
                                                       : IPv6BitCount); // assign routing prefix

            // validate routing prefix
            if (this.RoutingPrefix < 0 ||
                (isIPv4 && this.RoutingPrefix > IPv4BitCount) ||
                (isIPv6 && this.RoutingPrefix > IPv6BitCount))
            {
                throw new ArgumentException("Routing prefix is out of range", nameof(routingPrefix));
            }

            var addressBytes      = ipAddress.GetAddressBytes(); // get the byte of the ip address
            var addressByteLength = addressBytes.Length * 8;     // get the number of bytes in the address

            // calculate net mask
            var netmaskBytes = ByteArrayUtilities.CreateFilledByteArray(isIPv6
                                                                            ? 16
                                                                            : 4)
                               .ShiftBitsLeft(addressByteLength - this.RoutingPrefix);

            this.Netmask = new IPAddress(netmaskBytes); // assign net mask as ip address

            var networkBytes = addressBytes.BitwiseAnd(netmaskBytes);

            this.NetworkPrefixAddress = new IPAddress(networkBytes);

            var broadcastBytes = addressBytes.BitwiseOr(netmaskBytes.BitwiseNot());

            this.BroadcastAddress = new IPAddress(broadcastBytes);

            this._length = BigInteger.Pow(2, (isIPv4
                                                  ? IPv4BitCount
                                                  : IPv6BitCount) - this.RoutingPrefix);
        }
        public void DecodeBase58CheckVersion0Test()
        {
            // Version 0 (0x00) Prefixes (Bitcoin Addresses)

            var data     = "16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM";
            var expected = HexUtilities.HexStringToBytes("00010966776006953D5567439E5E39F86A0D273BEE");
            var actual   = Base58Utilities.DecodeBase58Check(data);

            Assert.IsTrue(ByteArrayUtilities.CompareByteArrays(expected, actual));

            data     = "1NqmBmniPt6viRizQjbjWzA6zc3M3Fvdqt";
            expected = HexUtilities.HexStringToBytes("00EF937DF494BC737F158D6593856DFEBD80988BED");
            actual   = Base58Utilities.DecodeBase58Check(data);
            Assert.IsTrue(ByteArrayUtilities.CompareByteArrays(expected, actual));
        }
示例#6
0
        public static byte[] BitwiseNot([NotNull] this byte[] input)
        {
            if (input == null)
            {
                throw new ArgumentNullException(nameof(input));
            }

            var result = ByteArrayUtilities.CreateFilledByteArray(input.Length, 0x00);

            for (var i = 0; i < input.Length; i++)
            {
                result[i] = (byte)~input[i];
            }

            return(result);
        }
示例#7
0
        private static byte[] Decompress(byte[] data)
        {
            if (data.Length != COMPRESSED_LENGTH)
            {
                throw new ArgumentException($"Compressed public key must be {COMPRESSED_LENGTH} bytes", nameof(data));
            }
            if (data[0] != ODD_PREFIX && data[0] != EVEN_PREFIX)
            {
                throw new ArgumentException("Compressed prefix missing", nameof(data));
            }

            var point = CurvePars.Curve.DecodePoint(data).Normalize();

            byte[] decompressed = new byte[UNCOMPRESSED_LENGTH];
            decompressed[0] = UNCOMPRESSED_PREFIX;
            Array.Copy(ByteArrayUtilities.PadLeft(point.XCoord.ToBigInteger().ToByteArrayUnsigned(), 32), 0, decompressed, 1, COOR_LENGTH);
            Array.Copy(ByteArrayUtilities.PadLeft(point.YCoord.ToBigInteger().ToByteArrayUnsigned(), 32), 0, decompressed, 1 + COOR_LENGTH, COOR_LENGTH);

            return(decompressed);
        }
        public void DecodeBase58CheckNonStandardTest()
        {
            // Version 0 (0x00) Prefixes, non-standard lengths

            var data     = "173RKgkk7fMbYUYBGyyAHeZ6rwfKRMn17h7DtGsmpEdab8TV6UB";
            var expected = HexUtilities.HexStringToBytes("00031bab84e687e36514eeaf5a017c30d32c1f59dd4ea6629da7970ca374513dd006");
            var actual   = Base58Utilities.DecodeBase58Check(data);

            Assert.IsTrue(ByteArrayUtilities.CompareByteArrays(expected, actual));

            data     = "12ANjYr7zPnxRdZfnmC2e6jjHDpBY";
            expected = HexUtilities.HexStringToBytes("005361746f736869204e616b616d6f746f");
            actual   = Base58Utilities.DecodeBase58Check(data);
            Assert.IsTrue(ByteArrayUtilities.CompareByteArrays(expected, actual));

            // Version 42 (0x2A) Prefix, non standard data

            data     = "7DTXS6pY6a98XH2oQTZUbbd1Z7P4NzkJqfraixprPutXQVTkwBGw";
            expected = HexUtilities.HexStringToBytes("2a031bab84e687e36514eeaf5a017c30d32c1f59dd4ea6629da7970ca374513dd006");
            actual   = Base58Utilities.DecodeBase58Check(data);
            Assert.IsTrue(ByteArrayUtilities.CompareByteArrays(expected, actual));
        }
示例#9
0
        public void HexStringToBytesTest()
        {
            string hexLower = "0180ff";
            var    expected = new byte[] { 0x01, 0x80, 0xFF };
            var    bytes    = HexUtilities.HexStringToBytes(hexLower);

            Assert.IsTrue(ByteArrayUtilities.CompareByteArrays(expected, bytes));

            string hexUpper = "0180FF";

            bytes = HexUtilities.HexStringToBytes(hexUpper);
            Assert.IsTrue(ByteArrayUtilities.CompareByteArrays(expected, bytes));

            string hexEmpty = string.Empty;

            bytes = HexUtilities.HexStringToBytes(hexEmpty);
            Assert.IsTrue(bytes.Length == 0);

            string hexZero = "0000";

            expected = new byte[] { 0x00, 0x00 };
            bytes    = HexUtilities.HexStringToBytes(hexZero);
            Assert.IsTrue(ByteArrayUtilities.CompareByteArrays(expected, bytes));
        }
示例#10
0
        public static byte[] GetBits([NotNull] this byte[] sourceBytes,
                                     int start,
                                     int?length = null)
        {
            if (sourceBytes == null)
            {
                throw new ArgumentNullException(nameof(sourceBytes));
            }

            var sourceBitLength = sourceBytes.Length * 8;            // the total number of bits in the source
            var readLength      = length ?? sourceBitLength - start; // the length of bits that will be read

            if (readLength > sourceBitLength)
            {
                throw new InvalidOperationException("can't capture more bits than present");
            }

            var writeBytesLength = (int)System.Math.Ceiling((double)readLength / 8);                 // the length of the writable bytes
            var writeBytes       = ByteArrayUtilities.CreateFilledByteArray(writeBytesLength, 0x00); // destination array

            var destinationBitIndex = 0;                                                             // the bit index of the destination

            for (var i = 0; i < readLength; i++, destinationBitIndex++)
            {
                var sourceBitIndex = start + readLength - 1 - i;         // bit index of the source
                var bitMask        = (byte)(0x80 >> sourceBitIndex % 8); // bit mask built from current bit position in byte

                if ((sourceBytes[sourceBitIndex / 8] & bitMask) > 0)     // bit is set
                {
                    writeBytes[destinationBitIndex / 8] |= (byte)(0x01 << destinationBitIndex % 8);
                }
            }

            return(writeBytes.Reverse()
                   .ToArray());
        }
示例#11
0
        public override string ToString()
        {
            var publicKeyToken = _publicKeyToken;

            return(string.Format(CultureInfo.InvariantCulture, "{0} {1}", base.ToString(), FormatXResource(typeof(DotNetStrongAssemblyNameReference), "ToString", _assemblyName, _version, _culture, (publicKeyToken == null ? null : ByteArrayUtilities.ToHexString(publicKeyToken)))));
        }
示例#12
0
        public string ToFullyQualifiedName()
        {
            var name           = _assemblyName;
            var version        = _version;
            var culture        = _culture;
            var publicKeyToken = _publicKeyToken;

            EnsureNotDisposeState();
            return(string.Format("{0}{4} Version={1}{4} Culture={2}{4} PublicKeyToken={3}", name, version, culture, publicKeyToken == null ? "null" : ByteArrayUtilities.ToHexString(publicKeyToken), new string(FullyQualifiedNameComponentDelimiter, 1)));
        }
示例#13
0
 public byte[] CreateFilledArrayDefaultsTo0xFF(int size)
 => ByteArrayUtilities.CreateFilledByteArray(size);
示例#14
0
 public byte[] CreateFilledArrayTests(int size, byte intializer)//TODO: test necessary?
 => ByteArrayUtilities.CreateFilledByteArray(size, intializer);