From b0e623384b3e0fdede5b8958f5a9e9f96d2068c4 Mon Sep 17 00:00:00 2001 From: Robert von Burg Date: Tue, 11 Jul 2017 11:27:56 +0200 Subject: [PATCH] [Major] Refactored StringHelper.as* methods to .to* methods --- .../handler/DefaultEncryptionHandler.java | 2 +- .../privilege/helper/PasswordCreator.java | 4 +- .../xml/PrivilegeUsersDomWriter.java | 4 +- .../li/strolch/privilege/test/XmlTest.java | 6 +- .../li/strolch/fileserver/FileClientUtil.java | 4 +- .../li/strolch/fileserver/FileHandler.java | 4 +- .../li/strolch/utils/helper/FileHelper.java | 12 ++-- .../li/strolch/utils/helper/StringHelper.java | 70 ++++++++++++------- .../utils/helper/AesCryptoHelperTest.java | 4 +- 9 files changed, 63 insertions(+), 47 deletions(-) diff --git a/li.strolch.privilege/src/main/java/li/strolch/privilege/handler/DefaultEncryptionHandler.java b/li.strolch.privilege/src/main/java/li/strolch/privilege/handler/DefaultEncryptionHandler.java index e2846025f..adf6ade02 100644 --- a/li.strolch.privilege/src/main/java/li/strolch/privilege/handler/DefaultEncryptionHandler.java +++ b/li.strolch.privilege/src/main/java/li/strolch/privilege/handler/DefaultEncryptionHandler.java @@ -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 diff --git a/li.strolch.privilege/src/main/java/li/strolch/privilege/helper/PasswordCreator.java b/li.strolch.privilege/src/main/java/li/strolch/privilege/helper/PasswordCreator.java index 0598f38ca..fbab33506 100644 --- a/li.strolch.privilege/src/main/java/li/strolch/privilege/helper/PasswordCreator.java +++ b/li.strolch.privilege/src/main/java/li/strolch/privilege/helper/PasswordCreator.java @@ -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(); diff --git a/li.strolch.privilege/src/main/java/li/strolch/privilege/xml/PrivilegeUsersDomWriter.java b/li.strolch.privilege/src/main/java/li/strolch/privilege/xml/PrivilegeUsersDomWriter.java index 7eacc5ba9..443b86a0c 100644 --- a/li.strolch.privilege/src/main/java/li/strolch/privilege/xml/PrivilegeUsersDomWriter.java +++ b/li.strolch.privilege/src/main/java/li/strolch/privilege/xml/PrivilegeUsersDomWriter.java @@ -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())) { diff --git a/li.strolch.privilege/src/test/java/li/strolch/privilege/test/XmlTest.java b/li.strolch.privilege/src/test/java/li/strolch/privilege/test/XmlTest.java index 59e743fc9..1ad31166a 100644 --- a/li.strolch.privilege/src/test/java/li/strolch/privilege/test/XmlTest.java +++ b/li.strolch.privilege/src/test/java/li/strolch/privilege/test/XmlTest.java @@ -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()); diff --git a/li.strolch.utils/src/main/java/li/strolch/fileserver/FileClientUtil.java b/li.strolch.utils/src/main/java/li/strolch/fileserver/FileClientUtil.java index 6d4d08104..d5c304087 100644 --- a/li.strolch.utils/src/main/java/li/strolch/fileserver/FileClientUtil.java +++ b/li.strolch.utils/src/main/java/li/strolch/fileserver/FileClientUtil.java @@ -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); diff --git a/li.strolch.utils/src/main/java/li/strolch/fileserver/FileHandler.java b/li.strolch.utils/src/main/java/li/strolch/fileserver/FileHandler.java index ccd270b0d..18fd07d04 100644 --- a/li.strolch.utils/src/main/java/li/strolch/fileserver/FileHandler.java +++ b/li.strolch.utils/src/main/java/li/strolch/fileserver/FileHandler.java @@ -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); diff --git a/li.strolch.utils/src/main/java/li/strolch/utils/helper/FileHelper.java b/li.strolch.utils/src/main/java/li/strolch/utils/helper/FileHelper.java index 4613f6de8..0e8387703 100644 --- a/li.strolch.utils/src/main/java/li/strolch/utils/helper/FileHelper.java +++ b/li.strolch.utils/src/main/java/li/strolch/utils/helper/FileHelper.java @@ -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 diff --git a/li.strolch.utils/src/main/java/li/strolch/utils/helper/StringHelper.java b/li.strolch.utils/src/main/java/li/strolch/utils/helper/StringHelper.java index 89b0497d8..e6203edf3 100644 --- a/li.strolch.utils/src/main/java/li/strolch/utils/helper/StringHelper.java +++ b/li.strolch.utils/src/main/java/li/strolch/utils/helper/StringHelper.java @@ -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)); } /** diff --git a/li.strolch.utils/src/test/java/li/strolch/utils/helper/AesCryptoHelperTest.java b/li.strolch.utils/src/test/java/li/strolch/utils/helper/AesCryptoHelperTest.java index a3b01faf7..f7650f7a7 100644 --- a/li.strolch.utils/src/test/java/li/strolch/utils/helper/AesCryptoHelperTest.java +++ b/li.strolch.utils/src/test/java/li/strolch/utils/helper/AesCryptoHelperTest.java @@ -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);