I'm afraid you're wrong. DigestUtils.sha() returns a string or an array of bytes containing the hash without conversion.
Here is a sketched example ( corrected ):
import org.apache.commons.codec.digest.DigestUtils; class Test { public static void main(String[] args) { byte[] digest; digest = DigestUtils.sha("The quick brown fox jumps over the lazy dog"); System.out.println(new String(digest)); } }
Here is the result:
C:\usr\project\my\java-digestutils>javac Test.java Note: Test.java uses or overrides a deprecated API. Note: Recompile with -Xlint:deprecation for details.
Here we are talking about the fact that sha() slightly stale ( sha1 or sha256 should be used, sha384 , sha512 - to taste)
C:\usr\project\my\java-digestutils>java Test /ФбЖz-(ьн??б?vз9←?л↕
This is in cp1251. If you save the result to a file and view it in hex:
0000000000: 2F D4 E1 C6 7A 2D 28 FC │ ED 84 9E E1 BB 76 E7 39 /ФбЖz-(ьн„ћб»vз9 0000000010: 1B 93 EB 12 0D 0A │ ←“л↕♪◙
Supplemented If you want to get a result similar to the result in Python, you need to use sha1() . It turns out the same result, which is shown above. For comparison:
import hashlib stri = 'The quick brown fox jumps over the lazy dog' ash = hashlib.sha1() ash.update(stri) print ash.digest()
Gives out:
/ ╘с╞z- (#EDэ╗╗ч9 ← Uy↕
The difference in the displayed strings is caused by differences in the encoding of the console. If you want non-printable characters to be rendered as in Python (for example, '\ xd4 \ xe1'), you will also need to escape these characters.
Once again added to escape characters that are not included in latin1 will have to write its function. Suppose we add the following method to our class:
public static StringBuffer escapeNonPrintable(byte[] byteArray) { StringBuffer result = new StringBuffer(); for (byte b : byteArray) { if (b < 32 || b > 127) { result.append(String.format("\\x%02x", b)); } else { result.append((char)b); } } return result; }
Now rewrite main() :
public static void main(String[] args) { byte[] digest; digest = DigestUtils.sha1("The quick brown fox jumps over the lazy dog"); System.out.println(escapeNonPrintable(digest)); }
As a result:
/ \ xd4 \ xe1 \ xc6z - (\ xfc \ xed \ x84 \ x9e \ xe1 \ xbbv \ xe79 \ x1b \ x93 \ xeb \ x12
sha()is obsolete, secondly, why did you decide that hex is returned? - tutankhamun