age-setup 0.1.1

a rust library that creates X25519 key pairs and uses age as its foundation and is very easy to use
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
# age-setup

> **Simple, secure X25519 keypair generation for age encryption — with validation and automatic memory zeroization.**

<p align="center">
  <a href="https://crates.io/crates/age-setup"><img src="https://img.shields.io/crates/v/age-setup.svg?style=flat-square&logo=rust" alt="crates.io version"></a>
  <a href="https://docs.rs/age-setup"><img src="https://img.shields.io/docsrs/age-setup?style=flat-square&logo=docs.rs" alt="docs.rs"></a>
  <a href="https://crates.io/crates/age-setup"><img src="https://img.shields.io/crates/d/age-setup?style=flat-square&logo=rust" alt="crates.io downloads"></a>
  <a href="https://github.com/neuxdotdev/age-setup/blob/main/LICENSE"><img src="https://img.shields.io/crates/l/age-setup?style=flat-square" alt="license"></a>
  <a href="https://github.com/neuxdotdev/age-setup/actions"><img src="https://img.shields.io/github/actions/workflow/status/neuxdotdev/age-setup/ci.yml?branch=main&style=flat-square&logo=github" alt="build status"></a>
  <a href="https://www.rust-lang.org"><img src="https://img.shields.io/badge/Rust-2021-orange?style=flat-square&logo=rust" alt="Rust 2021"></a>
</p>

---

## Table of Contents

- [Features]#features
- [Installation]#installation
- [Quick Start]#quick-start
- [Usage Examples]#usage-examples
    - [Basic Key Generation]#basic-key-generation
    - [Error Handling]#error-handling
    - [Accessing Keys]#accessing-keys
- [API Reference]#api-reference
    - [Functions]#functions
    - [Types]#types
    - [Error Types]#error-types
- [Security Features]#security-features
    - [Memory Zeroization]#memory-zeroization
    - [Display Redaction]#display-redaction
    - [Public Key Validation]#public-key-validation
- [Development]#development
    - [Project Structure]#project-structure
    - [Building]#building
    - [Testing]#testing
    - [Benchmarking]#benchmarking
- [Contributing]#contributing
- [License]#license
- [Credits]#credits

---

## Features

**Simple API** — One function call generates a complete keypair  
**Secure by Default** — Automatic memory zeroization on drop  
**Validated Keys** — Public keys guaranteed to start with "age1"  
️ **Privacy-First** — Secret keys redacted in `Display` output  
**Zero Config** — Works out of the box with sensible defaults  
**Well-Tested** — Comprehensive test coverage across all modules  
**Fast** — Built on the battle-tested `age` crate  
**Documented** — Full API documentation with examples

---

## Installation

Add `age-setup` to your `Cargo.toml`:

```toml
[dependencies]
age-setup = "0.1"
```

Or use `cargo add`:

```bash
cargo add age-setup
```

**Minimum Supported Rust Version (MSRV):** 1.70.0

---

## Quick Start

Generate an age keypair in just two lines:

```rust
use age_setup::build_keypair;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let keypair = build_keypair()?;

    println!("Public key: {}", keypair.public);
    println!("Secret key: {}", keypair.secret); // Prints: [REDACTED]

    // Access raw secret (use with caution!)
    let secret_str = keypair.secret.expose();

    Ok(())
}
```

That's it! You now have a cryptographically secure X25519 keypair ready for age encryption.

---

## Usage Examples

### Basic Key Generation

The simplest way to generate a keypair:

```rust
use age_setup::build_keypair;

let keypair = build_keypair().expect("failed to generate keypair");

// Public key is validated and guaranteed to start with "age1"
assert!(keypair.public.expose().starts_with("age1"));

// Use the keys with age encryption
println!("Share this public key: {}", keypair.public);
```

### Error Handling

Handle different error types explicitly:

