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();
            }
        }
    }

https://blog.csdn.net/jinqilin721/article/details/94429400

Java  后端  SHA  CRC32 
更新时间:2020-05-21 19:37:01

本文由 新逸Cary 创作,如果您觉得本文不错,请随意赞赏
采用 知识共享署名4.0 国际许可协议进行许可
本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名
原文链接:https://blog.xinac.cn/archives/java计算文件的sha值字符串的sha值和crc32值.html
最后更新:2020-05-21 19:37:01

评论

Your browser is out of date!

Update your browser to view this website correctly. Update my browser now

×