pub struct Guard { /* private fields */ }Expand description
请求守卫:一个可跨请求共享、可跨线程克隆的加密句柄。
构造之后的一切加解密都经由它转发到 EncryptionManager;克隆只是
Arc 引用计数递增,不含任何密钥拷贝。
Implementations§
Source§impl Guard
impl Guard
Sourcepub fn new(manager: EncryptionManager) -> Self
pub fn new(manager: EncryptionManager) -> Self
从一个已经建好的 Manager 构造。
Sourcepub fn from_master_key(
master_key: &[u8],
default_identifier: &str,
) -> Result<Self>
pub fn from_master_key( master_key: &[u8], default_identifier: &str, ) -> Result<Self>
从 32 字节主密钥构造(内部走 crate::factory::EncryptionManagerFactory)。
构造时即完成全部校验(主密钥长度、默认算法是否注册),配置错了就在启动时炸。
Sourcepub fn from_env() -> Result<Self>
pub fn from_env() -> Result<Self>
从进程环境变量构造。
读取 ENCRYPTION_MASTER_KEY(必需)与 ENCRYPTION_ALGORITHM(可选,
缺省 aes-256-gcm)。主密钥值的格式见 Self::from_env_with。
Sourcepub fn from_env_with<F: Fn(&str) -> Option<String>>(lookup: F) -> Result<Self>
pub fn from_env_with<F: Fn(&str) -> Option<String>>(lookup: F) -> Result<Self>
从可注入的查找函数构造:测试与自定义配置源(KMS / 配置中心)走这里。
主密钥取值 lookup("ENCRYPTION_MASTER_KEY"),格式为显式前缀
hex:<十六进制> / base64:<Base64>;无前缀时自动判定:去掉首尾空白后
恰为 64 个十六进制字符按 hex,否则按 Base64。解码后必须恰好 32 字节,
否则 Error::InvalidMasterKey;算法名未注册时冒泡
Error::UnknownIdentifier。
use encryption::guard::Guard;
// 假 lookup:真实场景用 std::env::var 或配置中心
let lookup = |key: &str| match key {
"ENCRYPTION_MASTER_KEY" => Some("00".repeat(32)), // 裸 hex(64 位)
_ => None,
};
let guard = Guard::from_env_with(lookup)?;
assert_eq!(guard.manager().default_identifier(), "aes-256-gcm");
let blob = guard.encrypt("13800138000".as_bytes())?;
assert_eq!(guard.decrypt(&blob)?, "13800138000".as_bytes());Sourcepub fn manager(&self) -> &EncryptionManager
pub fn manager(&self) -> &EncryptionManager
共享的 Manager。总是存在。
Sourcepub fn identifiers(&self) -> Vec<&str>
pub fn identifiers(&self) -> Vec<&str>
已注册算法标识(健康检查 / 日志用)。
Trait Implementations§
Source§impl<'ex> Extractible<'ex> for Guard
impl<'ex> Extractible<'ex> for Guard
Source§impl FromRequest for Guard
impl FromRequest for Guard
Source§fn from_request(req: &HttpRequest, _payload: &mut Payload) -> Self::Future
fn from_request(req: &HttpRequest, _payload: &mut Payload) -> Self::Future
Self from request parts asynchronously.Source§impl<'a> FromRequest<'a> for Guard
impl<'a> FromRequest<'a> for Guard
Source§async fn from_request(req: &'a Request, _body: &mut RequestBody) -> Result<Self>
async fn from_request(req: &'a Request, _body: &mut RequestBody) -> Result<Self>
Source§impl<'r> FromRequest<'r> for Guard
impl<'r> FromRequest<'r> for Guard
Source§type Error = GuardNotConfigured
type Error = GuardNotConfigured
Auto Trait Implementations§
impl !RefUnwindSafe for Guard
impl !UnwindSafe for Guard
impl Freeze for Guard
impl Send for Guard
impl Sync for Guard
impl Unpin for Guard
impl UnsafeUnpin for Guard
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<S, T> FromRequest<S, ViaParts> for T
impl<S, T> FromRequest<S, ViaParts> for T
Source§type Rejection = <T as FromRequestParts<S>>::Rejection
type Rejection = <T as FromRequestParts<S>>::Rejection
Source§fn from_request(
req: Request<Body>,
state: &S,
) -> impl Future<Output = Result<T, <T as FromRequest<S, ViaParts>>::Rejection>>
fn from_request( req: Request<Body>, state: &S, ) -> impl Future<Output = Result<T, <T as FromRequest<S, ViaParts>>::Rejection>>
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoCollection<T> for T
impl<T> IntoCollection<T> for T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Paint for Twhere
T: ?Sized,
impl<T> Paint for Twhere
T: ?Sized,
Source§fn fg(&self, value: Color) -> Painted<&T>
fn fg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the foreground set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like red() and
green(), which have the same functionality but are
pithier.
§Example
Set foreground color to white using fg():
use yansi::{Paint, Color};
painted.fg(Color::White);Set foreground color to white using white().
use yansi::Paint;
painted.white();Source§fn bright_black(&self) -> Painted<&T>
fn bright_black(&self) -> Painted<&T>
Source§fn bright_red(&self) -> Painted<&T>
fn bright_red(&self) -> Painted<&T>
Source§fn bright_green(&self) -> Painted<&T>
fn bright_green(&self) -> Painted<&T>
Source§fn bright_yellow(&self) -> Painted<&T>
fn bright_yellow(&self) -> Painted<&T>
Source§fn bright_blue(&self) -> Painted<&T>
fn bright_blue(&self) -> Painted<&T>
Source§fn bright_magenta(&self) -> Painted<&T>
fn bright_magenta(&self) -> Painted<&T>
Source§fn bright_cyan(&self) -> Painted<&T>
fn bright_cyan(&self) -> Painted<&T>
Source§fn bright_white(&self) -> Painted<&T>
fn bright_white(&self) -> Painted<&T>
Source§fn bg(&self, value: Color) -> Painted<&T>
fn bg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the background set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like on_red() and
on_green(), which have the same functionality but
are pithier.
§Example
Set background color to red using fg():
use yansi::{Paint, Color};
painted.bg(Color::Red);Set background color to red using on_red().
use yansi::Paint;
painted.on_red();Source§fn on_primary(&self) -> Painted<&T>
fn on_primary(&self) -> Painted<&T>
Source§fn on_magenta(&self) -> Painted<&T>
fn on_magenta(&self) -> Painted<&T>
Source§fn on_bright_black(&self) -> Painted<&T>
fn on_bright_black(&self) -> Painted<&T>
Source§fn on_bright_red(&self) -> Painted<&T>
fn on_bright_red(&self) -> Painted<&T>
Source§fn on_bright_green(&self) -> Painted<&T>
fn on_bright_green(&self) -> Painted<&T>
Source§fn on_bright_yellow(&self) -> Painted<&T>
fn on_bright_yellow(&self) -> Painted<&T>
Source§fn on_bright_blue(&self) -> Painted<&T>
fn on_bright_blue(&self) -> Painted<&T>
Source§fn on_bright_magenta(&self) -> Painted<&T>
fn on_bright_magenta(&self) -> Painted<&T>
Source§fn on_bright_cyan(&self) -> Painted<&T>
fn on_bright_cyan(&self) -> Painted<&T>
Source§fn on_bright_white(&self) -> Painted<&T>
fn on_bright_white(&self) -> Painted<&T>
Source§fn attr(&self, value: Attribute) -> Painted<&T>
fn attr(&self, value: Attribute) -> Painted<&T>
Enables the styling Attribute value.
This method should be used rarely. Instead, prefer to use
attribute-specific builder methods like bold() and
underline(), which have the same functionality
but are pithier.
§Example
Make text bold using attr():
use yansi::{Paint, Attribute};
painted.attr(Attribute::Bold);Make text bold using using bold().
use yansi::Paint;
painted.bold();Source§fn rapid_blink(&self) -> Painted<&T>
fn rapid_blink(&self) -> Painted<&T>
Source§fn quirk(&self, value: Quirk) -> Painted<&T>
fn quirk(&self, value: Quirk) -> Painted<&T>
Enables the yansi Quirk value.
This method should be used rarely. Instead, prefer to use quirk-specific
builder methods like mask() and
wrap(), which have the same functionality but are
pithier.
§Example
Enable wrapping using .quirk():
use yansi::{Paint, Quirk};
painted.quirk(Quirk::Wrap);Enable wrapping using wrap().
use yansi::Paint;
painted.wrap();Source§fn clear(&self) -> Painted<&T>
👎Deprecated since 1.0.1: renamed to resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.
fn clear(&self) -> Painted<&T>
renamed to resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.
Source§fn whenever(&self, value: Condition) -> Painted<&T>
fn whenever(&self, value: Condition) -> Painted<&T>
Conditionally enable styling based on whether the Condition value
applies. Replaces any previous condition.
See the crate level docs for more details.
§Example
Enable styling painted only when both stdout and stderr are TTYs:
use yansi::{Paint, Condition};
painted.red().on_yellow().whenever(Condition::STDOUTERR_ARE_TTY);