Skip to content

Commit c0e9d0c

Browse files
committed
Peppy long pseudonym
1 parent 0f911f5 commit c0e9d0c

File tree

1 file changed

+48
-21
lines changed

1 file changed

+48
-21
lines changed

src/bin/peppy.rs

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use libpep::high_level::keys::{
1010
use libpep::high_level::ops::{
1111
decrypt_pseudonym, encrypt_pseudonym, encrypt_pseudonym_global, rerandomize, transcrypt,
1212
};
13+
use libpep::high_level::padding::LongPseudonym;
1314
use libpep::high_level::secrets::{EncryptionSecret, PseudonymizationSecret};
1415
use libpep::internal::arithmetic::{ScalarNonZero, ScalarTraits};
1516
use rand_core::OsRng;
@@ -43,9 +44,9 @@ struct PseudonymFromOrigin {
4344

4445
#[derive(Command, Debug, Default)]
4546
#[command("pseudonym-to-origin")]
46-
#[description("Try to convert a pseudonym back to its origin identifier.")]
47+
#[description("Try to convert a pseudonym (or long pseudonym) back to its origin identifier.")]
4748
struct PseudonymToOrigin {
48-
#[positional("pseudonym-hex", 1, 1)]
49+
#[positional("pseudonym-hex...", 1, 100)]
4950
args: Vec<String>,
5051
}
5152

@@ -191,35 +192,61 @@ fn main() {
191192
}
192193
Some(Sub::PseudonymFromOrigin(arg)) => {
193194
let origin = arg.args[0].as_bytes();
194-
let pseudonym: Pseudonym = match origin.len().cmp(&16) {
195+
match origin.len().cmp(&16) {
195196
Ordering::Greater => {
196-
eprintln!("Origin identifier must be 16 bytes long.");
197-
std::process::exit(1);
197+
eprintln!("Warning: Origin identifier is longer than 16 bytes, using long pseudonym with PKCS#7 padding.");
198+
let long_pseudonym = LongPseudonym::from_bytes_padded(origin)
199+
.expect("Failed to create long pseudonym");
200+
eprint!("Long pseudonym ({} blocks): ", long_pseudonym.0.len());
201+
let hex_blocks: Vec<String> =
202+
long_pseudonym.0.iter().map(|p| p.encode_as_hex()).collect();
203+
println!("{}", hex_blocks.join(" "));
198204
}
199205
Ordering::Less => {
200206
let mut padded = [0u8; 16];
201207
padded[..origin.len()].copy_from_slice(origin);
202-
Pseudonym::from_bytes(&padded)
208+
let pseudonym = Pseudonym::from_bytes(&padded);
209+
eprint!("Pseudonym: ");
210+
println!("{}", &pseudonym.encode_as_hex());
211+
}
212+
Ordering::Equal => {
213+
let pseudonym = Pseudonym::from_bytes(origin.try_into().unwrap());
214+
eprint!("Pseudonym: ");
215+
println!("{}", &pseudonym.encode_as_hex());
203216
}
204-
Ordering::Equal => Pseudonym::from_bytes(origin.try_into().unwrap()),
205217
};
206-
eprint!("Pseudonym: ");
207-
println!("{}", &pseudonym.encode_as_hex());
208218
}
209219
Some(Sub::PseudonymToOrigin(arg)) => {
210-
let pseudonym = Pseudonym::decode_from_hex(&arg.args[0]).expect("Invalid pseudonym.");
211-
let origin = pseudonym.as_bytes();
212-
if origin.is_none() {
213-
eprintln!("Pseudonym does not have a lizard representation.");
214-
std::process::exit(1);
220+
if arg.args.len() == 1 {
221+
// Single pseudonym - try lizard decoding
222+
let pseudonym =
223+
Pseudonym::decode_from_hex(&arg.args[0]).expect("Invalid pseudonym.");
224+
let origin = pseudonym.as_bytes();
225+
if origin.is_none() {
226+
eprintln!("Pseudonym does not have a lizard representation.");
227+
std::process::exit(1);
228+
}
229+
eprint!("Origin: ");
230+
println!(
231+
"{}",
232+
String::from_utf8_lossy(
233+
&origin.expect("Lizard representation cannot be displayed.")
234+
)
235+
);
236+
} else {
237+
// Multiple pseudonyms - decode as long pseudonym
238+
let pseudonyms: Vec<Pseudonym> = arg
239+
.args
240+
.iter()
241+
.map(|hex| Pseudonym::decode_from_hex(hex).expect("Invalid pseudonym"))
242+
.collect();
243+
let long_pseudonym = LongPseudonym(pseudonyms);
244+
let text = long_pseudonym
245+
.to_string_padded()
246+
.expect("Failed to decode long pseudonym");
247+
eprint!("Origin: ");
248+
println!("{}", text);
215249
}
216-
eprint!("Origin: ");
217-
println!(
218-
"{}",
219-
String::from_utf8_lossy(
220-
&origin.expect("Lizard representation cannot be displayed.")
221-
)
222-
);
223250
}
224251
Some(Sub::Encrypt(arg)) => {
225252
let public_key =

0 commit comments

Comments
 (0)