1 package cn.home1.environment;
2
3 import cn.home1.oss.lib.common.crypto.AesCbcKey;
4 import cn.home1.oss.lib.common.crypto.AesKeyGenerator;
5 import cn.home1.oss.lib.common.crypto.JwtKeyGenerator;
6 import cn.home1.oss.lib.common.crypto.RsaKeys;
7
8 import org.springframework.boot.Banner;
9 import org.springframework.boot.CommandLineRunner;
10 import org.springframework.boot.builder.SpringApplicationBuilder;
11
12 public class Keygen implements CommandLineRunner {
13
14 public static void main(final String... args) {
15 new SpringApplicationBuilder()
16 .sources(Keygen.class)
17 .bannerMode(Banner.Mode.OFF)
18 .run(args);
19 }
20
21 @Override
22 public void run(final String... args) throws Exception {
23 if (args.length == 0) {
24 System.err.println(this.usage());
25 } else {
26 final String option = args[0];
27 final String result = this.generateKey(option);
28 if (result != null) {
29 System.out.print(result);
30 } else {
31 System.err.println(this.usage());
32 }
33 }
34 }
35
36 public String generateKey(final String option) {
37 final String spec;
38 final String result;
39 switch (option) {
40 case "-aes":
41 spec = AesCbcKey.keySpec(256);
42 result = new AesKeyGenerator(spec).generateKey().toString();
43 break;
44 case "-jwt":
45 spec = "HS512";
46 result = new JwtKeyGenerator(spec).generateKey().toString();
47 break;
48 case "-rsa":
49 final int keySize = 1024;
50 result = RsaKeys.generateRsaKey(keySize);
51 break;
52
53 default:
54 result = null;
55 break;
56 }
57 return result;
58 }
59
60 private String usage() {
61 return "" +
62 "Usage: java -jar oss-lib-common-*.jar [OPTION]\n" +
63 "\t-aes\n" +
64 "\t\tgenerate random AES CBC key\n" +
65 "\t-jwt\n" +
66 "\t\tgenerate random JWT HS512 key\n" +
67 "\t-rsa\n" +
68 "\t\tgenerate random RSA1024 key\n" +
69 "";
70 }
71 }