android--md5和sha算法

1:md5简单实现

整体两个参数,传入加密字符串以及boolean类型的大小写区分标识。

/**
     * @param string 代价密字符串
     * @param isLowcase 是否小写
     * @return
     */
    public static String md5(String string,boolean isLowcase){
        if (TextUtils.isEmpty(string)){
            return "";
        }
        MessageDigest md5=null;
        try {
            md5=MessageDigest.getInstance("MD5");
            byte[] bytes=  md5.digest(string.getBytes());

            StringBuilder result = new StringBuilder();

            for (byte b:bytes){
                String temp=Integer.toHexString(b&0xff);//b&0xff转int 再转string
                if (temp.length()==1){
                    temp="0"+temp;
                }
                result.append(temp);
            }

            //返回值大小写
            return isLowcase?result.toString().toLowerCase():result.toString().toUpperCase();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }

2:加盐加密

      

 /**
     * @param string
     * @param slat 加盐
     * @param isLowcase
     * @return
     */
    public static String md5(String string,String slat,boolean isLowcase){
        if (TextUtils.isEmpty(string)){
            return "";
        }

        try {
            MessageDigest md5=MessageDigest.getInstance("MD5");
            byte[] bytes=  md5.digest((string+slat).getBytes());

            StringBuilder result = new StringBuilder();

            for (byte b:bytes){
                String temp=Integer.toHexString(b&0xff);
                if (temp.length()==1){
                    temp="0"+temp;
                }
                result.append(temp);
            }

            //返回值大小写
            return isLowcase?result.toString().toLowerCase():result.toString().toUpperCase();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }

如上所示,在md5方法的基础上拼接了一个string的串。从而获取到新的md5值,

其中加盐的方式多种多样,如添加固定的验签串,或者利用用户名密码当salt,或者随机生成字符串等。

总之,都是为了增加被破解的难度。

3:获取文件的md5

 /**
     *
     * 获取文件的md5
     * @param file
     * @param isLowcase
     * @return
     */
    public static String md5(File file,boolean isLowcase) {
        if (file == null || !file.isFile() || !file.exists()) {
            return "";
        }
        FileInputStream in = null;
        String result = "";
        byte buffer[] = new byte[1024*8];
        int len;
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            in = new FileInputStream(file);
            while ((len = in.read(buffer)) != -1) {
                md5.update(buffer, 0, len);
            }
            byte[] bytes = md5.digest();

            for (byte b : bytes) {
                String temp = Integer.toHexString(b & 0xff);
                if (temp.length() == 1) {
                    temp = "0" + temp;
                }
                result += temp;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(null!=in){
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return isLowcase?result.toLowerCase():result.toUpperCase();
    }

总体的方法都差不多,利用update(byte[] input, int offset, int len) 传入数据。

4:获取sha值

     获取sha的值跟md5一样,只需要更改MessageDigest.getInstance(string shaType);

    

 /**
     *
     * sha
     * @param authString
     * @param shaType  SHA-1,SHA-256,SHA-384,SHA-512
     * @return
     */
    public static String getSHA(String authString,String shaType) {
        try {
            // SHA1签名生成
            MessageDigest md = MessageDigest.getInstance(shaType);
            md.update(authString.getBytes());
            byte[] digest = md.digest();

            StringBuffer hexstr = new StringBuffer();
            String shaHex = "";
            for (int i = 0; i < digest.length; i++) {
                shaHex = Integer.toHexString(digest[i] & 0xFF);
                if (shaHex.length() < 2) {
                    hexstr.append(0);
                }
                hexstr.append(shaHex);
            }
            return hexstr.toString();
        } catch (Exception e) {
            Log.e(shaType,e.toString());
            return "";

        }
    }

猜你喜欢

转载自blog.csdn.net/qq_23025319/article/details/85716889