From 52b7b6a9d7058d086166d67bde66002da18ea19f Mon Sep 17 00:00:00 2001 From: heavyrain2012 Date: Tue, 14 Dec 2021 12:18:14 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=BF=E6=8D=A2=E6=96=B0=E7=9A=84id=E7=94=9F?= =?UTF-8?q?=E6=88=90=E6=96=B9=E6=B3=95=EF=BC=8C=E7=94=9F=E6=88=909?= =?UTF-8?q?=E4=BD=8Did?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/win/liyufan/im/IDUtils.java | 113 ++++-------------- 1 file changed, 23 insertions(+), 90 deletions(-) diff --git a/broker/src/main/java/win/liyufan/im/IDUtils.java b/broker/src/main/java/win/liyufan/im/IDUtils.java index 3c8b3a3a..7e414c0d 100644 --- a/broker/src/main/java/win/liyufan/im/IDUtils.java +++ b/broker/src/main/java/win/liyufan/im/IDUtils.java @@ -8,107 +8,40 @@ package win.liyufan.im; +import java.util.HashMap; +import java.util.HashSet; + public class IDUtils { - private static char getChar(int number) { - if (number < 0 || number >= 64) { - number = number % 64; - } + private static final int RADIX = 36; + private static final int IDLEN = 9; + private static final int[] FACTOR = {29,19,1,11,7,17,13,23,5}; + + private static char getChar(long number) { if (number < 26) { - return (char)('A' + number); - } else if(number < 52) { - return (char)('a' + number - 26); - } else if(number < 62) { - return (char)('0' + number - 52); - } else if(number == 62) { - return '_'; + return (char)('a' + number); + } else { + return (char)('0' + number - 26); } - return '-'; } - private static int getNumber(char ch) { - - if (ch >= 'A' && ch <= 'Z') { - return ch - 'A'; - } else if(ch >= 'a' && ch <= 'z') { - return ch - 'a' + 26; - } else if(ch >= '0' && ch <= '9') { - return ch - '0' + 52; - } else if(ch == '+') { - return 62; - } - return 63; - } - - public static String toUid(int id) { + public static String toUid(long id) { StringBuilder sb = new StringBuilder(); - int rand0 = 0; - int rand3 = 0; - int rand6 = 0; - for (int i = 0; i < 8; i++) { - if (i == 0 || i == 3 || i == 6) { - int rand = (int)(Math.random()*64+0.5); - sb.append(getChar(rand)); - if (i == 0) { - rand0 = rand; - } else if (i == 3) { - rand3 = rand; - } else { - rand6 = rand; - } - } else { - if (i < 5) { - sb.append(getChar((id + rand0) % 64)); - } else if(i == 5) { - sb.append(getChar((id + rand3) % 64)); - } else { - sb.append(getChar((id + rand6) % 64)); - } - id = id / 64; - } + for (int i = 0; i < IDLEN; i++) { + long bit = ((id % RADIX) * FACTOR[i] + FACTOR[i] + 5)%RADIX; + sb.append(getChar(bit)); + id = id / RADIX; } return sb.toString(); } - - public static int fromUid(String uid) { - int id = 0; - int rand0 = 0; - int rand3 = 0; - int rand6 = 0; - for (int i = 0; i < 8; i++) { - if (i == 0 || i == 3 || i == 6) { - int rand = getNumber(uid.charAt(i)); - if (i == 0) { - rand0 = rand; - } else if (i == 3) { - rand3 = rand; - } else { - rand6 = rand; - } - } else { - int c = getNumber(uid.charAt(i)); - - if (i < 5) { - c = (c+64-rand0)%64; - } else if(i == 5) { - c = (c+64-rand3)%64; - } else { - c = (c+64-rand6)%64; - } - if (i < 3) - id = id + (c << (i-1)*6); - else if(i < 6) - id = id + (c << (i-2)*6); - else - id = id + (c << (i-3)*6); - } - } - return id; - } - + public static void main(String[] args) { - for (int i = 0; i < 100000000; i++) { + HashSet idset = new HashSet<>(); + for (long i = 0; i < 1000000000000L; i++) { String uid = toUid(i); - System.out.println(i + ", " + uid + ", " + fromUid(uid)); + if(!idset.add(uid)) { + System.out.println("error, dupliate id"); + System.exit(-1); + } } } }