示例#1
0
        internal EncryptedType(Type type, EncryptionProxyConfiguration configuration = null)
        {
            if (Generator == null)
            {
                Generator = new ProxyGenerator();
            }

            OriginalType  = type;
            Configuration = configuration ?? new EncryptionProxyConfiguration();
            MixinType     = DataStorageMixinFactory.Generate(OriginalType).GetType();

            Properties = new Dictionary <string, EncryptedProperty>();
            PendingGenerations.Add(type);
            var generatedSample = GenerateInstance(type);

            PendingGenerations.Remove(type);
            var eligibleProperties = DataStorageMixinFactory.GetEncryptionEligibleProperties(generatedSample.GetType());

            foreach (var eligibleProperty in eligibleProperties)
            {
                Properties.Add(eligibleProperty.Name, new EncryptedProperty(eligibleProperty));
            }

            ProxyType = generatedSample.GetType();
            Keyring   = new Keyring();
        }
示例#2
0
        public TObject GenerateSampleObject(bool useEncryption)
        {
            var options = new EncryptionProxyConfiguration((property, serializableObject) =>
            {
                var memoryStream = new MemoryStream();
                Serializer.Serialize(memoryStream, serializableObject);
                return(memoryStream.ToArray());
            }, (property, data) =>
            {
                var genericInvoke = typeof(Serializer).GetMethod("Deserialize", BindingFlags.Static | BindingFlags.Public).MakeGenericMethod(property.PropertyType);
                return(genericInvoke.Invoke(null, new object[] { new MemoryStream(data) }));
            });

            TObject instance;

            if (useEncryption)
            {
                instance = Activator.CreateInstance <TObject>().AsEncrypted(GeneratedKeyring, options);
            }
            else
            {
                instance = Activator.CreateInstance <TObject>();
            }

            instance.Populate();
            return(instance);
        }
        internal static object GenerateTrackedInstance(Type type, EncryptionProxyConfiguration configuration = null)
        {
            var trackedType     = GetTrackedType(type, configuration);
            var trackedInstance = new EncryptedInstance(trackedType, trackedType.GenerateInstance(type));

            Instances.Add(trackedInstance);
            return(trackedInstance.Reference.Target);
        }
        internal static void AttachInterceptor(object obj, EncryptionProxyConfiguration configuration = null)
        {
            var trackedType = GetTrackedType(obj.GetType());

            obj.GetType().GetField("__interceptors", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public).SetValue(obj, new IInterceptor[1] {
                new EncryptedDataStorageInterceptor()
            });
            if (GetTrackedInstance(obj) == null)
            {
                Instances.Add(new EncryptedInstance(trackedType, obj));
            }
        }
        internal static void AttachToExistingObject(object obj, EncryptionProxyConfiguration configuration = null)
        {
            var trackedType  = GetTrackedType(obj.GetType());
            var storageMixin = Activator.CreateInstance(trackedType.MixinType);

            obj.GetType().GetField("__interceptors", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public).SetValue(obj, new IInterceptor[1] {
                new EncryptedDataStorageInterceptor()
            });
            obj.GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public).FirstOrDefault(f => f.Name.StartsWith("__mixin_IEncryptedData_")).SetValue(obj, storageMixin);

            if (GetTrackedInstance(obj) == null)
            {
                Instances.Add(new EncryptedInstance(trackedType, obj));
            }
        }
        internal static EncryptedType GetTrackedType(Type type, EncryptionProxyConfiguration configuration = null)
        {
            var encryptedType = GetTrackedTypeByEncrypted(type);

            if (encryptedType != null)
            {
                return(encryptedType);
            }

            var existingType = GetTrackedTypeOrNull(type);

            if (existingType == null)
            {
                if (configuration == null)
                {
                    configuration = new EncryptionProxyConfiguration();
                }
                existingType = new EncryptedType(type, configuration);
                Types.Add(existingType);
                TypesByProxy.Add(existingType.ProxyType.FullName, existingType);
                TypesByOriginal.Add(type.FullName, existingType);
            }
            return(existingType);
        }