Created
February 12, 2022 17:32
-
-
Save adamjasinski/8632cbd10a21ea20aeadf0cf16e6080f to your computer and use it in GitHub Desktop.
Encodes a UUID command-line parameter to Base32, using .NET Guid byte order
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
import uuid | |
import base64 | |
import sys | |
def base32encode(guid): | |
# NB - bytes_le order matches .NET Guid.ToByteArray() order | |
# cf. https://docs.microsoft.com/en-us/dotnet/api/system.guid.tobytearray?view=net-6.0#system-guid-tobytearray | |
bytes = guid.bytes_le | |
encoded_bytes = base64.b32encode(bytes) | |
# Strip out padding and convert to lowercase string | |
return encoded_bytes[0:26].decode().lower() | |
if len(sys.argv) < 2: | |
print(f"Usage: {sys.argv[0]} uuid") | |
exit(1) | |
guid = uuid.UUID(hex=sys.argv[1]) | |
result = base32encode(guid) | |
print(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment