I need to generate the ODIN-1 of a string in Python. The official documentation specifies applying SHA-1 to the input string/identifier, but I'm not sure if I need to perform other operations to it beforehand? Also, is the final output the hex digest of the SHA-1 or something else?
E.g. How can I convert this MAC to ODIN-1 in Python? "74e2f543d2ce"
Thanks in advance!
from hashlib import sha1
def odin1(mac_addr):
"""SHA1 hexdigest of hex representaiton of MAC address"""
to_hash ''.join([i.decode('hex') for i in mac_addr.split(':')])
return sha1(to_hash).hexdigest()
>>> odin1('1a:2b:3c:4d:5e:6f')
'82a53f1222f8781a5063a773231d4a7ee41bdd6f'
Let's break this down, line by line between the documentation you linked to, and my answer:
// NOTE: iOS returns MAC Address NOT a string, but a 6-byte array.
// A human readable MAC Address may be represented as the following:
@"1a:2b:3c:4d:5e:6f";
@"1A2B3C4D5E6F";
In python:
>>> '1A'.decode('hex') == '1a'.decode('hex')
True
So we can convert the string given to us into a more agreeable format (that reduces "any ambiguity around punctuation and capitalization"):
>>> mac = "1a:2b:3c:4d:5e:6f".split(':')
>>> hex_mac = [m.decode('hex') for m in mac]
>>> hex_mac
['\x1a', '+', '<', 'M', '^', 'o']
We can treat this list as a string (just the same as if we used a byte array) to get the same result from the SHA1 hash function.
Of course, we can receive MAC addresses this way:
>>> mac = '1A2B3C4D5E6F'
>>> hex_chunks = lambda s: [s[i: i+2] for i in range(0, len(s), 2)]
>>> [m.decode('hex') for m in hex_chunks(mac)]
['\x1a', '+', '<', 'M', '^', 'o']
So it would be up to us to properly unify the input for the single function to operate across all possible forms. Regardless, our function could take either form, the end result is what matters:
>>> sha1(''.join(['\x1a', '+', '<', 'M', '^', 'o'])).hexdigest()
Will produce the correct hash (according to the link you posted).
Hope this helps make my answer clearer.