Permalink
Browse files

Fixed missing leading zero for single digit hex values.

Closes #39
  • Loading branch information...
1 parent e3dc5fd commit 5c5882993dda2c62cdd2b8e9b3a43a2b445af5cd @jschuster jschuster committed with May 19, 2011
Showing with 15 additions and 11 deletions.
  1. +15 −11 GreenDroid/src/greendroid/util/Md5Util.java
@@ -4,35 +4,39 @@
import java.security.NoSuchAlgorithmException;
public class Md5Util {
-
+
private static MessageDigest sMd5MessageDigest;
private static StringBuilder sStringBuilder;
-
+
static {
try {
sMd5MessageDigest = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
- // TODO cyril: I'm quite sure about my "MD5" algorithm
+ // TODO Cyril: I'm quite sure about my "MD5" algorithm
// but this is not a correct way to handle an exception ...
}
sStringBuilder = new StringBuilder();
}
-
+
private Md5Util() {
}
-
+
public static String md5(String s) {
-
+
sMd5MessageDigest.reset();
sMd5MessageDigest.update(s.getBytes());
-
+
byte digest[] = sMd5MessageDigest.digest();
-
+
sStringBuilder.setLength(0);
- for (int i=0; i<digest.length; i++) {
- sStringBuilder.append(Integer.toHexString(0xFF & digest[i]));
+ for (int i = 0; i < digest.length; i++) {
+ final int b = digest[i] & 255;
+ if (b < 16) {
+ sStringBuilder.append('0');
+ }
+ sStringBuilder.append(Integer.toHexString(b));
}
-
+
return sStringBuilder.toString();
}
}

0 comments on commit 5c58829

Please sign in to comment.