Java md5, the PHP way

前端 未结 5 1106
执念已碎
执念已碎 2020-12-14 12:49

I have been working on this for hours, but I can\'t get it to work.

Basically I am developing a REST client in Java for a REST server in PHP. Both the client and the

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-14 13:30

    Give this a try:

    public static String md5(String input) throws NoSuchAlgorithmException {
        String result = input;
        if(input != null) {
            MessageDigest md = MessageDigest.getInstance("MD5"); //or "SHA-1"
            md.update(input.getBytes());
            BigInteger hash = new BigInteger(1, md.digest());
            result = hash.toString(16);
            while(result.length() < 32) { //40 for SHA-1
                result = "0" + result;
            }
        }
        return result;
    }
    

    code from http://web.archive.org/web/20140209230440/http://www.sergiy.ca/how-to-make-java-md5-and-sha-1-hashes-compatible-with-php-or-mysql/

提交回复
热议问题