```rust
use age_setup::{build_keypair, Error};

match build_keypair() {
    Ok(keypair) => {
        println!("✓ Generated keypair successfully");
        println!("  Public: {}", keypair.public);
    }
    Err(Error::Generation(e)) => {
        eprintln!("✗ Key generation failed: {}", e);
    }
    Err(Error::Validation(e)) => {
        eprintln!("✗ Key validation failed: {}", e);
    }
    Err(Error::Security(e)) => {
        eprintln!("✗ Security operation failed: {}", e);
    }
}
```

### Accessing Keys

```rust
use age_setup::build_keypair;

let keypair = build_keypair()?;

// Public key (safe to display)
let public_str: &str = keypair.public.expose();
let public_owned: String = keypair.public.to_string();

// Secret key (handle with care!)
let secret_str: &str = keypair.secret.expose();

// Convert to AsRef<str> for compatibility
fn print_key(key: &impl AsRef<str>) {
    println!("{}", key.as_ref());
}
print_key(&keypair.public);

// Secret is automatically zeroized when dropped
{
    let temp_keypair = build_keypair()?;
    // Use temp_keypair...
} // <-- Memory is securely wiped here
```

### Integration with age Encryption

```rust
use age_setup::build_keypair;
use std::io::Write;

let keypair = build_keypair()?;

// Encrypt a file with age
let encrypted = age::encrypt(
    &age::x25519::Recipient::from_str(keypair.public.expose())?,
    plaintext.as_bytes(),
)?;

// Save the secret key securely
let key_path = dirs::home_dir().unwrap().join(".age/key.txt");
std::fs::write(&key_path, format!("# age identity\n{}", keypair.secret.expose()))?;

// Set restrictive permissions (Unix only)
#[cfg(unix)]
{
    use std::os::unix::fs::PermissionsExt;
    std::fs::set_permissions(&key_path, std::fs::Permissions::from_mode(0o600))?;
}
```

---

## API Reference

### Functions

#### `build_keypair()`

Generates a new age X25519 keypair.

```rust
pub fn build_keypair() -> Result<KeyPair>
```

**Returns:**

- `Ok(KeyPair)` — A new keypair with validated public key
- `Err(Error)` — If generation or validation fails

**Example:**

```rust
let keypair = age_setup::build_keypair()?;
```

---

### Types

#### `KeyPair`

A keypair consisting of an age public key and its corresponding secret key.

```rust
pub struct KeyPair {
    pub public: PublicKey,
    pub secret: SecretKey,
}
```

**Fields:**

- `public: PublicKey` — The public key (starts with "age1")
- `secret: SecretKey` — The secret key (zeroized on drop)

**Example:**

```rust
let keypair = build_keypair()?;
println!("Public: {}", keypair.public);
println!("Secret: {}", keypair.secret); // Prints: [REDACTED]
```

---

#### `PublicKey`

An age public key, guaranteed to start with "age1".

```rust
pub struct PublicKey(String);
```

**Methods:**

##### `expose(&self) -> &str`

Returns the raw string representation of the public key.

```rust
let public_str = keypair.public.expose();
assert!(public_str.starts_with("age1"));
```

**Traits Implemented:**

- `Display` — Prints the public key
- `Debug` — Debug representation
- `Clone` — Can be cloned safely
- `AsRef<str>` — Convert to string reference

---

#### `SecretKey`

An age secret key that securely wipes its memory when dropped.

```rust
pub struct SecretKey { /* private fields */ }
```

**Methods:**

##### `expose(&self) -> &str`

Exposes the raw secret key as a string.

**️Security Warning:** Only use this when absolutely necessary, as it exposes the secret.

```rust
let secret_str = keypair.secret.expose();
// Use secret_str with caution!
```

**Traits Implemented:**

- `Display` — Prints `[REDACTED]` instead of the actual secret
- `Debug` — Debug representation (does not expose secret)
- `Clone` — Can be cloned (new copy is also zeroized on drop)
- `Drop` — Automatically zeroizes memory when dropped

---

### Error Types

