Beispiel #1
0
        public void ReadEntity(IDictionary <string, EntityProperty> properties, OperationContext operationContext)
        {
#if RT
            IEnumerable <PropertyInfo> myProperties = entity.GetType().GetRuntimeProperties();
            var classAttributes = System.Attribute.GetCustomAttributes(entity.GetType());
#else
            IEnumerable <PropertyInfo> myProperties = this.GetType().GetProperties();
            var classAttributes = System.Attribute.GetCustomAttributes(this.GetType());
#endif
            EncryptionTicks = properties["EncryptionTicks"]?.Int64Value ?? -1;
            EncryptionKey keyAttribute = (EncryptionKey)classAttributes.FirstOrDefault(x => !((x as EncryptionKey)?.IsDefaultValue ?? true));
            if (keyAttribute == null)
            {
                keyAttribute = (EncryptionKey)classAttributes.First(x => (x as EncryptionKey) != null);
                keyAttribute = keyAttribute ?? new EncryptionKey(24, 8, 3);
            }

            foreach (PropertyInfo property in myProperties)
            {
                // reserved properties
                if (property.Name == "PartitionKey" ||
                    property.Name == "RowKey" ||
                    property.Name == "Timestamp" ||
                    property.Name == "ETag" || property.Name == "EncryptionTicks")
                {
                    continue;
                }

                // Enforce public getter / setter
#if RT
                if (property.SetMethod == null || !property.SetMethod.IsPublic || property.GetMethod == null || !property.GetMethod.IsPublic)
#else
                if (property.GetSetMethod() == null || !property.GetSetMethod().IsPublic || property.GetGetMethod() == null || !property.GetGetMethod().IsPublic)
#endif
                {
                    continue;
                }

                // only proceed with properties that have a corresponding entry in the dictionary
                if (!properties.ContainsKey(property.Name))
                {
                    continue;
                }

                EntityProperty entityProperty = properties[property.Name];



                if (IsPropertyNull(entityProperty))
                {
                    property.SetValue(this, null, null);
                }
                else if (property.GetCustomAttribute(typeof(EncryptedProperty)) is EncryptedProperty encryptedAttribute)
                {
                    object propValue = null;
                    try
                    {
                        if (entityProperty.PropertyType == EdmType.String)
                        {
                            if (keyAttribute.isSingleKey)
                            {
                                using (var manager = new EncryptionManager(keyAttribute.Key))
                                {
                                    propValue = JsonConvert.DeserializeObject(AESEncryptor.Decrypt(entityProperty.StringValue, properties[property.Name + "IV"].StringValue), property.PropertyType);
                                }
                            }
                            else
                            {
                                using (var manager = new EncryptionManager(keyAttribute.A, keyAttribute.B, keyAttribute.C))
                                {
                                    propValue = JsonConvert.DeserializeObject(manager.Decrypt(entityProperty.StringValue, properties[property.Name + "IV"].StringValue, EncryptionTicks), property.PropertyType);
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.Write(ex);
                    }

                    if (propValue != null)
                    {
                        property.SetValue(this, propValue, null);
                    }

                    else
                    {
                        switch (entityProperty.PropertyType)
                        {
                        case EdmType.String:
                            if (property.PropertyType != typeof(string) && property.PropertyType != typeof(String))
                            {
                                continue;
                            }
                            var propertyValue = AESEncryptor.Decrypt(entityProperty.StringValue, properties[property.Name + "IV"].StringValue);
                            property.SetValue(this, propertyValue, null);
                            break;

                        case EdmType.Binary:
                            if (property.PropertyType != typeof(byte[]))
                            {
                                continue;
                            }

                            var binaryValue = JsonConvert.DeserializeObject <byte[]>(AESEncryptor.Decrypt(entityProperty.StringValue, properties[property.Name + "IV"].StringValue));
                            property.SetValue(this, binaryValue, null);
                            break;

                        case EdmType.Boolean:
                            if (property.PropertyType != typeof(bool) && property.PropertyType != typeof(Boolean) && property.PropertyType != typeof(Boolean?) && property.PropertyType != typeof(bool?))
                            {
                                continue;
                            }

                            property.SetValue(this, entityProperty.BooleanValue, null);
                            break;

                        case EdmType.DateTime:
                            if (property.PropertyType == typeof(DateTimeOffset))
                            {
                                var dateValue = JsonConvert.DeserializeObject <DateTimeOffset>(AESEncryptor.Decrypt(entityProperty.StringValue, properties[property.Name + "IV"].StringValue));
                                property.SetValue(this, dateValue.UtcDateTime, null);
                            }
                            else if (property.PropertyType == typeof(DateTimeOffset))
                            {
                                var dateValue = JsonConvert.DeserializeObject <DateTime>(AESEncryptor.Decrypt(entityProperty.StringValue, properties[property.Name + "IV"].StringValue));
                                property.SetValue(this, dateValue.ToUniversalTime(), null);
                            }


                            break;

                        case EdmType.Double:
                            if (property.PropertyType != typeof(double) && property.PropertyType != typeof(Double) && property.PropertyType != typeof(Double?) && property.PropertyType != typeof(double?))
                            {
                                continue;
                            }

                            var doubleValue = JsonConvert.DeserializeObject <double>(AESEncryptor.Decrypt(entityProperty.StringValue, properties[property.Name + "IV"].StringValue));
                            property.SetValue(this, doubleValue, null);
                            break;

                        case EdmType.Guid:
                            if (property.PropertyType != typeof(Guid) && property.PropertyType != typeof(Guid?))
                            {
                                continue;
                            }

                            var guidValue = JsonConvert.DeserializeObject <Guid>(AESEncryptor.Decrypt(entityProperty.StringValue, properties[property.Name + "IV"].StringValue));
                            property.SetValue(this, guidValue, null);
                            break;

                        case EdmType.Int32:
                            if (property.PropertyType != typeof(int) && property.PropertyType != typeof(Int32) && property.PropertyType != typeof(Int32?) && property.PropertyType != typeof(int?))
                            {
                                continue;
                            }

                            var intValue = JsonConvert.DeserializeObject <int>(AESEncryptor.Decrypt(entityProperty.StringValue, properties[property.Name + "IV"].StringValue));
                            property.SetValue(this, intValue, null);
                            break;

                        case EdmType.Int64:
                            if (property.PropertyType != typeof(long) && property.PropertyType != typeof(Int64) && property.PropertyType != typeof(long?) && property.PropertyType != typeof(Int64?))
                            {
                                continue;
                            }
                            var intValue2 = JsonConvert.DeserializeObject <int>(AESEncryptor.Decrypt(entityProperty.StringValue, properties[property.Name + "IV"].StringValue));
                            property.SetValue(this, intValue2, null);
                            break;
                        }
                    }
                }
                else
                {
                    switch (entityProperty.PropertyType)
                    {
                    case EdmType.String:
                        if (property.PropertyType != typeof(string) && property.PropertyType != typeof(String))
                        {
                            continue;
                        }

                        property.SetValue(this, entityProperty.StringValue, null);
                        break;

                    case EdmType.Binary:
                        if (property.PropertyType != typeof(byte[]))
                        {
                            continue;
                        }

                        property.SetValue(this, entityProperty.BinaryValue, null);
                        break;

                    case EdmType.Boolean:
                        if (property.PropertyType != typeof(bool) && property.PropertyType != typeof(Boolean) && property.PropertyType != typeof(Boolean?) && property.PropertyType != typeof(bool?))
                        {
                            continue;
                        }

                        property.SetValue(this, entityProperty.BooleanValue, null);
                        break;

                    case EdmType.DateTime:
                        if (property.PropertyType == typeof(DateTime))
                        {
                            property.SetValue(this, entityProperty.DateTimeOffsetValue.Value.UtcDateTime, null);
                        }
                        else if (property.PropertyType == typeof(DateTime?))
                        {
                            property.SetValue(this, entityProperty.DateTimeOffsetValue.HasValue ? entityProperty.DateTimeOffsetValue.Value.UtcDateTime : (DateTime?)null, null);
                        }
                        else if (property.PropertyType == typeof(DateTimeOffset))
                        {
                            property.SetValue(this, entityProperty.DateTimeOffsetValue.Value, null);
                        }
                        else if (property.PropertyType == typeof(DateTimeOffset?))
                        {
                            property.SetValue(this, entityProperty.DateTimeOffsetValue, null);
                        }

                        break;

                    case EdmType.Double:
                        if (property.PropertyType != typeof(double) && property.PropertyType != typeof(Double) && property.PropertyType != typeof(Double?) && property.PropertyType != typeof(double?))
                        {
                            continue;
                        }

                        property.SetValue(this, entityProperty.DoubleValue, null);
                        break;

                    case EdmType.Guid:
                        if (property.PropertyType != typeof(Guid) && property.PropertyType != typeof(Guid?))
                        {
                            continue;
                        }

                        property.SetValue(this, entityProperty.GuidValue, null);
                        break;

                    case EdmType.Int32:
                        if (property.PropertyType != typeof(int) && property.PropertyType != typeof(Int32) && property.PropertyType != typeof(Int32?) && property.PropertyType != typeof(int?))
                        {
                            continue;
                        }

                        property.SetValue(this, entityProperty.Int32Value, null);
                        break;

                    case EdmType.Int64:
                        if (property.PropertyType != typeof(long) && property.PropertyType != typeof(Int64) && property.PropertyType != typeof(long?) && property.PropertyType != typeof(Int64?))
                        {
                            continue;
                        }

                        property.SetValue(this, entityProperty.Int64Value, null);
                        break;
                    }
                }
            }
        }
Beispiel #2
0
        public IDictionary <string, EntityProperty> WriteEntity(OperationContext operationContext)
        {
            Dictionary <string, EntityProperty> retVals = new Dictionary <string, EntityProperty>();

#if RT
            IEnumerable <PropertyInfo> objectProperties = entity.GetType().GetRuntimeProperties();
            var classAttributes = System.Attribute.GetCustomAttributes(entity.GetType());
#else
            IEnumerable <PropertyInfo> objectProperties = this.GetType().GetProperties();
            var classAttributes = System.Attribute.GetCustomAttributes(this.GetType());
#endif
            EncryptionTicks = DateTime.Now.Ticks;

            EncryptionKey keyAttribute = (EncryptionKey)classAttributes.FirstOrDefault(x => !((x as EncryptionKey)?.IsDefaultValue ?? true));
            if (keyAttribute == null)
            {
                keyAttribute = (EncryptionKey)classAttributes.First(x => (x as EncryptionKey) != null);
                keyAttribute = keyAttribute ?? new EncryptionKey(24, 8, 3);
            }

            foreach (PropertyInfo property in objectProperties)
            {
                // reserved properties
                if (property.Name == "PartitionKey" ||
                    property.Name == "RowKey" ||
                    property.Name == "Timestamp" ||
                    property.Name == "ETag")
                {
                    continue;
                }

                // Enforce public getter / setter
#if RT
                if (property.SetMethod == null || !property.SetMethod.IsPublic || property.GetMethod == null || !property.GetMethod.IsPublic)
#else
                if (property.GetSetMethod() == null || !property.GetSetMethod().IsPublic || property.GetGetMethod() == null || !property.GetGetMethod().IsPublic)
#endif
                {
                    continue;
                }

                EntityProperty newProperty = null;
                if (property.GetCustomAttribute(typeof(EncryptedProperty)) != null)
                {
                    EntityProperty ivProperty = null;
                    if (keyAttribute.isSingleKey)
                    {
                        using (var manager = new EncryptionManager(keyAttribute.Key))
                        {
                            ivProperty  = CreateEntityPropertyFromObject(AESEncryptor.CreateInitializor(), false);
                            newProperty = CreateEntityPropertyFromObject(manager.Encrypt(JsonConvert.SerializeObject(property.GetValue(this, null)), ivProperty.StringValue), false);
                        }
                    }
                    else
                    {
                        using (var manager = new EncryptionManager(keyAttribute.A, keyAttribute.B, keyAttribute.C))
                        {
                            ivProperty  = CreateEntityPropertyFromObject(AESEncryptor.CreateInitializor(), false);
                            newProperty = CreateEntityPropertyFromObject(manager.Encrypt(JsonConvert.SerializeObject(property.GetValue(this, null)), ivProperty.StringValue, EncryptionTicks), false);
                        }
                    }
                    if (newProperty != null)
                    {
                        retVals.Add(property.Name + "IV", ivProperty);
                    }
                }
                else
                {
                    newProperty = CreateEntityPropertyFromObject(property.GetValue(this, null), false);
                }

                // property will be null if unknown type
                if (newProperty != null)
                {
                    retVals.Add(property.Name, newProperty);
                }
            }

            return(retVals);
        }