🎨 短链算法

master
luochuan 1 year ago
parent 01e3e7e8de
commit eb3e3fa72b

@ -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();
}
}
Loading…
Cancel
Save