Java md5, the PHP way

前端 未结 5 1108
执念已碎
执念已碎 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:25

    You need to convert the result into the HEX representation. This is how it is done in Fast MD5 library:

        private static final char[] HEX_CHARS = { '0', '1', '2', '3', '4', '5',
                '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', };
    
        /**
         * Turns array of bytes into string representing each byte as unsigned hex
         * number.
         * 
         * @param hash
         *            Array of bytes to convert to hex-string
         * @return Generated hex string
         */
        public static String asHex(byte hash[]) {
            char buf[] = new char[hash.length * 2];
            for (int i = 0, x = 0; i < hash.length; i++) {
                buf[x++] = HEX_CHARS[(hash[i] >>> 4) & 0xf];
                buf[x++] = HEX_CHARS[hash[i] & 0xf];
            }
            return new String(buf);
        }
    

    So you will need to call System.out.println("String3: " + asHex(thedigest));

提交回复
热议问题