0

I'm hashing a password in Java using google's Hashing.

password = Hashing
        .sha256()
        .hashString(input, StandardCharsets.UTF_8)
        .toString();

When I pass any text to that line, it hashes and outputs everything with lowercase characters, for example, if I pass "foo", the value of password becomes:

2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae

However, if I use this site to hash "foo", the value it outputs is

2C26B46B68FFC68FF99B453C1D30413413422D706483BFA0F98A5E886266E7AE

As far as I can tell that's just the exact same password except with uppercase letters instead of lowercase.

What's causing those to output different values, and how can I get guava to output with uppercase letters (without just calling toUpperCase, unless that's really the only way)

3
  • 7
    They're hex digits, and case isn't semantically significant. Nov 30, 2018 at 18:47
  • 2
    It is simply hex-encoding of binary data. Hex-encoding does not specify upper-/lower-case, because it doesn't matter to hex-encoding. If you compare the hex-encoded string, you need to do a case-insensitive comparison, or you need to standardize on either upper or lower case.
    – Andreas
    Nov 30, 2018 at 18:47
  • Cool, thanks. The explanation was really what I was asking for. I already knew I could just call toUpperCase
    – realmature
    Nov 30, 2018 at 18:53

1 Answer 1

5

The main reason why Guava is making the result string in lower case, is because of the implementation of: com.google.common.hash.HashCode.toString() method.

You can simply call toUpperCase() method, from String class, on your result hash string value:

password = Hashing
        .sha256()
        .hashString(input, StandardCharsets.UTF_8)
        .toString()
        .toUpperCase();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.