Skip to content

Commit efcc2b7

Browse files
authored
Merge pull request #9 from pkgxdev/sign-hex-strings
`bpb sign-hex 0xfooo`
2 parents d37bf7a + 9c3ab04 commit efcc2b7

1 file changed

Lines changed: 32 additions & 1 deletion

File tree

bpb/src/main.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ fn main() -> Result<(), Error> {
3535
Some("print") => print_public_key(),
3636
Some("fingerprint") => print_fingerprint(),
3737
Some("key-id") => print_key_id(),
38+
Some("sign-hex") => {
39+
if let Some(hex) = args.next() {
40+
sign_from_hex(hex)
41+
} else {
42+
bail!("Must specify a hex string to sign, e.g.: `bpb sign-hex 1234abcd`")
43+
}
44+
}
3845
Some("--help") => print_help_message(),
3946
Some(arg) if gpg_sign_arg(arg) => verify_commit(),
4047
_ => {
@@ -59,7 +66,8 @@ fn print_help_message() -> Result<(), Error> {
5966
println!(" import <key>: Import a key from the command line.");
6067
println!(" print: Print public key in OpenPGP format.");
6168
println!(" fingerprint: Print the fingerprint of the public key.");
62-
println!(" key-id: Print the key ID of the public key.\n");
69+
println!(" key-id: Print the key ID of the public key.");
70+
println!(" sign-hex <hex>: Sign a hex string and print the signature and public key.\n");
6371
println!("See https://github.com/pkgxdev/bpb for more information.");
6472
Ok(())
6573
}
@@ -155,6 +163,29 @@ fn verify_commit() -> Result<(), Error> {
155163
Ok(())
156164
}
157165

166+
// Signs a hex string and prints the signature
167+
fn sign_from_hex(hex: String) -> Result<(), Error> {
168+
let config = Config::load()?;
169+
let service = config.service();
170+
let account = config.user_id();
171+
let secret_str = get_keychain_item(service, account)?;
172+
let secret = to_32_bytes(&secret_str)?;
173+
174+
let keypair = KeyData::load(&config, secret)?;
175+
// remove any leading 0x prefix
176+
let hex = hex.trim().to_lowercase();
177+
let hex = hex.trim_start_matches("0x");
178+
let data = hex::decode(hex)?;
179+
180+
let signed = keypair.sign(&data)?;
181+
let signature = hex::encode(signed.as_bytes());
182+
183+
let public_key = hex::encode_upper(keypair.public().as_bytes());
184+
println!("signature:\n\n{signature}\n");
185+
println!("public key:\n\n{public_key}\n");
186+
Ok(())
187+
}
188+
158189
fn delegate() -> ! {
159190
use std::process;
160191

0 commit comments

Comments
 (0)