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));
    }
Exemple #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());
        }
Exemple #3
0
        public void HexStringsShouldRoundTrip()
        {
            var hex   = "0A12F5";
            var bytes = HexEncoding.GetBytes(hex);
            var hex2  = HexEncoding.GetString(bytes);

            Assert.Equal(hex.ToLower(), hex2.ToLower());
        }
Exemple #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);
        }
Exemple #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);
        }
Exemple #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
        }
Exemple #8
0
        public void ShouldReturnAllLowercaseLetters_WhenAskingForAllLowercase()
        {
            string value = HexEncoding.GetString(new byte[] { 170, 170, 170, 170 });

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

            Assert.Equal("ffffffff", value);
        }
Exemple #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);
        }
Exemple #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);
        }
Exemple #12
0
        public void ShouldReturn00_When0IsPassedIn()
        {
            string value = HexEncoding.GetString(new byte[] { 0 });

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

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

            Assert.AreEqual("", actual);
        }