Friday, 15 August 2014

java - Hashed string in C# is not readable -



java - Hashed string in C# is not readable -

this question has reply here:

java: syntax , meaning behind “[b@1ef9157”? binary/address? 5 answers

i'm trying hash same string in c# , in java.

c# hash method:

public static string hashvalue (string value) { byte[] input = null; hashalgorithm digest = hashalgorithm.create("sha-512"); input = digest.computehash(encoding.utf8.getbytes(value)); homecoming system.text.utf8encoding.utf8.getstring(input); }

the output, in wpf textbox, looking like: "՘"�?n[��"��2��d��j��t!z}7�h�p�j����gƼop�enbfhڄ�x���" .

the same function, in java, returning result: "[b@41e2db20".

the java hash method this:

public static string hashvalue(string value) { byte[] input = null; messagedigest digest; seek { digest = messagedigest.getinstance("sha-512"); seek { input = digest.digest(value.getbytes("utf-8")); } grab (unsupportedencodingexception e) { e.printstacktrace(); } } grab (nosuchalgorithmexception e1) { e1.printstacktrace(); } homecoming input.tostring(); }

can please allow me know i'm doing wrong? why result looking weird in c#?

your c# result looking "weird" because you've converted random bytes of hash utf-8 string. isn't going result in pretty-looking, since many of byte values map unprintable characters.

you may wish convert hash hexadecimal instead. that, utilize datatypeconverter class:

return datatypeconverter.printhexbinary(input);

i'm not sure c# equivalent - check google.

for record, java equivalent of current c# code be:

return new string(input, "utf-8");

currently calling .tostring(), java byte array results in phone call object.tostring() method. prints type , hashcode of object, not contents.

java c# hash hashcode

No comments:

Post a Comment