[Major] Refactored StringHelper.as* methods to .to* methods

This commit is contained in:
Robert von Burg 2017-07-11 11:27:56 +02:00
parent db8c541346
commit b0e623384b
9 changed files with 63 additions and 47 deletions

View File

@ -77,7 +77,7 @@ public class DefaultEncryptionHandler implements EncryptionHandler {
public String nextToken() {
byte[] bytes = new byte[32];
this.secureRandom.nextBytes(bytes);
return StringHelper.getHexString(bytes);
return StringHelper.toHexString(bytes);
}
@Override

View File

@ -119,11 +119,11 @@ public class PasswordCreator {
if (saltTemp.isEmpty()) {
saltTemp = encryptionHandler.nextToken();
}
String saltS = StringHelper.getHexString(saltTemp.getBytes());
String saltS = StringHelper.toHexString(saltTemp.getBytes());
byte[] salt = StringHelper.fromHexString(saltS);
byte[] passwordHash = encryptionHandler.hashPassword(password, salt);
String passwordHashS = StringHelper.getHexString(passwordHash);
String passwordHashS = StringHelper.toHexString(passwordHash);
System.out.println("Hash is: " + passwordHashS);
System.out.println("Salt is: " + saltS);
System.out.println();

View File

@ -60,9 +60,9 @@ public class PrivilegeUsersDomWriter {
userElement.setAttribute(XmlConstants.XML_ATTR_USER_ID, user.getUserId());
userElement.setAttribute(XmlConstants.XML_ATTR_USERNAME, user.getUsername());
if (user.getPassword() != null)
userElement.setAttribute(XmlConstants.XML_ATTR_PASSWORD, StringHelper.getHexString(user.getPassword()));
userElement.setAttribute(XmlConstants.XML_ATTR_PASSWORD, StringHelper.toHexString(user.getPassword()));
if (user.getSalt() != null)
userElement.setAttribute(XmlConstants.XML_ATTR_SALT, StringHelper.getHexString(user.getSalt()));
userElement.setAttribute(XmlConstants.XML_ATTR_SALT, StringHelper.toHexString(user.getSalt()));
// add first name element
if (StringHelper.isNotEmpty(user.getFirstname())) {

View File

@ -160,7 +160,7 @@ public class XmlTest {
PrivilegeConfigDomWriter configSaxWriter = new PrivilegeConfigDomWriter(containerModel, configFile);
configSaxWriter.write();
String fileHash = StringHelper.getHexString(FileHelper.hashFileSha256(configFile));
String fileHash = StringHelper.toHexString(FileHelper.hashFileSha256(configFile));
assertEquals("800b8e42e15b6b3bb425fa9c12a011d587d2b12343a1d1371eaa36dc1b2ea5f4", fileHash);
}
@ -185,8 +185,8 @@ public class XmlTest {
assertEquals("1", admin.getUserId());
assertEquals("admin", admin.getUsername());
assertEquals("cb69962946617da006a2f95776d78b49e5ec7941d2bdb2d25cdb05f957f64344",
StringHelper.getHexString(admin.getPassword()));
assertEquals("61646d696e", StringHelper.getHexString(admin.getSalt()));
StringHelper.toHexString(admin.getPassword()));
assertEquals("61646d696e", StringHelper.toHexString(admin.getSalt()));
assertEquals("Application", admin.getFirstname());
assertEquals("Administrator", admin.getLastname());
assertEquals(UserState.ENABLED, admin.getUserState());

View File

@ -95,7 +95,7 @@ public class FileClientUtil {
}
// now validate hashes
String dstFileHash = StringHelper.getHexString(FileHelper.hashFileSha256(dstFile));
String dstFileHash = StringHelper.toHexString(FileHelper.hashFileSha256(dstFile));
if (!dstFileHash.equals(origFilePart.getFileHash())) {
msg = "Downloading the file {0} failed because the hashes don''t match. Expected: {1} / Actual: {2}"; //$NON-NLS-1$
msg = MessageFormat.format(msg, origFilePart.getFileName(), origFilePart.getFileHash(), dstFileHash);
@ -123,7 +123,7 @@ public class FileClientUtil {
// get the size of the file
long fileLength = srcFile.length();
String fileHash = StringHelper.getHexString(FileHelper.hashFileSha256(srcFile));
String fileHash = StringHelper.toHexString(FileHelper.hashFileSha256(srcFile));
// create the file part to send
FilePart filePart = new FilePart(srcFile.getName(), fileType);

View File

@ -104,7 +104,7 @@ public class FileHandler {
filePart.setFileLength(fileSize);
// set the SHA256 of the file
filePart.setFileHash(StringHelper.getHexString(FileHelper.hashFileSha256(file)));
filePart.setFileHash(StringHelper.toHexString(FileHelper.hashFileSha256(file)));
}
// variables defining the part of the file we're going to return
@ -221,7 +221,7 @@ public class FileHandler {
// if this is the last part, then validate the hashes
if (filePart.isLastPart()) {
String dstFileHash = StringHelper.getHexString(FileHelper.hashFileSha256(dstFile));
String dstFileHash = StringHelper.toHexString(FileHelper.hashFileSha256(dstFile));
if (!dstFileHash.equals(filePart.getFileHash())) {
String msg = "Uploading the file {0} failed because the hashes don''t match. Expected: {1} / Actual: {2}"; //$NON-NLS-1$
msg = MessageFormat.format(msg, filePart.getFileName(), filePart.getFileHash(), dstFileHash);

View File

@ -300,8 +300,8 @@ public class FileHelper {
outBuffer.flush();
if (checksum) {
String fromFileMD5 = StringHelper.getHexString(FileHelper.hashFileMd5(fromFile));
String toFileMD5 = StringHelper.getHexString(FileHelper.hashFileMd5(toFile));
String fromFileMD5 = StringHelper.toHexString(FileHelper.hashFileMd5(fromFile));
String toFileMD5 = StringHelper.toHexString(FileHelper.hashFileMd5(toFile));
if (!fromFileMD5.equals(toFileMD5)) {
FileHelper.logger.error(MessageFormat.format("Copying failed, as MD5 sums are not equal: {0} / {1}", //$NON-NLS-1$
fromFileMD5, toFileMD5));
@ -492,7 +492,7 @@ public class FileHelper {
/**
* Creates the MD5 hash of the given file, returning the hash as a byte array. Use
* {@link StringHelper#getHexString(byte[])} to create a HEX string of the bytes
* {@link StringHelper#toHexString(byte[])} to create a HEX string of the bytes
*
* @param file
* the file to hash
@ -505,7 +505,7 @@ public class FileHelper {
/**
* Creates the SHA1 hash of the given file, returning the hash as a byte array. Use
* {@link StringHelper#getHexString(byte[])} to create a HEX string of the bytes
* {@link StringHelper#toHexString(byte[])} to create a HEX string of the bytes
*
* @param file
* the file to hash
@ -518,7 +518,7 @@ public class FileHelper {
/**
* Creates the SHA256 hash of the given file, returning the hash as a byte array. Use
* {@link StringHelper#getHexString(byte[])} to create a HEX string of the bytes
* {@link StringHelper#toHexString(byte[])} to create a HEX string of the bytes
*
* @param file
* the file to hash
@ -531,7 +531,7 @@ public class FileHelper {
/**
* Creates the hash of the given file with the given algorithm, returning the hash as a byte array. Use
* {@link StringHelper#getHexString(byte[])} to create a HEX string of the bytes
* {@link StringHelper#toHexString(byte[])} to create a HEX string of the bytes
*
* @param file
* the file to hash

View File

@ -15,6 +15,7 @@
*/
package li.strolch.utils.helper;
import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
@ -56,11 +57,15 @@ public class StringHelper {
(byte) '5', (byte) '6', (byte) '7', (byte) '8', (byte) '9', (byte) 'a', (byte) 'b', (byte) 'c', (byte) 'd',
(byte) 'e', (byte) 'f' };
public static String asHexString(byte data) {
public static String toHexString(byte data) {
return String.format("%02x", data);
}
public static String asPrettyHexString(byte[] raw) {
public static byte fromHexByte(String encoded) {
return Byte.valueOf(encoded, 16);
}
public static String toPrettyHexString(byte[] raw) {
try {
byte[] hex = new byte[3 * raw.length + (raw.length / 8)];
int index = 0;
@ -85,19 +90,15 @@ public class StringHelper {
}
}
/**
* Converts each byte of the given byte array to a HEX value and returns the concatenation of these values
*
* @param raw
* the bytes to convert to String using numbers in hexadecimal
*
* @return the encoded string
*
* @throws RuntimeException
* if {@link UnsupportedEncodingException} is thrown
*/
public static String getHexString(byte[] raw) throws RuntimeException {
return getHexString(raw, 0, raw.length);
public static byte[] fromPrettyHexString(String prettyHex) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
String[] split = prettyHex.split(" ");
for (String encoded : split) {
out.write(fromHexByte(encoded));
}
return out.toByteArray();
}
/**
@ -111,7 +112,22 @@ public class StringHelper {
* @throws RuntimeException
* if {@link UnsupportedEncodingException} is thrown
*/
public static String getHexString(byte[] raw, int offset, int length) throws RuntimeException {
public static String toHexString(byte[] raw) throws RuntimeException {
return toHexString(raw, 0, raw.length);
}
/**
* Converts each byte of the given byte array to a HEX value and returns the concatenation of these values
*
* @param raw
* the bytes to convert to String using numbers in hexadecimal
*
* @return the encoded string
*
* @throws RuntimeException
* if {@link UnsupportedEncodingException} is thrown
*/
public static String toHexString(byte[] raw, int offset, int length) throws RuntimeException {
try {
byte[] hex = new byte[2 * length];
int index = 0;
@ -165,11 +181,11 @@ public class StringHelper {
* @return the hash or null, if an exception was thrown
*/
public static String hashMd5AsHex(String string) {
return getHexString(hashMd5(string.getBytes()));
return toHexString(hashMd5(string.getBytes()));
}
/**
* Generates the MD5 Hash of a string. Use {@link StringHelper#getHexString(byte[])} to convert the byte array to a
* Generates the MD5 Hash of a string. Use {@link StringHelper#toHexString(byte[])} to convert the byte array to a
* Hex String which is printable
*
* @param string
@ -182,7 +198,7 @@ public class StringHelper {
}
/**
* Generates the MD5 Hash of a byte array Use {@link StringHelper#getHexString(byte[])} to convert the byte array to
* Generates the MD5 Hash of a byte array Use {@link StringHelper#toHexString(byte[])} to convert the byte array to
* a Hex String which is printable
*
* @param bytes
@ -203,11 +219,11 @@ public class StringHelper {
* @return the hash or null, if an exception was thrown
*/
public static String hashSha1AsHex(String string) {
return getHexString(hashSha1(string.getBytes()));
return toHexString(hashSha1(string.getBytes()));
}
/**
* Generates the SHA1 Hash of a string Use {@link StringHelper#getHexString(byte[])} to convert the byte array to a
* Generates the SHA1 Hash of a string Use {@link StringHelper#toHexString(byte[])} to convert the byte array to a
* Hex String which is printable
*
* @param string
@ -220,7 +236,7 @@ public class StringHelper {
}
/**
* Generates the SHA1 Hash of a byte array Use {@link StringHelper#getHexString(byte[])} to convert the byte array
* Generates the SHA1 Hash of a byte array Use {@link StringHelper#toHexString(byte[])} to convert the byte array
* to a Hex String which is printable
*
* @param bytes
@ -241,11 +257,11 @@ public class StringHelper {
* @return the hash or null, if an exception was thrown
*/
public static String hashSha256AsHex(String string) {
return getHexString(hashSha256(string.getBytes()));
return toHexString(hashSha256(string.getBytes()));
}
/**
* Generates the SHA-256 Hash of a string Use {@link StringHelper#getHexString(byte[])} to convert the byte array to
* Generates the SHA-256 Hash of a string Use {@link StringHelper#toHexString(byte[])} to convert the byte array to
* a Hex String which is printable
*
* @param string
@ -258,7 +274,7 @@ public class StringHelper {
}
/**
* Generates the SHA1 Hash of a byte array Use {@link StringHelper#getHexString(byte[])} to convert the byte array
* Generates the SHA1 Hash of a byte array Use {@link StringHelper#toHexString(byte[])} to convert the byte array
* to a Hex String which is printable
*
* @param bytes
@ -281,7 +297,7 @@ public class StringHelper {
* @return the hash or null, if an exception was thrown
*/
public static String hashAsHex(String algorithm, String string) {
return getHexString(hash(algorithm, string));
return toHexString(hash(algorithm, string));
}
/**
@ -319,7 +335,7 @@ public class StringHelper {
* @return the hash or null, if an exception was thrown
*/
public static String hashAsHex(String algorithm, byte[] bytes) {
return getHexString(hash(algorithm, bytes));
return toHexString(hash(algorithm, bytes));
}
/**

View File

@ -146,8 +146,8 @@ public class AesCryptoHelperTest {
AesCryptoHelper.encrypt(password, salt, clearTextFileS, encryptedFileS);
AesCryptoHelper.decrypt(password, salt, encryptedFileS, decryptedFileS);
String inputSha256 = StringHelper.getHexString(FileHelper.hashFileSha256(new File(clearTextFileS)));
String doutputSha256 = StringHelper.getHexString(FileHelper.hashFileSha256(new File(decryptedFileS)));
String inputSha256 = StringHelper.toHexString(FileHelper.hashFileSha256(new File(clearTextFileS)));
String doutputSha256 = StringHelper.toHexString(FileHelper.hashFileSha256(new File(decryptedFileS)));
assertEquals(inputSha256, doutputSha256);