public final class Hashids extends Object
See hashids.org. This is a Java port of the javascript version.
The Hashids's salt is used as a secret to generate unique strings using a given alphabet. Generated strings can have a minimumLength.
If you use this to obfuscates identities, do not expose your salt, alphabet nor separators to a client, client-side is not safe.
Only positive numbers are supported. All methods in this class will throw an IllegalArgumentException
if a negative number is given. If you want to use negative numbers you'll have to handle prepending - to the hash string yourself and would be limited to single number hashes.
Here is sample code to handle negative numbers prepending - to them:
Hashids hashids = new Hashids( "this is your salt" ); long number = -1234567890; String enc = ( Math.abs( number ) != number ? "-" : "" ) + hashids.encodeToString( Math.abs( number ) ); long dec = enc.startsWith( "-" ) ? -hashids.decodeLongs( enc.substring( 1 ) )[0] : hashids.decodeLongs( enc )[0];Note that this isn't true Hashids anymore and that this is limited to single number hashes.
Hashids instances are thread-safe.
Modifier and Type | Class and Description |
---|---|
static class |
Hashids.Builder
Hashids Builder.
|
Modifier and Type | Field and Description |
---|---|
static String |
DEFAULT_ALPHABET
Default alphabet.
|
static String |
DEFAULT_SEPARATORS
Default separators.
|
static long |
MAX_NUMBER_VALUE
Maximum number value.
|
Constructor and Description |
---|
Hashids(String salt) |
Hashids(String salt, int minimumLength) |
Hashids(String salt, int minimumLength, String alphabet) |
Hashids(String salt, int minimumLength, String alphabet, String separators) |
Hashids(String salt, String alphabet) |
Modifier and Type | Method and Description |
---|---|
Hashid |
decode(String hash)
Decrypt string.
|
String |
decodeHex(String hash)
Decrypt string to numbers as hex.
|
int[] |
decodeInts(String hash)
Decrypt string to integers.
|
long[] |
decodeLongs(String hash)
Decrypt string to longs.
|
Hashid |
encode(long... numbers)
Encrypt number(s).
|
String |
encodeToString(int... numbers)
Encrypt number(s) to string.
|
String |
encodeToString(long... numbers)
Encrypt number(s) to string.
|
String |
encodeToString(String hexa)
Encrypt hexa string to string.
|
public static final String DEFAULT_ALPHABET
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890
public static final String DEFAULT_SEPARATORS
Used to prevent the generation of strings that contains rude english words.
cfhistuCFHISTU
public static final long MAX_NUMBER_VALUE
This is 9_007_199_254_740_991, or 2^53-1, or Number.MAX_VALUE-1 in Javascript. This limit is mandatory in order to ensure interoperability.
public Hashids(String salt)
public Hashids(String salt, int minimumLength)
public Hashid encode(long... numbers)
numbers
- The number(s) to encrypt
public String encodeToString(long... numbers)
numbers
- The number(s) to encrypt
public String encodeToString(int... numbers)
numbers
- The number(s) to encrypt
public String encodeToString(String hexa)
hexa
- The hexa string to encrypt
public Hashid decode(String hash)
hash
- The encrypted string
public long[] decodeLongs(String hash)
hash
- The encrypted string
public int[] decodeInts(String hash)
hash
- The encrypted string
IllegalArgumentException
- if decoded number is out of integer range, shouldn't you be using longs instead?