/// <summary>
        /// Ctor.
        /// </summary>
        /// <param name="storage"></param>
        /// <param name="serializer"></param>
        /// <param name="forceEncrypt">If true, checks if existed entries are encrypted, and encrypt them if not.</param>
        public EncryptedTableStorageDecorator(
            [NotNull] INoSQLTableStorage <T> storage,
            [NotNull] ICryptographicSerializer serializer,
            bool forceEncrypt = true)
        {
            _storage      = storage ?? throw new ArgumentNullException(nameof(storage));
            _serializer   = serializer ?? throw new ArgumentNullException(nameof(serializer));
            _forceEncrypt = forceEncrypt;

            var encryptAttribute = typeof(EncryptAttribute);

            _encryptedProperties = typeof(T).GetProperties().Where(x => Attribute.IsDefined(x, encryptAttribute)).ToList();
            if (_encryptedProperties.Count == 0)
            {
                throw new ArgumentException("No properties marked as encrypted.");
            }
            if (_encryptedProperties.Any(x => x.PropertyType != typeof(string)))
            {
                throw new ArgumentException($"Only {typeof(string).FullName} type properties can be marked as encrypted.");
            }
            if (_encryptedProperties.Any(x => x.SetMethod == null || x.GetMethod == null))
            {
                throw new ArgumentException("Only properties with both Get and Set method allowed. Please check this: " + string.Join(", ",
                                                                                                                                      _encryptedProperties.Where(x => x.SetMethod == null || x.GetMethod == null).Select(x => x.Name)));
            }
        }
Exemple #2
0
        public bool TrySetKey(string key, out string error)
        {
            error = null;

            if (HasKey)
            {
                error = "Key is already installed.";
                return(false);
            }

            ICryptographicSerializer serializer;

            try
            {
                serializer = new AesSerializer(key);
            }
            catch (Exception ex)
            {
                error = $"Wrong key format. {ex.Message}";
                return(false);
            }
            var encryptedStorage = EncryptedTableStorageDecorator <EncryptionInitModel> .Create(_storage, serializer);

            if (WasEncryptionSet())
            {
                try
                {
                    var existingValue = encryptedStorage.GetDataAsync(InitKey, InitKey).GetAwaiter().GetResult();
                    if (existingValue.Data == InitKey)
                    {
                        Serializer = serializer;
                        return(true);
                    }
                    else
                    {
                        error = "The specified key is incorrect.";
                        return(false);
                    }
                }
                catch (System.Security.Cryptography.CryptographicException)
                {
                    error = "The specified key is incorrect.";
                    return(false);
                }
            }
            else
            {
                // this is a new and the only one key
                encryptedStorage.InsertAsync(new EncryptionInitModel {
                    PartitionKey = InitKey, RowKey = InitKey, Data = InitKey
                }).GetAwaiter().GetResult();
                Serializer = serializer;
                return(true);
            }
        }
 public static INoSQLTableStorage <T> Create(INoSQLTableStorage <T> storage, ICryptographicSerializer algo)
 {
     return(new EncryptedTableStorageDecorator <T>(storage, algo));
 }