From 80d1dd5d2b1369fc186ddcca6a85910e82796387 Mon Sep 17 00:00:00 2001 From: Richard Clubb Date: Fri, 10 Jan 2025 14:58:02 +0000 Subject: [PATCH 1/2] AUTHORS.md: adding @richClubb and licensing my contributions I, Richard Clubb, hereby agree to license all contributions I make to this project under the terms of the Apache License, Version 2.0. --- AUTHORS.md | 1 + 1 file changed, 1 insertion(+) diff --git a/AUTHORS.md b/AUTHORS.md index 7bff44e9..a16d9fba 100644 --- a/AUTHORS.md +++ b/AUTHORS.md @@ -19,3 +19,4 @@ contributions under the terms of the [Apache License, Version 2.0] * Yin Guanhao ([@sopium](https://github.com/sopium)) * Will Speak ([@iwillspeak](https://github.com/iwillspeak)) * Zach Reizner ([@zachreizner](https://github.com/zachreizner)) +* Rich Clubb ([@richClubb](https://github.com/richClubb)) From 776805b47ba0a41762c1c85354551c5f7488f0ff Mon Sep 17 00:00:00 2001 From: Richard Clubb Date: Fri, 10 Jan 2025 14:59:01 +0000 Subject: [PATCH 2/2] Adding small doc example for secrecy --- secrecy/docs/example.md | 46 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 secrecy/docs/example.md diff --git a/secrecy/docs/example.md b/secrecy/docs/example.md new file mode 100644 index 00000000..2394db00 --- /dev/null +++ b/secrecy/docs/example.md @@ -0,0 +1,46 @@ +# Secrecy Example + +This is a very simple implementation of the `secrecy` crate to get started. + +The idea is that the values put into the `Password` struct are easy to use for debugging / logging but you can't easily expose the secrets. + +```rust +use std::fmt::{Debug, Display, Formatter, Result}; + +use secrecy::{ExposeSecret, SecretBox}; + +#[derive(Debug)] +struct Password { + value: SecretBox +} + +impl Password { + pub fn new(value: String) -> Self { + Self {value: SecretBox::new(Box::new(value))} + } +} + +impl Display for Password { + fn fmt(&self, f: &mut Formatter) -> Result { + if cfg!(debug_assertions) { + write!(f, "{}", self.value.expose_secret()) + } + else { + write!(f,"REDACTED") + } + } +} + +fn main() { + + let basic_password: SecretBox = SecretBox::new(Box::new(String::from("pass"))); + println!("Manually typed password printed with expose_secret(): \"{}\"", basic_password.expose_secret()); + + /* + This method would easily allow you to create logs with usernames / passwords visible for debugging + but have confidence that they wouldn't get printed in release builds. + */ + let custom_typed_password:Password = Password::new("test".to_string()); + println!("Custom struct password printed with traits: \"{}\"", custom_typed_password); +} +``` \ No newline at end of file