Class Hashids

java.lang.Object
org.miaixz.bus.core.codec.Hashids
All Implemented Interfaces:
Decoder<String,long[]>, Encoder<long[],String>

public class Hashids extends Object implements Encoder<long[],String>, Decoder<String,long[]>
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 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

      public static Hashids of(char[] salt)
      根据参数值,创建Hashids,使用默认DEFAULT_ALPHABET作为字母表,不限制最小长度
      Parameters:
      salt - 加盐值
      Returns:
      Hashids
    • of

      public static Hashids of(char[] salt, int minLength)
      根据参数值,创建Hashids,使用默认DEFAULT_ALPHABET作为字母表
      Parameters:
      salt - 加盐值
      minLength - 限制最小长度,-1表示不限制
      Returns:
      Hashids
    • of

      public static Hashids of(char[] salt, char[] alphabet, int minLength)
      根据参数值,创建Hashids
      Parameters:
      salt - 加盐值
      alphabet - hash字母表
      minLength - 限制最小长度,-1表示不限制
      Returns:
      Hashids
    • encodeFromHex

      public String encodeFromHex(String hexNumbers)
      编码给定的16进制数字
      Parameters:
      hexNumbers - 16进制数字
      Returns:
      编码后的值, null if numbersnull.
      Throws:
      IllegalArgumentException - 数字不支持抛出此异常
    • encode

      public String encode(long... numbers)
      编码给定的数字数组
      Specified by:
      encode in interface Encoder<long[],String>
      Parameters:
      numbers - 数字数组
      Returns:
      编码后的值, null if numbersnull.
      Throws:
      IllegalArgumentException - 数字不支持抛出此异常
    • decodeToHex

      public String decodeToHex(String hash)
      解码Hash值为16进制数字
      Parameters:
      hash - hash值
      Returns:
      解码后的16进制值, null if numbersnull.
      Throws:
      IllegalArgumentException - if the hash is invalid.
    • decode

      public long[] decode(String hash)
      解码Hash值为数字数组
      Specified by:
      decode in interface Decoder<String,long[]>
      Parameters:
      hash - hash值
      Returns:
      解码后的16进制值, null if numbersnull.
      Throws:
      IllegalArgumentException - if the hash is invalid.