read-secret 0.1.2

A rust library that provides an easy way to read and decrypt secrets from your environment variables and files.
Documentation
  • Coverage
  • 84.62%
    11 out of 13 items documented0 out of 5 items with examples
  • Size
  • Source code size: 11.47 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.59 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 13s Average build duration of successful builds.
  • all releases: 13s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Homepage
  • Repository
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • Ziqi-Yang

Read Secret

docs

A rust library that provides an easy way to read and decrypt secrets from your environment variables and files.

Code is hosted on sourcehut, and it is recommended to open issues/send patches on this platform. However, GitHub is also acceptable.

Usage

Run command cargo add read-secret or put the following code into your cargo.toml

[dependencies]
read-secret = <version>

Example

This example code is in file examples/e1.es, and you can run it by executing cargo run --example e1

use read_secret::{DecryptMethod, SecretType};
use std::process::Command;

fn main() {
    // read secret from environment variable.
    let mut dm = DecryptMethod::None;
    let st = SecretType::Env("XDG_SESSION_TYPE".to_string());
    let sr = read_secret::read_secret(st, &mut dm).unwrap();
    assert_eq!("wayland", sr);

    // read secret from file and decrypt it using gnupg.
    let mut dm = DecryptMethod::GPG;
    let st = SecretType::File("examples/pass_gpg.asc".to_string());
    let sr = read_secret::read_secret(st, &mut dm).unwrap();
    assert_eq!("El Psy Kongaroo", sr);

    // store encrypted secret inside string and decrypt it using custom command.
    let mut temp = Command::new("wc"); // avoid E0716 -- temporary value is being dropped
    let custom_command = temp.args(["-c"]);
    let mut dm = DecryptMethod::Custom(custom_command);
    let st = SecretType::String("El Psy Kongaroo".to_string());
    let sr = read_secret::read_secret(st, &mut dm).unwrap();
    assert_eq!("15\n", sr);
}