Skip to content

Commit

Permalink
fix: remove guava (#280)
Browse files Browse the repository at this point in the history
* refactor:replace guava's HashFunction with md5 function that only depends on jdk

* chore: remove guava

Co-authored-by: 1619khz <wangyi@huasisoft.com>
  • Loading branch information
i1619khz and 1619khz authored Jun 18, 2022
1 parent 2dbd55f commit e777aec
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 15 deletions.
7 changes: 0 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -223,13 +223,6 @@
<artifactId>gson</artifactId>
<version>2.9.0</version>
</dependency>

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>31.1-jre</version>
</dependency>

</dependencies>

</project>
21 changes: 13 additions & 8 deletions src/main/java/org/casbin/jcasbin/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@

package org.casbin.jcasbin.util;

import com.google.common.hash.HashCode;
import com.google.common.hash.HashFunction;
import com.google.common.hash.Hashing;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
Expand All @@ -25,7 +22,9 @@

import java.io.IOException;
import java.io.StringReader;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand All @@ -42,8 +41,7 @@ public class Util {

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

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

/**
* logPrint prints the log.
Expand Down Expand Up @@ -290,7 +288,14 @@ public static String replaceEval(String s, String replacement) {
}

public static String md5(String data) {
HashCode hash = hf.newHasher().putString(data, defaultCharset).hash();
return hash.toString();
return new String(getDigest(md5AlgorithmName).digest(data.getBytes(StandardCharsets.UTF_8)));
}

private static MessageDigest getDigest(String algorithm) {
try {
return MessageDigest.getInstance(algorithm);
} catch (NoSuchAlgorithmException e) {
throw new IllegalArgumentException(e);
}
}
}

0 comments on commit e777aec

Please # to comment.