Skip to content

Instantly share code, notes, and snippets.

@nwithan8
Last active June 11, 2020 20:15
Show Gist options
  • Select an option

  • Save nwithan8/3900f19d198697535cdc28a8d59fe958 to your computer and use it in GitHub Desktop.

Select an option

Save nwithan8/3900f19d198697535cdc28a8d59fe958 to your computer and use it in GitHub Desktop.
Chained-forward UUID5 (SHA-1) hashing in Python. The next hash is based on the previous hash and the current epoch, making it nearly impossible to edit.
#!/usr/bin/python3
import uuid
import time
def makeUUID(text, start=False):
if start:
return uuid.uuid4() # random uuid to begin with
timestamp = time.time()
return uuid.uuid5(uuid.NAMESPACE_URL, f"{text}{timestamp}") # each new uuid is based on the one before it, plus a now timestamp
"""
# base key -> key 1 -> key 2
# base key is random uuid4
# key 1 is uuid5 based on base key and epoch
# key 2 is uuid5 based on key 1 and epoch
# key 3 is uuid5 based on key 2 and epoch
# etc.
# Would have to reverse SHA-1 encryption on key 3 to get {key2}{epoch},
# then reverse SHA-1 encryption on key 2 to get {key1}{epoch}
# etc.
# Changing key1 (plus change in epoch) would change key 2, which (plus change in epoch),
# would change key 3, etc. Can't change an older key without a domino effect to the future keys.
"""
all_keys = []
new_key = None
old_key = makeUUID(text=None, start=True)
for i in range(0, 10):
new_key = makeUUID(text=f"{old_key}")
print(new_key)
all_keys.append(new_key)
old_key = new_key
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment