Skip to content

Instantly share code, notes, and snippets.

@binjoo
Created October 21, 2012 08:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save binjoo/3926300 to your computer and use it in GitHub Desktop.
Save binjoo/3926300 to your computer and use it in GitHub Desktop.
JAVA:MD5加密
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class Md5 {
public static String toMd5(String arg) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(arg.getBytes());
byte b[] = md.digest();
int i;
StringBuffer buf = new StringBuffer("");
for (int offset = 0; offset < b.length; offset++) {
i = b[offset];
if (i < 0)
i += 256;
if (i < 16)
buf.append("0");
buf.append(Integer.toHexString(i));
}
return buf.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
System.out.println(Md5.toMd5("123"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment