3

I use this function for hashing my passwords:

// RETURNS: rAyZOnlNBxO2WA53z2rAtFlhdS+M7kec9hskSCpeL6j+WwcuUvfFbpFJUtHvv7ji   
base64_encode(hash_hmac('sha384', $str . SC_NONCE, SC_SITEKEY, true));

And I store hashes in char(64) field (MySQL -InnoDB).

Should I use varchar(64) instead of char(64)? Why?

Edit: I changed sha256 with sha384. Because in this example, sha256 always returns 44 bytes for me. Sorry for confusing. Now it's 64-bytes.

beytarovski
  • 2,649
  • 5
  • 31
  • 41

3 Answers3

5

varchars save storage by only using up to the length required. If the 64 bit hash is always 64 then it makes no difference in terms of storage so probably char is just as good as varchar in this case.

If you have variable length data to store, then a varchar will save wasting unnecessary space.

Trevor North
  • 2,236
  • 1
  • 12
  • 19
  • 2
    Well, you actually use 1 byte overhead with varchar compared to char, how much difference that makes is another story :) – Marcus Oct 24 '11 at 11:17
5

You should use CHAR(64) since your hash is fixed in length. Using VARCHAR will add another byte, wasting space.

Question Overflow
  • 9,707
  • 18
  • 68
  • 108
2

Even though you are using a Base 64 encoded string, the result is not necessarily 64 bits in length. In this case, VARCHAR is better because the result can be shorter than 64 bits.

In fact as seen here, 64 bits is the maximum length rather than the set length.

Brendan Bullen
  • 10,827
  • 1
  • 28
  • 39