BO4E-dotnet.Encryption/Anonymizer.cs
/// <summary>
///     Applies recursive Hashing on <paramref name="input" /> for all fields of given DataCategory
///     <paramref name="dataCategory" />
/// </summary>
/// <param name="input">object to be hashed (will be modified by reference)</param>
/// <param name="dataCategory">Category of fields to be modified</param>
Applies recursive Hashing on  for all fields of given DataCategory
Applies recursive Hashing on for all fields of given DataCategory
protected void HashObject(ref object input, DataCategory? dataCategory = null)
        {
            if (input == null) return;
            var inputType = input.GetType();
            if (inputType == typeof(string))
            {
                var inputString = (string)input;
                HashString(ref inputString, dataCategory);
                input = inputString;
            }
            else if (inputType == typeof(int))
            {
                _logger.LogWarning($"Hashing {inputType} is not supported.");
            }
            else if (inputType.ToString().StartsWith("BO4E.ENUM"))
            {
                _logger.LogWarning(
                    $"Hashing {inputType} is not supported, since the result would not be a valid ENUM value.");
            }
            else if (inputType.IsGenericType && inputType.GetGenericTypeDefinition() == typeof(List<>))
            {
                var inputList = input as IList;
                for (var i = 0; i < inputList.Count; i++)
                {
                    var listItem = inputList[i];
                    HashObject(ref listItem, dataCategory);
                    inputList[i] = listItem;
                }
            }
            else if (
                typeof(IDictionary<string, object>).IsAssignableFrom(inputType)) 
            {
                dynamic dict = input as IDictionary<string, object>;
                foreach (var dictKey in ((ICollection<string>)dict.Keys).ToList()
                    .Where(key => !_configuration.UnaffectedUserProperties.Contains(key)))
                    if (dict[dictKey] != null)
                    {
                        var inputString = (string)dict[dictKey];
                        HashString(ref inputString, dataCategory);
                        dict[dictKey] = JToken.FromObject(inputString);
                    }
                input = dict;
            }
            else
            {
                var properties = inputType.GetProperties();
                foreach (var prop in properties)
                {
                    if (prop.GetValue(input) == null || !prop.IsHashingRelevant(dataCategory)) continue;
                    try
                    {
                        var o = prop.GetValue(input);
                        HashObject(ref o, dataCategory);
                        prop.SetValue(input, o);
                    }
                    catch (Exception f)
                    {
                        throw new ArgumentException($"Couldn't hash field {prop.Name}: {f.Message}");
                    }
                }
                if (!properties.Any())
                    throw new NotImplementedException(
                        $"Type {inputType} with value '{input}' has no subfields but is not handled separately.");
            }
        }
protected void HashObject ( ref object input , DataCategory ? dataCategory = null ) { if ( input == null ) return ; var inputType = input . GetType ( ) ; if ( inputType == typeof ( string ) ) { var inputString = ( string ) input ; HashString ( ref inputString , dataCategory ) ; input = inputString ; } else if ( inputType == typeof ( int ) ) { _logger . LogWarning ( $" Hashing  { inputType }  is not supported. " ) ; } else if ( inputType . ToString ( ) . StartsWith ( " " ) ) { _logger . LogWarning ( $" Hashing  { inputType }  is not supported, since the result would not be a valid ENUM value. " ) ; } else if ( inputType . IsGenericType && inputType . GetGenericTypeDefinition ( ) == typeof ( List < > ) ) { var inputList = input as IList ; for ( var i = 0 ; i < inputList . Count ; i ++ ) { var listItem = inputList [ i ] ; HashObject ( ref listItem , dataCategory ) ; inputList [ i ] = listItem ; } } else if ( typeof ( IDictionary < string , object > ) . IsAssignableFrom ( inputType ) ) { dynamic dict = input as IDictionary < string , object > ; foreach ( var dictKey in ( ( ICollection < string > ) dict . Keys ) . ToList ( ) . Where ( key => ! _configuration . UnaffectedUserProperties . Contains ( key ) ) ) if ( dict [ dictKey ] != null ) { var inputString = ( string ) dict [ dictKey ] ; HashString ( ref inputString , dataCategory ) ; dict [ dictKey ] = JToken . FromObject ( inputString ) ; } input = dict ; } else { var properties = inputType . GetProperties ( ) ; foreach ( var prop in properties ) { if ( prop . GetValue ( input ) == null || ! prop . IsHashingRelevant ( dataCategory ) ) continue ; try { var o = prop . GetValue ( input ) ; HashObject ( ref o , dataCategory ) ; prop . SetValue ( input , o ) ; } catch ( Exception f ) { throw new ArgumentException ( $" Couldn't hash field  { prop . Name } :  { f . Message } " ) ; } } if ( ! properties . Any ( ) ) throw new NotImplementedException ( $" Type  { inputType }  with value ' { input } ' has no subfields but is not handled separately. " ) ; } }
Applies recursive Hashing on  for all fields of given DataCategory
Applies recursive Hashing on for all fields of given DataCategory
/*else if (inputType == typeof(JValue)) // entry of BusinessObject.userProperties
            {
                string inputString = ((JValue)input).Value<string>();
                HashString(ref inputString, dataCategory);
                input = JToken.FromObject(inputString);
            }*/ //affectedField.SetValue(mappedObject, Sha256HashEnum(affectedField.GetValue(mappedObject).ToString())); //throw new NotSupportedException($"Hashing {affectedField.FieldType} is not supported, since the result would not be a valid ENUM value."); // typeof BusinessObject.userProperties /*//dict[dictKey].Set(dict[dictKey].ToString());
                        HashObject(ref dict[dictKey]);
                        string newValue = dict[dictKey].ToString();
                        dict.Remove(dictKey);
                        dict.Add(dictKey, newValue);*/
object
ref
dataCategory
DataCategory?
Category of fields to be modified
Category of fields to be modified
input
object to be hashed (will be modified by reference)
object to be hashed ( will be modified by reference )