This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes problem with SHA-256 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "problem with SHA-256" Watch "problem with SHA-256" New topic
Author

problem with SHA-256

R Amlani
Greenhorn

Joined: May 09, 2011
Posts: 6
Hi All,

Before few days I got a requirement from a client to form one url to call one webservice. One of the params was to be processed by sha256 and base 64.

--> the prior requirement was to give stringToBeHashed and key as input params. I set this two params for the input of sha256 method. It was working fine, I also checked my code with online converters also.

--> Now the problem is that he wants the output data in raw binary output format about which I don't know. As per his instruction and my info this is the only one line code in PHP as following :



Let me also give you example of output he wants.
stringToBeHashed --> ABCDEF
key --> 123
output --> t����Y�m@�����k/gba۫�6���M (this is what I want to get)

And what I'm and all tools are getting is --> 56c2201a9c7a4eaeaf3664724d42b678b10d03ce73e5e72a526d45f1daa53d97


the code which currently I use is,



will any one can help me out to solve this ?
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

There is no need to implement all that Java! The JCE has the algorithm build in.



The result is not Base64 encoded so it is binary.


Note - printing out the binary as you have done will not display anything sensible. You should just compare the content of the desired result with result you obtain.


Retired horse trader.
 Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
R Amlani
Greenhorn

Joined: May 09, 2011
Posts: 6
Hi James,

Thanks for replying. But I've already tried out this solution and I also compared the values.

--> Now, I just got more info that the output which I got, need to be given as an input of the base 64 and I must get the output = "dL7Y8OuzWaxtQJj33gKqEO1rL2diYdurxAM2o//2TRI="
(for the input which I've described in que.)

and currently I am getting "W0JAOTExZjcx".
R Amlani
Greenhorn

Joined: May 09, 2011
Posts: 6
I also tried to convert it into string using UTF-8 and then gave it to base64 but still not getting proper/desired output.

Please help me out.
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

R Amlani wrote:Hi James,

Thanks for replying. But I've already tried out this solution and I also compared the values.


And do you get the same value from both? If not, then your roll-your-own solution is incorrect.


--> Now, I just got more info that the output which I got, need to be given as an input of the base 64 and I must get the output = "dL7Y8OuzWaxtQJj33gKqEO1rL2diYdurxAM2o//2TRI="
(for the input which I've described in que.)

and currently I am getting "W0JAOTExZjcx".


In your original post post you wanted binary (presumably a byte array) and now you want Base64 encoding. I don't know how you Base64 encoded to get the result "W0JAOTExZjcx" but there is no way that that result is the Base64 encoding of the 20 bytes of the digest. It is in fact the Base64 encoding of "[B@911f71" which is typical of the value obtained from the toString() method of a byte array so it seems you are trying to convert your digest to a string before Base64 encoding it.

Sorry but until you specify exactly what you want and show the code you are currently working with I can't help any further. Please please please get rid of that monstrous roll-your-own HMAC and use that provided by the JCE; I'm not interested in trying to debug code that can be replaced by 4 lines.
R Amlani
Greenhorn

Joined: May 09, 2011
Posts: 6
Ok...now see my this attempt.

I'm giving bytes to the input of base 64. And for base 64 I've written this line of code.


Now, have a look on this code..


And I'm getting the output = "VsIgGpx6Tq6vNmRyTUK2eLENA85z5ecqUm1F8dqlPZc=" while my required result is "dL7Y8OuzWaxtQJj33gKqEO1rL2diYdurxAM2o//2TRI="

And Previously it was my mistake in giving the input to base64.My sincere apology for that.
R Amlani
Greenhorn

Joined: May 09, 2011
Posts: 6
please give me some hint that how can I get this output (like the example I've given above). As I dont have any requirement specification. I just know that in PHP it can be done using this two lines,
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

R Amlani wrote:
And I'm getting the output = "VsIgGpx6Tq6vNmRyTUK2eLENA85z5ecqUm1F8dqlPZc=" while my required result is "dL7Y8OuzWaxtQJj33gKqEO1rL2diYdurxAM2o//2TRI="


The HMACSHA256 of "ABCDEF" using key "123" is "VsIgGpx6Tq6vNmRyTUK2eLENA85z5ecqUm1F8dqlPZc=".
The HMACSHA256 of "123" using key "ABCDEF" is "dL7Y8OuzWaxtQJj33gKqEO1rL2diYdurxAM2o//2TRI=" .
R Amlani
Greenhorn

Joined: May 09, 2011
Posts: 6
Hi James,

I tried to hash in that way and it worked. I'm really thankful to you sir. Actually it was mistake from the client's documentation.
Now I'll be careful with this kind of issues.

Thanks again.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem with SHA-256
 
Similar Threads
Why is Java's MD5 different from MySQL MD5?
where to find HMAC-SHA 256 hash function
How to use AES DECRYPT_MODE?
Exception errror in a program
Why learn Clojure