MD5工具类

转载地址:http://www.oschina.net/code/snippet_1031745_36882

md5的作用 就是检验文件有没有被别人修改类似的。主要用于验证信息。

?
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
48
49
50
51
52
53
54
55
56
57
58
59
60
import java.security.MessageDigest;
 
public class Md5 {
     Log log;
 
     public static String getMd5(String s) {
         char hexDigits[] = { '0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , 'a' , 'b' , 'c' , 'd' , 'e' , 'f' };
 
         try {
             byte [] strTemp = s.getBytes();
             MessageDigest mdTemp = MessageDigest.getInstance( "md5" );
             mdTemp.update(strTemp);
             byte [] md = mdTemp.digest();
             int j = md.length;
             char str[] = new char [j * 2 ];
             int k = 0 ;
             for ( int i = 0 ; i < j; i++) {
                 byte byte0 = md[i];
                 str[k++] = hexDigits[byte0 >>> 4 & 0xf ];
                 str[k++] = hexDigits[byte0 & 0xf ];
             }
             return new String(str);
         } catch (Exception e) {
             return null ;
         }
     }
 
     /**
      * 标准MD5加密
      *
      * @param inStr
      * @return
      * @throws Exception
      */
     public static String toMD5(String inStr) {
         StringBuffer sb = new StringBuffer();
         try {
             MessageDigest md = MessageDigest.getInstance( "MD5" );
             md.update(inStr.getBytes());
             byte b[] = md.digest();
             int i;
             for ( int offset = 0 ; offset < b.length; offset++) {
                 i = b[offset];
                 if (i < 0 )
                     i += 256 ;
                 if (i < 16 )
                     sb.append( "0" );
                 sb.append(Integer.toHexString(i));
             }
         } catch (Exception e) {
             return null ;
             //          e.printStackTrace();
         }
         return sb.toString().toUpperCase();
     }
 
     public static void main(String[] args) throws Exception {
         System.out.println(toMD5( "MATICSOFT" ).toUpperCase());
     }
}

你可能感兴趣的:(MD5工具类)