I have a csv file with 1000+ emailadreses which I want to hash using a SHA256 HMAC and a shared key, encoded to Base64.
There was a similiar problem here, but I can't adapt the solution to work for me. I am new to python and I don't know where to change the code in order to make use of the shared key.
This is the slightly adapted code from the answer:
import csv
import hashlib
import hmac
import base64
IN_PATH = 'test.csv'
OUT_PATH = 'test_hashed.csv'
ENCODING = 'utf8'
HASH_COLUMNS = dict(Mail='md5')
def main():
with open(IN_PATH, 'rt', encoding=ENCODING, newline='') as in_file, \
open(OUT_PATH, 'wt', encoding=ENCODING, newline='') as out_file:
reader = csv.DictReader(in_file)
writer = csv.DictWriter(out_file, reader.fieldnames)
writer.writeheader()
for row in reader:
for column, method in HASH_COLUMNS.items():
data = row[column].encode(ENCODING)
digest = hashlib.new(method, data).hexdigest()
row[column] = '0x' + digest.upper()
writer.writerow(row)
if __name__ == '__main__':
main()
The input file (.csv) looks like this:
Mail
DHSKA@gmail.com
DJÖANw12@gmail.com
JSNÖS83@ymail.com
HDKDLSA@gmail.com
KKKDLAmS19@yamil.com
And with the code above, the output file looks like this:
0xB6A77B6EB853CC4CC8342B312293FA9C
0xEB439592D8EEC2A38A597350EF80E512
0x833EB6AEC1D03D7D8C94606E0D749B80
0x8007D8D1702E8A749EBD6033A52A7897
0x415E067487C4A5FBDB86AB0F855DB114
But since I do want to use a HMAC with secret key and sha256, the above solution doesn't work for me and I don't know how to incorporate this approach.
The key would be something like this:
123Abc
I was trying to do something like this, but for the whole file:
import hmac
import hashlib
import base64
secret = "123Abc"
secret_bytes = bytes(secret, 'latin-1')
data = "DHSKA@gmail.com"
data_bytes = bytes(data, 'latin-1')
digest = hmac.new(secret_bytes, msg=data_bytes, digestmod=hashlib.sha256).digest()
signature = base64.b64encode(digest).decode()
Thus, my question is how I can incorporate the HMAC SHA 256 hashing wile using the a secret key, in the above code? I just can't figure out which parameters to change?