Files
notfiles/crates/notsecrets/src/identities/mod.rs
Joseph O'Brien 6427d188e6
Some checks failed
Public Repo Readiness / Check Public Repo Readiness (push) Has been cancelled
feat(notfiles): hexagonal architecture refactor with adapters, integration tests, and notsecrets cleanup
- Extract FileStore port and InMemoryFileStore/FileStoreImpl/Reporter adapters
- Add Reporter trait to notcore with TerminalReporter and JsonReporter
- Refactor linker, package, status modules to use FileStore port
- Add integration test suite with dotfiles fixtures
- Add cross-crate integration tests
- Remove ssh_rsa identity/recipient (unsupported)
- Add rustqual.toml, .sccignore, notfiles.toml config files
- Update CLAUDE.md, README.md, AGENTS.md with current architecture
2026-07-08 13:07:42 -04:00

75 lines
1.8 KiB
Rust

use crate::error::AgeError;
pub mod x25519;
pub use x25519::X25519Identity;
pub mod ssh_ed25519;
pub use ssh_ed25519::SshEd25519Identity;
pub mod scrypt;
pub use scrypt::ScryptIdentity;
pub mod encrypted;
pub use encrypted::EncryptedIdentity;
/// The symmetric file encryption key — 16 random bytes.
#[derive(Clone, Debug)]
pub struct FileKey([u8; 16]);
impl FileKey {
pub fn new(bytes: [u8; 16]) -> Self {
Self(bytes)
}
pub fn as_bytes(&self) -> &[u8; 16] {
&self.0
}
pub fn generate() -> Self {
use rand::RngCore;
let mut bytes = [0u8; 16];
rand::thread_rng().fill_bytes(&mut bytes);
Self(bytes)
}
}
impl TryFrom<&[u8]> for FileKey {
type Error = AgeError;
fn try_from(b: &[u8]) -> Result<Self, AgeError> {
b.try_into().map(Self).map_err(|_| {
AgeError::ParseError(format!("file key must be 16 bytes, got {}", b.len()))
})
}
}
impl Drop for FileKey {
fn drop(&mut self) {
// Zero out key material on drop
self.0.iter_mut().for_each(|b| *b = 0);
}
}
/// A single recipient stanza in the age header.
#[derive(Debug, Clone)]
pub struct Stanza {
pub tag: String,
pub args: Vec<String>,
pub body: Vec<u8>,
}
/// The full age header (all stanzas + MAC).
#[derive(Debug, Clone)]
pub struct Header {
pub recipients: Vec<Stanza>,
pub mac: Vec<u8>,
}
/// Domain port: an identity that can attempt to unwrap a recipient stanza.
///
/// Returns `None` if the stanza tag/args do not match this identity type.
/// Returns `Some(Err(...))` if the stanza matches but decryption fails.
/// Returns `Some(Ok(file_key))` on success.
pub trait Identity {
fn unwrap_file_key(&self, stanza: &Stanza) -> Option<Result<FileKey, AgeError>>;
}