#### `Error`

Main error type for the crate.

```rust
pub enum Error {
    Generation(GenerationError),
    Validation(ValidationError),
    Security(SecurityError),
}
```

**Variants:**

##### `Error::Generation`

Error during key generation (e.g., internal library failure).

```rust
pub enum GenerationError {
    IdentityCreationFailed,
}
```

##### `Error::Validation`

Error validating public key format.

```rust
pub enum ValidationError {
    InvalidPublicKeyFormat { reason: String },
}
```

**Example:**

```rust
use age_setup::types::PublicKey;

let result = PublicKey::new("invalid_key".to_string());
assert!(result.is_err()); // Does not start with "age1"
```

##### `Error::Security`

Error during security operations (e.g., memory wipe).

```rust
pub enum SecurityError {
    MemoryWipeFailed,
}
```

**All errors implement:**

- `std::error::Error` — Standard error trait
- `Display` — Human-readable error messages
- `Debug` — Detailed debug information

---

## Security Features

### Memory Zeroization

The `SecretKey` type automatically zeroes its memory when dropped, preventing secrets from lingering in RAM.

```rust
{
    let keypair = build_keypair()?;
    // Secret is stored in memory
    let secret = keypair.secret.expose();
    // ... use secret ...
} // <-- Memory is securely overwritten with zeros here
```

**Implementation:**

- Uses the [`zeroize`]https://crates.io/crates/zeroize crate
- Compiler optimizations cannot remove the zeroing operation
- Applies to both the original and cloned instances

---

### Display Redaction

Secret keys are automatically redacted when printed:

```rust
let keypair = build_keypair()?;

println!("{}", keypair.secret);        // Prints: [REDACTED]
println!("{:?}", keypair.secret);      // Prints: SecretKey { ... }

// Only expose() shows the actual secret
println!("{}", keypair.secret.expose()); // Prints actual key
```

This prevents accidental logging or display of sensitive material.

---

### Public Key Validation

All public keys are validated to ensure they start with the "age1" prefix:

```rust
use age_setup::types::PublicKey;

// Valid key
let valid = PublicKey::new("age1abcdef...".to_string())?;

// Invalid key (will return error)
let invalid = PublicKey::new("ssh-rsa AAAA...".to_string());
assert!(invalid.is_err());
```

**Validation Rules:**

- Must not be empty
- Must start with "age1"
- Automatically applied during keypair generation

---

## Development

### Project Structure

```
age-setup/
├── src/
│   ├── apis/
│   │   ├── build.rs          # Keypair building API
│   │   └── mod.rs
│   ├── build/
│   │   ├── identity.rs       # Internal identity generation
│   │   ├── recipient.rs      # Recipient extraction
│   │   └── mod.rs
│   ├── errors/
│   │   ├── buildings.rs      # Generation errors
│   │   ├── security.rs       # Security errors
│   │   ├── validation.rs     # Validation errors
│   │   └── mod.rs
│   ├── security/
│   │   ├── zeroize.rs        # Memory zeroization utilities
│   │   └── mod.rs
│   ├── types/
│   │   ├── keypair.rs        # KeyPair structure
│   │   ├── public_key.rs     # PublicKey type
│   │   ├── secret_key.rs     # SecretKey type
│   │   ├── validation.rs     # Validation logic
│   │   └── mod.rs
│   └── lib.rs                # Library entry point
├── benches/
│   └── keygen.rs             # Benchmarks
├── Cargo.toml
└── README.md
```

---

### Building

```bash
# Clone the repository
git clone https://github.com/neuxdotdev/age-setup.git
cd age-setup

# Build the library
cargo build

# Build with optimizations
cargo build --release

# Build documentation
cargo doc --open
```

---

### Testing

The library has comprehensive test coverage across all modules.

```bash
# Run all tests
cargo test

# Run tests with output
cargo test -- --nocapture

# Run tests for a specific module
cargo test types::

# Run tests with coverage (requires cargo-tarpaulin)
cargo tarpaulin --out Html
```

