Skip to content
Snippets Groups Projects
Commit 4e1d780f authored by felmer's avatar felmer
Browse files

add encrypt() with test

SVN: 3705
parent de0ec5fc
No related branches found
No related tags found
No related merge requests found
......@@ -16,12 +16,15 @@
package ch.systemsx.cisd.common.utilities;
import java.security.MessageDigest;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang.StringUtils;
import ch.systemsx.cisd.common.exceptions.CheckedExceptionTunnel;
/**
* Some useful utlities methods for {@link String}s.
* <p>
......@@ -68,6 +71,36 @@ public final class StringUtilities
{
// This class can not be instantiated.
}
/**
* Encrypts the specified string by calculating its MD5 hash value.
*
* @return 32-character hexadecimal representation of the calculated value.
*/
public static String encrypt(String string)
{
assert string != null : "Unspecified string.";
try
{
MessageDigest algorithm = MessageDigest.getInstance("MD5");
algorithm.reset();
algorithm.update(string.getBytes("utf8"));
byte messageDigest[] = algorithm.digest();
StringBuilder builder = new StringBuilder();
for (int i = 0; i < messageDigest.length; i++)
{
byte b = messageDigest[i];
builder.append(Integer.toHexString(0xF & (b >> 4)));
builder.append(Integer.toHexString(0xF & b));
}
return builder.toString();
} catch (Exception ex)
{
throw CheckedExceptionTunnel.wrapIfNecessary(ex);
}
}
/**
* <em>Note that this method will transform <code>cAptcha</code> into <code>Captcha</code>, while
......
......@@ -57,6 +57,13 @@ public class StringUtilitiesTest
final String brokenDownInLines = String.format("one%1$stwo%1$sthree", System.getProperty("line.separator"));
assertEquals(brokenDownInLines, StringUtilities.concatenateWithNewLine(list));
}
@Test
public void testEncrypt()
{
assertEquals("d41d8cd98f00b204e9800998ecf8427e", StringUtilities.encrypt(""));
assertEquals("900150983cd24fb0d6963f7d28e17f72", StringUtilities.encrypt("abc"));
}
@Test
public final void testGetOrdinal()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment