Пример #1
0
        public async Task DecryptEntityAsync <TEntity>(TEntity entity, string key, string iv, Action <TEntity, string, string> decryptCallBack,
                                                       CancellationToken cancellationToken = new CancellationToken()) where TEntity : class, IEncryptedEntity
        {
            var encryptedProperties = entity.GetType().GetProperties()
                                      .Where(p => p.GetCustomAttributes(typeof(PropertyEncrypted), true).Any(a => p.PropertyType == typeof(string)));

            foreach (var property in encryptedProperties)
            {
                var encryptedValue = property.GetValue(entity) as string;
                if (string.IsNullOrEmpty(encryptedValue))
                {
                    continue;
                }
                var value = await _cryptoProvider.DecryptStringAsync(encryptedValue, entity.DecryptionPrivateKey, iv, cancellationToken).ConfigureAwait(false);

                decryptCallBack.Invoke(entity, property.Name, value);
            }
        }