1

There are some SO quetions but no helped me. I would like to convert byte[] from org.apache.commons.codec.digest.HmacUtils to String. This code produces some weird output:

final String value = "value";
final String key = "key";
byte[] bytes = HmacUtils.hmacSha1(key, value);
String s = new String(bytes);

What am I doing wrong?

asked Sep 26, 2015 at 19:37
2
  • 1
    Generally, you display sha1 hashes in hex. Commons Codec has a hex encoder. Commented Sep 26, 2015 at 19:41
  • Probably relevant, possibly duplicate: stackoverflow.com/questions/9655181/… The resulting 'random binary' of the hash function will otherwise not be a useful text/string value. Commented Sep 26, 2015 at 19:53

4 Answers 4

2

Try to use:

String st = HmacUtils.hmacSha1Hex(key, value);
answered Sep 26, 2015 at 20:02
Sign up to request clarification or add additional context in comments.

Comments

2

For a more general solution, if you don't have HmacUtils available:

// Prepare a buffer for the string
StringBuilder builder = new StringBuilder(bytes.length*2);
// Iterate through all bytes in the array
for(byte b : bytes) {
 // Convert them into a hex string
 builder.append(String.format("%02x",b));
 // builder.append(String.format("%02x",b).toUpperCase()); // for upper case characters
}
// Done
String s = builder.toString();

To explain your problem: You are using a hash function. So a hash is usually an array of bytes which should look quite random.

If you use new String(bytes) you try to create a string from these bytes. But Java will try to convert the bytes to characters.

For example: The byte 65 (hex 0x41) becomes the letter 'A'. 66 (hex 0x42) the letter 'B' and so on. Some numbers can't be converted into readable characters. Thats why you see strange characters like '�'.

So new String(new byte[]{0x41, 0x42, 0x43}) will become 'ABC'.

You want something else: You want each byte converted into a 2 digit hex String (and append these strings).

Greetings!

answered Sep 26, 2015 at 20:55

Comments

1

First, the result of hmacSha1 would produce a digest, not not a clear String. Besides, you may have to specify an encoding format, for example

String s = new String(bytes, "US-ASCII");

or

String s = new String(bytes, "UTF-8");
answered Sep 26, 2015 at 19:44

2 Comments

Well, still I am getting a weird output ("WD:L#P�F8�]d�f�/�3") that doesn't correspond to this testing application - freeformatter.com/hmac-generator.html.
@user1315357 The confusion is the problem/expected output is not clearly specified.
0

You may need to have an encoding format. Check out this link here.

UTF-8 byte[] to String

answered Sep 26, 2015 at 19:46

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.