luks 0.4.3

Pure-Rust Library for the Linux Unified Key Setup
Documentation
use luks::KeySlotId;
use std::env;
use std::fs::File;
use std::process;

fn main() {
    let args: Vec<String> = env::args().collect();
    if args.len() < 2 {
        eprintln!("Usage: {} <device> [keyslot]", args[0]);
        process::exit(1);
    }

    let device_path = &args[1];
    let mut file = File::open(device_path).unwrap_or_else(|e| {
        eprintln!("Error opening {}: {}", device_path, e);
        process::exit(1);
    });

    let mut device = match luks::LuksHeader::open(&mut file) {
        Ok(d) => d,
        Err(e) => {
            eprintln!("Error opening LUKS device: {}", e);
            process::exit(1);
        }
    };

    let passphrase = rpassword::prompt_password("Enter passphrase: ").unwrap();
    let key = luks::UnlockKey::from(passphrase);

    if args.len() > 2 {
        let keyslot_id = args[2].parse::<KeySlotId>().unwrap_or_else(|e| {
            eprintln!("Invalid keyslot ID: {}", e);
            process::exit(1);
        });
        match device.unlock(&keyslot_id, &key) {
            Ok(_) => println!("Passphrase verified successfully for keyslot {}!", keyslot_id),
            Err(e) => {
                eprintln!("Error verifying passphrase: {}", e);
                process::exit(1);
            }
        }
    } else {
        println!("No keyslot specified, trying all keyslots...");
        let mut ids: Vec<_> = device.keyslots.keys().cloned().collect();
        ids.sort();

        let mut found = false;
        for id in ids {
            match device.unlock(&id, &key) {
                Ok(_) => {
                    println!("Passphrase verified successfully for keyslot {}!", id);
                    found = true;
                    break;
                }
                Err(_) => continue,
            }
        }
        if !found {
            println!("Passphrase could not be verified for any keyslot.");
        }
    }
}