Ejemplo n.º 1
0
        public TData Unprotect(string protectedText)
        {
            try
            {
                if (protectedText == null)
                {
                    return(default(TData));
                }

                byte[] protectedData = Base64UrlTextEncoder.Decode(protectedText);
                if (protectedData == null)
                {
                    return(default(TData));
                }

                byte[] userData = _protector.Unprotect(protectedData);
                if (userData == null)
                {
                    return(default(TData));
                }

                TData model = _serializer.Deserialize(userData);
                return(model);
            }
            catch
            {
                // TODO trace exception, but do not leak other information
                return(default(TData));
            }
        }
Ejemplo n.º 2
0
        public TData Unprotect(string protectedText, string purpose)
        {
            try
            {
                if (protectedText == null)
                {
                    return(default(TData));
                }

                var protectedData = Base64UrlTextEncoder.Decode(protectedText);
                if (protectedData == null)
                {
                    return(default(TData));
                }

                var protector = _protector;
                if (!string.IsNullOrEmpty(purpose))
                {
                    protector = protector.CreateProtector(purpose);
                }

                var userData = protector.Unprotect(protectedData);
                if (userData == null)
                {
                    return(default(TData));
                }

                return(_serializer.Deserialize(userData));
            }
            catch
            {
                // TODO trace exception, but do not leak other information
                return(default(TData));
            }
        }
Ejemplo n.º 3
0
        public string Protect(TData data)
        {
            byte[] userData      = _serializer.Serialize(data);
            byte[] protectedData = _protector.Protect(userData);
            string protectedText = Base64UrlTextEncoder.Encode(protectedData);

            return(protectedText);
        }
Ejemplo n.º 4
0
        public string Protect(TData data, string purpose)
        {
            var userData = _serializer.Serialize(data);

            var protector = _protector;

            if (!string.IsNullOrEmpty(purpose))
            {
                protector = protector.CreateProtector(purpose);
            }

            var protectedData = protector.Protect(userData);

            return(Base64UrlTextEncoder.Encode(protectedData));
        }
Ejemplo n.º 5
0
        public void DataOfVariousLengthRoundTripCorrectly()
        {
            for (int length = 0; length != 256; ++length)
            {
                var data = new byte[length];
                for (int index = 0; index != length; ++index)
                {
                    data[index] = (byte)(5 + length + (index * 23));
                }
                string text   = Base64UrlTextEncoder.Encode(data);
                byte[] result = Base64UrlTextEncoder.Decode(text);

                for (int index = 0; index != length; ++index)
                {
                    Assert.Equal(data[index], result[index]);
                }
            }
        }
        public void DataOfVariousLengthRoundTripCorrectly()
        {
            var encoder = new Base64UrlTextEncoder();
            for (int length = 0; length != 256; ++length)
            {
                var data = new byte[length];
                for (int index = 0; index != length; ++index)
                {
                    data[index] = (byte)(5 + length + (index * 23));
                }
                string text = encoder.Encode(data);
                byte[] result = encoder.Decode(text);

                for (int index = 0; index != length; ++index)
                {
                    result[index].ShouldBe(data[index]);
                }
            }
        }