-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMd5Instance.java
executable file
·47 lines (42 loc) · 1.48 KB
/
Md5Instance.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package md5_string;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class Md5Instance {
public static void main(String[] args) throws NoSuchAlgorithmException{
System.out.println("begin");
String source=new String("yfgbcfkfpjqrfvytgbcmvj");
printBytes(getMd5(source.getBytes()));
// printBytes2(getMd5(source.getBytes()));
System.out.println("-end-");
}
private static void printBytes(byte[] message){
StringBuffer hexString = new StringBuffer();
for (int i=0;i<message.length;i++) {
// hexString.append(Integer.toHexString(0xFF & message[i]));
hexString.append(Integer.toString((message[i] & 0xff) + 0x100, 16).substring(1));
}
System.out.println(" md5: "+hexString.toString());
}
/*
private static void printBytes2(byte[] message){
StringBuffer hexString = new StringBuffer();
for (int i=0;i<message.length;i++) {
hexString.append(Integer.toHexString(0xFF & message[i]));
// hexString.append(Integer.toString((message[i] & 0xff) + 0x100, 16).substring(1));
}
System.out.println(" md5: "+hexString.toString());
}
*/
/** ïîëó÷èòü MD5 êîä íà îñíîâàíèè çàäàííîãî çíà÷åíèÿ */
private static byte[] getMd5(byte[] source) {
try{
MessageDigest algorithm = MessageDigest.getInstance("MD5");
algorithm.reset();
algorithm.update(source);
return algorithm.digest();
}catch(Exception ex){
System.err.println("Exception: "+ex.getMessage());
return null;
}
}
}