Java计算文件的SHA值、字符串的SHA值和CRC32值

1、Java计算文件的SHA值

/** hash类型 */
    public static final String HASH_TYPE_MD5    = "MD5";
    public static final String HASH_TYPE_SHA1   = "SHA-1";
    public static final String HASH_TYPE_SHA256 = "SHA-256";
    public static final String HASH_TYPE_SHA384 = "SHA-384";
    public static final String HASH_TYPE_SHA512 = "SHA-512";

/**
     * 计算文件的Hash值
     * 
     * @param file
     * @return hash:md5
     * @throws FileNotFoundException
     */
    public static String getHashByFile(File file) throws FileNotFoundException {
        return getHashByFile(file, HASH_TYPE_MD5);
    }

/**
     * 计算文件的Hash值
     * 
     * @param file
     * @param hashType
     * @return hash:hashType
     * @throws FileNotFoundException
     */
    public static String getHashByFile(File file, String hashType) throws FileNotFoundException {
        String value = null;
        FileInputStream fis = new FileInputStream(file);
        try {
            MappedByteBuffer byteBuffer = fis.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, file.length());
            MessageDigest digest = MessageDigest.getInstance(hashType);
            digest.update(byteBuffer);
            BigInteger bigInteger = new BigInteger(1, digest.digest());
            value = bigInteger.toString(16);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != fis) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return value;
    }

 

2、Java计算字符串的SHA值

/** hash类型 */
    public static final String HASH_TYPE_MD5    = "MD5";
    public static final String HASH_TYPE_SHA1   = "SHA-1";
    public static final String HASH_TYPE_SHA256 = "SHA-256";
    public static final String HASH_TYPE_SHA384 = "SHA-384";
    public static final String HASH_TYPE_SHA512 = "SHA-512";

/**
     * 计算字符串的SHA-1值
     * @param value
     * @return
     */
    public static String hexSHA1(String value) {
        return hexSHA1(value, HASH_TYPE_SHA1);
    }

/**
     * 计算字符串的SHA值,可指定hash算法
     * @param value
     * @param hashType HASH_TYPE_
     * @return
     */
    public static String hexSHA1(String value, String hashType) {
        try {
            MessageDigest md = MessageDigest.getInstance(hashType);
            md.update(value.getBytes("utf-8"));
            byte[] digest = md.digest();
            return byteToHexString(digest);
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

/**
     * 计算Hex值
     * @param bytes
     * @return
     */
    public static String byteToHexString(byte[] bytes) {
        return String.valueOf(Hex.encodeHex(bytes));
    }

 

3、Java计算文件和字符串的CRC32值

/**
     * 字符串CRC32校验
     * @param value
     * @return
     */
    public static String getCRC32(String value) {
        CRC32 crc32 = new CRC32();
        crc32.update(value.getBytes());
        return String.valueOf(crc32.getValue());
    }

/**
     * 校验文件的CRC32值
     * @param file
     * @return
     */
    public static String getCRC32ByFile(File file) {
        CRC32 crc32 = new CRC32();
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(file);
            byte[] buffer = new byte[8192];
            int length;
            while ((length = fileInputStream.read(buffer)) != -1) {
                crc32.update(buffer, 0, length);
            }
            return String.valueOf(crc32.getValue());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } finally {
            try {
                if (fileInputStream != null)
                    fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值