Last active
April 3, 2016 18:39
-
-
Save shangoyanyi/0c66210d64ebef7b6bbb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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