diff --git a/org.eclipse.jgit.test/META-INF/MANIFEST.MF b/org.eclipse.jgit.test/META-INF/MANIFEST.MF index 18f16d91f..cfd1a0414 100644 --- a/org.eclipse.jgit.test/META-INF/MANIFEST.MF +++ b/org.eclipse.jgit.test/META-INF/MANIFEST.MF @@ -18,7 +18,6 @@ Import-Package: com.googlecode.javaewah;version="[1.1.6,2.0.0)", org.apache.commons.compress.compressors.gzip;version="[1.15.0,2.0)", org.apache.commons.compress.compressors.xz;version="[1.15.0,2.0)", org.assertj.core.api;version="[3.14.0,4.0.0)", - org.bouncycastle.util.encoders;version="[1.61.0,2.0.0)", org.eclipse.jgit.annotations;version="[5.7.0,5.8.0)", org.eclipse.jgit.api;version="[5.7.0,5.8.0)", org.eclipse.jgit.api.errors;version="[5.7.0,5.8.0)", diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/HexTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/HexTest.java new file mode 100644 index 000000000..32af07f25 --- /dev/null +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/HexTest.java @@ -0,0 +1,49 @@ +/* + * Copyright (C) 2010, Google Inc. + * Copyright (C) 2020 Michael Dardis and others + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +package org.eclipse.jgit.util; + +import static org.eclipse.jgit.util.Hex.decode; +import static org.eclipse.jgit.util.Hex.toHexString; +import static org.junit.Assert.assertEquals; + +import org.eclipse.jgit.junit.JGitTestUtil; +import org.eclipse.jgit.lib.Constants; +import org.junit.Test; + +public class HexTest { + @Test + public void testEncode() { + assertEquals("68690a", toHexString(b("hi\n"))); + assertEquals("0001020d0a0971", toHexString(b("\0\1\2\r\n\tq"))); + } + + @Test + public void testDecode() { + JGitTestUtil.assertEquals(b("hi\n"), decode("68690a")); + JGitTestUtil.assertEquals(b("\0\1\2\r\n\tq"), decode("0001020d0a0971")); + JGitTestUtil.assertEquals(b("\u000EB"), decode("0E42")); + } + + @Test + public void testEncodeMatchesDecode() { + String[] testStrings = { "", "cow", "a", "a secret string", + "\0\1\2\r\n\t" }; + for (String e : testStrings) { + JGitTestUtil.assertEquals(b(e), decode(toHexString(b(e)))); + } + } + + private static byte[] b(String str) { + return Constants.encode(str); + } + +} diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/transport/WalkEncryption.java b/org.eclipse.jgit/src/org/eclipse/jgit/transport/WalkEncryption.java index df9f11ecf..d079a51bb 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/transport/WalkEncryption.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/transport/WalkEncryption.java @@ -34,9 +34,9 @@ import javax.crypto.spec.PBEKeySpec; import javax.crypto.spec.PBEParameterSpec; import javax.crypto.spec.SecretKeySpec; -import org.bouncycastle.util.encoders.Hex; import org.eclipse.jgit.internal.JGitText; import org.eclipse.jgit.util.Base64; +import org.eclipse.jgit.util.Hex; abstract class WalkEncryption { static final WalkEncryption NONE = new NoEncryption(); diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/util/Hex.java b/org.eclipse.jgit/src/org/eclipse/jgit/util/Hex.java new file mode 100644 index 000000000..935903652 --- /dev/null +++ b/org.eclipse.jgit/src/org/eclipse/jgit/util/Hex.java @@ -0,0 +1,63 @@ +/* + * Copyright (C) 2020, Michael Dardis. and others + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +package org.eclipse.jgit.util; + +/** + * Encodes and decodes to and from hexadecimal notation. + * + * @since 5.7 + */ +public final class Hex { + + private static final char[] HEX = "0123456789abcdef".toCharArray(); //$NON-NLS-1$ + + /** Defeats instantiation. */ + private Hex() { + // empty + } + + /** + * Decode a hexadecimal string to a byte array. + * + * Note this method performs no validation on input content. + * + * @param s hexadecimal string + * @return decoded array + */ + public static byte[] decode(String s) { + int len = s.length(); + byte[] b = new byte[len / 2]; + + for (int i = 0; i < len; i += 2) { + b[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) | Character.digit(s.charAt(i + 1), 16)); + } + return b; + } + + /** + * Encode a byte array to a hexadecimal string. + * + * @param b byte array + * @return hexadecimal string + */ + public static String toHexString(byte[] b) { + char[] c = new char[b.length * 2]; + + for (int i = 0; i < b.length; i++) { + int v = b[i] & 0xFF; + + c[i * 2] = HEX[v >>> 4]; + c[i * 2 + 1] = HEX[v & 0x0F]; + } + + return new String(c); + } +}