Skip to content

Instantly share code, notes, and snippets.

@shangoyanyi
Last active April 3, 2016 18:39
Show Gist options
  • Save shangoyanyi/0c66210d64ebef7b6bbb to your computer and use it in GitHub Desktop.
Save shangoyanyi/0c66210d64ebef7b6bbb to your computer and use it in GitHub Desktop.
//MD5
import java.security.MessageDigest;
public class MD5 {
public static String hash(String plainText) {
MessageDigest cry = null;
try {
cry = MessageDigest.getInstance("MD5");
} catch (Exception e) {
}
cry.reset(); // Resets the digest for further use.
return stringify(cry.digest(plainText.getBytes()));
}
/**
* 計算MD5
* @param buf
* @return
*/
private static String stringify(byte buf[]) {
StringBuffer sb = new StringBuffer(2 * buf.length);
for (int i = 0; i < buf.length; i++) {
int h = (buf[i] & 0xf0) >> 4;
int l = (buf[i] & 0x0f);
sb.append(new Character((char) ((h > 9) ? 'a' + h - 10 : '0' + h)));
sb.append(new Character((char) ((l > 9) ? 'a' + l - 10 : '0' + l)));
}
return sb.toString();
}
}
public static void main(String[] args){
String plainText;
String chsm = MD5.hash(plainText);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment