feat: generate keypair
This commit is contained in:
@@ -8,13 +8,9 @@ def buildJSON = JsonSlurper.newInstance().parseText(new File("build.json").text)
|
|||||||
if (buildJSON.application) { apply plugin: 'application' }
|
if (buildJSON.application) { apply plugin: 'application' }
|
||||||
|
|
||||||
def baseProjectName = buildJSON?.project?.name ?: '__project_name__';
|
def baseProjectName = buildJSON?.project?.name ?: '__project_name__';
|
||||||
def shellCommandName = baseProjectName
|
|
||||||
def eclipseProjectName = baseProjectName
|
|
||||||
def eclipseProjectComment = buildJSON?.project?.comment ?: '__project_name_comment__'
|
|
||||||
def jarManifestMainClass = buildJSON?.project?.main ?: 'SampleMain'
|
def jarManifestMainClass = buildJSON?.project?.main ?: 'SampleMain'
|
||||||
|
|
||||||
if (buildJSON.application) { mainClassName = jarManifestMainClass }
|
if (buildJSON.application) { mainClassName = jarManifestMainClass }
|
||||||
archivesBaseName = buildJSON?.project?.archiveName ?: baseProjectName
|
|
||||||
sourceCompatibility = 1.8
|
sourceCompatibility = 1.8
|
||||||
targetCompatibility = 1.8
|
targetCompatibility = 1.8
|
||||||
|
|
||||||
@@ -28,34 +24,27 @@ repositories {
|
|||||||
}
|
}
|
||||||
|
|
||||||
tasks.withType(JavaCompile) {
|
tasks.withType(JavaCompile) {
|
||||||
options.encoding = "UTF-8"
|
options.encoding = "UTF-8"
|
||||||
}
|
}
|
||||||
|
|
||||||
// '-x test' skip unit test
|
// '-x test' skip unit test
|
||||||
defaultTasks 'packjar'
|
defaultTasks 'build'
|
||||||
|
|
||||||
task packjarsrc << {
|
buildscript {
|
||||||
ant.jar(destfile: "${baseProjectName}-sources.jar") {
|
repositories {
|
||||||
fileset(dir: 'src/main/java', includes: '**/*.java')
|
mavenLocal()
|
||||||
}
|
maven() { url 'https://maven.aliyun.com/repository/central' }
|
||||||
|
mavenCentral()
|
||||||
|
jcenter()
|
||||||
|
}
|
||||||
|
dependencies {
|
||||||
|
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.11.RELEASE")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
packjarsrc.dependsOn build
|
apply plugin: 'org.springframework.boot'
|
||||||
|
springBoot {
|
||||||
task packjar << {
|
mainClass = jarManifestMainClass
|
||||||
def packtempclasses = "packtempclasses"
|
|
||||||
def libs = ant.path {
|
|
||||||
fileset(dir: 'build/libs', includes: '*.jar')
|
|
||||||
}
|
|
||||||
libs.list().each {
|
|
||||||
ant.unzip(dest: packtempclasses, src: it)
|
|
||||||
}
|
|
||||||
new File(packtempclasses + "/jar-version-build.txt").write(new Date().format("yyyyMMdd"), "UTF-8")
|
|
||||||
ant.jar(destfile: "${baseProjectName}.jar") {
|
|
||||||
fileset(dir: packtempclasses, includes: '**/*.*')
|
|
||||||
}
|
|
||||||
ant.delete(dir: packtempclasses)
|
|
||||||
}
|
}
|
||||||
packjar.dependsOn packjarsrc
|
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
compile files(fileTree(dir: 'lib', includes: ['*.jar'], excludes: ['*-sources.jar', '*-javadoc.jar']))
|
compile files(fileTree(dir: 'lib', includes: ['*.jar'], excludes: ['*-sources.jar', '*-javadoc.jar']))
|
||||||
@@ -71,30 +60,3 @@ dependencies {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
eclipse {
|
|
||||||
project {
|
|
||||||
name = eclipseProjectName
|
|
||||||
comment = eclipseProjectComment
|
|
||||||
}
|
|
||||||
classpath {
|
|
||||||
defaultOutputDir = file('classes')
|
|
||||||
downloadSources = true
|
|
||||||
file {
|
|
||||||
whenMerged { classpath ->
|
|
||||||
classpath.entries.findAll { it.kind=='lib' }.each {
|
|
||||||
if ((it.path != null) && (it.sourcePath == null) && file(it.path.replace(".jar", "-sources.jar")).exists()) {
|
|
||||||
it.sourcePath = getFileReferenceFactory().fromPath(it.path.replace(".jar", "-sources.jar"))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
eclipseJdt << {
|
|
||||||
File f = file('.settings/org.eclipse.core.resources.prefs')
|
|
||||||
f.write('eclipse.preferences.version=1\n')
|
|
||||||
f.append('encoding/<project>=utf-8')
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"project": {
|
"project": {
|
||||||
"name": "yubikey-ca-java",
|
"name": "yubikey-ca-java",
|
||||||
"main": "SampleMain",
|
"main": "me.hatter.tools.yubikeyca.YubikeyCaMain",
|
||||||
"archiveName": "yubikey-ca-java"
|
"archiveName": "yubikey-ca-java"
|
||||||
},
|
},
|
||||||
"application": false,
|
"application": false,
|
||||||
@@ -12,7 +12,9 @@
|
|||||||
},
|
},
|
||||||
"repo": {
|
"repo": {
|
||||||
"dependencies": [
|
"dependencies": [
|
||||||
"me.hatter:commons:3.0"
|
"info.picocli:picocli:4.6.1",
|
||||||
|
"me.hatter:commons:3.66",
|
||||||
|
"me.hatter:crypto:1.10"
|
||||||
],
|
],
|
||||||
"testDependencies": [
|
"testDependencies": [
|
||||||
"junit:junit:4.12"
|
"junit:junit:4.12"
|
||||||
|
|||||||
@@ -0,0 +1,25 @@
|
|||||||
|
package me.hatter.tools.yubikeyca;
|
||||||
|
|
||||||
|
import picocli.CommandLine;
|
||||||
|
|
||||||
|
@CommandLine.Command(name = YubikeyCaConstant.NAME, version = YubikeyCaConstant.NAME + " v" + YubikeyCaConstant.VERSION, description = "\n" +
|
||||||
|
"Yubikey CA tool\n" +
|
||||||
|
"\n" +
|
||||||
|
"Argument details:")
|
||||||
|
public class YubikeyCaArgs {
|
||||||
|
|
||||||
|
@CommandLine.Option(names = {"-k", "--generate-keypair"}, description = "Generate keypair")
|
||||||
|
boolean generateKeypair = false;
|
||||||
|
|
||||||
|
@CommandLine.Option(names = {"--keypair-type"}, description = "Keypair type, e.g." +
|
||||||
|
" RSA1024, RSA2048, RSA3072, RSA4096," +
|
||||||
|
" secp192k1, secp192r1, secp224k1, secp256k1," +
|
||||||
|
" secp224r1, secp256r1, secp384r1, secp521r1;")
|
||||||
|
String keypairType;
|
||||||
|
|
||||||
|
@CommandLine.Option(names = {"-h", "--help"}, usageHelp = true, description = "Display a help message")
|
||||||
|
boolean helpRequested = false;
|
||||||
|
|
||||||
|
@CommandLine.Option(names = {"-V", "--version"}, versionHelp = true, description = "Display a version message")
|
||||||
|
boolean versionRequested = false;
|
||||||
|
}
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
package me.hatter.tools.yubikeyca;
|
||||||
|
|
||||||
|
import me.hatter.tools.commons.log.LogTool;
|
||||||
|
import me.hatter.tools.commons.log.LogTools;
|
||||||
|
import picocli.CommandLine;
|
||||||
|
|
||||||
|
public class YubikeyCaArgsUtil {
|
||||||
|
private static final LogTool log = LogTools.getLogTool(YubikeyCaArgsUtil.class);
|
||||||
|
|
||||||
|
public static YubikeyCaArgs parseArgs(String[] args) {
|
||||||
|
try {
|
||||||
|
return innerParseArgs(args);
|
||||||
|
} catch (CommandLine.UnmatchedArgumentException unmatchedArgumentException) {
|
||||||
|
log.error("Parse args failed: " + unmatchedArgumentException.getMessage());
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static YubikeyCaArgs innerParseArgs(String[] args) {
|
||||||
|
final YubikeyCaArgs yubikeyCaArgs = new YubikeyCaArgs();
|
||||||
|
final CommandLine cmd = new CommandLine(yubikeyCaArgs);
|
||||||
|
cmd.parseArgs(args);
|
||||||
|
|
||||||
|
if (cmd.isUsageHelpRequested()) {
|
||||||
|
cmd.usage(cmd.getOut());
|
||||||
|
return null;
|
||||||
|
} else if (cmd.isVersionHelpRequested()) {
|
||||||
|
cmd.printVersionHelp(cmd.getOut());
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return yubikeyCaArgs;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package me.hatter.tools.yubikeyca;
|
||||||
|
|
||||||
|
public interface YubikeyCaConstant {
|
||||||
|
String NAME = "yubikey-ca";
|
||||||
|
String VERSION = "0.1.0";
|
||||||
|
}
|
||||||
@@ -0,0 +1,55 @@
|
|||||||
|
package me.hatter.tools.yubikeyca;
|
||||||
|
|
||||||
|
import me.hatter.tools.commons.log.LogConfig;
|
||||||
|
import me.hatter.tools.commons.log.LogTool;
|
||||||
|
import me.hatter.tools.commons.log.LogTools;
|
||||||
|
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.commons.string.StringUtil;
|
||||||
|
|
||||||
|
import java.security.KeyPair;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
public class YubikeyCaMain {
|
||||||
|
private static final LogTool log;
|
||||||
|
|
||||||
|
static {
|
||||||
|
LogConfig.initMuteInfoMode();
|
||||||
|
log = LogTools.getLogTool(YubikeyCaMain.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] stringArgs) {
|
||||||
|
final YubikeyCaArgs args = YubikeyCaArgsUtil.parseArgs(stringArgs);
|
||||||
|
if (args == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (args.generateKeypair) {
|
||||||
|
generateKeyPair(args);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
log.error("Unknown command, use --help for help");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void generateKeyPair(YubikeyCaArgs args) {
|
||||||
|
if (StringUtil.isEmpty(args.keypairType)) {
|
||||||
|
log.error("Keypair type is required.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Optional<PKType> pkTypeOpt = Arrays.stream(PKType.values())
|
||||||
|
.filter(t -> t.name().equalsIgnoreCase(args.keypairType))
|
||||||
|
.findFirst();
|
||||||
|
if (!pkTypeOpt.isPresent()) {
|
||||||
|
log.error("Invalid keypair type: " + args.keypairType);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
final KeyPair keyPair = KeyPairTool.instance(pkTypeOpt.get())
|
||||||
|
.generateKeyPair().getKeyPair();
|
||||||
|
|
||||||
|
System.out.println("Private key:\n" + KeyUtil.serializePrivateKeyToPEM(keyPair.getPrivate()) + "\n");
|
||||||
|
System.out.println("Public key: \n" + KeyUtil.serializePublicKeyToPEM(keyPair.getPublic()) + "\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user