Storing SHA1 Hash Values in MySQL: How Long Should the VARCHAR Field Be?
When storing the result of a SHA1 hash in a MySQL database, it is important to determine the appropriate length of the VARCHAR field. A SHA1 value is 160 bits long, so it should be stored in a field that can accommodate this length.
The article argues against using a VARCHAR field for storing SHA1 hash values because it wastes an additional byte for the length of the fixed-length field. Instead, it recommends using a BINARY(20) field, which is 20 characters long and can store the 160-bit SHA1 value without wasting space.
In addition, the article suggests using the UNHEX function to convert the SHA1 value to binary before storing it in the BINARY field. This reduces the storage requirements compared to storing the raw SHA1 value.
The article concludes by comparing the storage requirements for BINARY(20) and CHAR(40) fields. For a million records, BINARY(20) requires less space than CHAR(40) when using the InnoDB engine.
The above is the detailed content of Storing SHA1 Hashes in MySQL: BINARY(20) or VARCHAR(40)?. For more information, please follow other related articles on the PHP Chinese website!