I need to create a hash from a String containing users password. To create the hash, I use a byte array which I get by calling String.getBytes()
. But when I cal
Yes. Not only is it guaranteed to be UTF-16, but the byte order is defined too:
When decoding, the UTF-16 charset interprets the byte-order mark at the beginning of the input stream to indicate the byte-order of the stream but defaults to big-endian if there is no byte-order mark; when encoding, it uses big-endian byte order and writes a big-endian byte-order mark.
(The BOM isn't relevant when the caller doesn't ask for it, so String.getBytes(...)
won't include it.)
So long as you have the same string content - i.e. the same sequence of char
values - then you'll get the same bytes on every implementation of Java, barring bugs. (Any such bug would be pretty surprising, given that UTF-16 is probably the simplest encoding to implement in Java...)
The fact that UTF-16 is the native representation for char
(and usually for String
) is only relevant in terms of ease of implementation, however. For example, I'd also expect String.getBytes("UTF-8")
to give the same results on every platform.