public override string ToString()
    {
        StringBuilder Content = new StringBuilder();

        // Go through the contents and build a
        // typical query string
        foreach (string key in base.Keys)
        {
            Content.Append(HttpUtility.UrlEncode(key));
            Content.Append("=");
            Content.Append(HttpUtility.UrlEncode(base[key]));
            Content.Append("&");
        }

        // Remove the last '&'
        Content.Remove(Content.Length - 1, 1);

        // Now encrypt the contents using DPAPI
        byte[] EncryptedData = ProtectedData.Protect(
            Encoding.UTF8.GetBytes(Content.ToString()),
            null, DataProtectionScope.LocalMachine);

        // Convert encrypted byte array to a URL-legal string
        // This would also be a good place to check that data
        // is not larger than typical 4KB query string
        return(HexEncoding.GetString(EncryptedData));
    }
Пример #2
0
        public void OddNumberOfHexDigitsShouldRoundTripWithLeadingZero()
        {
            var hex   = "A12F5";
            var bytes = HexEncoding.GetBytes(hex);
            var hex2  = HexEncoding.GetString(bytes);

            Assert.Equal("0" + hex.ToLower(), hex2.ToLower());
        }
Пример #3
0
        public void HexStringsShouldRoundTrip()
        {
            var hex   = "0A12F5";
            var bytes = HexEncoding.GetBytes(hex);
            var hex2  = HexEncoding.GetString(bytes);

            Assert.Equal(hex.ToLower(), hex2.ToLower());
        }
Пример #4
0
        public void ByteArraysShouldRoundTrip()
        {
            var bytes  = new byte[] { 10, 18, 245 };
            var hex    = HexEncoding.GetString(bytes);
            var bytes2 = HexEncoding.GetBytes(hex);

            Assert.Equal(bytes, bytes2);
        }
Пример #5
0
        public void GetString_RandomBytes()
        {
            var buf = new byte[1000];

            (new Random()).NextBytes(buf);

            var expected = BitConverter.ToString(buf).Replace("-", "");
            var actual   = HexEncoding.GetString(buf);

            Assert.AreEqual(expected, actual);
        }
Пример #6
0
        public void GetString_AllByteValues()
        {
            var buf = new byte[1];

            for (byte testByte = 0; testByte < 0xff; testByte++)
            {
                buf[0] = testByte;
                var actual = HexEncoding.GetString(buf);
                Assert.AreEqual(testByte.ToString("X2"), actual);
            }
        }
        protected void GivenEncryption()
        {
#if NET452
            Configuration.Encryption = new EncryptionElement
            {
                Enabled  = true,
                Provider = "AES",
                Key      = HexEncoding.GetString(KeyGenerator.GenerateAesKey().GetSymmetricKey())
            };
#endif
#if NETCOREAPP2_0
            var section = Configuration.GetSection("encryption");
            section["enabled"]  = "true";
            section["provider"] = "aes";
            section["key"]      = HexEncoding.GetString(KeyGenerator.GenerateAesKey().Key);
#endif
        }
Пример #8
0
        public void ShouldReturnAllLowercaseLetters_WhenAskingForAllLowercase()
        {
            string value = HexEncoding.GetString(new byte[] { 170, 170, 170, 170 });

            Assert.Equal("aaaaaaaa", value);
        }
Пример #9
0
        public void ShouldReturnAllFs_WhenAllBitsAreSet()
        {
            string value = HexEncoding.GetString(new byte[] { 255, 255, 255, 255 });

            Assert.Equal("ffffffff", value);
        }
Пример #10
0
        public void ShouldReturnHighByteValues_WhenOnlyHighByteValuesArePassedIn()
        {
            string value = HexEncoding.GetString(new byte[] { 16, 32, 48, 64, 80, 96, 112, 128, 144, 160, 176, 192, 208, 224, 240 });

            Assert.Equal("102030405060708090a0b0c0d0e0f0", value);
        }
Пример #11
0
        public void ShouldReturnLowByteValues_WhenOnlyLowByteValuesArePassedIn()
        {
            string value = HexEncoding.GetString(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 });

            Assert.Equal("0102030405060708090a0b0c0d0e0f", value);
        }
Пример #12
0
        public void ShouldReturn00_When0IsPassedIn()
        {
            string value = HexEncoding.GetString(new byte[] { 0 });

            Assert.Equal("00", value);
        }
Пример #13
0
        public void ShouldReturnEmptyString_WhenAnEmptyArrayIsPassedIn()
        {
            string value = HexEncoding.GetString(new byte[0]);

            Assert.Equal("", value);
        }
Пример #14
0
 public void ShouldThrowArgumentNullException_WhenNullIsPassedIn()
 {
     Assert.Throws <ArgumentNullException>(() => HexEncoding.GetString(null));
 }
Пример #15
0
        public void GetString_EmptyByteArray()
        {
            var actual = HexEncoding.GetString(new byte[0]);

            Assert.AreEqual("", actual);
        }