我正在尝试模拟C#应用程序将UUID转换为Base64值的方式。出于某种原因,我可以让字符串的一部分与预期值匹配,但不是整个字符串。
我收到的C#代码:
public static string ToShortGuid(this Guid newGuid) {
string modifiedBase64 = Convert.ToBase64String(newGuid.ToByteArray())
.Replace('+', '-').Replace('/', '_') // avoid invalid URL characters
.Substring(0, 22);
return modifiedBase64;
}
我在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"是一个已知的UUID,应用程序显然使用以上C#代码来生成一个'ShortGuid'值为"NQUZ-gBrUkSKsTGccwgrYA"。
当我通过我的Python代码处理相同的UUID时,我得到的结果是:"-hkFNWsARFKKsTGccwgrYA=="
从这两个输出字符串中,这部分匹配:"KsTGccwgrYA",但其余部分不匹配。
"
urlsafe_b64decode
解码NQUZ-gBrUkSKsTGccwgrYA==
得到b'5\x05\x19\xfa\x00kRD\x8a\xb11\x9cs\x08+\
',但是
UUID("fa190535-6b00-4452-8ab1-319c73082b60").bytes是
b'\xfa\x19\x055k\x00DR\x8a\xb11\x9cs\x08+`'`,两者不同。你确定C#和Python中的UUID是相同的吗? - Aran-Fey