feat: subcommand key

This commit is contained in:
2025-03-13 00:07:26 +08:00
parent d0a6a7491d
commit 79bf827fa6
6 changed files with 132 additions and 1 deletions

View File

@@ -0,0 +1,24 @@
package me.hatter.tools.jwtutil;
import java.util.List;
public class JwtCliUtil {
public static void executeSubCommand(List<SubCommand> subCommands, String[] args) {
final String arg0 = args[0];
final String[] leftArgs = subStringArray1(args);
for (SubCommand subCommand : subCommands) {
if (subCommand.matches(arg0)) {
subCommand.execute(leftArgs);
}
}
System.err.println("[ERROR] Unknown sub command: " + arg0);
}
private static String[] subStringArray1(String[] args) {
final String[] newArgs = new String[args.length - 1];
System.arraycopy(args, 1, newArgs, 0, newArgs.length);
return newArgs;
}
}

View File

@@ -1,8 +1,28 @@
package me.hatter.tools.jwtutil;
import me.hatter.tools.jwtutil.commands.HelpSubCommand;
import me.hatter.tools.jwtutil.commands.KeySubCommand;
import java.util.ArrayList;
import java.util.List;
public class JwtUtilMain {
static List<SubCommand> SUB_COMMANDS = new ArrayList<>();
static {
SUB_COMMANDS.add(new HelpSubCommand());
SUB_COMMANDS.add(new KeySubCommand());
}
public static void main(String[] args) {
if (args == null || args.length == 0) {
System.out.println("jwt-util v0.0.1");
System.out.println("\n");
System.out.println("jwt-util --help for help");
return;
}
JwtCliUtil.executeSubCommand(SUB_COMMANDS, args);
}
}

View File

@@ -0,0 +1,8 @@
package me.hatter.tools.jwtutil;
public interface SubCommand {
boolean matches(String arg0);
void execute(String[] args);
}

View File

@@ -0,0 +1,17 @@
package me.hatter.tools.jwtutil.commands;
import me.hatter.tools.jwtutil.SubCommand;
import java.util.Arrays;
public class HelpSubCommand implements SubCommand {
@Override
public boolean matches(String arg0) {
return Arrays.asList("help", "-h", "--help").contains(arg0);
}
@Override
public void execute(String[] args) {
System.out.println("help message ...");
}
}

View File

@@ -0,0 +1,28 @@
package me.hatter.tools.jwtutil.commands;
import me.hatter.tools.commons.security.key.KeyPairTool;
import me.hatter.tools.commons.security.key.KeyUtil;
import me.hatter.tools.commons.security.key.PKType;
import me.hatter.tools.jwtutil.SubCommand;
import java.security.KeyPair;
public class KeyGenerateSubCommand implements SubCommand {
@Override
public boolean matches(String arg0) {
return "generate".equals(arg0);
}
@Override
public void execute(String[] args) {
final KeyPair kp = KeyPairTool.ins(PKType.RSA2048).generateKeyPair().getKeyPair();
System.out.println(KeyUtil.serializePublicKeyToPEM(kp.getPublic()));
System.out.println(KeyUtil.serializePrivateKeyToPEM(kp.getPrivate()));
final KeyPair kp2 = KeyPairTool.ins(PKType.secp256r1).generateKeyPair().getKeyPair();
System.out.println(KeyUtil.serializePublicKeyToPEM(kp2.getPublic()));
System.out.println(KeyUtil.serializePrivateKeyToPEM(kp2.getPrivate()));
}
}

View File

@@ -0,0 +1,34 @@
package me.hatter.tools.jwtutil.commands;
import me.hatter.tools.commons.security.key.KeyPairTool;
import me.hatter.tools.commons.security.key.KeyUtil;
import me.hatter.tools.commons.security.key.PKType;
import me.hatter.tools.jwtutil.JwtCliUtil;
import me.hatter.tools.jwtutil.SubCommand;
import java.security.KeyPair;
import java.util.ArrayList;
import java.util.List;
public class KeySubCommand implements SubCommand {
private static List<SubCommand> SUB_COMMANDS = new ArrayList<>();
static {
SUB_COMMANDS.add(new KeyGenerateSubCommand());
}
@Override
public boolean matches(String arg0) {
return "key".equals(arg0);
}
@Override
public void execute(String[] args) {
if (args.length == 0) {
System.out.println("jwt-util key --help for help");
return;
}
JwtCliUtil.executeSubCommand(SUB_COMMANDS, args);
}
}