How can I get sha1 without hex, now I use simple

DigestUtils.sha("The quick brown fox jumps over the lazy dog".getBytes()) 

gives out

2fd4e1c67a2d28fced849ee1bb76e7391b93eb12

and it is necessary to give out

'/ \ xd4 \ xe1 \ xc6z - (\ xfc \ xed \ x84 \ x9e \ xe1 \ xbbv \ xe79 \ x1b \ x93 \ xeb \ x12'

on python it goes like this

 stri = 'The quick brown fox jumps over the lazy dog' ash = hashlib.sha1() ash.update(stri) ash.digest() 

'/ \ xd4 \ xe1 \ xc6z - (\ xfc \ xed \ x84 \ x9e \ xe1 \ xbbv \ xe79 \ x1b \ x93 \ xeb \ x12'

 ash.hexdigest() 

'2fd4e1c67a2d28fced849ee1bb76e7391b93eb12'

  • one
    firstly sha() is obsolete, secondly, why did you decide that hex is returned? - tutankhamun
  • I don't know what that means, but I need the result as in python, with hexdigest () it gives '2fd4e1c67a2d28fced849ee1bb76e7391b93eb12' and with digest () returns xbbv \ xe79 \ x1b \ x93 \ xeb \ x12 - J Mas
  • Please explain your purpose. I get the feeling that I did not answer the question to which you wanted to get an answer. See meta.ru.stackoverflow.com/a/710/177613 - tutankhamun
  • I want to get a sha of this type '/ \ xd4 \ xe1 \ xc6z - (\ xfc \ xed \ x84 \ x9e \ xe1 \ xbbv \ xe79 \ x1b \ x93 \ xeb \ x12' and not '2fd4e1c67a2d28fced849ee1bb76e7391b93eb12', such is a function, a function, a function, a function, a function, a function, a function, a function, a function, a function, a function, a function, a function, a function, a function, a function, a function, a function, a function, a function, a function, a function, a function, a function, a function, a function, a function, a function, a function, a xx1 but I can't find one for java - J Mas
  • Once again added the answer. You are still hiding something :) I don’t understand why this line is needed. - tutankhamun

1 answer 1

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

  • I tried to use other versions of sha256 b 512 I do not go out / \ xd4 \ xe1 \ xc6z - (\ xfc \ xed \ x84 \ x9e \ xe1 \ xbbv \ xe79 \ x1b \ x93 \ xeb \ x12 - J Mas
  • @JTan Other versions definitely will not give such a result. Same other hash functions. If you need an analogue sha() this is sha1() . I will add the answer. - tutankhamun
  • Thanks, I think this is what I need, I use DigestUtils.sha - J Mas