清理Hex
This commit is contained in:
@@ -25,7 +25,7 @@ import java.net.Proxy;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import static org.jackhuang.hmcl.util.DigestUtils.digest;
|
import static org.jackhuang.hmcl.util.DigestUtils.digest;
|
||||||
import static org.jackhuang.hmcl.util.Hex.encodeHexString;
|
import static org.jackhuang.hmcl.util.Hex.encodeHex;
|
||||||
import static org.jackhuang.hmcl.util.Lang.tryCast;
|
import static org.jackhuang.hmcl.util.Lang.tryCast;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -57,7 +57,7 @@ public class OfflineAccountFactory extends AccountFactory<OfflineAccount> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static String getUUIDFromUserName(String username) {
|
private static String getUUIDFromUserName(String username) {
|
||||||
return encodeHexString(digest("MD5", username));
|
return encodeHex(digest("MD5", username));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ import java.util.Map;
|
|||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
|
|
||||||
import static org.jackhuang.hmcl.util.DigestUtils.digest;
|
import static org.jackhuang.hmcl.util.DigestUtils.digest;
|
||||||
import static org.jackhuang.hmcl.util.Hex.encodeHexString;
|
import static org.jackhuang.hmcl.util.Hex.encodeHex;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@@ -93,7 +93,7 @@ public final class GameAssetDownloadTask extends Task {
|
|||||||
try {
|
try {
|
||||||
// check the checksum of file to ensure that the file is not need to re-download.
|
// check the checksum of file to ensure that the file is not need to re-download.
|
||||||
if (file.exists()) {
|
if (file.exists()) {
|
||||||
String sha1 = encodeHexString(digest("SHA-1", FileUtils.readBytes(file)));
|
String sha1 = encodeHex(digest("SHA-1", FileUtils.readBytes(file)));
|
||||||
if (sha1.equals(assetObject.getHash())) {
|
if (sha1.equals(assetObject.getHash())) {
|
||||||
++downloaded;
|
++downloaded;
|
||||||
Logging.LOG.finest("File $file has been downloaded successfully, skipped downloading");
|
Logging.LOG.finest("File $file has been downloaded successfully, skipped downloading");
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import org.jackhuang.hmcl.task.Task;
|
|||||||
import org.jackhuang.hmcl.util.*;
|
import org.jackhuang.hmcl.util.*;
|
||||||
|
|
||||||
import static org.jackhuang.hmcl.util.DigestUtils.digest;
|
import static org.jackhuang.hmcl.util.DigestUtils.digest;
|
||||||
import static org.jackhuang.hmcl.util.Hex.encodeHexString;
|
import static org.jackhuang.hmcl.util.Hex.encodeHex;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
@@ -80,7 +80,7 @@ public final class LibraryDownloadTask extends Task {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
byte[] fileData = FileUtils.readBytes(libPath);
|
byte[] fileData = FileUtils.readBytes(libPath);
|
||||||
boolean valid = checksums.contains(encodeHexString(digest("SHA-1", fileData)));
|
boolean valid = checksums.contains(encodeHex(digest("SHA-1", fileData)));
|
||||||
if ((!valid) && (libPath.getName().endsWith(".jar"))) {
|
if ((!valid) && (libPath.getName().endsWith(".jar"))) {
|
||||||
}
|
}
|
||||||
return validateJar(fileData, checksums);
|
return validateJar(fileData, checksums);
|
||||||
@@ -101,7 +101,7 @@ public final class LibraryDownloadTask extends Task {
|
|||||||
hashes = new String(eData, Charset.forName("UTF-8")).split("\n");
|
hashes = new String(eData, Charset.forName("UTF-8")).split("\n");
|
||||||
}
|
}
|
||||||
if (!entry.isDirectory()) {
|
if (!entry.isDirectory()) {
|
||||||
files.put(entry.getName(), encodeHexString(digest("SHA-1", eData)));
|
files.put(entry.getName(), encodeHex(digest("SHA-1", eData)));
|
||||||
}
|
}
|
||||||
entry = jar.getNextJarEntry();
|
entry = jar.getNextJarEntry();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ import java.util.LinkedList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static org.jackhuang.hmcl.util.DigestUtils.digest;
|
import static org.jackhuang.hmcl.util.DigestUtils.digest;
|
||||||
import static org.jackhuang.hmcl.util.Hex.encodeHexString;
|
import static org.jackhuang.hmcl.util.Hex.encodeHex;
|
||||||
|
|
||||||
public final class MinecraftInstanceTask<T> extends Task {
|
public final class MinecraftInstanceTask<T> extends Task {
|
||||||
|
|
||||||
@@ -64,7 +64,7 @@ public final class MinecraftInstanceTask<T> extends Task {
|
|||||||
if (path.startsWith("/") || path.startsWith("\\"))
|
if (path.startsWith("/") || path.startsWith("\\"))
|
||||||
path = path.substring(1);
|
path = path.substring(1);
|
||||||
|
|
||||||
overrides.add(new ModpackConfiguration.FileInformation(path, encodeHexString(digest("SHA-1", zip))));
|
overrides.add(new ModpackConfiguration.FileInformation(path, encodeHex(digest("SHA-1", zip))));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ import java.util.Set;
|
|||||||
import java.util.function.Predicate;
|
import java.util.function.Predicate;
|
||||||
|
|
||||||
import static org.jackhuang.hmcl.util.DigestUtils.digest;
|
import static org.jackhuang.hmcl.util.DigestUtils.digest;
|
||||||
import static org.jackhuang.hmcl.util.Hex.encodeHexString;
|
import static org.jackhuang.hmcl.util.Hex.encodeHex;
|
||||||
|
|
||||||
public class ModpackInstallTask<T> extends Task {
|
public class ModpackInstallTask<T> extends Task {
|
||||||
|
|
||||||
@@ -94,8 +94,8 @@ public class ModpackInstallTask<T> extends Task {
|
|||||||
byte[] data = os.toByteArray();
|
byte[] data = os.toByteArray();
|
||||||
|
|
||||||
if (files.contains(path) && entryFile.exists()) {
|
if (files.contains(path) && entryFile.exists()) {
|
||||||
String oldHash = encodeHexString(digest("SHA-1", new FileInputStream(entryFile)));
|
String oldHash = encodeHex(digest("SHA-1", new FileInputStream(entryFile)));
|
||||||
String newHash = encodeHexString(digest("SHA-1", new ByteArrayInputStream(data)));
|
String newHash = encodeHex(digest("SHA-1", new ByteArrayInputStream(data)));
|
||||||
if (!oldHash.equals(newHash)) {
|
if (!oldHash.equals(newHash)) {
|
||||||
try (FileOutputStream fos = new FileOutputStream(entryFile)) {
|
try (FileOutputStream fos = new FileOutputStream(entryFile)) {
|
||||||
IOUtils.copyTo(new ByteArrayInputStream(data), fos, buf);
|
IOUtils.copyTo(new ByteArrayInputStream(data), fos, buf);
|
||||||
|
|||||||
@@ -17,22 +17,18 @@
|
|||||||
*/
|
*/
|
||||||
package org.jackhuang.hmcl.util;
|
package org.jackhuang.hmcl.util;
|
||||||
|
|
||||||
import java.nio.charset.Charset;
|
import java.io.IOException;
|
||||||
|
|
||||||
import static java.nio.charset.StandardCharsets.UTF_8;
|
|
||||||
|
|
||||||
public final class Hex {
|
public final class Hex {
|
||||||
|
|
||||||
private static final char[] DIGITS_LOWER = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
|
private static final char[] DIGITS_LOWER = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
|
||||||
|
|
||||||
private static final char[] DIGITS_UPPER = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
|
public static byte[] decodeHex(String str) throws IOException {
|
||||||
private final Charset charset;
|
char[] data = str.toCharArray();
|
||||||
|
|
||||||
public static byte[] decodeHex(char[] data) throws Exception {
|
|
||||||
int len = data.length;
|
int len = data.length;
|
||||||
|
|
||||||
if ((len & 0x1) != 0)
|
if ((len & 0x1) != 0)
|
||||||
throw new Exception("Odd number of characters.");
|
throw new IOException("Odd number of characters.");
|
||||||
|
|
||||||
byte[] out = new byte[len >> 1];
|
byte[] out = new byte[len >> 1];
|
||||||
|
|
||||||
@@ -48,83 +44,24 @@ public final class Hex {
|
|||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static char[] encodeHex(byte[] data) {
|
public static String encodeHex(byte[] data) {
|
||||||
return encodeHex(data, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static char[] encodeHex(byte[] data, boolean toLowerCase) {
|
|
||||||
return encodeHex(data, toLowerCase ? DIGITS_LOWER : DIGITS_UPPER);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected static char[] encodeHex(byte[] data, char[] toDigits) {
|
|
||||||
int l = data.length;
|
int l = data.length;
|
||||||
char[] out = new char[l << 1];
|
char[] out = new char[l << 1];
|
||||||
|
|
||||||
int i = 0;
|
int i = 0;
|
||||||
for (int j = 0; i < l; i++) {
|
for (int j = 0; i < l; i++) {
|
||||||
out[(j++)] = toDigits[((0xF0 & data[i]) >>> 4)];
|
out[(j++)] = DIGITS_LOWER[((0xF0 & data[i]) >>> 4)];
|
||||||
out[(j++)] = toDigits[(0xF & data[i])];
|
out[(j++)] = DIGITS_LOWER[(0xF & data[i])];
|
||||||
}
|
}
|
||||||
return out;
|
return new String(out);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String encodeHexString(byte[] data) {
|
private static int toDigit(char ch, int index) throws IOException {
|
||||||
return new String(encodeHex(data));
|
|
||||||
}
|
|
||||||
|
|
||||||
protected static int toDigit(char ch, int index) {
|
|
||||||
int digit = Character.digit(ch, 16);
|
int digit = Character.digit(ch, 16);
|
||||||
if (digit == -1)
|
if (digit == -1)
|
||||||
throw new IllegalArgumentException("Illegal hexadecimal character " + ch + " at index " + index);
|
throw new IOException("Illegal hexadecimal character " + ch + " at index " + index);
|
||||||
return digit;
|
return digit;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Hex() {
|
private Hex() {}
|
||||||
this(UTF_8);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Hex(Charset charset) {
|
|
||||||
this.charset = charset;
|
|
||||||
}
|
|
||||||
|
|
||||||
public byte[] decode(byte[] array) throws Exception {
|
|
||||||
return decodeHex(new String(array, getCharset()).toCharArray());
|
|
||||||
}
|
|
||||||
|
|
||||||
public Object decode(Object object) throws Exception {
|
|
||||||
try {
|
|
||||||
char[] charArray = (object instanceof String) ? ((String) object).toCharArray() : (char[]) object;
|
|
||||||
return decodeHex(charArray);
|
|
||||||
} catch (ClassCastException e) {
|
|
||||||
throw new Exception(e.getMessage(), e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public byte[] encode(byte[] array) {
|
|
||||||
return encodeHexString(array).getBytes(getCharset());
|
|
||||||
}
|
|
||||||
|
|
||||||
public Object encode(Object object)
|
|
||||||
throws Exception {
|
|
||||||
try {
|
|
||||||
byte[] byteArray = (object instanceof String) ? ((String) object).getBytes(getCharset()) : (byte[]) object;
|
|
||||||
|
|
||||||
return encodeHex(byteArray);
|
|
||||||
} catch (ClassCastException e) {
|
|
||||||
throw new Exception(e.getMessage(), e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public Charset getCharset() {
|
|
||||||
return this.charset;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getCharsetName() {
|
|
||||||
return this.charset.name();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return super.toString() + "[charsetName=" + this.charset + "]";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user