projects
/
lgpl
/
argeo-commons.git
/ blobdiff
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Introduce BytesUtils
[lgpl/argeo-commons.git]
/
org.argeo.util
/
src
/
org
/
argeo
/
util
/
DigestUtils.java
diff --git
a/org.argeo.util/src/org/argeo/util/DigestUtils.java
b/org.argeo.util/src/org/argeo/util/DigestUtils.java
index ce018007c6c637dce08d0c563e80d27a16c575cc..e1a19160bd5a04afe23f834b6556e41bf53d91dd 100644
(file)
--- a/
org.argeo.util/src/org/argeo/util/DigestUtils.java
+++ b/
org.argeo.util/src/org/argeo/util/DigestUtils.java
@@
-1,5
+1,7
@@
package org.argeo.util;
package org.argeo.util;
+import static org.argeo.util.BytesUtils.toHexString;
+
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
@@
-23,29
+25,34
@@
public class DigestUtils {
// TODO: make it configurable
private final static Integer byteBufferCapacity = 100 * 1024;// 100 KB
// TODO: make it configurable
private final static Integer byteBufferCapacity = 100 * 1024;// 100 KB
- public static byte[] sha1(byte[] bytes) {
+ public static byte[] sha1(byte[]
...
bytes) {
try {
MessageDigest digest = MessageDigest.getInstance(SHA1);
try {
MessageDigest digest = MessageDigest.getInstance(SHA1);
- digest.update(bytes);
+ for (byte[] arr : bytes)
+ digest.update(arr);
byte[] checksum = digest.digest();
return checksum;
} catch (NoSuchAlgorithmException e) {
byte[] checksum = digest.digest();
return checksum;
} catch (NoSuchAlgorithmException e) {
- throw new
IllegalArgumentException(
e);
+ throw new
UnsupportedOperationException("SHA1 is not avalaible",
e);
}
}
}
}
- public static
String digest(String algorithm, byte[]
bytes) {
+ public static
byte[] digestAsBytes(String algorithm, byte[]...
bytes) {
try {
MessageDigest digest = MessageDigest.getInstance(algorithm);
try {
MessageDigest digest = MessageDigest.getInstance(algorithm);
- digest.update(bytes);
+ for (byte[] arr : bytes)
+ digest.update(arr);
byte[] checksum = digest.digest();
byte[] checksum = digest.digest();
- String res = encodeHexString(checksum);
- return res;
+ return checksum;
} catch (NoSuchAlgorithmException e) {
} catch (NoSuchAlgorithmException e) {
- throw new
IllegalArgument
Exception("Cannot digest with algorithm " + algorithm, e);
+ throw new
UnsupportedOperation
Exception("Cannot digest with algorithm " + algorithm, e);
}
}
}
}
+ public static String digest(String algorithm, byte[]... bytes) {
+ return toHexString(digestAsBytes(algorithm, bytes));
+ }
+
public static String digest(String algorithm, InputStream in) {
try {
MessageDigest digest = MessageDigest.getInstance(algorithm);
public static String digest(String algorithm, InputStream in) {
try {
MessageDigest digest = MessageDigest.getInstance(algorithm);
@@
-60,7
+67,7
@@
public class DigestUtils {
}
byte[] checksum = digest.digest();
}
byte[] checksum = digest.digest();
- String res =
encode
HexString(checksum);
+ String res =
to
HexString(checksum);
return res;
} catch (NoSuchAlgorithmException e) {
throw new IllegalArgumentException("Cannot digest with algorithm " + algorithm, e);
return res;
} catch (NoSuchAlgorithmException e) {
throw new IllegalArgumentException("Cannot digest with algorithm " + algorithm, e);
@@
-101,7
+108,7
@@
public class DigestUtils {
MessageDigest digest = MessageDigest.getInstance(algorithm);
digest.update(bb);
byte[] checksum = digest.digest();
MessageDigest digest = MessageDigest.getInstance(algorithm);
digest.update(bb);
byte[] checksum = digest.digest();
- String res =
encode
HexString(checksum);
+ String res =
to
HexString(checksum);
long end = System.currentTimeMillis();
if (debug)
System.out.println((end - begin) + " ms / " + ((end - begin) / 1000) + " s");
long end = System.currentTimeMillis();
if (debug)
System.out.println((end - begin) + " ms / " + ((end - begin) / 1000) + " s");
@@
-116,11
+123,11
@@
public class DigestUtils {
}
public static String digest(String algorithm, Path path, long bufferSize) {
}
public static String digest(String algorithm, Path path, long bufferSize) {
- byte[] digest = digest
Raw
(algorithm, path, bufferSize);
- return
encode
HexString(digest);
+ byte[] digest = digest
AsBytes
(algorithm, path, bufferSize);
+ return
to
HexString(digest);
}
}
- public static byte[] digest
Raw
(String algorithm, Path file, long bufferSize) {
+ public static byte[] digest
AsBytes
(String algorithm, Path file, long bufferSize) {
long begin = System.currentTimeMillis();
try {
MessageDigest md = MessageDigest.getInstance(algorithm);
long begin = System.currentTimeMillis();
try {
MessageDigest md = MessageDigest.getInstance(algorithm);
@@
-181,21
+188,10
@@
public class DigestUtils {
}
}
}
}
- final private static char[] hexArray = "0123456789abcdef".toCharArray();
-
- /**
- * From
- * http://stackoverflow.com/questions/9655181/how-to-convert-a-byte-array-to
- * -a-hex-string-in-java
- */
+ /** @deprecated Use {@link BytesUtils#toHexString(byte[])} instead */
+ @Deprecated
public static String encodeHexString(byte[] bytes) {
public static String encodeHexString(byte[] bytes) {
- char[] hexChars = new char[bytes.length * 2];
- for (int j = 0; j < bytes.length; j++) {
- int v = bytes[j] & 0xFF;
- hexChars[j * 2] = hexArray[v >>> 4];
- hexChars[j * 2 + 1] = hexArray[v & 0x0F];
- }
- return new String(hexChars);
+ return BytesUtils.toHexString(bytes);
}
}
}
}