Contents
Is SHA256 Base64 encoded?
It is a hex-encoded string representing the hash value. You need to un-encode it to the byte-sequence that is in fact the hash before base64 encoding. Currently you’re just base64 encoding the string.
What is Base64 hashing?
In programming, Base64 is a group of binary-to-text encoding schemes that represent binary data (more specifically, a sequence of 8-bit bytes) in an ASCII string format by translating the data into a radix-64 representation. The term Base64 originates from a specific MIME content transfer encoding.
How can you tell Base64 encoding?
Encoded data will always have the following characteristic:
- The length of a Base64-encoded string is always a multiple of 4.
- Only these characters are used by the encryption: “A” to “Z”, “a” to “z”, “0” to “9”, “+” and “/”
How to create Base64 hashes using HMAC SHA256?
PHP HMAC SHA256 PHP has built in methods for hash_hmac(PHP 5) and base64_encode(PHP 4, PHP 5) resulting in no outside dependencies. Say what you want about PHP but they have the cleanest code for this example. $s = hash_hmac(‘sha256’, ‘Message’, ‘secret’, true);echo base64_encode($s); Java HMAC SHA256
When to use Base64 to encode a hex string?
You’ve converted that into a hex string, then base64-encoded that hex string. Instead, you should be base64-encoding the original binary data, without converting it to hex first. If you have to go via the hex first, you should parse that back to the original bytes, then base64 encode the result:
How to test the length of a Base64 hash?
See Digest::SHA documentation. By convention, the Digest modules do not pad their Base64 output. To fix this you can test the length of the hash and append equal signs “=” until it is the length is a multiple of 4. We will use a modulus function below.
How to convert ASCII to Base64 for MYM?
You are doing a conversion from hex encoding (or base16 if you like) to base64. So you are base64 encoding the ASCII characters 9 (57), B (66) and 2 (50) giving you OUIy. What you should be doing is base64 encoding the raw bytes. So you should encode 0x9B (155) and 0x23 (35) giving you myM.