diff --git a/alis-rpc/alis-rpc-common/src/main/java/org/alis/rpc/common/util/HashShortUrl.java b/alis-rpc/alis-rpc-common/src/main/java/org/alis/rpc/common/util/HashShortUrl.java new file mode 100644 index 0000000..0e6aed9 --- /dev/null +++ b/alis-rpc/alis-rpc-common/src/main/java/org/alis/rpc/common/util/HashShortUrl.java @@ -0,0 +1,43 @@ +package org.alis.rpc.common.util; + +import cn.hutool.core.lang.hash.MurmurHash; + +import java.util.stream.IntStream; + +/** + * @author lc + * @date 2023/5/27 15:06 + */ +public class HashShortUrl{ + + private static final String[] CHARS = new String[62]; + + static { + IntStream.range(0, 10).forEach(i -> CHARS[i] = String.valueOf(i)); + for (int i = 1; i <= 26; i++) { + CHARS[9 + i] = String.valueOf((char) (i + 64)); + } + for (int i = 1; i <= 26; i++) { + CHARS[35 + i] = String.valueOf((char) (i + 96)); + } + + } + + + public String generator(String old) { + int i = MurmurHash.hash32(old); + if (i < 0) { + i = Math.abs(i); + } + int num = Integer.MAX_VALUE - i; + StringBuilder sb = new StringBuilder(); + while (num > 0) { + int x = num % CHARS.length; + sb.append(CHARS[x]); + num /= CHARS.length; + } + return sb.reverse().toString(); + + + } +}