Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.7k views
in Technique[技术] by (71.8m points)

azure - x5t JWT Thumbprint Python Conversion

I'm trying to generate an x5t parameter for a header to make a request to Azure using a certificate to authenticate. In the example given in the docs here: https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-certificate-credentials, it's saying that the SHA-1 hash of 84E05C1D98BCE3A5421D225B140B36E86A3D5534 should give an x5t value of hOBcHZi846VCHSJbFAs26Go9VTQ=

When I try to convert this hash using the following, I find the x5t value to be ODRFMDVDMUQ5OEJDRTNBNTQyMUQyMjVCMTQwQjM2RTg2QTNENTUzNA==

What am I doing wrong in the conversion process?

import base64
x="84E05C1D98BCE3A5421D225B140B36E86A3D5534"
x5t = base64.b64encode(x.encode()).decode()
print(x)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The given SHA-1 hash

84E05C1D98BCE3A5421D225B140B36E86A3D5534

is a long hexadecimal number. In your code you treat it as a string, (e.g. "84") but you need to interpret it as hexadecimal representation of a byte array (e.g. first byte is 0x84):

import base64
x = "84E05C1D98BCE3A5421D225B140B36E86A3D5534"
x5t = base64.b64encode(bytearray.fromhex(x))
print(x5t.decode())

The result is:

hOBcHZi846VCHSJbFAs26Go9VTQ=


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...