Home > Database > Mysql Tutorial > body text

Storing SHA1 Hashes in MySQL: BINARY(20) or VARCHAR(40)?

Mary-Kate Olsen
Release: 2024-11-20 20:11:18
Original
694 people have browsed it

 Storing SHA1 Hashes in MySQL: BINARY(20) or VARCHAR(40)?

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template