示例#1
0
        public void TestFromHexWithSpace()
        {
            const string hex = "01-0203-0405\t0607\r\n0809\r0a0b0c\n0d0e0f10 ";

            byte[] bin = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };

            Assert.AreEqual(0, BinaryComparer.Compare(bin, HexEncoding.DecodeBytes(hex)));
            Assert.AreEqual(0, BinaryComparer.Compare(bin, HexEncoding.DecodeBytes(Encoding.ASCII.GetBytes(hex))));
        }
示例#2
0
        public void TestToFromHexPartial()
        {
            const string hex = "0102030405060708090a0b0c0d0e0f10";

            byte[] bin = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };

            Assert.AreEqual(hex.Substring(4, 10), HexEncoding.EncodeBytes(bin, 2, 5));
            Assert.AreEqual(0, BinaryComparer.Compare(new byte[] { 3, 4, 5, 6 }, HexEncoding.DecodeBytes(hex, 4, 8)));
        }
示例#3
0
        public void TestToFromHex()
        {
            byte[] all = new byte[256];
            for (int i = 0; i < all.Length; i++)
            {
                all[i] = (byte)i;
            }

            Assert.AreEqual(AllHex, HexEncoding.EncodeBytes(all));
            Assert.AreEqual(0, BinaryComparer.Compare(all, HexEncoding.DecodeBytes(AllHex)));
            Assert.AreEqual(0, BinaryComparer.Compare(all, HexEncoding.DecodeBytes(AllHex.ToUpper())));
        }
示例#4
0
        public void TestWriteHexStream()
        {
            using (Stream mem = new MemoryStream())
            {
                using (Stream io = new HexStream(new NonClosingStream(mem), CryptoStreamMode.Write))
                    io.Write(HexEncoding.DecodeBytes(AllHex), 0, AllHex.Length / 2);

                Assert.AreEqual(AllHex.Length, mem.Position);
                mem.Position = 0;
                string test = new StreamReader(mem).ReadToEnd();
                Assert.AreEqual(AllHex, test);
            }
        }
示例#5
0
        private void TestAes256CBC(bool enc, string key, string iv, string input, string expect)
        {
            ModifiedRijndael r = ModifiedRijndael.Create();

            r.Mode      = CipherMode.CBC;
            r.Padding   = PaddingMode.None;
            r.BlockSize = iv.Length / 2 * 8;
            r.IV        = HexEncoding.DecodeBytes(iv);
            r.KeySize   = key.Length / 2 * 8;
            r.Key       = HexEncoding.DecodeBytes(key);
            ICryptoTransform xf = enc ? r.CreateEncryptor() : r.CreateDecryptor();

            byte[] result = xf.TransformFinalBlock(HexEncoding.DecodeBytes(input), 0, input.Length / 2);
            byte[] test   = HexEncoding.DecodeBytes(expect);
            Assert.AreEqual(0, BinaryComparer.Compare(test, result));
        }
示例#6
0
        public void TestFromHexWithUnevenCount()
        {
            const string hex = "0a 0";

            HexEncoding.DecodeBytes(hex);
        }
示例#7
0
        public void TestFromHexWithIllegalCharacter()
        {
            const string hex = "0x01";

            HexEncoding.DecodeBytes(hex);
        }