From cf077e2a4f9a5dcc6fb9b0b82e5d74274f3b9d0c Mon Sep 17 00:00:00 2001 From: Jeroen van Erp Date: Thu, 24 Aug 2017 11:20:35 +0200 Subject: [PATCH] Removed use of DataTypeConverter as that is no longer in default JDK9 --- .../schmizz/sshj/common/ByteArrayUtils.java | 30 +++++++++++++++++++ .../userauth/keyprovider/PKCS5KeyFile.java | 4 +-- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/main/java/net/schmizz/sshj/common/ByteArrayUtils.java b/src/main/java/net/schmizz/sshj/common/ByteArrayUtils.java index baf1048a..4d96c3e6 100644 --- a/src/main/java/net/schmizz/sshj/common/ByteArrayUtils.java +++ b/src/main/java/net/schmizz/sshj/common/ByteArrayUtils.java @@ -94,4 +94,34 @@ public class ByteArrayUtils { return sb.toString(); } + + public static byte[] parseHex(String hex) { + if (hex == null) { + throw new IllegalArgumentException("Hex string is null"); + } + if (hex.length() % 2 != 0) { + throw new IllegalArgumentException("Hex string '" + hex + "' should have even length."); + } + + byte[] result = new byte[hex.length() / 2]; + for (int i = 0; i < result.length; i++) { + int hi = parseHexDigit(hex.charAt(i * 2)) << 4; + int lo = parseHexDigit(hex.charAt(i * 2 + 1)); + result[i] = (byte) (hi + lo); + } + return result; + } + + private static int parseHexDigit(char c) { + if (c >= '0' && c <= '9') { + return c - '0'; + } + if (c >= 'a' && c <= 'f') { + return c - 'a' + 10; + } + if (c >= 'A' && c <= 'F') { + return c - 'A' + 10; + } + throw new IllegalArgumentException("Digit '" + c + "' out of bounds [0-9a-fA-F]"); + } } diff --git a/src/main/java/net/schmizz/sshj/userauth/keyprovider/PKCS5KeyFile.java b/src/main/java/net/schmizz/sshj/userauth/keyprovider/PKCS5KeyFile.java index 66a28cd8..1befe88d 100644 --- a/src/main/java/net/schmizz/sshj/userauth/keyprovider/PKCS5KeyFile.java +++ b/src/main/java/net/schmizz/sshj/userauth/keyprovider/PKCS5KeyFile.java @@ -24,9 +24,9 @@ import java.nio.CharBuffer; import java.security.*; import java.security.spec.*; import java.util.Arrays; -import javax.xml.bind.DatatypeConverter; import net.schmizz.sshj.common.Base64; +import net.schmizz.sshj.common.ByteArrayUtils; import net.schmizz.sshj.common.IOUtils; import net.schmizz.sshj.common.KeyType; import net.schmizz.sshj.transport.cipher.*; @@ -126,7 +126,7 @@ public class PKCS5KeyFile extends BaseFileKeyProvider { } else { throw new FormatException("Not a supported algorithm: " + algorithm); } - iv = Arrays.copyOfRange(DatatypeConverter.parseHexBinary(line.substring(ptr + 1)), 0, cipher.getIVSize()); + iv = Arrays.copyOfRange(ByteArrayUtils.parseHex(line.substring(ptr + 1)), 0, cipher.getIVSize()); } } else if (line.length() > 0) { sb.append(line);