清理DigestUtils
This commit is contained in:
@@ -19,12 +19,13 @@ package org.jackhuang.hmcl.auth.offline;
|
||||
|
||||
import org.jackhuang.hmcl.auth.AccountFactory;
|
||||
import org.jackhuang.hmcl.auth.CharacterSelector;
|
||||
import org.jackhuang.hmcl.util.DigestUtils;
|
||||
import org.jackhuang.hmcl.util.UUIDTypeAdapter;
|
||||
|
||||
import java.net.Proxy;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.jackhuang.hmcl.util.DigestUtils.digest;
|
||||
import static org.jackhuang.hmcl.util.Hex.encodeHexString;
|
||||
import static org.jackhuang.hmcl.util.Lang.tryCast;
|
||||
|
||||
/**
|
||||
@@ -56,7 +57,7 @@ public class OfflineAccountFactory extends AccountFactory<OfflineAccount> {
|
||||
}
|
||||
|
||||
private static String getUUIDFromUserName(String username) {
|
||||
return DigestUtils.md5Hex(username);
|
||||
return encodeHexString(digest("MD5", username));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -22,7 +22,6 @@ import org.jackhuang.hmcl.game.AssetObject;
|
||||
import org.jackhuang.hmcl.game.Version;
|
||||
import org.jackhuang.hmcl.task.FileDownloadTask;
|
||||
import org.jackhuang.hmcl.task.Task;
|
||||
import org.jackhuang.hmcl.util.DigestUtils;
|
||||
import org.jackhuang.hmcl.util.FileUtils;
|
||||
import org.jackhuang.hmcl.util.Logging;
|
||||
import org.jackhuang.hmcl.util.NetworkUtils;
|
||||
@@ -35,6 +34,9 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.logging.Level;
|
||||
|
||||
import static org.jackhuang.hmcl.util.DigestUtils.digest;
|
||||
import static org.jackhuang.hmcl.util.Hex.encodeHexString;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author huangyuhui
|
||||
@@ -91,7 +93,7 @@ public final class GameAssetDownloadTask extends Task {
|
||||
try {
|
||||
// check the checksum of file to ensure that the file is not need to re-download.
|
||||
if (file.exists()) {
|
||||
String sha1 = DigestUtils.sha1Hex(FileUtils.readBytes(file));
|
||||
String sha1 = encodeHexString(digest("SHA-1", FileUtils.readBytes(file)));
|
||||
if (sha1.equals(assetObject.getHash())) {
|
||||
++downloaded;
|
||||
Logging.LOG.finest("File $file has been downloaded successfully, skipped downloading");
|
||||
|
||||
@@ -7,6 +7,9 @@ import org.jackhuang.hmcl.task.FileDownloadTask;
|
||||
import org.jackhuang.hmcl.task.Task;
|
||||
import org.jackhuang.hmcl.util.*;
|
||||
|
||||
import static org.jackhuang.hmcl.util.DigestUtils.digest;
|
||||
import static org.jackhuang.hmcl.util.Hex.encodeHexString;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.*;
|
||||
@@ -77,7 +80,7 @@ public final class LibraryDownloadTask extends Task {
|
||||
return true;
|
||||
}
|
||||
byte[] fileData = FileUtils.readBytes(libPath);
|
||||
boolean valid = checksums.contains(DigestUtils.sha1Hex(fileData));
|
||||
boolean valid = checksums.contains(encodeHexString(digest("SHA-1", fileData)));
|
||||
if ((!valid) && (libPath.getName().endsWith(".jar"))) {
|
||||
}
|
||||
return validateJar(fileData, checksums);
|
||||
@@ -98,7 +101,7 @@ public final class LibraryDownloadTask extends Task {
|
||||
hashes = new String(eData, Charset.forName("UTF-8")).split("\n");
|
||||
}
|
||||
if (!entry.isDirectory()) {
|
||||
files.put(entry.getName(), DigestUtils.sha1Hex(eData));
|
||||
files.put(entry.getName(), encodeHexString(digest("SHA-1", eData)));
|
||||
}
|
||||
entry = jar.getNextJarEntry();
|
||||
}
|
||||
|
||||
@@ -21,7 +21,6 @@ import org.apache.commons.compress.archivers.ArchiveEntry;
|
||||
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
|
||||
import org.jackhuang.hmcl.task.Task;
|
||||
import org.jackhuang.hmcl.util.Constants;
|
||||
import org.jackhuang.hmcl.util.DigestUtils;
|
||||
import org.jackhuang.hmcl.util.FileUtils;
|
||||
|
||||
import java.io.File;
|
||||
@@ -29,6 +28,9 @@ import java.io.FileInputStream;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.jackhuang.hmcl.util.DigestUtils.digest;
|
||||
import static org.jackhuang.hmcl.util.Hex.encodeHexString;
|
||||
|
||||
public final class MinecraftInstanceTask<T> extends Task {
|
||||
|
||||
private final File zipFile;
|
||||
@@ -62,7 +64,7 @@ public final class MinecraftInstanceTask<T> extends Task {
|
||||
if (path.startsWith("/") || path.startsWith("\\"))
|
||||
path = path.substring(1);
|
||||
|
||||
overrides.add(new ModpackConfiguration.FileInformation(path, DigestUtils.sha1Hex(zip)));
|
||||
overrides.add(new ModpackConfiguration.FileInformation(path, encodeHexString(digest("SHA-1", zip))));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -20,7 +20,6 @@ package org.jackhuang.hmcl.mod;
|
||||
import org.apache.commons.compress.archivers.ArchiveEntry;
|
||||
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
|
||||
import org.jackhuang.hmcl.task.Task;
|
||||
import org.jackhuang.hmcl.util.DigestUtils;
|
||||
import org.jackhuang.hmcl.util.FileUtils;
|
||||
import org.jackhuang.hmcl.util.IOUtils;
|
||||
|
||||
@@ -31,6 +30,9 @@ import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import static org.jackhuang.hmcl.util.DigestUtils.digest;
|
||||
import static org.jackhuang.hmcl.util.Hex.encodeHexString;
|
||||
|
||||
public class ModpackInstallTask<T> extends Task {
|
||||
|
||||
private final File modpackFile;
|
||||
@@ -92,8 +94,8 @@ public class ModpackInstallTask<T> extends Task {
|
||||
byte[] data = os.toByteArray();
|
||||
|
||||
if (files.contains(path) && entryFile.exists()) {
|
||||
String oldHash = DigestUtils.sha1Hex(new FileInputStream(entryFile));
|
||||
String newHash = DigestUtils.sha1Hex(new ByteArrayInputStream(data));
|
||||
String oldHash = encodeHexString(digest("SHA-1", new FileInputStream(entryFile)));
|
||||
String newHash = encodeHexString(digest("SHA-1", new ByteArrayInputStream(data)));
|
||||
if (!oldHash.equals(newHash)) {
|
||||
try (FileOutputStream fos = new FileOutputStream(entryFile)) {
|
||||
IOUtils.copyTo(new ByteArrayInputStream(data), fos, buf);
|
||||
|
||||
@@ -32,6 +32,8 @@ import java.net.URL;
|
||||
import java.security.MessageDigest;
|
||||
import java.util.logging.Level;
|
||||
|
||||
import static org.jackhuang.hmcl.util.DigestUtils.getDigest;
|
||||
|
||||
/**
|
||||
* A task that can download a file online.
|
||||
*
|
||||
@@ -154,7 +156,7 @@ public class FileDownloadTask extends Task {
|
||||
temp = FileUtils.createTempFile();
|
||||
rFile = new RandomAccessFile(temp, "rw");
|
||||
|
||||
MessageDigest digest = DigestUtils.getSha1Digest();
|
||||
MessageDigest digest = getDigest("SHA-1");
|
||||
|
||||
stream = con.getInputStream();
|
||||
int lastDownloaded = 0, downloaded = 0;
|
||||
|
||||
@@ -35,11 +35,6 @@ public final class DigestUtils {
|
||||
|
||||
private static final int STREAM_BUFFER_LENGTH = 1024;
|
||||
|
||||
private static byte[] digest(MessageDigest digest, InputStream data)
|
||||
throws IOException {
|
||||
return updateDigest(digest, data).digest();
|
||||
}
|
||||
|
||||
public static MessageDigest getDigest(String algorithm) {
|
||||
try {
|
||||
return MessageDigest.getInstance(algorithm);
|
||||
@@ -48,193 +43,23 @@ public final class DigestUtils {
|
||||
}
|
||||
}
|
||||
|
||||
public static MessageDigest getMd2Digest() {
|
||||
return getDigest("MD2");
|
||||
public static byte[] digest(String algorithm, String data) {
|
||||
return digest(algorithm, data.getBytes(UTF_8));
|
||||
}
|
||||
|
||||
public static MessageDigest getMd5Digest() {
|
||||
return getDigest("MD5");
|
||||
public static byte[] digest(String algorithm, byte[] data) {
|
||||
return getDigest(algorithm).digest(data);
|
||||
}
|
||||
|
||||
public static MessageDigest getSha1Digest() {
|
||||
return getDigest("SHA-1");
|
||||
public static byte[] digest(String algorithm, InputStream data) throws IOException {
|
||||
return digest(getDigest(algorithm), data);
|
||||
}
|
||||
|
||||
public static MessageDigest getSha256Digest() {
|
||||
return getDigest("SHA-256");
|
||||
public static byte[] digest(MessageDigest digest, InputStream data) throws IOException {
|
||||
return updateDigest(digest, data).digest();
|
||||
}
|
||||
|
||||
public static MessageDigest getSha384Digest() {
|
||||
return getDigest("SHA-384");
|
||||
}
|
||||
|
||||
public static MessageDigest getSha512Digest() {
|
||||
return getDigest("SHA-512");
|
||||
}
|
||||
|
||||
public static byte[] md2(byte[] data) {
|
||||
return getMd2Digest().digest(data);
|
||||
}
|
||||
|
||||
public static byte[] md2(InputStream data)
|
||||
throws IOException {
|
||||
return digest(getMd2Digest(), data);
|
||||
}
|
||||
|
||||
public static byte[] md2(String data) {
|
||||
return md2(data.getBytes(UTF_8));
|
||||
}
|
||||
|
||||
public static String md2Hex(byte[] data) {
|
||||
return Hex.encodeHexString(md2(data));
|
||||
}
|
||||
|
||||
public static String md2Hex(InputStream data)
|
||||
throws IOException {
|
||||
return Hex.encodeHexString(md2(data));
|
||||
}
|
||||
|
||||
public static String md2Hex(String data) {
|
||||
return Hex.encodeHexString(md2(data));
|
||||
}
|
||||
|
||||
public static byte[] md5(byte[] data) {
|
||||
return getMd5Digest().digest(data);
|
||||
}
|
||||
|
||||
public static byte[] md5(InputStream data)
|
||||
throws IOException {
|
||||
return digest(getMd5Digest(), data);
|
||||
}
|
||||
|
||||
public static byte[] md5(String data) {
|
||||
return md5(data.getBytes(UTF_8));
|
||||
}
|
||||
|
||||
public static String md5Hex(byte[] data) {
|
||||
return Hex.encodeHexString(md5(data));
|
||||
}
|
||||
|
||||
public static String md5Hex(InputStream data)
|
||||
throws IOException {
|
||||
return Hex.encodeHexString(md5(data));
|
||||
}
|
||||
|
||||
public static String md5Hex(String data) {
|
||||
return Hex.encodeHexString(md5(data));
|
||||
}
|
||||
|
||||
public static byte[] sha1(byte[] data) {
|
||||
return getSha1Digest().digest(data);
|
||||
}
|
||||
|
||||
public static byte[] sha1(InputStream data)
|
||||
throws IOException {
|
||||
return digest(getSha1Digest(), data);
|
||||
}
|
||||
|
||||
public static byte[] sha1(String data) {
|
||||
return sha1(data.getBytes(UTF_8));
|
||||
}
|
||||
|
||||
public static String sha1Hex(byte[] data) {
|
||||
return Hex.encodeHexString(sha1(data));
|
||||
}
|
||||
|
||||
public static String sha1Hex(InputStream data)
|
||||
throws IOException {
|
||||
return Hex.encodeHexString(sha1(data));
|
||||
}
|
||||
|
||||
public static String sha1Hex(String data) {
|
||||
return Hex.encodeHexString(sha1(data));
|
||||
}
|
||||
|
||||
public static byte[] sha256(byte[] data) {
|
||||
return getSha256Digest().digest(data);
|
||||
}
|
||||
|
||||
public static byte[] sha256(InputStream data)
|
||||
throws IOException {
|
||||
return digest(getSha256Digest(), data);
|
||||
}
|
||||
|
||||
public static byte[] sha256(String data) {
|
||||
return sha256(data.getBytes(UTF_8));
|
||||
}
|
||||
|
||||
public static String sha256Hex(byte[] data) {
|
||||
return Hex.encodeHexString(sha256(data));
|
||||
}
|
||||
|
||||
public static String sha256Hex(InputStream data)
|
||||
throws IOException {
|
||||
return Hex.encodeHexString(sha256(data));
|
||||
}
|
||||
|
||||
public static String sha256Hex(String data) {
|
||||
return Hex.encodeHexString(sha256(data));
|
||||
}
|
||||
|
||||
public static byte[] sha384(byte[] data) {
|
||||
return getSha384Digest().digest(data);
|
||||
}
|
||||
|
||||
public static byte[] sha384(InputStream data)
|
||||
throws IOException {
|
||||
return digest(getSha384Digest(), data);
|
||||
}
|
||||
|
||||
public static byte[] sha384(String data) {
|
||||
return sha384(data.getBytes(UTF_8));
|
||||
}
|
||||
|
||||
public static String sha384Hex(byte[] data) {
|
||||
return Hex.encodeHexString(sha384(data));
|
||||
}
|
||||
|
||||
public static String sha384Hex(InputStream data)
|
||||
throws IOException {
|
||||
return Hex.encodeHexString(sha384(data));
|
||||
}
|
||||
|
||||
public static String sha384Hex(String data) {
|
||||
return Hex.encodeHexString(sha384(data));
|
||||
}
|
||||
|
||||
public static byte[] sha512(byte[] data) {
|
||||
return getSha512Digest().digest(data);
|
||||
}
|
||||
|
||||
public static byte[] sha512(InputStream data)
|
||||
throws IOException {
|
||||
return digest(getSha512Digest(), data);
|
||||
}
|
||||
|
||||
public static byte[] sha512(String data) {
|
||||
return sha512(data.getBytes(UTF_8));
|
||||
}
|
||||
|
||||
public static String sha512Hex(byte[] data) {
|
||||
return Hex.encodeHexString(sha512(data));
|
||||
}
|
||||
|
||||
public static String sha512Hex(InputStream data)
|
||||
throws IOException {
|
||||
return Hex.encodeHexString(sha512(data));
|
||||
}
|
||||
|
||||
public static String sha512Hex(String data) {
|
||||
return Hex.encodeHexString(sha512(data));
|
||||
}
|
||||
|
||||
public static MessageDigest updateDigest(MessageDigest messageDigest, byte[] valueToDigest) {
|
||||
messageDigest.update(valueToDigest);
|
||||
return messageDigest;
|
||||
}
|
||||
|
||||
public static MessageDigest updateDigest(MessageDigest digest, InputStream data)
|
||||
throws IOException {
|
||||
public static MessageDigest updateDigest(MessageDigest digest, InputStream data) throws IOException {
|
||||
byte[] buffer = new byte[STREAM_BUFFER_LENGTH];
|
||||
int read = data.read(buffer, 0, STREAM_BUFFER_LENGTH);
|
||||
|
||||
@@ -246,8 +71,4 @@ public final class DigestUtils {
|
||||
return digest;
|
||||
}
|
||||
|
||||
public static MessageDigest updateDigest(MessageDigest messageDigest, String valueToDigest) {
|
||||
messageDigest.update(valueToDigest.getBytes(UTF_8));
|
||||
return messageDigest;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user