Hash id s generates short and unique IDs

Generally, we use the incremental id as the identification of the requested resource, but if the site directly uses the incremental id field as the business attribute, it will have the following impact on the site (not limited to the following):

  • For user resources, the site is easy to detect the total number of registered users, the number of registered users in the time period, etc. The personal home page addresses of stations A and B use the incremental id, e.g.: the first user: ~ / 1; Second user: ~ / 2, and so on.

  • For video resources, it is easy to get all video resources of the site through crawlers. Station B has changed from the original "av" + incremental id to "BV" + base58(av No.) However, the essence is only to the database table av number base58 Operation: on the video page, the av number corresponding to the BV number can be obtained by entering aid in the browser F12 console, and the HTML of the video page The av number address still exists in the meta tag.

When some resources are provided externally, we hope to use a coding algorithm to encode the incremental identification code into a non incremental identification.

Hashids

Hashids Is a very small cross language open source library, which can convert numbers or hexadecimal strings into short and only discontinuous strings. Hashids is bidirectional coding (supports encode and decode). For example, it can convert numbers such as 347 into strings such as yr8, or re decode strings such as yr8 into numbers such as 347.

YouTube video address link? Hashids is used after v = parameter. Tencent video and iqiyi should use a Hash algorithm similar to this.

Unlike youtube, the short video coding of TikTok web version is based on the unique id of the sender.

Hashids implemented in Java

Hashids is implemented in different languages. We can officially refer to the corresponding tool library according to different languages. We choose the Java version org.hashids >> hashids >> 1.0.3 Implementation library.

The principle and implementation of Hashids java version can refer to the source code and reference This article.

pom.xml dependency

<dependency>
    <groupId>org.hashids</groupId>
    <artifactId>hashids</artifactId>
    <version>1.0.3</version>
</dependency>

test case

Hashids can customize the salt, but it must be ensured that the same set of salt is used in the encoding and decoding process. Be sure to keep the salt properly. The salt used in the test case is this is my salt.

Points needing attention:

  • The salt set in the production environment should maintain sufficient complexity, and the salt should be properly preserved. The repair work to be done for leaking or missing salt will be very troublesome.

  • Hashids can customize the minimum length of the encoded result, and the minimum length of the 11 bit encoded result is uniformly used in the test case. The minimum coding length of 11 bits means that the length of results obtained from different inputs may exceed 11 bits.

  • org. Hashids > > hashids > > 1.0.3 strictly distinguish case in the process of encoding and decoding.

The variable parameter of encoding long type is string

@Test
void test_hashids_encode_method() {
final String SALT = "this is my salt";
final int MIN_HASH_LENGTH = 11;

Hashids hashids = new Hashids(SALT, MIN_HASH_LENGTH);
String encryptString = hashids.encode(347L);

System.out.println(encryptString); // Y5bAyr8dLO4
}

The parameters in encode(long...numbers) are variable parameters of long type. You can encode multiple parameters of long type into a string. The scene of encoding variable parameters into a string can be considered and designed flexibly by yourself.

The decoded string is an array of Long type

@Test
void test_hashids_decode_method() {
    final String SALT = "this is my salt";
    final int MIN_HASH_LENGTH = 11;

    Hashids hashids = new Hashids(SALT, MIN_HASH_LENGTH);
    long[] decrypedNumbers = hashids.decode("Y5bAyr8dLO4");

    Arrays.stream(decrypedNumbers).forEach(item -> System.out.println(item)); // 347

}

If the salt used in encoding and decoding is inconsistent, long [] is an empty array.

The encoded hexadecimal string is Hashids string

Hashids supports encoding hexadecimal strings, so if you use mongodb database, it can be automatically generated by the system_ The id string is encoded using hashids.

/**
 * {@link <a href="https://stackoverflow.com/a/27137224">node.js - get hash from strings, like hashids - Stack Overflow</a>}
 * <p>
 * {@link <a href="https://hashids.org/java/">hashids</a>}
 */
@Test
void test_hashids_encodeHex_method() {
    final String SALT = "this is my salt";
    final int MIN_HASH_LENGTH = 11;

    Hashids hashids = new Hashids(SALT, MIN_HASH_LENGTH);
    String encryptString = hashids.encodeHex(cn.hutool.core.util.HexUtil.encodeHexStr("this is a string"));
    System.out.println(encryptString); // 1prnZLrKPlS5EEe61reMCNzkJXP
}

Decode Hashids string to hexadecimal string

Corresponding to the encoded hexadecimal string, the hexadecimal string can be decoded according to the string.

/**
 * {@link <a href="https://stackoverflow.com/a/27137224">node.js - get hash from strings, like hashids - Stack Overflow</a>}
 * <p>
 * {@link <a href="https://hashids.org/java/">hashids</a>}
 */
@Test
void test_hashids_decodeHex_method() {
    final String SALT = "this is my salt";
    final int MIN_HASH_LENGTH = 11;

    Hashids hashids = new Hashids(SALT, MIN_HASH_LENGTH);
    String decrypedNumbers = hashids.decodeHex("1prnZLrKPlS5EEe61reMCNzkJXP");

    System.out.println(cn.hutool.core.util.HexUtil.decodeHexStr(decrypedNumbers)); // this is a string
}

If the salt used in encoding and decoding is inconsistent, long [] is an empty array.

Custom alphabet mapping set

The default mapping alphabet is abcdefghijklmnopqrstuvwxyz1234567890. You can customize the mapping alphabet according to its constructor. In the test case, we use the mapping alphabet similar to Youtube.

The characters in the custom alphabet should contain at least 16 characters.

@Test
void test_hashids_costom_alphabet_by_Constructor() {
    final String SALT = "this is my salt";
    final int MIN_HASH_LENGTH = 11;
    final String ALPHABET = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_-";


    Hashids hashids = new Hashids(SALT, MIN_HASH_LENGTH, ALPHABET);
    String encryptString = hashids.encode(347L);

    System.out.println(encryptString); // kqBg-Kpg7_J

}

The articles are published simultaneously on major mainstream knowledge sharing platforms, so github is set as a unified feedback area

Questions, discussions and feedback: https://github.com/weixsun/discussion

WeChat official account obmq to keep abreast of the latest developments

This paper is based on the operation tool platform of blog group sending one article and multiple sending OpenWrite release

Added by mikesmith76 on Tue, 08 Mar 2022 06:42:47 +0200