user3547774
user3547774

Reputation: 1689

SHA512Managed hashSize

the official documentation of MSDN says

The hash size for the SHA512Managed algorithm is 512 bits.

When I run the following code

        byte[] dataToEncode = Encoding.UTF8.GetBytes("Hello Worlds");
        SHA512Managed sha = new SHA512Managed(); 
        byte[] hashedData = sha.ComputeHash(dataToEncode);
        string hashedDataString = Convert.ToBase64String(hashedData);

my HasheDataString is only 64 long (hashedDataString.Length) and hasheData is only 64 long (hashedData.Length). What exactly is this 512?

Upvotes: 2

Views: 1162

Answers (3)

Orace
Orace

Reputation: 8359

user3547774 question has a typo, HasheDataString is 88 characters long.

Guffa has the good response.

About c# string : Each character in a string is defined by a Unicode scalar value ... encoded by using UTF-16

So Base64 encoding only use 6 bits over 16 per character to encode the data.

Upvotes: 0

Guffa
Guffa

Reputation: 700382

Each byte has eight bits, so 64 bytes is 64 * 8 = 512 bits.

The base64 encoded string of the 64 bytes is 88 characters long. The base64 encoding stores six bit in each character, so 512 bits needs 512 / 6 ~ 85.3 characters, plus two extra characters to get to an even four-character boundary.

Upvotes: 3

Jerodev
Jerodev

Reputation: 33196

512bits means the size in binary, ones and zeroes. When you convert this to a string, all ones and zeroes become characters, each character consists of 8bits. So then you have 512bits divided 8bits per character makes 64 characters in a string.

Upvotes: 3

Related Questions