[安卓/Android/Java] 计算校验值 MD5、SHA-1,256,384,512

目录

 算法类型(Algorithm)

 字节数组转16进制字符(Hex)

计算校验值(字节数组和文本)

计算校验值(文件)

调用


 算法类型(Algorithm)

    /**
     算法类型
     */
    @Retention( RetentionPolicy.SOURCE )
    public @interface AlgorithmType {
        String MD5 = "MD5";
        String SHA1 = "SHA-1";
        String SHA256 = "SHA-256";
        String SHA384 = "SHA-384";
        String SHA512 = "SHA-512";
    }

 字节数组转16进制字符(Hex)

    /**
     转为16进制
     @param bs  字节数组
     @return    16进制字符串
     */
    private static String toHexString(byte[] bs) {
        StringBuilder sb = new StringBuilder();
        String s;
        for( byte b : bs ) {
            s = Integer.toHexString( b & 0xFF );
            //1得到一位的进行补0操作
            if (s.length() == 1) sb.append("0");
            sb.append(s);
        }
        return sb.toString();
    }

计算校验值(字节数组和文本)

    /**
     * 计算字节数组的校验值
     * @param data          数据
     * @param algorithm     算法类型
     * @return              返回结果
     */
    @Nullable
    public static String toDigest(@NonNull byte[] data, @NonNull @AlgorithmType String algorithm) {
        try {
            MessageDigest digest = MessageDigest.getInstance( algorithm );
            digest.update( data );
            //转为16进制
            return toHexString( digest.digest() );
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     计算字符串的校验值
     @param data            数据
     @param charset         编码类型
     @param algorithm       算法类型
     @return                返回结果
     */
    public static String toDigest(String data, Charset charset, @NonNull @AlgorithmType String algorithm) {
        return toDigest( data.getBytes( charset ), algorithm );
    }

计算校验值(文件)

    /**
     * 计算文件的校验值
     * @param filePath      文件路径
     * @param algorithm     算法类型
     * @return              返回结果
     */
    @Nullable
    public static String toFileDigest(@NonNull String filePath, @NonNull @AlgorithmType String algorithm) {
        InputStream is = null;
        byte[] bsData;
        try {
            is = new FileInputStream( filePath );
            bsData = new byte[ is.available() ];
            if( is.read( bsData ) == bsData.length ) return toDigest( bsData, algorithm );
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if ( is != null ) {
                try {
                    is.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    /**
     计算文件的校验值
     @param file        文件
     @param algorithm   算法类型
     @return            返回结果
     */
    public static String toFileDigest(@NonNull File file, @NonNull @AlgorithmType String algorithm) {
        return toFileDigest( file.getAbsolutePath(), algorithm );
    }

调用

    //=============== 字节数组 ===============
    public static String toMD5(byte[] data) {
        return toDigest( data, AlgorithmType.MD5 );
    }
    public static String toSHA1(byte[] data) {
        return toDigest( data, AlgorithmType.SHA1 );
    }
    public static String toSHA256(byte[] data) {
        return toDigest( data, AlgorithmType.SHA256 );
    }
    public static String toSHA384(byte[] data) {
        return toDigest( data, AlgorithmType.SHA384 );
    }
    public static String toSHA512(byte[] data) {
        return toDigest( data, AlgorithmType.SHA512 );
    }
    //========================================

    //=============== 字符串 ===============
    public static String toMD5(String data) {
        return toDigest( data, utf8(), AlgorithmType.MD5 );
    }
    public static String toSHA1(String data) {
        return toDigest( data, utf8(), AlgorithmType.SHA1 );
    }
    public static String toSHA256(String data) {
        return toDigest( data, utf8(), AlgorithmType.SHA256 );
    }
    public static String toSHA384(String data) {
        return toDigest( data, utf8(), AlgorithmType.SHA384);
    }
    public static String toSHA512(String data) {
        return toDigest( data, utf8(), AlgorithmType.SHA512 );
    }
    //========================================

    //=============== 文件(path) ===============
    public static String toFileMD5(String filePath) {
        return toFileDigest( filePath, AlgorithmType.MD5 );
    }
    public static String toFileSHA1(String filePath) {
        return toFileDigest( filePath, AlgorithmType.SHA1 ); }
    public static String toFileSHA256(String filePath) {
        return toFileDigest( filePath, AlgorithmType.SHA256 );
    }
    public static String toFileSHA384(String filePath) {
        return toFileDigest( filePath, AlgorithmType.SHA384 );
    }
    public static String toFileSHA512(String filePath) {
        return toFileDigest( filePath, AlgorithmType.SHA512 );
    }
    //========================================

    //=============== 文件(File) ===============
    public static String toFileMD5(File file) {
        return toFileDigest( file, AlgorithmType.MD5 );
    }
    public static String toFileSHA1(File file) {
        return toFileDigest( file, AlgorithmType.SHA1 );
    }
    public static String toFileSHA256(File file) {
        return toFileDigest( file, AlgorithmType.SHA256 );
    }
    public static String toFileSHA384(File file) {
        return toFileDigest( file, AlgorithmType.SHA384 );
    }
    public static String toFileSHA512(File file) {
        return toFileDigest( file, AlgorithmType.SHA512 );
    }
    //========================================

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

米歪(MiWi)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值