Hashing a csv file in python with a key

2019-07-27 04:30发布

问题:

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?

回答1:

I don't think you need to trouble yourself with the dictionary; you don't have a variable number of columns here, you are applying your transformation to just one column.

It'll be easier to follow if you just put your working HMAC method into a function:

import hmac
import hashlib
import base64

secret = "123Abc"
secret_bytes = bytes(secret, 'latin-1')

def create_signature(email, secret_bytes):
    data_bytes = email.encode('latin-1')
    digest = hmac.new(secret_bytes, msg=data_bytes, digestmod=hashlib.sha256).digest()
    signature = base64.b64encode(digest).decode()
    return signature

This now neatly produces a Base64 string with a HMAC digest from an email address and your (encoded) secret:

>>> create_signature('DHSKA@gmail.com', secret_bytes)
'3KaSw4QeA5l0rz49uutaDGemn4Et4CQnbnngm6mmpjE='

Now you can apply that to the 'Mail' column values, and write out the new CSV with the results:

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:
        row['Mail'] = create_signature(row['Mail'], secret_bytes)
        writer.writerow(row)

Demo:

>>> import sys, csv, io
>>> demo_input = io.StringIO('''\
... Mail
... DHSKA@gmail.com
... DJÖANw12@gmail.com
... JSNÖS83@ymail.com
... HDKDLSA@gmail.com
... KKKDLAmS19@yamil.com
... ''')
>>> demo_output = io.StringIO()
>>> with demo_input as in_file:
...     reader = csv.DictReader(in_file)
...     writer = csv.DictWriter(demo_output, reader.fieldnames)
...     writer.writeheader()
...     for row in reader:
...         row['Mail'] = create_signature(row['Mail'], secret_bytes)
...         writer.writerow(row)
...
46
46
46
46
46
>>> print(demo_output.getvalue())
Mail
3KaSw4QeA5l0rz49uutaDGemn4Et4CQnbnngm6mmpjE=
dP9IU66yKnYP/6mFRZ6TAAAN3lmxAcUPk9o1iFfpGDs=
ajNdCZF8ndw2SrgtSzcVCbeSpFsXI/Z6Ep0IC2fj+WU=
TgeFEj8CgvcQbVcLHTIIY1ULLnYkWAZaia5k01IQiJY=
Xu94abwV/5/HUXY+T3NpUgulGvew+L0UYzkPuRSv/98=