aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit/src/org/eclipse/jgit/util/Hex.java
diff options
context:
space:
mode:
authorMichael Dardis <git@md-5.net>2020-02-14 12:09:37 +1100
committerMatthias Sohn <matthias.sohn@sap.com>2020-02-22 00:12:20 +0100
commit67b9effc655d8ba75acb7db5e49687224d1c7a6a (patch)
treef8ea071da67ba38c900606a530361117ddd15b97 /org.eclipse.jgit/src/org/eclipse/jgit/util/Hex.java
parent40555223594005481cba95d09b32c4f6bb5dcff3 (diff)
downloadjgit-67b9effc655d8ba75acb7db5e49687224d1c7a6a.tar.gz
jgit-67b9effc655d8ba75acb7db5e49687224d1c7a6a.zip
Remove use of org.bouncycastle.util.encoders.Hex
Change-Id: I5c1ed0397ef99eb5d4f120da331b66c2d0f1707a Signed-off-by: Michael Dardis <git@md-5.net> Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
Diffstat (limited to 'org.eclipse.jgit/src/org/eclipse/jgit/util/Hex.java')
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/util/Hex.java63
1 files changed, 63 insertions, 0 deletions
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 0000000000..9359036524
--- /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);
+ }
+}