Package org.miaixz.bus.core.codec
Class Hashids
java.lang.Object
org.miaixz.bus.core.codec.Hashids
Hashids 协议实现,以实现:
- 生成简短、唯一、大小写敏感并无序的hash值
- 自然数字的Hash值
- 可以设置不同的盐,具有保密性
- 可配置的hash长度
- 递增的输入产生的输出无法预测
来自:https://github.com/davidafsilva/java-hashids
Hashids可以将数字或者16进制字符串转为短且唯一不连续的字符串,采用双向编码实现,比如,它可以将347之类的数字转换为yr8之类的字符串,也可以将yr8之类的字符串重新解码为347之类的数字。
此编码算法主要是解决爬虫类应用对连续ID爬取问题,将有序的ID转换为无序的Hashids,而且一一对应。
- Since:
- Java 17+
- Author:
- Kimi Liu
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionlong[]解码Hash值为数字数组decodeToHex(String hash) 解码Hash值为16进制数字encode(long... numbers) 编码给定的数字数组encodeFromHex(String hexNumbers) 编码给定的16进制数字static Hashidsof(char[] salt) 根据参数值,创建Hashids,使用默认DEFAULT_ALPHABET作为字母表,不限制最小长度static Hashidsof(char[] salt, char[] alphabet, int minLength) 根据参数值,创建Hashidsstatic Hashidsof(char[] salt, int minLength) 根据参数值,创建Hashids,使用默认DEFAULT_ALPHABET作为字母表
-
Field Details
-
DEFAULT_ALPHABET
public static final char[] DEFAULT_ALPHABET默认编解码字符串
-
-
Constructor Details
-
Hashids
public Hashids(char[] salt, char[] alphabet, int minLength) 构造- Parameters:
salt- 加盐值alphabet- hash字母表minLength- 限制最小长度,-1表示不限制
-
-
Method Details
-
of
根据参数值,创建Hashids,使用默认DEFAULT_ALPHABET作为字母表,不限制最小长度- Parameters:
salt- 加盐值- Returns:
Hashids
-
of
根据参数值,创建Hashids,使用默认DEFAULT_ALPHABET作为字母表- Parameters:
salt- 加盐值minLength- 限制最小长度,-1表示不限制- Returns:
Hashids
-
of
根据参数值,创建Hashids- Parameters:
salt- 加盐值alphabet- hash字母表minLength- 限制最小长度,-1表示不限制- Returns:
Hashids
-
encodeFromHex
编码给定的16进制数字- Parameters:
hexNumbers- 16进制数字- Returns:
- 编码后的值,
nullifnumbers是null. - Throws:
IllegalArgumentException- 数字不支持抛出此异常
-
encode
编码给定的数字数组- Specified by:
encodein interfaceEncoder<long[],String> - Parameters:
numbers- 数字数组- Returns:
- 编码后的值,
nullifnumbers是null. - Throws:
IllegalArgumentException- 数字不支持抛出此异常
-
decodeToHex
解码Hash值为16进制数字- Parameters:
hash- hash值- Returns:
- 解码后的16进制值,
nullifnumbers是null. - Throws:
IllegalArgumentException- if the hash is invalid.
-
decode
解码Hash值为数字数组- Specified by:
decodein interfaceDecoder<String,long[]> - Parameters:
hash- hash值- Returns:
- 解码后的16进制值,
nullifnumbers是null. - Throws:
IllegalArgumentException- if the hash is invalid.
-