MySQL SHA256 哈希值有多长?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2240973/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 15:15:54  来源:igfitidea点击:

How long is the SHA256 hash?

mysqlsha256

提问by Tony Stark

I'm going to run SHA256on a password + salt, but I don't know how long to make my VARCHARwhen setting up the MySQL database. What is a good length?

我将SHA256使用密码 + salt运行,但我不知道VARCHAR在设置 MySQL 数据库时需要多长时间。什么是好的长度?

回答by Pascal MARTIN

A sha256 is 256 bits long -- as its name indicates.

sha256 是 256 位长——正如它的名字所示。

Since sha256 returns a hexadecimal representation, 4 bits are enough to encode each character (instead of 8, like for ASCII), so 256 bits would represent 64 hex characters, therefore you need a varchar(64), or even a char(64), as the length is always the same, not varying at all.

由于 sha256 返回十六进制表示,4 位足以编码每个字符(而不是 8 位,如 ASCII),因此 256 位将表示 64 个十六进制字符,因此您需要 a varchar(64),甚至 a char(64),因为长度始终相同,完全没有变化。

And the demo :

和演示:

$hash = hash('sha256', 'hello, world!');
var_dump($hash);

Will give you :

会给你 :

$ php temp.php
string(64) "68e656b251e67e8358bef8483ab0d51c6619f3e7a1a9f0e75838d41ff368f728"

i.e. a string with 64 characters.

即具有 64 个字符的字符串。

回答by RickNZ

Encoding options for SHA256's 256 bits:

SHA256 256 位的编码选项:

  1. Base64: 6 bits per char = CHAR(44)including padding character
  2. Hex: 4 bits per char = CHAR(64)
  3. Binary: 8 bits per byte = BINARY(32)
  1. Base64:每个字符 6 位 =CHAR(44)包括填充字符
  2. 十六进制:每个字符 4 位 = CHAR(64)
  3. 二进制:每字节 8 位 = BINARY(32)

回答by marctrem

I prefer to use BINARY(32) since it's the optimized way!

我更喜欢使用 BINARY(32) 因为它是优化的方式!

You can place in that 32 hex digits from (00 to FF).

您可以放置​​从(00 到 FF)的 32 个十六进制数字。

Therefore BINARY(32)!

因此二进制(32)!

回答by ceejayoz

Why would you make it VARCHAR? It doesn't vary. It's always 64 characters, which can be determined by running anything into one of the online SHA-256 calculators.

你为什么要把它变成 VARCHAR?它没有变化。它始终为 64 个字符,可以通过在其中一个在线 SHA-256 计算器中运行任何内容来确定。

回答by Noushad

It will be fixed 64 chars, so use char(64)

它将固定为 64 个字符,因此请使用 char(64)