Will String.getBytes(“UTF-16”) return the same result on all platforms?

后端 未结 3 913
执念已碎
执念已碎 2021-01-18 17:46

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

3条回答
  •  一向
    一向 (楼主)
    2021-01-18 17:50

    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.

提交回复
热议问题