[Major] Refactored StringHelper.as* methods to .to* methods
This commit is contained in:
parent
db8c541346
commit
b0e623384b
|
@ -77,7 +77,7 @@ public class DefaultEncryptionHandler implements EncryptionHandler {
|
||||||
public String nextToken() {
|
public String nextToken() {
|
||||||
byte[] bytes = new byte[32];
|
byte[] bytes = new byte[32];
|
||||||
this.secureRandom.nextBytes(bytes);
|
this.secureRandom.nextBytes(bytes);
|
||||||
return StringHelper.getHexString(bytes);
|
return StringHelper.toHexString(bytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -119,11 +119,11 @@ public class PasswordCreator {
|
||||||
if (saltTemp.isEmpty()) {
|
if (saltTemp.isEmpty()) {
|
||||||
saltTemp = encryptionHandler.nextToken();
|
saltTemp = encryptionHandler.nextToken();
|
||||||
}
|
}
|
||||||
String saltS = StringHelper.getHexString(saltTemp.getBytes());
|
String saltS = StringHelper.toHexString(saltTemp.getBytes());
|
||||||
byte[] salt = StringHelper.fromHexString(saltS);
|
byte[] salt = StringHelper.fromHexString(saltS);
|
||||||
|
|
||||||
byte[] passwordHash = encryptionHandler.hashPassword(password, salt);
|
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("Hash is: " + passwordHashS);
|
||||||
System.out.println("Salt is: " + saltS);
|
System.out.println("Salt is: " + saltS);
|
||||||
System.out.println();
|
System.out.println();
|
||||||
|
|
|
@ -60,9 +60,9 @@ public class PrivilegeUsersDomWriter {
|
||||||
userElement.setAttribute(XmlConstants.XML_ATTR_USER_ID, user.getUserId());
|
userElement.setAttribute(XmlConstants.XML_ATTR_USER_ID, user.getUserId());
|
||||||
userElement.setAttribute(XmlConstants.XML_ATTR_USERNAME, user.getUsername());
|
userElement.setAttribute(XmlConstants.XML_ATTR_USERNAME, user.getUsername());
|
||||||
if (user.getPassword() != null)
|
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)
|
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
|
// add first name element
|
||||||
if (StringHelper.isNotEmpty(user.getFirstname())) {
|
if (StringHelper.isNotEmpty(user.getFirstname())) {
|
||||||
|
|
|
@ -160,7 +160,7 @@ public class XmlTest {
|
||||||
PrivilegeConfigDomWriter configSaxWriter = new PrivilegeConfigDomWriter(containerModel, configFile);
|
PrivilegeConfigDomWriter configSaxWriter = new PrivilegeConfigDomWriter(containerModel, configFile);
|
||||||
configSaxWriter.write();
|
configSaxWriter.write();
|
||||||
|
|
||||||
String fileHash = StringHelper.getHexString(FileHelper.hashFileSha256(configFile));
|
String fileHash = StringHelper.toHexString(FileHelper.hashFileSha256(configFile));
|
||||||
assertEquals("800b8e42e15b6b3bb425fa9c12a011d587d2b12343a1d1371eaa36dc1b2ea5f4", fileHash);
|
assertEquals("800b8e42e15b6b3bb425fa9c12a011d587d2b12343a1d1371eaa36dc1b2ea5f4", fileHash);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -185,8 +185,8 @@ public class XmlTest {
|
||||||
assertEquals("1", admin.getUserId());
|
assertEquals("1", admin.getUserId());
|
||||||
assertEquals("admin", admin.getUsername());
|
assertEquals("admin", admin.getUsername());
|
||||||
assertEquals("cb69962946617da006a2f95776d78b49e5ec7941d2bdb2d25cdb05f957f64344",
|
assertEquals("cb69962946617da006a2f95776d78b49e5ec7941d2bdb2d25cdb05f957f64344",
|
||||||
StringHelper.getHexString(admin.getPassword()));
|
StringHelper.toHexString(admin.getPassword()));
|
||||||
assertEquals("61646d696e", StringHelper.getHexString(admin.getSalt()));
|
assertEquals("61646d696e", StringHelper.toHexString(admin.getSalt()));
|
||||||
assertEquals("Application", admin.getFirstname());
|
assertEquals("Application", admin.getFirstname());
|
||||||
assertEquals("Administrator", admin.getLastname());
|
assertEquals("Administrator", admin.getLastname());
|
||||||
assertEquals(UserState.ENABLED, admin.getUserState());
|
assertEquals(UserState.ENABLED, admin.getUserState());
|
||||||
|
|
|
@ -95,7 +95,7 @@ public class FileClientUtil {
|
||||||
}
|
}
|
||||||
|
|
||||||
// now validate hashes
|
// now validate hashes
|
||||||
String dstFileHash = StringHelper.getHexString(FileHelper.hashFileSha256(dstFile));
|
String dstFileHash = StringHelper.toHexString(FileHelper.hashFileSha256(dstFile));
|
||||||
if (!dstFileHash.equals(origFilePart.getFileHash())) {
|
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 = "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);
|
msg = MessageFormat.format(msg, origFilePart.getFileName(), origFilePart.getFileHash(), dstFileHash);
|
||||||
|
@ -123,7 +123,7 @@ public class FileClientUtil {
|
||||||
|
|
||||||
// get the size of the file
|
// get the size of the file
|
||||||
long fileLength = srcFile.length();
|
long fileLength = srcFile.length();
|
||||||
String fileHash = StringHelper.getHexString(FileHelper.hashFileSha256(srcFile));
|
String fileHash = StringHelper.toHexString(FileHelper.hashFileSha256(srcFile));
|
||||||
|
|
||||||
// create the file part to send
|
// create the file part to send
|
||||||
FilePart filePart = new FilePart(srcFile.getName(), fileType);
|
FilePart filePart = new FilePart(srcFile.getName(), fileType);
|
||||||
|
|
|
@ -104,7 +104,7 @@ public class FileHandler {
|
||||||
filePart.setFileLength(fileSize);
|
filePart.setFileLength(fileSize);
|
||||||
|
|
||||||
// set the SHA256 of the file
|
// 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
|
// 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 this is the last part, then validate the hashes
|
||||||
if (filePart.isLastPart()) {
|
if (filePart.isLastPart()) {
|
||||||
String dstFileHash = StringHelper.getHexString(FileHelper.hashFileSha256(dstFile));
|
String dstFileHash = StringHelper.toHexString(FileHelper.hashFileSha256(dstFile));
|
||||||
if (!dstFileHash.equals(filePart.getFileHash())) {
|
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$
|
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);
|
msg = MessageFormat.format(msg, filePart.getFileName(), filePart.getFileHash(), dstFileHash);
|
||||||
|
|
|
@ -300,8 +300,8 @@ public class FileHelper {
|
||||||
outBuffer.flush();
|
outBuffer.flush();
|
||||||
|
|
||||||
if (checksum) {
|
if (checksum) {
|
||||||
String fromFileMD5 = StringHelper.getHexString(FileHelper.hashFileMd5(fromFile));
|
String fromFileMD5 = StringHelper.toHexString(FileHelper.hashFileMd5(fromFile));
|
||||||
String toFileMD5 = StringHelper.getHexString(FileHelper.hashFileMd5(toFile));
|
String toFileMD5 = StringHelper.toHexString(FileHelper.hashFileMd5(toFile));
|
||||||
if (!fromFileMD5.equals(toFileMD5)) {
|
if (!fromFileMD5.equals(toFileMD5)) {
|
||||||
FileHelper.logger.error(MessageFormat.format("Copying failed, as MD5 sums are not equal: {0} / {1}", //$NON-NLS-1$
|
FileHelper.logger.error(MessageFormat.format("Copying failed, as MD5 sums are not equal: {0} / {1}", //$NON-NLS-1$
|
||||||
fromFileMD5, toFileMD5));
|
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
|
* 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
|
* @param file
|
||||||
* the file to hash
|
* 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
|
* 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
|
* @param file
|
||||||
* the file to hash
|
* 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
|
* 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
|
* @param file
|
||||||
* the file to hash
|
* 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
|
* 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
|
* @param file
|
||||||
* the file to hash
|
* the file to hash
|
||||||
|
|
|
@ -15,6 +15,7 @@
|
||||||
*/
|
*/
|
||||||
package li.strolch.utils.helper;
|
package li.strolch.utils.helper;
|
||||||
|
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
import java.security.MessageDigest;
|
import java.security.MessageDigest;
|
||||||
import java.security.NoSuchAlgorithmException;
|
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) '5', (byte) '6', (byte) '7', (byte) '8', (byte) '9', (byte) 'a', (byte) 'b', (byte) 'c', (byte) 'd',
|
||||||
(byte) 'e', (byte) 'f' };
|
(byte) 'e', (byte) 'f' };
|
||||||
|
|
||||||
public static String asHexString(byte data) {
|
public static String toHexString(byte data) {
|
||||||
return String.format("%02x", 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 {
|
try {
|
||||||
byte[] hex = new byte[3 * raw.length + (raw.length / 8)];
|
byte[] hex = new byte[3 * raw.length + (raw.length / 8)];
|
||||||
int index = 0;
|
int index = 0;
|
||||||
|
@ -85,19 +90,15 @@ public class StringHelper {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public static byte[] fromPrettyHexString(String prettyHex) {
|
||||||
* Converts each byte of the given byte array to a HEX value and returns the concatenation of these values
|
|
||||||
*
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||||
* @param raw
|
String[] split = prettyHex.split(" ");
|
||||||
* the bytes to convert to String using numbers in hexadecimal
|
for (String encoded : split) {
|
||||||
*
|
out.write(fromHexByte(encoded));
|
||||||
* @return the encoded string
|
}
|
||||||
*
|
|
||||||
* @throws RuntimeException
|
return out.toByteArray();
|
||||||
* if {@link UnsupportedEncodingException} is thrown
|
|
||||||
*/
|
|
||||||
public static String getHexString(byte[] raw) throws RuntimeException {
|
|
||||||
return getHexString(raw, 0, raw.length);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -111,7 +112,22 @@ public class StringHelper {
|
||||||
* @throws RuntimeException
|
* @throws RuntimeException
|
||||||
* if {@link UnsupportedEncodingException} is thrown
|
* 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 {
|
try {
|
||||||
byte[] hex = new byte[2 * length];
|
byte[] hex = new byte[2 * length];
|
||||||
int index = 0;
|
int index = 0;
|
||||||
|
@ -165,11 +181,11 @@ public class StringHelper {
|
||||||
* @return the hash or null, if an exception was thrown
|
* @return the hash or null, if an exception was thrown
|
||||||
*/
|
*/
|
||||||
public static String hashMd5AsHex(String string) {
|
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
|
* Hex String which is printable
|
||||||
*
|
*
|
||||||
* @param string
|
* @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
|
* a Hex String which is printable
|
||||||
*
|
*
|
||||||
* @param bytes
|
* @param bytes
|
||||||
|
@ -203,11 +219,11 @@ public class StringHelper {
|
||||||
* @return the hash or null, if an exception was thrown
|
* @return the hash or null, if an exception was thrown
|
||||||
*/
|
*/
|
||||||
public static String hashSha1AsHex(String string) {
|
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
|
* Hex String which is printable
|
||||||
*
|
*
|
||||||
* @param string
|
* @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
|
* to a Hex String which is printable
|
||||||
*
|
*
|
||||||
* @param bytes
|
* @param bytes
|
||||||
|
@ -241,11 +257,11 @@ public class StringHelper {
|
||||||
* @return the hash or null, if an exception was thrown
|
* @return the hash or null, if an exception was thrown
|
||||||
*/
|
*/
|
||||||
public static String hashSha256AsHex(String string) {
|
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
|
* a Hex String which is printable
|
||||||
*
|
*
|
||||||
* @param string
|
* @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
|
* to a Hex String which is printable
|
||||||
*
|
*
|
||||||
* @param bytes
|
* @param bytes
|
||||||
|
@ -281,7 +297,7 @@ public class StringHelper {
|
||||||
* @return the hash or null, if an exception was thrown
|
* @return the hash or null, if an exception was thrown
|
||||||
*/
|
*/
|
||||||
public static String hashAsHex(String algorithm, String string) {
|
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
|
* @return the hash or null, if an exception was thrown
|
||||||
*/
|
*/
|
||||||
public static String hashAsHex(String algorithm, byte[] bytes) {
|
public static String hashAsHex(String algorithm, byte[] bytes) {
|
||||||
return getHexString(hash(algorithm, bytes));
|
return toHexString(hash(algorithm, bytes));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -146,8 +146,8 @@ public class AesCryptoHelperTest {
|
||||||
AesCryptoHelper.encrypt(password, salt, clearTextFileS, encryptedFileS);
|
AesCryptoHelper.encrypt(password, salt, clearTextFileS, encryptedFileS);
|
||||||
AesCryptoHelper.decrypt(password, salt, encryptedFileS, decryptedFileS);
|
AesCryptoHelper.decrypt(password, salt, encryptedFileS, decryptedFileS);
|
||||||
|
|
||||||
String inputSha256 = StringHelper.getHexString(FileHelper.hashFileSha256(new File(clearTextFileS)));
|
String inputSha256 = StringHelper.toHexString(FileHelper.hashFileSha256(new File(clearTextFileS)));
|
||||||
String doutputSha256 = StringHelper.getHexString(FileHelper.hashFileSha256(new File(decryptedFileS)));
|
String doutputSha256 = StringHelper.toHexString(FileHelper.hashFileSha256(new File(decryptedFileS)));
|
||||||
|
|
||||||
assertEquals(inputSha256, doutputSha256);
|
assertEquals(inputSha256, doutputSha256);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue