• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

problem with SHA-256

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?
 
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
R Amlani
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 781
Netbeans IDE Ubuntu Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Roses are red, violets are blue. Some poems rhyme and some don't. And some poems are a tiny ad.
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic