From fe7c556f34c8f32abf36d473d2f7a21a0c67bf92 Mon Sep 17 00:00:00 2001 From: David Pletcher Date: Mon, 9 Mar 2015 14:48:32 -0700 Subject: [PATCH] Fix an invalid format string The %x format specifier is not valid for a byte array. This patch fixes a bug that would cause an IllegalFormatConversionException. Change-Id: I025975eca7b2f10bbafa39f5519f8668e6536541 Signed-off-by: David Pletcher --- .../jgit/transport/HMACSHA1NonceGenerator.java | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/transport/HMACSHA1NonceGenerator.java b/org.eclipse.jgit/src/org/eclipse/jgit/transport/HMACSHA1NonceGenerator.java index c24784771..222ca55d5 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/transport/HMACSHA1NonceGenerator.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/transport/HMACSHA1NonceGenerator.java @@ -99,9 +99,7 @@ public class HMACSHA1NonceGenerator implements NonceGenerator { } catch (UnsupportedEncodingException e) { throw new IllegalStateException(e); } - String sentNonce = String.format( - "%d-%20X", new Long(timestamp), rawHmac); //$NON-NLS-1$ - return sentNonce; + return Long.toString(timestamp) + "-" + toHex(rawHmac); //$NON-NLS-1$ } @Override @@ -146,4 +144,15 @@ public class HMACSHA1NonceGenerator implements NonceGenerator { return NonceStatus.SLOP; } } + + private static final String HEX = "0123456789ABCDEF"; //$NON-NLS-1$ + + private static String toHex(byte[] bytes) { + StringBuilder builder = new StringBuilder(2 * bytes.length); + for (byte b : bytes) { + builder.append(HEX.charAt((b & 0xF0) >> 4)); + builder.append(HEX.charAt(b & 0xF)); + } + return builder.toString(); + } }