**Test Coverage:**

- Keypair generation
- Public key validation
- Secret key zeroization
- Display implementations
- Error handling
- Type conversions

---

### Benchmarking

Performance benchmarks are available using Criterion:

```bash
# Run benchmarks
cargo bench

# Run specific benchmark
cargo bench keygen

# Generate benchmark report
cargo bench -- --save-baseline main
```

**Benchmark Results** (example on Apple M1):

```
keygen/build_keypair   time:   [45.2 µs 45.8 µs 46.5 µs]
```

---

## Contributing

Contributions are welcome! Here's how you can help:

1. **Fork the repository**

    ```bash
    git clone https://github.com/neuxdotdev/age-setup.git
    ```

2. **Create a feature branch**

    ```bash
    git checkout -b feat/your-feature
    ```

3. **Make your changes**
    - Write tests for new functionality
    - Ensure all tests pass: `cargo test`
    - Format code: `cargo fmt`
    - Run clippy: `cargo clippy`

4. **Commit your changes**

    ```bash
    git commit -m "feat: add new feature"
    ```

    Use [Conventional Commits]https://www.conventionalcommits.org/ format:
    - `feat:` — New feature
    - `fix:` — Bug fix
    - `docs:` — Documentation changes
    - `test:` — Test additions/changes
    - `refactor:` — Code refactoring

5. **Push and create a Pull Request**
    ```bash
    git push origin feat/your-feature
    ```

### Code of Conduct

Please be respectful and constructive in all interactions. We're here to build great software together.

---

## License

**MIT License** — see [LICENSE](LICENSE) for details.

This library is free to use in both open-source and commercial projects.

```
Copyright (c) 2026 neuxdotdev

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
```

---

## Credits

- Built on the excellent [`age`]https://crates.io/crates/age crate by [@str4d]https://github.com/str4d
- Uses [`zeroize`]https://crates.io/crates/zeroize for secure memory handling
- Inspired by the simplicity of the [age specification]https://age-encryption.org/

---

## Related Projects

- **[age]https://age-encryption.org/** — A simple, modern, and secure file encryption tool
- **[rage]https://github.com/str4d/rage** — A Rust implementation of age
- **[age-plugin]https://crates.io/crates/age-plugin** — Framework for age plugins

---

## FAQ

### Why use this instead of calling age directly?

`age-setup` provides:

- **Validation** — Ensures public keys always have the correct format
- **Security** — Automatic memory zeroization of secrets
- **Simplicity** — Single function call instead of multiple steps
- **Safety** — Type-safe wrappers prevent misuse

### Is this production-ready?

Yes! The library:

- Uses battle-tested dependencies (`age`, `zeroize`)
- Has comprehensive test coverage
- Follows Rust security best practices
- Is actively maintained

### How do I save keys to disk?

```rust
use age_setup::build_keypair;
use std::fs;

let keypair = build_keypair()?;

// Save public key
fs::write("key.pub", keypair.public.expose())?;

// Save secret key (with proper permissions!)
let secret_content = format!("# age identity\n{}", keypair.secret.expose());
fs::write("key.txt", secret_content)?;

#[cfg(unix)]
{
    use std::os::unix::fs::PermissionsExt;
    fs::set_permissions("key.txt", fs::Permissions::from_mode(0o600))?;
}
```

### Can I use this with async code?

Yes! Key generation is CPU-bound and very fast (~45µs), so you can call it directly:

```rust
tokio::task::spawn_blocking(|| {
    age_setup::build_keypair()
}).await??;
```

---

> **Repository:** https://github.com/neuxdotdev/age-setup  
> **Issues:** https://github.com/neuxdotdev/age-setup/issues  
> **Crates.io:** https://crates.io/crates/age-setup  
> **Documentation:** https://docs.rs/age-setup

---

**Made with ️ by [neuxdotdev](https://github.com/neuxdotdev)**