I have a simple question which occured when I wanted to store the result of a SHA1 hash in a MySQL database:
How long should the VARCHAR field be in which I store the hash’s result?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I would use
VARCHARfor variable length data, but not with fixed length data. Because a SHA-1 value is always 160 bit long, theVARCHARwould just waste an additional byte for the length of the fixed-length field.And I also wouldn’t store the value the
SHA1is returning. Because it uses just 4 bit per character and thus would need 160/4 = 40 characters. But if you use 8 bit per character, you would only need a 160/8 = 20 character long field.So I recommend you to use
BINARY(20)and theUNHEXfunction to convert theSHA1value to binary.I compared storage requirements for
BINARY(20)andCHAR(40).With million of records
binary(20)takes 44.56M, whilechar(40)takes 64.57M.InnoDBengine.