信息摘要工具类-MD5、CRC32、SHA1
2012-12-28 15:12
357 查看
信息摘要,通俗来说,就是信息的指纹。就像人类的指纹是不会重复的一样,使用信息摘要算法计算任何两个不同信息,得出的摘要(指纹)也是不一样的。
这通常用在校验文件是否被修改过,在Web项目中MD5也用在密码加密上。
下面的信息摘要工具类MsgDigest,可以计算文件的MD5、CRC32、SHA1摘要,字符串的MD5、SHA1摘要。
MsgDigest类:
这通常用在校验文件是否被修改过,在Web项目中MD5也用在密码加密上。
下面的信息摘要工具类MsgDigest,可以计算文件的MD5、CRC32、SHA1摘要,字符串的MD5、SHA1摘要。
MsgDigest类:
package com.dong.framework.tool; import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.zip.CRC32; /** * 信息摘要,可以获得CRC32、MD5、SHA-1值 * @version 1.0 * @author dongliyang */ public class MsgDigest { /** * 计算文件的32位循环冗余校验和CRC32 * @param filepath 文件名 * @return String * @throws IOException IOException异常 */ public static String crc32Digest(String filepath) { try { InputStream in = new BufferedInputStream(new FileInputStream(filepath)); CRC32 crc = new CRC32(); int c; while((c = in.read()) != -1){ crc.update(c); } in.close(); return Long.toHexString(crc.getValue()); } catch (FileNotFoundException e) { System.out.println("文件未找到异常!"); throw new RuntimeException(e.getMessage()); } catch (IOException e) { System.out.println("IO异常!"); throw new RuntimeException(e.getMessage()); } } /** * 计算文件的32位循环冗余校验和CRC32 * @param file 文件 * @return String * @throws IOException IOException异常 */ public static String crc32Digest(File file){ try { InputStream in = new BufferedInputStream(new FileInputStream(file)); CRC32 crc = new CRC32(); int c; while((c = in.read()) != -1){ crc.update(c); } in.close(); return Long.toHexString(crc.getValue()); } catch (FileNotFoundException e) { System.out.println("文件未找到异常!"); throw new RuntimeException(e.getMessage()); } catch (IOException e) { System.out.println("IO异常!"); throw new RuntimeException(e.getMessage()); } } /** * 计算字符串的MD5值 * @param input 字符串 * @return String */ public static String md5Digest(String input){ byte[] data = input.getBytes(); try { MessageDigest messageDigest = getMD5(); messageDigest.update(data); return toHexString(messageDigest.digest()); } catch (NoSuchAlgorithmException e) { System.out.println("MD5算法初始化失败!"); throw new RuntimeException(e.getMessage()); } } /** * 计算文件的MD5值 * @param filename 文件名 * @return String */ public static String md5FileDigest(String filepath){ String md5 = ""; File file = new File(filepath); if(file.exists()){ try { MessageDigest messageDigest = getMD5(); InputStream in = new BufferedInputStream(new FileInputStream(file)); byte[] cache = new byte[CACHE_SIZE]; int len; while((len = in.read(cache)) != -1){ messageDigest.update(cache, 0, len); } in.close(); byte[] data = messageDigest.digest(); md5 = toHexString(data); } catch (NoSuchAlgorithmException e) { System.out.println("MD5算法初始化失败!"); throw new RuntimeException(e.getMessage()); } catch (FileNotFoundException e) { System.out.println("文件未找到异常!"); throw new RuntimeException(e.getMessage()); } catch (IOException e) { System.out.println("IO异常!"); throw new RuntimeException(e.getMessage()); } } return md5; } /** * 计算文件的MD5值 * @param file 文件 * @return String */ public static String md5FileDigest(File file){ String md5 = ""; if(file.exists()){ try { MessageDigest messageDigest = getMD5(); InputStream in = new BufferedInputStream(new FileInputStream(file)); byte[] cache = new byte[CACHE_SIZE]; int len; while((len = in.read(cache)) != -1){ messageDigest.update(cache, 0, len); } in.close(); byte[] data = messageDigest.digest(); md5 = toHexString(data); } catch (NoSuchAlgorithmException e) { System.out.println("MD5算法初始化失败!"); throw new RuntimeException(e.getMessage()); } catch (FileNotFoundException e) { System.out.println("文件未找到异常!"); throw new RuntimeException(e.getMessage()); } catch (IOException e) { System.out.println("IO异常!"); throw new RuntimeException(e.getMessage()); } } return md5; } /** * 计算字符串的SHA-1值 * @param input 字符串 * @return String */ public static String sha1Digest(String input){ byte[] data = input.getBytes(); try { MessageDigest messageDigest = getSHA1(); messageDigest.update(data); return toHexString(messageDigest.digest()); } catch (NoSuchAlgorithmException e) { System.out.println("MD5算法初始化失败!"); throw new RuntimeException(e.getMessage()); } } /** * 计算文件的SHA-1值 * @param filepath 文件名 * @return String */ public static String sha1FileDigest(String filepath){ String sha1 = ""; File file = new File(filepath); if(file.exists()){ try { MessageDigest messageDigest = getSHA1(); InputStream in = new BufferedInputStream(new FileInputStream(file)); byte[] cache = new byte[CACHE_SIZE]; int len; while((len = in.read(cache)) != -1){ messageDigest.update(cache, 0, len); } in.close(); byte[] data = messageDigest.digest(); sha1 = toHexString(data); } catch (NoSuchAlgorithmException e) { System.out.println("MD5算法初始化失败!"); throw new RuntimeException(e.getMessage()); } catch (FileNotFoundException e) { System.out.println("文件未找到异常!"); throw new RuntimeException(e.getMessage()); } catch (IOException e) { System.out.println("IO异常!"); throw new RuntimeException(e.getMessage()); } } return sha1; } /** * 计算文件的SHA-1值 * @param file 文件 * @return String */ public static String sha1FileDigest(File file){ String sha1 = ""; if(file.exists()){ try { MessageDigest messageDigest = getSHA1(); InputStream in = new BufferedInputStream(new FileInputStream(file)); byte[] cache = new byte[CACHE_SIZE]; int len; while((len = in.read(cache)) != -1){ messageDigest.update(cache, 0, len); } in.close(); byte[] data = messageDigest.digest(); sha1 = toHexString(data); } catch (NoSuchAlgorithmException e) { System.out.println("MD5算法初始化失败!"); throw new RuntimeException(e.getMessage()); } catch (FileNotFoundException e) { System.out.println("文件未找到异常!"); throw new RuntimeException(e.getMessage()); } catch (IOException e) { System.out.println("IO异常!"); throw new RuntimeException(e.getMessage()); } } return sha1; } /** * 字节数组转换为16进制字符串 * @param data 字节数组 * @return String */ private static String toHexString(byte[] data){ StringBuilder digestStr = new StringBuilder(); String stmp = ""; for(int i = 0;i < data.length; i++){ stmp = Integer.toHexString(data[i] & 0XFF); if(stmp.length() == 1){ digestStr.append("0" + stmp); }else { digestStr.append(stmp); } } return digestStr.toString(); } /** * 获取MD5实例 * @return MessageDigest * @throws NoSuchAlgorithmException 异常 */ private static MessageDigest getMD5() throws NoSuchAlgorithmException{ return MessageDigest.getInstance(ALGORIGTHM_MD5); } private static MessageDigest getSHA1() throws NoSuchAlgorithmException { return MessageDigest.getInstance(ALGORIGTHM_SHA1); } /** MD5算法名称 */ private static final String ALGORIGTHM_MD5 = "MD5"; /** SHA-1算法名称 */ private static final String ALGORIGTHM_SHA1 ="SHA-1"; /** 字节数组缓存大小 */ private static final int CACHE_SIZE = 2048; }
相关文章推荐
- 信息摘要工具类-MD5、CRC32、SHA1
- 摘要算法CRC8、CRC16、CRC32,MD2 、MD4、MD5,SHA1、SHA256、SHA384、SHA512,RIPEMD、PANAMA、TIGER、ADLER32
- 摘要算法CRC8、CRC16、CRC32,MD2 、MD4、MD5,SHA1、SHA256、SHA384、SHA512,RIPEMD、PANAMA、TIGER、ADLER32
- 常用编码工具类,支持base64,md5,des,crc32
- 小巧好用的哈希计算器,支持文件拖放,速度很快,可以计算文件的 MD5、SHA1、CRC32 的值。
- 校验知识:CRC32、MD5、SHA1概念及可靠性现状
- 对指定文件生成数字摘要的MD5工具类
- Powershell 下计算SHA1 以及MD5 摘要[转]
- 常用编码工具类,支持base64,md5,des,crc32
- android keystore 获取md5 sha1 等详细信息
- 网络安全-安全散列函数,信息摘要SHA-1,MD5原理
- 用python计算md5,sha1,crc32
- 【入门】Java信息摘要(MD5与SHA)
- 通过签名查看项目SHA1、MD5、SHA256以及相关证书信息
- android studio签名文件在哪?以及查看MD5和SHA1信息
- JAVA 生成 MD5摘要 和SHA1摘要 及MD5的彩虹表破解
- 常用编码工具类,支持base64,md5,des,crc32
- MD5(信息摘要)在非法图片验证中的应用
- Windows下计算文件摘要信息MD5、SHA-1、SHA-256
- Hasher 1.0 Final, Supported Hashes/Checksums: MD5, CRC32, SHA1, ELF, SUM64