I have a problem, maybe a silly question, I want to store data in a database after I hash with the SHA1 algorithm. However, at a future time, the si开发者_如何学Cze in database will increase because size words in SHA1 is big.
Can we decrease the size of SHA1 algorithm, maybe half the size. I'm sorry for my silly question, and for my bad English. Thanks. :D
I am using JAVA.
Is 20 bytes per hash(assuming binary storage) really too much? If you currently use hex encoding switching to binary saves you 20 bytes per hash. Base64 saves about 10 bytes compared to hex.
If you simply truncate a cryptographic hash it is still a good cryptographic hash, but with a reduced output size. What output size you need depends on your application.
Integrity checks against random changes can use a much shorter hash of 32-64 bits and don't need a cryptographic hash functions.
If you need uniqueness you should have >>2*log_2(entries)
bits in your hash (See birthday paradox). At around 120 bits it's similar to a GUID/UUID (There is a sha1 based generation mode for GUIDs)
If you want cryptographic strength I'd avoid going below 128bits.
No; a SHA-1 hash has a size of 160 bits by definition. I strongly doubt that the size of the hash will be a problem; I suppose that you have other data in your database as well? Most likely, you will find that other parts of the data contribute even more to the database size. And how many rows to you expect to have with these hashes?
However, there is a size difference between storing the hash as a string (this will take at least 40 bytes, depending on the string encoding) and storing it as binary data (this will take 20 bytes).
You can switch to another algorithm, as others have noted, but that might not be a good choice from a security perspective - the shorter the output length of a hash algorithm is, the weaker it is.
If you reduce it it is no more SHA1 :). You have to think of a different algorithm
To store SHA1 hash in MySQL database, we need a CHAR(40)
.
SIZE REDUCTION
But, we can reduce size by 27% by choosing BASE64 encoding. The column type will be CHAR(29)
.
Example :
SHA1 -> digest Hex -> 40 chars :
5d41402abc4b2a76b9719d911017c575
SHA1 -> digest base64 -> 29 chars :
XUFAKrxLKna5cZ2REBfFdQ==
PERFORMANCE INCREASE
To guarantee more performance when reading (especially with PRIMARY, INDEX, UNIQUE, ... or using a JOIN) a BINARY(20) is more appropriate.
It's necessary to have a hash in Hex form (a-z/0-9) and apply the UNHEX() function of MySQL during the insertion.
INSERT INTO my_table (
id,
my_hash
) VALUES (
1,
UNHEX('5d41402abc4b2a76b9719d911017c575')
);
It could also be written with short X'...'
syntax like this :
INSERT INTO my_table (
id,
my_hash
) VALUES (
1,
X'5d41402abc4b2a76b9719d911017c575'
);
精彩评论