在MySQL中存储SHA1哈希值

编程入门 行业动态 更新时间:2024-10-28 14:28:36
本文介绍了在MySQL中存储SHA1哈希值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个简单的问题,当我想把一个SHA1哈希的结果存储在一个MySQL数据库中时:

I have a simple question which occured when I wanted to store the result of a SHA1 hash in a MySQL database:

/ em>字段在其中存储哈希的结果?

How long should the VARCHAR field be in which I store the hash's result?

推荐答案

我会使用 VARCHAR 用于可变长度数据,但不具有固定长度数据。因为SHA-1值总是 160位,所以 VARCHAR 只会浪费固定长度字段长度的附加字节。

I would use VARCHAR for variable length data, but not with fixed length data. Because a SHA-1 value is always 160 bit long, the VARCHAR would just waste an additional byte for the length of the fixed-length field.

我也不会将 SHA1 正在返回。因为它每个字符只使用4位,因此需要160/4 = 40个字符。但是如果你每个字符使用8位,你只需要一个160/8 = 20个字符的长字段。

And I also wouldn’t store the value the SHA1 is 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.

所以我建议你使用 BINARY(20) 和 UNHEX 函数将 SHA1 值转换为二进制。

So I recommend you to use BINARY(20) and the UNHEX function to convert the SHA1 value to binary.

我比较了 BINARY(20)和 CHAR(40)。

CREATE TABLE `binary` ( `id` int unsigned auto_increment primary key, `password` binary(20) not null ); CREATE TABLE `char` ( `id` int unsigned auto_increment primary key, `password` char(40) not null );

拥有百万条记录 binary(20)需要44.56M,而 char(40)需要64.57M。 InnoDB 引擎。

With million of records binary(20) takes 44.56M, while char(40) takes 64.57M. InnoDB engine.

更多推荐

在MySQL中存储SHA1哈希值

本文发布于:2023-11-29 17:50:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1647147.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:MySQL   哈希值

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!