Skip to content

mask

Hides a secret but for its last few characters, such as "****************7890".

Every character except the last visible becomes *. To be safe with short secrets, at most a quarter of the characters are ever shown, whatever visible asks for, so an 8-character password shows at most 2. The length of the result equals the length of the secret in characters.

use helpers4::secret::mask;

Cargo feature secret (enabled by default). To compile only this module:

cargo add helpers4 --no-default-features --features secret

or in Cargo.toml:

[dependencies]
helpers4 = { version = "0.0.6", default-features = false, features = ["secret"] }
pub fn mask(secret: &str, visible: usize) -> String
ParameterTypeDescription
secret&strThe value to mask.
visibleusizeHow many trailing characters to keep, at most a quarter of the secret.

String — The masked value.

use helpers4::secret::mask;

assert_eq!(mask("sk-abcdef1234567890", 4), "***************7890");
assert_eq!(mask("abc", 4), "***"); // too short to show anything

src/secret/mask.rs