Skip to main content

keepass/db/types/
value.rs

1use std::{fmt::Display, ops::Deref};
2
3use secrecy::{ExposeSecret, SecretBox};
4use zeroize::Zeroize;
5
6/// Value stored in an entry field.
7///
8/// Can be either unprotected or protected
9#[derive(Debug)]
10pub enum Value<T: Zeroize> {
11	/// unprotected data
12	Unprotected(T),
13
14	/// protected data
15	Protected(SecretBox<T>),
16}
17
18impl<T: Zeroize> Drop for Value<T> {
19	fn drop(&mut self) {
20		if let Value::Unprotected(data) = self {
21			data.zeroize();
22		}
23	}
24}
25
26impl<T: Zeroize + Display> std::fmt::Display for Value<T> {
27	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
28		match self {
29			Value::Unprotected(data) => write!(f, "{}", data),
30			Value::Protected(_) => write!(f, "[redacted]"),
31		}
32	}
33}
34
35impl<T: Zeroize + Default> Default for Value<T> {
36	fn default() -> Self {
37		Value::Unprotected(Default::default())
38	}
39}
40
41impl<T: Zeroize> Value<T> {
42	/// Create a new unprotected value
43	pub fn unprotected(data: impl Into<T>) -> Self {
44		Value::Unprotected(data.into())
45	}
46
47	/// Create a new protected text data value
48	pub fn protected(data: impl Into<T>) -> Self {
49		Value::Protected(SecretBox::new(Box::new(data.into())))
50	}
51
52	/// Returns true if the value is protected.
53	pub fn is_protected(&self) -> bool {
54		matches!(self, Value::Protected(_))
55	}
56
57	/// Returns a reference to the underlying value.
58	pub fn get(&self) -> &T {
59		match self {
60			Value::Unprotected(data) => data,
61			Value::Protected(data) => data.expose_secret(),
62		}
63	}
64}
65
66impl<T: Zeroize + Clone> Clone for Value<T> {
67	fn clone(&self) -> Self {
68		match self {
69			Value::Unprotected(data) => Value::Unprotected(data.clone()),
70			Value::Protected(data) => Value::Protected(SecretBox::new(Box::new(data.expose_secret().clone()))),
71		}
72	}
73}
74
75impl<T: Zeroize + PartialEq> PartialEq for Value<T> {
76	fn eq(&self, other: &Self) -> bool {
77		match (self, other) {
78			(Value::Unprotected(a), Value::Unprotected(b)) => a == b,
79			(Value::Protected(a), Value::Protected(b)) => a.expose_secret() == b.expose_secret(),
80			_ => false,
81		}
82	}
83}
84
85impl<T: Zeroize + Eq> Eq for Value<T> {}
86
87impl<T: Zeroize> Deref for Value<T> {
88	type Target = T;
89
90	fn deref(&self) -> &Self::Target {
91		self.get()
92	}
93}
94
95#[cfg(test)]
96mod tests {
97	use super::Value;
98
99	#[test]
100	fn test_value() {
101		let unprotected: Value<String> = Value::unprotected("test");
102		let protected: Value<String> = Value::protected("test");
103		assert!(!unprotected.is_protected());
104		assert!(protected.is_protected());
105		assert!(!unprotected.is_empty());
106		assert!(!protected.is_empty());
107
108		assert_eq!(unprotected.get(), "test");
109		assert_eq!(protected.get(), "test");
110
111		assert_eq!(format!("{}", unprotected), "test");
112		assert_eq!(format!("{}", protected), "[redacted]");
113
114		assert_ne!(unprotected, protected);
115	}
116}