Skip to content

Commit 5692a89

Browse files
committed
增加md5Base62
1 parent 77c0318 commit 5692a89

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

hsweb-core/pom.xml

+6
Original file line numberDiff line numberDiff line change
@@ -126,5 +126,11 @@
126126
<artifactId>netty-common</artifactId>
127127
</dependency>
128128

129+
<dependency>
130+
<groupId>io.seruco.encoding</groupId>
131+
<artifactId>base62</artifactId>
132+
<version>0.1.3</version>
133+
</dependency>
134+
129135
</dependencies>
130136
</project>

hsweb-core/src/main/java/org/hswebframework/web/utils/DigestUtils.java

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package org.hswebframework.web.utils;
22

33
import io.netty.util.concurrent.FastThreadLocal;
4+
import io.seruco.encoding.base62.Base62;
45
import org.apache.commons.codec.binary.Hex;
6+
import org.hswebframework.web.id.RandomIdGenerator;
57

68
import java.security.MessageDigest;
79
import java.util.function.Consumer;
@@ -22,15 +24,20 @@ protected MessageDigest initialValue() {
2224
return org.apache.commons.codec.digest.DigestUtils.getSha256Digest();
2325
}
2426
};
25-
;
27+
2628
public static final FastThreadLocal<MessageDigest> sha1 = new FastThreadLocal<MessageDigest>() {
2729
@Override
2830
protected MessageDigest initialValue() {
2931
return org.apache.commons.codec.digest.DigestUtils.getSha1Digest();
3032
}
3133
};
32-
;
3334

35+
private static final Base62 base62 = Base62.createInstance();
36+
37+
38+
public static Base62 base62(){
39+
return base62;
40+
}
3441
public static byte[] md5(Consumer<MessageDigest> digestHandler) {
3542
return digest(md5::get, digestHandler);
3643
}
@@ -66,6 +73,9 @@ public static byte[] md5(String str) {
6673
public static String md5Hex(String str) {
6774
return Hex.encodeHexString(md5(str.getBytes()));
6875
}
76+
public static String md5Base62(String str) {
77+
return new String(base62.encode(md5(str.getBytes())));
78+
}
6979

7080
public static byte[] sha256(byte[] data) {
7181
return org.apache.commons.codec.digest.DigestUtils.digest(sha256.get(), data);

hsweb-core/src/test/java/org/hswebframework/web/id/IDGeneratorTests.java

+14-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
import org.junit.Assert;
44
import org.junit.Test;
55

6+
import java.util.HashSet;
7+
import java.util.Set;
8+
69
/**
710
* @author zhouhao
811
* @since 3.0
@@ -19,9 +22,19 @@ public void test() {
1922
Assert.assertNotNull(IDGenerator.RANDOM.generate());
2023
Assert.assertNotNull(IDGenerator.SNOW_FLAKE.generate());
2124
Assert.assertNotNull(IDGenerator.SNOW_FLAKE_HEX.generate());
22-
25+
for (int i = 0; i < 100; i++) {
26+
System.out.println(IDGenerator.RANDOM.generate());
27+
}
2328
for (int i = 0; i < 100; i++) {
2429
System.out.println(IDGenerator.SNOW_FLAKE.generate());
2530
}
31+
32+
long time = System.currentTimeMillis();
33+
Set<String> set =new HashSet<>(100_0000);
34+
for (int i = 0; i < 100_0000; i++) {
35+
set.add(IDGenerator.RANDOM.generate());
36+
}
37+
System.out.println(set.size());
38+
System.out.println((System.currentTimeMillis()-time));
2639
}
2740
}

0 commit comments

Comments
 (0)