The Question :
256 people think this question is useful
I’m going to run
SHA256 on a password + salt, but I don’t know how long to make my
VARCHAR when setting up the MySQL database. What is a good length?
The Question Comments :
The Answer 1
354 people think this answer is useful
A sha256 is 256 bits long — as its name indicates.
Since sha256 returns a hexadecimal representation, 4 bits are enough to encode each character (instead of 8, like for ASCII), so 256 bits would represent 64 hex characters, therefore you need a
varchar(64), or even a
char(64), as the length is always the same, not varying at all.
And the demo :
$hash = hash('sha256', 'hello, world!');
Will give you :
$ php temp.php
i.e. a string with 64 characters.
The Answer 2
77 people think this answer is useful
Encoding options for SHA256’s 256 bits:
- Base64: 6 bits per char =
CHAR(44) including padding character
- Hex: 4 bits per char =
- Binary: 8 bits per byte =
The Answer 3
30 people think this answer is useful
I prefer to use BINARY(32) since it’s the optimized way!
You can place in that 32 hex digits from (00 to FF).
The Answer 4
22 people think this answer is useful
Why would you make it VARCHAR? It doesn’t vary. It’s always 64 characters, which can be determined by running anything into one of the online SHA-256 calculators.
The Answer 5
7 people think this answer is useful
It will be fixed 64 chars, so use