Bug in StringUtil.toHexString

I have come across what I think is a bug in the com.couchbase.client.deps.io.netty.util.internal.StringUtil class’ toHexString((byte[] ) method.

What I am trying to do is have a string, hash it using SHA-1 that generates a byte array and then pass it on to StringUtil.toHexString that takes it as a parameter. What I have found is that if there is a “0” (zero) at the beginning of the output hexadecimal string, it is being curtailed off.

public static void main(String args[]) throws Exception{ String corrId = "5eca54bd-6985-45ed-8db1-5b0b7c0f29b7"; MessageDigest md = MessageDigest.getInstance("SHA-1"); byte[] correlHash = md.digest(corrId.getBytes()); String correlIdHashHex = StringUtil.toHexString(correlHash);

StringUtil.toHexString for this will give me: d9aaa18bad57b5fddb1b0232196772e3022ee03

The correct Hex should be: 0d9aaa18bad57b5fddb1b0232196772e3022ee03

This is by no means a blocker - I mean, the easier thing to do here would be to simply use one of the thousands methods available in other libraries to convert a byte array to Hexadecimal. I just want to be sure that this is a bug and point it out so that it can be corrected.


Interesting-- as you can tell from the classpath, that’s something we shadow into our library. We do support it, but it comes from the Netty project. We contribute to that project (@daschl specifically).

A quick search indicates there is a toHexStringPadded(). Perhaps that gives you what you need, but I’ve not really looked at the docs there yet.

How is this coming up in the context of Couchbase?

1 Like

Thanks for your reply. Once I found out about the issue, I simply used a similar method in the Apache commons.

How is this coming up in the context of Couchbase?

It may not, actually. That is why I was quite hesitant to put it here. My only intention was to document this as a bug at the right forum because I have searched it online and I don’t think it has been caught yet. The problem is that this is an intermittent bug and something almost very difficult to reproduce, at least in the context that we are using it in our project.

I just thought that since this is a Couchbase Java SDK forum, it may be worthwhile to point it out here. But I will do as you folks suggest. Should I move it to the Netty Project?

I’d recommend it if that other method doesn’t work for you, since we’re just shadowing that project’s code.

@adityeah yeah I’d open a ticket on the netty bugtracker on github and take it from there. The SDK on a regular basis adopts new netty releases and then at some point we’ll catch the upstream fixes too.