G.D
G.D

Reputation: 315

How is SHA-256 hash size calculated?

SHA-256 gives us a 256-bit hash value for a given input. When I tried, I got the below hash for a random input string:

2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824

This hash has 64 characters. Considering common encodings, its size would be:

UTF-8 = 64 * 8 = 512 bits

UTF-16 = 64 * 16 = 1024 bits

So how come it's called a 256-bit hash? Am I doing an incorrect conversion?

Upvotes: 3

Views: 3983

Answers (2)

user7605325
user7605325

Reputation:

The string 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824 is a hex-encoded String. It's the bytes represented in base 16 numbers, so every 2 characters represent a single byte.

So 2c is the first byte, f2 is the second byte and so on. As the string has 64 characters, it represents 32 bytes.

And 32 bytes = 256 bits

Upvotes: 2

Tatsuyuki Ishi
Tatsuyuki Ishi

Reputation: 4031

This is base16/hex representation, which only has 4 (log2 16) bits of data per byte.

Thus, sha256 is 4*64 here. Nothing is wrong.

Upvotes: 3

Related Questions