简体   繁体   中英

Enable Java sha1 raw mode like php sha1

I get a problem with php.sha1 and my java code. My php code

echo base64_encode(sha1("test", TRUE));
qUqP5cyxm6YcTAhz05Hph5gvu9M=

And my java code is:

 static String Hash(String input) throws Exception {

    MessageDigest mDigest = MessageDigest.getInstance("SHA1");
    byte[] result = mDigest.digest(input.getBytes());
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < result.length; i++) {
       sb.append((result[i / Byte.SIZE] << i % Byte.SIZE & 0x80) == 0 ? '0' : '1');
    }
    return base64_encode(sb.toString());

}

output MTAxMDEwMDEwMTAwMTAxMDEwMDA=

How should I change the loop to get equal strings?

This one should works like RAW SHA-1

String base64 = 
Base64.getEncoder().encodeToString(MessageDigest.
getInstance("SHA-1").digest(s.getBytes("ISO-8859-1")));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM