Android 实现SHA1加密算法代码

实现SHA1加密代码

    public static String getSecurityAppKey() {
                return encryptToSHA(RequestTools.AppId + "UZ" +
                        RequestTools.AppKey + "UZ" + System.currentTimeMillis()) +
                        "." + System.currentTimeMillis();
            }

    public static String encryptToSHA(String info) {
        byte[] digesta = null;
        try {
            MessageDigest alga = MessageDigest.getInstance("SHA-1");
            alga.update(info.getBytes());
            digesta = alga.digest();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        String rs = byte2hex(digesta);
        return rs;
    }

    public static String byte2hex(byte[] b) {
        String hs = "";
        String stmp = "";
        for (int n = 0; n < b.length; n++) {
            stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
            if (stmp.length() == 1) {
                hs = hs + "0" + stmp;
            } else {
                hs = hs + stmp;
            }
        }
        return hs;
    }

使用方法按照getSecurityAppKey方法使用,将要加密的字串写到encryptToSHA中即可!

你可能感兴趣的:(Android)