main_19.py 601 B

12345678910111213141516
  1. def apply(config, val):
  2. """ Pseudonymize using format preserving encryption.
  3. Example config:
  4. {
  5. 'func': 'fpe',
  6. 'key': 'some-secret-key',
  7. 'alphabet': string.ascii_letters
  8. }
  9. """
  10. validate_func_params(config, MANDATORY_CONFIG_PARAMS)
  11. try:
  12. alphabet = config.get('alphabet', string.printable)
  13. e = pyffx.String(config['key'].encode("utf-8"), alphabet, length=len(val))
  14. return e.encrypt(val)
  15. except ValueError:
  16. raise PseudoFuncError("Could not pseudonymize '{0}'. Check alphabet compatibility ({1})".format(val, alphabet))