public class Hashids extends Object implements Encoder<long[],String>, Decoder<String,long[]>
来自:https://github.com/davidafsilva/java-hashids
Hashids可以将数字或者16进制字符串转为短且唯一不连续的字符串,采用双向编码实现,比如,它可以将347之类的数字转换为yr8之类的字符串,也可以将yr8之类的字符串重新解码为347之类的数字。
此编码算法主要是解决爬虫类应用对连续ID爬取问题,将有序的ID转换为无序的Hashids,而且一一对应。
| Modifier and Type | Field and Description |
|---|---|
static char[] |
DEFAULT_ALPHABET
默认编解码字符串
|
| Constructor and Description |
|---|
Hashids(char[] salt,
char[] alphabet,
int minLength)
构造
|
| Modifier and Type | Method and Description |
|---|---|
long[] |
decode(String hash)
解码Hash值为数字数组
|
String |
decodeToHex(String hash)
解码Hash值为16进制数字
|
String |
encode(long... numbers)
编码给定的数字数组
|
String |
encodeFromHex(String hexNumbers)
编码给定的16进制数字
|
static Hashids |
of(char[] salt)
根据参数值,创建
Hashids,使用默认DEFAULT_ALPHABET作为字母表,不限制最小长度 |
static Hashids |
of(char[] salt,
char[] alphabet,
int minLength)
根据参数值,创建
Hashids |
static Hashids |
of(char[] salt,
int minLength)
根据参数值,创建
Hashids,使用默认DEFAULT_ALPHABET作为字母表 |
public Hashids(char[] salt,
char[] alphabet,
int minLength)
salt - 加盐值alphabet - hash字母表minLength - 限制最小长度,-1表示不限制public static Hashids of(char[] salt)
Hashids,使用默认DEFAULT_ALPHABET作为字母表,不限制最小长度salt - 加盐值Hashidspublic static Hashids of(char[] salt, int minLength)
Hashids,使用默认DEFAULT_ALPHABET作为字母表salt - 加盐值minLength - 限制最小长度,-1表示不限制Hashidspublic static Hashids of(char[] salt, char[] alphabet, int minLength)
Hashidssalt - 加盐值alphabet - hash字母表minLength - 限制最小长度,-1表示不限制Hashidspublic String encodeFromHex(String hexNumbers)
hexNumbers - 16进制数字null if numbers 是 null.IllegalArgumentException - 数字不支持抛出此异常public String encode(long... numbers)
encode in interface Encoder<long[],String>numbers - 数字数组null if numbers 是 null.IllegalArgumentException - 数字不支持抛出此异常public String decodeToHex(String hash)
hash - hash值null if numbers 是 null.IllegalArgumentException - if the hash is invalid.public long[] decode(String hash)
decode in interface Decoder<String,long[]>hash - hash值null if numbers 是 null.IllegalArgumentException - if the hash is invalid.Copyright © 2025. All rights reserved.