Skip to content

Commit e777aec

Browse files
i1619khz1619khz
and
1619khz
authored
fix: remove guava (casbin#280)
* refactor:replace guava's HashFunction with md5 function that only depends on jdk * chore: remove guava Co-authored-by: 1619khz <[email protected]>
1 parent 2dbd55f commit e777aec

File tree

2 files changed

+13
-15
lines changed

2 files changed

+13
-15
lines changed

pom.xml

-7
Original file line numberDiff line numberDiff line change
@@ -223,13 +223,6 @@
223223
<artifactId>gson</artifactId>
224224
<version>2.9.0</version>
225225
</dependency>
226-
227-
<dependency>
228-
<groupId>com.google.guava</groupId>
229-
<artifactId>guava</artifactId>
230-
<version>31.1-jre</version>
231-
</dependency>
232-
233226
</dependencies>
234227

235228
</project>

src/main/java/org/casbin/jcasbin/util/Util.java

+13-8
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@
1414

1515
package org.casbin.jcasbin.util;
1616

17-
import com.google.common.hash.HashCode;
18-
import com.google.common.hash.HashFunction;
19-
import com.google.common.hash.Hashing;
2017
import org.apache.commons.csv.CSVFormat;
2118
import org.apache.commons.csv.CSVParser;
2219
import org.apache.commons.csv.CSVRecord;
@@ -25,7 +22,9 @@
2522

2623
import java.io.IOException;
2724
import java.io.StringReader;
28-
import java.nio.charset.Charset;
25+
import java.nio.charset.StandardCharsets;
26+
import java.security.MessageDigest;
27+
import java.security.NoSuchAlgorithmException;
2928
import java.util.ArrayList;
3029
import java.util.Collections;
3130
import java.util.List;
@@ -42,8 +41,7 @@ public class Util {
4241

4342
private static Logger LOGGER = LoggerFactory.getLogger("org.casbin.jcasbin");
4443

45-
private static HashFunction hf = Hashing.md5();
46-
private static Charset defaultCharset = Charset.forName("UTF-8");
44+
private static final String md5AlgorithmName = "MD5";
4745

4846
/**
4947
* logPrint prints the log.
@@ -290,7 +288,14 @@ public static String replaceEval(String s, String replacement) {
290288
}
291289

292290
public static String md5(String data) {
293-
HashCode hash = hf.newHasher().putString(data, defaultCharset).hash();
294-
return hash.toString();
291+
return new String(getDigest(md5AlgorithmName).digest(data.getBytes(StandardCharsets.UTF_8)));
292+
}
293+
294+
private static MessageDigest getDigest(String algorithm) {
295+
try {
296+
return MessageDigest.getInstance(algorithm);
297+
} catch (NoSuchAlgorithmException e) {
298+
throw new IllegalArgumentException(e);
299+
}
295300
}
296301
}

0 commit comments

Comments
 (0)