Why do SHA256 hashes end with an = character?

I created a web service that returns a security token after successful authentication.

However, when debugging, I noticed that each hash of the web service returns with "=", for example:

"tINH0JxmryvB6pRkEii1iBYP7FRedDqIEs0Ppbw83oc=" "INv7q72C1HvIixY1qmt5tNASFBEc0PnXRSb780Y5aeI=" "QkM8Kog8TtCczysDmKu6ZOjwwYlcR2biiUzxkb3uBio=" "6eNuCU6RBkwKMmVV6Mhm0Q0ehJ8Qo5SqcGm3LIl62uQ=" "dAPKN8aHl5tgKpmx9vNoYvXfAdF+76G4S+L+ep+TzU=" "O5qQNLEjmmgCIB0TOsNOPCHiquq8ALbHHLcWvWhMuI=" "N9ERYp+i7yhEblAjaKaS3qf9uvMja0odC7ERYllHCI=" "wsBTpxyNLVLbJEbMttFdSfOwv6W9rXba4GGodVVxgo=" "sr+nF83THUjYcjzRVQbnDFUQVTkuZOZYe3D3bmF1D8=" "9EosvgyYOG5a136S54HVmmebwiBJJ8a3qGVWD878j5k=" "8ORZmAXZ4dlWeaMOsyxAFphwKh9SeimwBzf8eYqTis=" "gVepn2Up5rjVplJUvDHtgIeaBL+X6TPzm2j9O2JTDFI=" 

Why is this behavior?

+5
source share
2 answers

This is because you do not see raw hash bytes, but rather Base64 encoding.

Base64 encoding converts a block of 3 bytes into a block of four characters. This works well if the number of bytes is divided by 3. If it is not, then you use a pad character, so the number of resulting characters is still divided by 4.

So:

 (no of bytes)%3 = 0 => no padding needed (no of bytes)%3 = 1 => pad with == (no of bytes)%3 = 2 => pad with = 

The SHA256 hash is 256 bits, it is 32 bytes. So you get 40 characters for the first 30 bytes, 3 characters for the last 2 bytes, and padding will always be one = .

+11
source

These strings are encoded using base64 , = characters are used as paddings so that the last block of the base64 string contains four characters.


To get a base64-decoded string, you can use the following Ruby code:

 require 'base64' s = "tINH0JxmryvB6pRkEii1iBYP7FRedDqIEs0Ppbw83oc=" puts Base64.decode64(s).bytes.map{|e| '%02x' % e}.join 

Output: b48347d09c66af2bc1ea94641228b588160fec545e743a8812cd0fa5bc3cde87

+4
source

All Articles