转载自:http://www.cnblogs.com/dongliyang/archive/2012/12/28/2837368.html
信息摘要,通俗来说,就是信息的指纹。就像人类的指纹是不会重复的一样,使用信息摘要算法计算任何两个不同信息,得出的摘要(指纹)也是不一样的。
这通常用在校验文件是否被修改过,在Web项目中MD5也用在密码加密上。
下面的信息摘要工具类MsgDigest,可以计算文件的MD5、CRC32、SHA1摘要,字符串的MD5、SHA1摘要。
MsgDigest类:
1 package com.dong.framework.tool; 2 3 import java.io.BufferedInputStream; 4 import java.io.File; 5 import java.io.FileInputStream; 6 import java.io.FileNotFoundException; 7 import java.io.IOException; 8 import java.io.InputStream; 9 import java.security.MessageDigest; 10 import java.security.NoSuchAlgorithmException; 11 import java.util.zip.CRC32; 12 13 14 /** 15 * 信息摘要,可以获得CRC32、MD5、SHA-1值 16 * @version 1.0 17 * @author dongliyang 18 */ 19 public class MsgDigest { 20 21 /** 22 * 计算文件的32位循环冗余校验和CRC32 23 * @param filepath 文件名 24 * @return String 25 * @throws IOException IOException异常 26 */ 27 public static String crc32Digest(String filepath) { 28 29 try { 30 31 InputStream in = new BufferedInputStream(new FileInputStream(filepath)); 32 CRC32 crc = new CRC32(); 33 34 int c; 35 while((c = in.read()) != -1){ 36 crc.update(c); 37 } 38 in.close(); 39 return Long.toHexString(crc.getValue()); 40 } catch (FileNotFoundException e) { 41 System.out.println("文件未找到异常!"); 42 throw new RuntimeException(e.getMessage()); 43 } catch (IOException e) { 44 System.out.println("IO异常!"); 45 throw new RuntimeException(e.getMessage()); 46 } 47 48 } 49 50 /** 51 * 计算文件的32位循环冗余校验和CRC32 52 * @param file 文件 53 * @return String 54 * @throws IOException IOException异常 55 */ 56 public static String crc32Digest(File file){ 57 58 try { 59 60 InputStream in = new BufferedInputStream(new FileInputStream(file)); 61 CRC32 crc = new CRC32(); 62 63 int c; 64 while((c = in.read()) != -1){ 65 crc.update(c); 66 } 67 in.close(); 68 return Long.toHexString(crc.getValue()); 69 } catch (FileNotFoundException e) { 70 System.out.println("文件未找到异常!"); 71 throw new RuntimeException(e.getMessage()); 72 } catch (IOException e) { 73 System.out.println("IO异常!"); 74 throw new RuntimeException(e.getMessage()); 75 } 76 77 78 } 79 80 /** 81 * 计算字符串的MD5值 82 * @param input 字符串 83 * @return String 84 */ 85 public static String md5Digest(String input){ 86 87 byte[] data = input.getBytes(); 88 try { 89 MessageDigest messageDigest = getMD5(); 90 messageDigest.update(data); 91 return toHexString(messageDigest.digest()); 92 } catch (NoSuchAlgorithmException e) { 93 System.out.println("MD5算法初始化失败!"); 94 throw new RuntimeException(e.getMessage()); 95 } 96 97 } 98 99 /** 100 * 计算文件的MD5值 101 * @param filename 文件名 102 * @return String 103 */ 104 public static String md5FileDigest(String filepath){ 105 String md5 = ""; 106 File file = new File(filepath); 107 if(file.exists()){ 108 try { 109 MessageDigest messageDigest = getMD5(); 110 InputStream in = new BufferedInputStream(new FileInputStream(file)); 111 byte[] cache = new byte[CACHE_SIZE]; 112 int len; 113 while((len = in.read(cache)) != -1){ 114 messageDigest.update(cache, 0, len); 115 } 116 in.close(); 117 byte[] data = messageDigest.digest(); 118 md5 = toHexString(data); 119 } catch (NoSuchAlgorithmException e) { 120 System.out.println("MD5算法初始化失败!"); 121 throw new RuntimeException(e.getMessage()); 122 } catch (FileNotFoundException e) { 123 System.out.println("文件未找到异常!"); 124 throw new RuntimeException(e.getMessage()); 125 } catch (IOException e) { 126 System.out.println("IO异常!"); 127 throw new RuntimeException(e.getMessage()); 128 } 129 } 130 return md5; 131 } 132 133 /** 134 * 计算文件的MD5值 135 * @param file 文件 136 * @return String 137 */ 138 public static String md5FileDigest(File file){ 139 140 String md5 = ""; 141 if(file.exists()){ 142 try { 143 MessageDigest messageDigest = getMD5(); 144 InputStream in = new BufferedInputStream(new FileInputStream(file)); 145 byte[] cache = new byte[CACHE_SIZE]; 146 int len; 147 while((len = in.read(cache)) != -1){ 148 messageDigest.update(cache, 0, len); 149 } 150 in.close(); 151 byte[] data = messageDigest.digest(); 152 md5 = toHexString(data); 153 } catch (NoSuchAlgorithmException e) { 154 System.out.println("MD5算法初始化失败!"); 155 throw new RuntimeException(e.getMessage()); 156 } catch (FileNotFoundException e) { 157 System.out.println("文件未找到异常!"); 158 throw new RuntimeException(e.getMessage()); 159 } catch (IOException e) { 160 System.out.println("IO异常!"); 161 throw new RuntimeException(e.getMessage()); 162 } 163 } 164 return md5; 165 } 166 167 /** 168 * 计算字符串的SHA-1值 169 * @param input 字符串 170 * @return String 171 */ 172 public static String sha1Digest(String input){ 173 174 byte[] data = input.getBytes(); 175 try { 176 MessageDigest messageDigest = getSHA1(); 177 messageDigest.update(data); 178 return toHexString(messageDigest.digest()); 179 } catch (NoSuchAlgorithmException e) { 180 System.out.println("MD5算法初始化失败!"); 181 throw new RuntimeException(e.getMessage()); 182 } 183 } 184 185 /** 186 * 计算文件的SHA-1值 187 * @param filepath 文件名 188 * @return String 189 */ 190 public static String sha1FileDigest(String filepath){ 191 192 String sha1 = ""; 193 File file = new File(filepath); 194 if(file.exists()){ 195 try { 196 MessageDigest messageDigest = getSHA1(); 197 InputStream in = new BufferedInputStream(new FileInputStream(file)); 198 byte[] cache = new byte[CACHE_SIZE]; 199 int len; 200 while((len = in.read(cache)) != -1){ 201 messageDigest.update(cache, 0, len); 202 } 203 in.close(); 204 byte[] data = messageDigest.digest(); 205 sha1 = toHexString(data); 206 } catch (NoSuchAlgorithmException e) { 207 System.out.println("MD5算法初始化失败!"); 208 throw new RuntimeException(e.getMessage()); 209 } catch (FileNotFoundException e) { 210 System.out.println("文件未找到异常!"); 211 throw new RuntimeException(e.getMessage()); 212 } catch (IOException e) { 213 System.out.println("IO异常!"); 214 throw new RuntimeException(e.getMessage()); 215 } 216 } 217 return sha1; 218 } 219 220 /** 221 * 计算文件的SHA-1值 222 * @param file 文件 223 * @return String 224 */ 225 public static String sha1FileDigest(File file){ 226 227 String sha1 = ""; 228 if(file.exists()){ 229 try { 230 MessageDigest messageDigest = getSHA1(); 231 InputStream in = new BufferedInputStream(new FileInputStream(file)); 232 byte[] cache = new byte[CACHE_SIZE]; 233 int len; 234 while((len = in.read(cache)) != -1){ 235 messageDigest.update(cache, 0, len); 236 } 237 in.close(); 238 byte[] data = messageDigest.digest(); 239 sha1 = toHexString(data); 240 } catch (NoSuchAlgorithmException e) { 241 System.out.println("MD5算法初始化失败!"); 242 throw new RuntimeException(e.getMessage()); 243 } catch (FileNotFoundException e) { 244 System.out.println("文件未找到异常!"); 245 throw new RuntimeException(e.getMessage()); 246 } catch (IOException e) { 247 System.out.println("IO异常!"); 248 throw new RuntimeException(e.getMessage()); 249 } 250 } 251 return sha1; 252 253 } 254 /** 255 * 字节数组转换为16进制字符串 256 * @param data 字节数组 257 * @return String 258 */ 259 private static String toHexString(byte[] data){ 260 StringBuilder digestStr = new StringBuilder(); 261 String stmp = ""; 262 for(int i = 0;i < data.length; i++){ 263 stmp = Integer.toHexString(data[i] & 0XFF); 264 if(stmp.length() == 1){ 265 digestStr.append("0" + stmp); 266 }else { 267 digestStr.append(stmp); 268 } 269 } 270 return digestStr.toString(); 271 } 272 273 /** 274 * 获取MD5实例 275 * @return MessageDigest 276 * @throws NoSuchAlgorithmException 异常 277 */ 278 private static MessageDigest getMD5() throws NoSuchAlgorithmException{ 279 return MessageDigest.getInstance(ALGORIGTHM_MD5); 280 } 281 282 private static MessageDigest getSHA1() throws NoSuchAlgorithmException { 283 return MessageDigest.getInstance(ALGORIGTHM_SHA1); 284 } 285 286 /** MD5算法名称 */ 287 private static final String ALGORIGTHM_MD5 = "MD5"; 288 /** SHA-1算法名称 */ 289 private static final String ALGORIGTHM_SHA1 ="SHA-1"; 290 /** 字节数组缓存大小 */ 291 private static final int CACHE_SIZE = 2048; 292 }