If you need to send electronic transactions to Swedish banks, you’ll be required to add anti-tampering seals to the files. The banks recommend you use a third-party system to create the HMAC SHA256-128 seals, but that could involve a fair amount of expensive server software and maintenance contracts (some linked to the number of people who work in your company).
Instead, you can do it yourself in Python like this:
import hmac
import hashlib
import string
NORMALISE = string.maketrans(
'\xC9\xC4\xD6\xC5\xDC\xE9\xE4\xF6\xE5\xFC' + ''.join(
[chr(x) for x in range(0,32)]) + ''.join(
[chr(x) for x in range(127,256)
if x not in (201,196,214,197,220,233,
228,246,229,252)]),
'\x40\x5B\x5C\x5D\x5E\x60\x7B\x7C\x7D\x7E' + ''.join(
[chr(195) for x in range(0,32)]) + ''.join(
[chr(195) for x in range(127,256)
if x not in (201,196,214,197,220,233,
228,246,229,252)]))
def hex_to_bytes(hexs):
"""Convert string of hex into bytes"""
return ''.join(['%s' % chr(int(hexs[i:i+2], 16))
for i in range(0, len(hexs), 2)])
def get_signature(contents, key):
"""Calculate the HMAC SHA256-128 signature
contents - an iso-8859-1 (latin-1) encoded string
key - a string of hex characters
Returns a 32 char string of hex characters (128 bits)
"""
key = hex_to_bytes(key)
#Normalise the contents
contents = contents.translate(NORMALISE, '\r\n')
dig = hmac.new(key, msg=contents,
digestmod=hashlib.sha256).digest()
return ''.join(['%02X' % ord(x) for x in dig[:16]])
And then to calculate the signature for a file:
>>> print get_signature(open('bankfile.dat').read(),
... '1234567890abcdef1234567890abcdef')
25122AE4179BD51DC87AD6EA08D16D45