From 29993d7fa6af8edbc51dcd903a1e27d3f62b4e82 Mon Sep 17 00:00:00 2001 From: Tor Brede Vekterli Date: Tue, 8 Nov 2022 17:06:15 +0100 Subject: Add a simple base conversion tool Currently supports converting from and to any combination of base {16, 58, 62, 64}. Input is read from STDIN and is intentionally limited in length due to the algorithmic complexity of base conversions that are not a power of two. Converted value is written to STDOUT. --- .../java/com/yahoo/vespa/security/tool/Main.java | 4 +- .../security/tool/crypto/ConvertBaseTool.java | 98 ++++++++++++++++++++++ 2 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 vespaclient-java/src/main/java/com/yahoo/vespa/security/tool/crypto/ConvertBaseTool.java (limited to 'vespaclient-java/src/main/java/com/yahoo') diff --git a/vespaclient-java/src/main/java/com/yahoo/vespa/security/tool/Main.java b/vespaclient-java/src/main/java/com/yahoo/vespa/security/tool/Main.java index 11bd8815d77..0498154aa91 100644 --- a/vespaclient-java/src/main/java/com/yahoo/vespa/security/tool/Main.java +++ b/vespaclient-java/src/main/java/com/yahoo/vespa/security/tool/Main.java @@ -1,6 +1,7 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.vespa.security.tool; +import com.yahoo.vespa.security.tool.crypto.ConvertBaseTool; import com.yahoo.vespa.security.tool.crypto.DecryptTool; import com.yahoo.vespa.security.tool.crypto.EncryptTool; import com.yahoo.vespa.security.tool.crypto.KeygenTool; @@ -45,7 +46,8 @@ public class Main { } private static final List TOOLS = List.of( - new KeygenTool(), new EncryptTool(), new DecryptTool(), new TokenInfoTool()); + new KeygenTool(), new EncryptTool(), new DecryptTool(), new TokenInfoTool(), + new ConvertBaseTool()); private static Optional toolFromCliArgs(String[] args) { if (args.length == 0) { diff --git a/vespaclient-java/src/main/java/com/yahoo/vespa/security/tool/crypto/ConvertBaseTool.java b/vespaclient-java/src/main/java/com/yahoo/vespa/security/tool/crypto/ConvertBaseTool.java new file mode 100644 index 00000000000..120fc8a6f98 --- /dev/null +++ b/vespaclient-java/src/main/java/com/yahoo/vespa/security/tool/crypto/ConvertBaseTool.java @@ -0,0 +1,98 @@ +// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.vespa.security.tool.crypto; + +import com.yahoo.security.Base58; +import com.yahoo.security.Base62; +import com.yahoo.vespa.security.tool.CliUtils; +import com.yahoo.vespa.security.tool.Tool; +import com.yahoo.vespa.security.tool.ToolDescription; +import com.yahoo.vespa.security.tool.ToolInvocation; +import org.apache.commons.cli.Option; + +import java.io.IOException; +import java.io.UncheckedIOException; +import java.util.Base64; +import java.util.List; + +import static com.yahoo.security.ArrayUtils.fromUtf8Bytes; +import static com.yahoo.security.ArrayUtils.hex; +import static com.yahoo.security.ArrayUtils.unhex; + +/** + * Simple tool to convert between different Base N encodings, for a fixed set of N. + * + * @author vekterli + */ +public class ConvertBaseTool implements Tool { + + private static final int MAX_IN_BYTES = 1024; + + static final String FROM_OPTION = "from"; + static final String TO_OPTION = "to"; + + private static final List