From e5872299145daa3cfd752872e1540b38667db658 Mon Sep 17 00:00:00 2001 From: Robert von Burg Date: Sun, 10 Mar 2013 23:58:06 +0100 Subject: [PATCH] [New] added new StringHelper.hash*AsHex() methods as a nice API --- .../eitchnet/utils/helper/StringHelper.java | 42 +++++++++++++++++-- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/src/main/java/ch/eitchnet/utils/helper/StringHelper.java b/src/main/java/ch/eitchnet/utils/helper/StringHelper.java index bbd6def33..e345c7157 100644 --- a/src/main/java/ch/eitchnet/utils/helper/StringHelper.java +++ b/src/main/java/ch/eitchnet/utils/helper/StringHelper.java @@ -39,8 +39,8 @@ public class StringHelper { /** * Hex char table for fast calculating of hex value */ - private static final byte[] HEX_CHAR_TABLE = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', - 'f' }; + private static final byte[] HEX_CHAR_TABLE = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', + 'd', 'e', 'f' }; /** * Converts each byte of the given byte array to a HEX value and returns the concatenation of these values @@ -94,7 +94,19 @@ public class StringHelper { } /** - * 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 and converts it to a HEX string + * + * @param string + * the string to hash + * + * @return the hash or null, if an exception was thrown + */ + public static String hashMd5AsHex(String string) { + return getHexString(StringHelper.hashMd5(string.getBytes())); + } + + /** + * Generates the MD5 Hash of a string. Use {@link StringHelper#getHexString(byte[])} to convert the byte array to a * Hex String which is printable * * @param string @@ -119,6 +131,18 @@ public class StringHelper { return StringHelper.hash("MD5", bytes); } + /** + * Generates the SHA1 Hash of a string and converts it to a HEX String + * + * @param string + * the string to hash + * + * @return the hash or null, if an exception was thrown + */ + public static String hashSha1AsHex(String string) { + return getHexString(StringHelper.hashSha1(string.getBytes())); + } + /** * Generates the SHA1 Hash of a string Use {@link StringHelper#getHexString(byte[])} to convert the byte array to a * Hex String which is printable @@ -145,6 +169,18 @@ public class StringHelper { return StringHelper.hash("SHA-1", bytes); } + /** + * Generates the SHA-256 Hash of a string and converts it to a HEX String + * + * @param string + * the string to hash + * + * @return the hash or null, if an exception was thrown + */ + public static String hashSha256AsHex(String string) { + return getHexString(StringHelper.hashSha256(string.getBytes())); + } + /** * Generates the SHA-256 Hash of a string Use {@link StringHelper#getHexString(byte[])} to convert the byte array to * a Hex String which is printable