hutool.crypto.digest¶
Module Contents¶
Classes¶
摘要工具类 |
API¶
- class hutool.crypto.digest.DigestUtil¶
摘要工具类
提供常用的摘要(哈希)算法封装,包括: - MD5 / SHA-1 / SHA-256 / SHA-384 / SHA-512 - HMAC-MD5 / HMAC-SHA1 / HMAC-SHA256 所有方法同时支持 str 和 bytes 输入。
- static _to_bytes(data: Union[str, bytes]) bytes¶
将输入统一转为 bytes。
- 参数:
data – 字符串或字节数据
- 返回:
字节数据
- static _digest(data: Union[str, bytes], algorithm: str) bytes¶
通用摘要计算,返回原始 bytes。
- 参数:
data – 输入数据
algorithm – 摘要算法名称
- 返回:
摘要字节数据
- static _hex_digest(data: Union[str, bytes], algorithm: str) str¶
通用摘要计算,返回十六进制字符串。
- 参数:
data – 输入数据
algorithm – 摘要算法名称
- 返回:
十六进制摘要字符串
- static md5(data: Union[str, bytes]) bytes¶
计算MD5摘要,返回原始bytes。
- 参数:
data – 输入数据
- 返回:
MD5摘要字节数据
- static md5_hex(data: Union[str, bytes]) str¶
计算MD5摘要,返回32位十六进制字符串。
- 参数:
data – 输入数据
- 返回:
32位十六进制MD5字符串
- static md5_hex16(data: Union[str, bytes]) str¶
计算MD5摘要,返回16位十六进制字符串(取32位结果的中间16位)。
- 参数:
data – 输入数据
- 返回:
16位十六进制MD5字符串
- static sha1(data: Union[str, bytes]) bytes¶
计算SHA-1摘要,返回原始bytes。
- 参数:
data – 输入数据
- 返回:
SHA-1摘要字节数据
- static sha1_hex(data: Union[str, bytes]) str¶
计算SHA-1摘要,返回十六进制字符串。
- 参数:
data – 输入数据
- 返回:
十六进制SHA-1字符串
- static sha256(data: Union[str, bytes]) bytes¶
计算SHA-256摘要,返回原始bytes。
- 参数:
data – 输入数据
- 返回:
SHA-256摘要字节数据
- static sha256_hex(data: Union[str, bytes]) str¶
计算SHA-256摘要,返回十六进制字符串。
- 参数:
data – 输入数据
- 返回:
十六进制SHA-256字符串
- static sha384(data: Union[str, bytes]) bytes¶
计算SHA-384摘要,返回原始bytes。
- 参数:
data – 输入数据
- 返回:
SHA-384摘要字节数据
- static sha384_hex(data: Union[str, bytes]) str¶
计算SHA-384摘要,返回十六进制字符串。
- 参数:
data – 输入数据
- 返回:
十六进制SHA-384字符串
- static sha512(data: Union[str, bytes]) bytes¶
计算SHA-512摘要,返回原始bytes。
- 参数:
data – 输入数据
- 返回:
SHA-512摘要字节数据
- static sha512_hex(data: Union[str, bytes]) str¶
计算SHA-512摘要,返回十六进制字符串。
- 参数:
data – 输入数据
- 返回:
十六进制SHA-512字符串
- static _hmac_digest(data: Union[str, bytes], key: Union[str, bytes], algorithm: str) bytes¶
通用HMAC计算,返回原始bytes。
- 参数:
data – 输入数据
key – HMAC密钥
algorithm – 摘要算法名称
- 返回:
HMAC摘要字节数据
- static _hmac_hex_digest(data: Union[str, bytes], key: Union[str, bytes], algorithm: str) str¶
通用HMAC计算,返回十六进制字符串。
- 参数:
data – 输入数据
key – HMAC密钥
algorithm – 摘要算法名称
- 返回:
十六进制HMAC摘要字符串
- static hmac_md5(data: Union[str, bytes], key: Union[str, bytes]) bytes¶
HMAC-MD5计算,返回原始bytes。
- 参数:
data – 输入数据
key – HMAC密钥
- 返回:
HMAC-MD5摘要字节数据
- static hmac_md5_hex(data: Union[str, bytes], key: Union[str, bytes]) str¶
HMAC-MD5计算,返回十六进制字符串。
- 参数:
data – 输入数据
key – HMAC密钥
- 返回:
十六进制HMAC-MD5字符串
- static hmac_sha1(data: Union[str, bytes], key: Union[str, bytes]) bytes¶
HMAC-SHA1计算,返回原始bytes。
- 参数:
data – 输入数据
key – HMAC密钥
- 返回:
HMAC-SHA1摘要字节数据
- static hmac_sha1_hex(data: Union[str, bytes], key: Union[str, bytes]) str¶
HMAC-SHA1计算,返回十六进制字符串。
- 参数:
data – 输入数据
key – HMAC密钥
- 返回:
十六进制HMAC-SHA1字符串
- static hmac_sha256(data: Union[str, bytes], key: Union[str, bytes]) bytes¶
HMAC-SHA256计算,返回原始bytes。
- 参数:
data – 输入数据
key – HMAC密钥
- 返回:
HMAC-SHA256摘要字节数据
- static hmac_sha256_hex(data: Union[str, bytes], key: Union[str, bytes]) str¶
HMAC-SHA256计算,返回十六进制字符串。
- 参数:
data – 输入数据
key – HMAC密钥
- 返回:
十六进制HMAC-SHA256字符串
- static md5_from_file(file_path: str) str¶
计算文件的MD5摘要(32位十六进制)
以8192字节为单位分块读取文件,适用于大文件。
- 参数:
file_path – 文件路径
- 返回:
32位十六进制MD5字符串
- static sha256_from_file(file_path: str) str¶
计算文件的SHA-256摘要
以8192字节为单位分块读取文件,适用于大文件。
- 参数:
file_path – 文件路径
- 返回:
十六进制SHA-256字符串
- static md5_hex_to_16(md5_hex32: str) str¶
将32位MD5转换为16位MD5(取中间16位)
- 参数:
md5_hex32 – 32位MD5十六进制字符串
- 返回:
16位MD5十六进制字符串
- static hmac(data: Union[str, bytes], key: Union[str, bytes], algorithm: str = 'sha256') bytes¶
通用HMAC计算工厂方法
- 参数:
data – 输入数据
key – HMAC密钥
algorithm – 摘要算法名称,如 ‘md5’, ‘sha1’, ‘sha256’
- 返回:
HMAC摘要字节数据
- static hmac_hex(data: Union[str, bytes], key: Union[str, bytes], algorithm: str = 'sha256') str¶
通用HMAC计算工厂方法(返回十六进制)
- 参数:
data – 输入数据
key – HMAC密钥
algorithm – 摘要算法名称
- 返回:
十六进制HMAC摘要字符串
- static digest(data: Union[str, bytes], algorithm: str = 'sha256') bytes¶
通用摘要计算工厂方法
- 参数:
data – 输入数据
algorithm – 摘要算法名称,如 ‘md5’, ‘sha1’, ‘sha256’, ‘sha384’, ‘sha512’
- 返回:
摘要字节数据
- static digest_hex(data: Union[str, bytes], algorithm: str = 'sha256') str¶
通用摘要计算工厂方法(返回十六进制)
- 参数:
data – 输入数据
algorithm – 摘要算法名称
- 返回:
十六进制摘要字符串
- static bcrypt(password: Union[str, bytes]) str¶
使用bcrypt算法加密密码
- 参数:
password – 明文密码
- 返回:
bcrypt哈希字符串
- 抛出:
ImportError – 未安装bcrypt库时抛出
- static bcrypt_check(password: Union[str, bytes], hashed: str) bool¶
校验密码是否与bcrypt哈希匹配
- 参数:
password – 明文密码
hashed – bcrypt哈希字符串
- 返回:
是否匹配
- 抛出:
ImportError – 未安装bcrypt库时抛出