Unique hash with a maximum of 4 characters?

What is the best way to create a String hash if the hash can contain no more than 4 characters, and these 4 characters can only be lowercase letters or numbers?

The strings I want to use have 1-255 characters. I know that it may not be possible to create a 4-char as a hash without a collision. But it would be enough if I had a good hash, where possible collisions are minimized.

What I tried is CRC16CCITT from here: http://introcs.cs.princeton.edu/java/61data/CRC16CCITT.java

 public class CRC16CCITT { public static void main(String[] args) { int crc = 0xFFFF; // initial value int polynomial = 0x1021; // 0001 0000 0010 0001 (0, 5, 12) // byte[] testBytes = "123456789".getBytes("ASCII"); byte[] bytes = args[0].getBytes(); for (byte b : bytes) { for (int i = 0; i < 8; i++) { boolean bit = ((b >> (7-i) & 1) == 1); boolean c15 = ((crc >> 15 & 1) == 1); crc <<= 1; if (c15 ^ bit) crc ^= polynomial; } } crc &= 0xffff; StdOut.println("CRC16-CCITT = " + Integer.toHexString(crc)); } } 

But it gives too many collisions. Are there any better algorithms?

+5
source share
1 answer

You are mistaken "hexadecimal digits" for "characters":

  int crc = 0xFFFF; // initial value 

Only 2 bytes ( 0xFF only 1 byte). For an ANSI 4-character CRC, you need 4 bytes ( 0xFFFFFFFF ).
You will have to adapt the rest of the code for working with a double leg, comment if you do not know how to do this.

PS: you can do this with less than 4 bytes, but it will complicate everything that is needed.

0
source

Source: https://habr.com/ru/post/1259893/


All Articles