I am attempting to emulate the way a C# application converts a UUID to a Base64 value. For some reason, I can get part of the string to match the expected value, but not the whole string.
The C# code I was given:
public static string ToShortGuid(this Guid newGuid) {
string modifiedBase64 = Convert.ToBase64String(newGuid.ToByteArray())
.Replace('+', '-').Replace('/', '_') // avoid invalid URL characters
.Substring(0, 22);
return modifiedBase64;
}
What I have tried in Python 3.6:
import uuid
import base64
encode_str = = base64.urlsafe_b64encode(uuid.UUID("fa190535-6b00-4452-8ab1-319c73082b60").bytes)
print(encode_str)
"fa190535-6b00-4452-8ab1-319c73082b60" is a known UUID, and the application apparently uses the above c# code to generate a 'ShortGuid' value of "NQUZ-gBrUkSKsTGccwgrYA".
When I process the same UUID through my Python code, I get: "-hkFNWsARFKKsTGccwgrYA=="
From both of these output strings, this part matches: "KsTGccwgrYA", but the rest doesn't.
You need to use bytes_le
to get the endianness to match Microsoft's:
base64.urlsafe_b64encode(uuid.UUID("fa190535-6b00-4452-8ab1-319c73082b60").bytes_le)
That gives b'NQUZ-gBrUkSKsTGccwgrYA=='
.