12.14 Encryption and Compression Functions加密和压缩功能

Table 12.18 Encryption Functions加密功能

Name名称Description描述
AES_DECRYPT()Decrypt using AES使用AES解密
AES_ENCRYPT()Encrypt using AES使用AES加密
COMPRESS()Return result as a binary string以二进制字符串形式返回结果
MD5()Calculate MD5 checksum计算MD5校验和
RANDOM_BYTES()Return a random byte vector返回一个随机字节向量
SHA1(), SHA()Calculate an SHA-1 160-bit checksum计算SHA-1 160位校验和
SHA2()Calculate an SHA-2 checksum计算SHA-2校验和
STATEMENT_DIGEST()Compute statement digest hash value计算语句摘要哈希值
STATEMENT_DIGEST_TEXT()Compute normalized statement digest计算规范化语句摘要
UNCOMPRESS()Uncompress a string compressed解压压缩的字符串
UNCOMPRESSED_LENGTH()Return the length of a string before compression返回压缩前字符串的长度
VALIDATE_PASSWORD_STRENGTH()Determine strength of password确定密码强度

Many encryption and compression functions return strings for which the result might contain arbitrary byte values. 许多加密和压缩函数返回字符串,其结果可能包含任意字节值。If you want to store these results, use a column with a VARBINARY or BLOB binary string data type. 如果要存储这些结果,请使用VARBINARYBLOB二进制字符串数据类型的列。This avoids potential problems with trailing space removal or character set conversion that would change data values, such as may occur if you use a nonbinary string data type (CHAR, VARCHAR, TEXT).这避免了尾部空格删除或字符集转换可能会更改数据值的潜在问题,例如使用非二进制字符串数据类型(CHARVARCHARTEXT)时可能出现的问题。

Some encryption functions return strings of ASCII characters: MD5(), SHA(), SHA1(), SHA2(), STATEMENT_DIGEST(), STATEMENT_DIGEST_TEXT(). 一些加密函数返回ASCII字符字符串:MD5()SHA()SHA1()SHA2()STATEMENT_DIGEST()STATEMENT_DIGEST_TEXT()Their return value is a string that has a character set and collation determined by the character_set_connection and collation_connection system variables. 它们的返回值是一个字符串,该字符串具有由character_set_connectioncollation_connection系统变量确定的字符集和排序规则。This is a nonbinary string unless the character set is binary.这是一个非二进制字符串,除非字符集是binary的。

If an application stores values from a function such as MD5() or SHA1() that returns a string of hex digits, more efficient storage and comparisons can be obtained by converting the hex representation to binary using UNHEX() and storing the result in a BINARY(N) column. 如果应用程序存储来自返回十六进制数字字符串的函数(如MD5()SHA1())的值,则可以通过使用UNHEX()将十六进制表示转换为二进制并将结果存储在二进制(N)列中来获得更有效的存储和比较。Each pair of hexadecimal digits requires one byte in binary form, so the value of N depends on the length of the hex string. 每对十六进制数字需要一个二进制字节,因此N的值取决于十六进制字符串的长度。N is 16 for an MD5() value and 20 for a SHA1() value. N对于MD5()值是16,对于SHA1()值是20。For SHA2(), N ranges from 28 to 32 depending on the argument specifying the desired bit length of the result.对于SHA2()N的范围从28到32,具体取决于指定结果所需位长度的参数。

The size penalty for storing the hex string in a CHAR column is at least two times, up to eight times if the value is stored in a column that uses the utf8 character set (where each character uses 4 bytes). CHAR列中存储十六进制字符串的大小惩罚至少为2倍,如果值存储在使用utf8字符集的列中(其中每个字符使用4个字节),则最多为8倍。Storing the string also results in slower comparisons because of the larger values and the need to take character set collation rules into account.存储字符串还会导致比较较慢,因为值较大,并且需要考虑字符集排序规则。

Suppose that an application stores MD5() string values in a CHAR(32) column:假设应用程序在CHAR(32)列中存储MD5()字符串值:

CREATE TABLE md5_tbl (md5_val CHAR(32), ...);
INSERT INTO md5_tbl (md5_val, ...) VALUES(MD5('abcdef'), ...);

To convert hex strings to more compact form, modify the application to use UNHEX() and BINARY(16) instead as follows:要将十六进制字符串转换为更紧凑的形式,请修改应用程序以使用UNHEX()BINARY(16),如下所示:

CREATE TABLE md5_tbl (md5_val BINARY(16), ...);
INSERT INTO md5_tbl (md5_val, ...) VALUES(UNHEX(MD5('abcdef')), ...);

Applications should be prepared to handle the very rare case that a hashing function produces the same value for two different input values. 应用程序应该准备好处理非常罕见的情况,即哈希函数为两个不同的输入值生成相同的值。One way to make collisions detectable is to make the hash column a primary key.使冲突可检测的一种方法是使哈希列成为主键。

Note注意

Exploits for the MD5 and SHA-1 algorithms have become known. MD5和SHA-1算法的漏洞已经为人所知。You may wish to consider using another one-way encryption function described in this section instead, such as SHA2().您可能希望考虑改用本节中描述的另一个单向加密函数,例如SHA2()

Caution小心

Passwords or other sensitive values supplied as arguments to encryption functions are sent as cleartext to the MySQL server unless an SSL connection is used. 除非使用SSL连接,否则作为加密函数参数提供的密码或其他敏感值将以明文形式发送到MySQL服务器。Also, such values appear in any MySQL logs to which they are written. 而且,这些值会出现在写入它们的任何MySQL日志中。To avoid these types of exposure, applications can encrypt sensitive values on the client side before sending them to the server. 为了避免这些类型的暴露,应用程序可以在将敏感值发送到服务器之前对客户端的敏感值进行加密。The same considerations apply to encryption keys. 同样的考虑也适用于加密密钥。To avoid exposing these, applications can use stored procedures to encrypt and decrypt values on the server side.为了避免暴露这些值,应用程序可以使用存储过程在服务器端对值进行加密和解密。