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

 

上一篇:php – 简单的登录检查密码


下一篇:像symfony2一样创建相同的哈希值(java-SHA-512)