Skip to content

Commit 5e250dd

Browse files
committed
gen key pair and readme
1 parent 6ca6a2c commit 5e250dd

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed

README.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,64 @@
11
# XQR Code (eXtended QR Code) CLI
22

33
A minimal CLI for generated extended QR codes.
4+
5+
## Usage
6+
7+
```
8+
eXtended QR Codes CLI
9+
10+
Usage: xqr <COMMAND>
11+
12+
Commands:
13+
encode Encode a value into a JWT and optionally into a QR code
14+
decode Decode a JWT from a QR code
15+
generate-key-pair Generate a new ECDSA (ES256) key pair for use with XQR codes
16+
help Print this message or the help of the given subcommand(s)
17+
18+
Options:
19+
-h, --help Print help
20+
```
21+
22+
### Encode
23+
24+
```
25+
Encode a value into a JWT and optionally into a QR code
26+
27+
Usage: xqr encode [OPTIONS] --private-key <PRIVATE_KEY> --kid <KID> [VALUE]
28+
29+
Arguments:
30+
[VALUE] The value to encode
31+
32+
Options:
33+
--private-key <PRIVATE_KEY>
34+
--kid <KID> The key ID to use
35+
--display Display the QR code in the terminal
36+
--save <SAVE_PATH> Save the QR code to a file
37+
-h, --help Print help
38+
```
39+
40+
### Decode
41+
42+
```
43+
Decode a JWT from a QR code
44+
45+
Usage: xqr decode [JWT]
46+
47+
Arguments:
48+
[JWT] The JWT to decode
49+
50+
Options:
51+
-h, --help Print help
52+
```
53+
54+
### Generate key pair
55+
56+
```
57+
Generate a new ECDSA (ES256) key pair for use with XQR codes
58+
59+
Usage: xqr generate-key-pair --save <SAVE_PATH>
60+
61+
Options:
62+
--save <SAVE_PATH> Save the key pair to a file
63+
-h, --help Print help
64+
```

src/main.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ fn cli() -> Command {
3939
.arg(arg!(jwt: [JWT] "The JWT to decode"))
4040
.arg_required_else_help(true),
4141
)
42+
.subcommand(
43+
Command::new("generate-key-pair")
44+
.about("Generate a new ECDSA (ES256) key pair for use with XQR codes")
45+
.arg(
46+
arg!(save: --save <SAVE_PATH> "Save the key pair to a file")
47+
.value_parser(value_parser!(PathBuf))
48+
.required(true),
49+
)
50+
.arg_required_else_help(true),
51+
)
4252
}
4353

4454
fn main() {
@@ -124,6 +134,40 @@ fn main() {
124134
}
125135
}
126136
}
137+
Some(("generate-key-pair", sub_matches)) => {
138+
let save_path = sub_matches.get_one::<PathBuf>("save").unwrap();
139+
let pub_key_path = save_path.with_extension("pub");
140+
let key_pair = xqr::generate_key_pair();
141+
let private_key_pem = match key_pair.to_pem() {
142+
Ok(private_key_pem) => private_key_pem,
143+
Err(e) => {
144+
eprintln!("Error generating private key: {}", e);
145+
std::process::exit(1);
146+
}
147+
};
148+
let pub_key_pem = match key_pair.public_key().to_pem() {
149+
Ok(pub_key_pem) => pub_key_pem,
150+
Err(e) => {
151+
eprintln!("Error generating public key: {}", e);
152+
std::process::exit(1);
153+
}
154+
};
155+
156+
match std::fs::write(save_path, private_key_pem) {
157+
Ok(_) => println!("Saved private key to {}", save_path.display()),
158+
Err(e) => {
159+
eprintln!("Error saving private key: {}", e);
160+
std::process::exit(1);
161+
}
162+
}
163+
match std::fs::write(pub_key_path.clone(), pub_key_pem) {
164+
Ok(_) => println!("Saved public key to {}", pub_key_path.display()),
165+
Err(e) => {
166+
eprintln!("Error saving public key: {}", e);
167+
std::process::exit(1);
168+
}
169+
}
170+
}
127171
_ => unreachable!(),
128172
}
129173
}

0 commit comments

Comments
 (0)