#2 - Added test to test the cypto library

This commit is contained in:
Isaac Parenteau
2019-09-30 15:40:29 -05:00
parent 91f9f5a71e
commit f717d42ea8
8 changed files with 426 additions and 12 deletions

View File

@@ -0,0 +1,188 @@
package net.locusworks.crypto.utils;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.MessageDigest;
/**
* Wrapper class that leverages java's MessageDigest to hash files
* @author Isaac Parenteau
*
*/
public class HashUtils {
private static final Charset UTF_8 = StandardCharsets.UTF_8;
/**
* Used to build output as Hex
*/
private static final char[] DIGITS_LOWER = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
/**
* Used to build output as Hex
*/
private static final char[] DIGITS_UPPER = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
/**
* Size of the streaming buffer
*/
public static final Integer STREAM_BUFFER_LENGTH = 1024;
/**
* Hash a string literal
* @param hashType Hash types supported by MessageDigest (i.e MD5, SHA-1, SHA-512)
* @param data String to hash
* @return hash value of the string literal
*/
public static String hash(String hashType, String data) {
return hash(hashType, data, true);
}
/**
* Hash a string literal
* @param hashType Hash types supported by MessageDigest (i.e MD5, SHA-1, SHA-512)
* @param data String to hash
* @param toLower True to output the hash in lower case. False to output in upper case
* @return hash value of the string literal
*/
public static String hash(String hashType, String data, boolean toLower) {
byte[] stringData = data.getBytes(UTF_8);
return hash(hashType, stringData, toLower);
}
@Deprecated
public static String hash(String hashType, File data) {
return hash(hashType, data.toPath());
}
/**
* Hash a file
* @param hashType Hash types supported by MessageDigest (i.e MD5, SHA-1, SHA-512)
* @param data File to hash
* @return hash value of the file
*/
public static String hash(String hashType, Path data) {
return hash(hashType, data, true);
}
@Deprecated
public static String hash(String hashType, File data, boolean toLower) {
return hash(hashType, data.toPath(), toLower);
}
/**
* Hash a file
* @param hashType Hash types supported by MessageDigest (i.e MD5, SHA-1, SHA-512)
* @param data File to hash
* @param toLower True to output the hash in lower case. False to output in upper case
* @return hash value of the file
*/
public static String hash(String hashType, Path data, boolean toLower) {
try (InputStream stream = Files.newInputStream(data)) {
return hash(stream, hashType, toLower);
} catch (IOException ex) {
throw new IllegalArgumentException(ex.getMessage());
}
}
/**
* Hash a byte array
* @param hashType Hash types supported by MessageDigest (i.e MD5, SHA-1, SHA-512)
* @param data data to hash
* @return hash value of the data
*/
public static String hash(String hashType, byte[] data) {
return hash(hashType, data, true);
}
/**
* Hash a byte array
* @param hashType Hash types supported by MessageDigest (i.e MD5, SHA-1, SHA-512)
* @param data data to hash
* @param toLower True to output the hash in lower case. False to output in upper case
* @return hash value of the data
*/
public static String hash(String hashType, byte[] data, boolean toLower) {
return hash(new BufferedInputStream(new ByteArrayInputStream(data)), hashType, toLower);
}
/**
* Hash an input stream
* @param stream Stream with the data to hash
* @param hashType Hash types supported by MessageDigest (i.e MD5, SHA-1, SHA-512)
* @return Hash value of the input stream
*/
public static String hash(InputStream stream, String hashType) {
return hash(stream, hashType, true);
}
/**
* Hash an input stream
* @param stream Stream with the data to hash
* @param hashType Hash types supported by MessageDigest (i.e MD5, SHA-1, SHA-512)
* @param toLower True to output the hash in lower case. False to output in upper case
* @return Hash value of the input stream
*/
public static String hash(InputStream stream, String hashType, boolean toLower) {
MessageDigest digest = null;
try(InputStream is = stream) {
digest = MessageDigest.getInstance(hashType);
byte[] buffer = new byte[STREAM_BUFFER_LENGTH];
int read = is.read(buffer, 0, STREAM_BUFFER_LENGTH);
while (read > -1) {
digest.update(buffer, 0, read);
read = is.read(buffer, 0, STREAM_BUFFER_LENGTH);
}
return encodeHexString(digest.digest(), toLower);
} catch (Exception ex) {
throw new IllegalArgumentException(ex.getMessage());
}
}
/**
* Encode the hash data back to a string
* @param data Data to encode
* @param toLower output to lower case
* @return
*/
private static String encodeHexString(byte[] data, boolean toLower) {
return new String(encodeHex(data, toLower));
}
/**
* Encode the hash data to a character array
* @param data Data to encode
* @param toLower output to lower case
* @return
*/
private static char[] encodeHex(byte[] data, boolean toLower) {
return encodeHex(data, toLower ? DIGITS_LOWER : DIGITS_UPPER);
}
/**
* Encode the hex to a character array
* @param data Data to encode
* @param toDigits digits to use
* @return
*/
private static char[] encodeHex(byte[] data, char[] toDigits) {
int l = data.length;
char[] out = new char[l << 1];
// two characters form the hex value.
for (int i = 0, j = 0; i < l; i++) {
out[j++] = toDigits[(0xF0 & data[i]) >>> 4];
out[j++] = toDigits[0x0F & data[i]];
}
return out;
}
}