I'm working in Python, and my goal is to hash a list of strings with a cryptographic hash function such as sha256. It would need the property that a change in any string, or the order of the strings, will affect the outcome. For example, this would not be sufficient.
['string1str', 'ing2'], as well as
['string1string2'] will all result in the same hash.
One idea I've had for this is to hash
repr(strings), but I'm concerned this may be inefficient, as unprintable characters are converted to the 4-byte form
strings = ['string1', 'string2', '\x00', '\\x00'] tohash = repr(strings) # tohash == "['string1', 'string2', '\\x00', '\\\\x00']" result = sha256(tohash).hexdigest() # 2da24fba01c065fe1f33cb937dec2c865de20ebdbd8f172d59d82cd920d3266d
This method works, but I haven't seen it used anywhere else, and it seems inefficient. Is there a standard way to hash multiple strings?