I'm looking to irreversibly hash valid email addresses which have been parsed from mail log data as part of a machine learning project. I need to ensure that the processed data has been scrubbed of personally identifiable information (specific users/client domain etc).
A quick google-fu led me to pyffx and the inbuilt secrets package.
I'm looking to scrub the email addresses while retain the formatting and beginning character sequence:
#!/usr/bin/env python # coding: utf-8 import pyffx, secrets def ffx_encrypt(email,secret): raw_user, raw_domain = email.split('@') #retaining first few characters to test entropy of bulk sender lists user_chars = raw_user[:3] user_rem = raw_user[3:] #get unique characters for each string to retain entropy uniq_user_chars = ''.join(set(raw_user)) uniq_dom_chars = ''.join(set(raw_domain)) e_user = pyffx.String(secret,alphabet=uniq_user_chars,length=len(user_rem)) e_dom = pyffx.String(secret,alphabet=uniq_dom_chars,length=len(raw_domain)) user_encrypt = e_user.encrypt(user_rem) dom_encrypt = e_dom.encrypt(raw_domain) return user_chars + user_encrypt + '@' + dom_encrypt; #To be generated at runtime secret = secrets.token_hex(32).encode() print(ffx_encrypt('firstname.lastname@example.org',secret)) print(ffx_encrypt('email@example.com',secret)) print(ffx_encrypt('firstname.lastname@example.org',secret)) print(ffx_encrypt('email@example.com',secret)) print(ffx_encrypt('firstname.lastname@example.org',secret)) print(ffx_encrypt('email@example.com',secret)) ##Sample run results #teste@limigooac #firms_smnnueefrna_@tnmaenmi #firstname.lastname@example.org #userersua@limigooac #userersua@tnmaenmi #email@example.com..3
At the moment I'm not focused on performance, elegance or robustness, it's more about avoiding stepping on a landmine if there's an obvious flaw with my implementation which could make the post-processed email addresses/domains reversible.
Feedback would be greatly appreciated.
EDIT: Clarified that input will be valid email addresses.