// 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);
- digest.update(bytes);
+ for (byte[] arr : bytes)
+ digest.update(arr);
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);
- digest.update(bytes);
+ for (byte[] arr : bytes)
+ digest.update(arr);
byte[] checksum = digest.digest();
- String res = encodeHexString(checksum);
- return res;
+ return checksum;
} catch (NoSuchAlgorithmException e) {
- throw new IllegalArgumentException("Cannot digest with algorithm " + algorithm, e);
+ throw new UnsupportedOperationException("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);
}
byte[] checksum = digest.digest();
- String res = encodeHexString(checksum);
+ String res = toHexString(checksum);
return res;
} catch (NoSuchAlgorithmException e) {
throw new IllegalArgumentException("Cannot digest with algorithm " + algorithm, e);
MessageDigest digest = MessageDigest.getInstance(algorithm);
digest.update(bb);
byte[] checksum = digest.digest();
- String res = encodeHexString(checksum);
+ String res = toHexString(checksum);
long end = System.currentTimeMillis();
if (debug)
System.out.println((end - begin) + " ms / " + ((end - begin) / 1000) + " s");
}
public static String digest(String algorithm, Path path, long bufferSize) {
- byte[] digest = digestRaw(algorithm, path, bufferSize);
- return encodeHexString(digest);
+ byte[] digest = digestAsBytes(algorithm, path, bufferSize);
+ return toHexString(digest);
}
- public static byte[] digestRaw(String algorithm, Path file, long bufferSize) {
+ public static byte[] digestAsBytes(String algorithm, Path file, long bufferSize) {
long begin = System.currentTimeMillis();
try {
MessageDigest md = MessageDigest.getInstance(algorithm);
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
- */
- public static String encodeHexString(byte[] bytes) {
+ /** Converts a byte array to an hex String. */
+ public static String toHexString(byte[] bytes) {
char[] hexChars = new char[bytes.length * 2];
for (int j = 0; j < bytes.length; j++) {
int v = bytes[j] & 0xFF;