Why does my Base64 encoded hash only contain 28 characters?

Viewed 7306

The inverse question exists with no answer and a comment that I don't understand.

I am attempting to create a Base64 encoded HMAC-SHA1 signature for a pseudo OAuth authentication header for an API. I found a support document (requires authenticated access) that takes you through the evolution of creating the signature. I'm able to create the same data up until the final step which is to Base64 encode the hash.

The support document states that the HMAC-SHA1 signature is:

cb5acd2d3ef689a8fbec4d06c576371834689673

And I get:

CB5ACD2D3EF689A8FBEC4D06C576371834689673

The support document then states

From the hex result string in step 3, encode the value using Base64

and provides the following Base64 encoded result (58 characters):

Y2I1YWNkMmQzZWY2ODlhOGZiZWM0ZDA2YzU3NjM3MTgzNDY4OTY3Mw==

When I use Convert.ToBase64String() to convert my signature I get (28 characters):

y1rNLT72iaj77E0GxXY3GDRolnM=

I'm stumped, I don't know if the support document is incorrect or if I'm doing something wrong. The fact that I'm generating a string that is 28 characters and the example is 56 is too interesting to ignore.

The comment in the aforementioned semi-duplicate question also stumps me. I don't see how the string "MDY" translates to any ascii or unicode digits that make sense to me - I don't understand how the comment author came to that conclusion.

The hex value is being encoded as text ("062..." == 0x30, 0x36, 0x32, ...) rather than as the large number it represents.

3 Answers

The answer is correct, here is a small python example

import base64
import hashlib

# replace "hello" with your value from which you get sha1    
sha = hashlib.sha1(b'hello') 
sha_bytes = sha.digest()  # 20 bytes
sha_str = sha.hexdigest()  # 40 bytes - you are trying to b64encode this value

b64_sha_bytes = base64.b64encode(sha_bytes)  # b'qvTGHdzF6KLavt4PO0gs2a6pQ00='
b64_sha_str = base64.b64encode(sha_str.encode())  # b'YWFmNGM2MWRkY2M1ZThhMmRhYmVkZTBmM2I0ODJjZDlhZWE5NDM0ZA=='
Related