简体   繁体   中英

Java SHA1 for unicode?

I tried to get a SHA1 hash for a unicode string, some chinese string in Android Java code in Eclipse. I used the usual MessageDigest class and some Hex conversion piece of code from web.

I wonder how to verify that the hash and its hex conversion are correct? I inserted that chinese string to a number of online hash converters, but all of them produced a different hash. The same coverters work fine with ascii strings.

I am newbie in this, so some pointers would be appreciated!

br, perza

What counts as "correct"? Hashes such as SHA-1 deal with binary data, not text... and there are various different ways of converting from text to binary.

What's the purpose here? Do you have to make the hash match what some other code produces? If so, find out what encoding that other code uses to convert text to binary. If it's only got to match your code, you have a free hand - I'd suggest using either UTF-8 or UTF-16, both of which are guaranteed to be present in Java, commonly available on other platforms for future options, and cover the whole of the Unicode character repertoire.

What do you mean by "Online Hash Converters"?

If you calculated the SHA1 Hash of some Unicode-String there is no possibility to get the String back from the Hash. That is the key of a Hash-Function, its "one directional".

You can use another SHA implementatin and check if you get the same hash as result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM