1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// This file is part of helpers4.
// Copyright (C) 2025 baxyz
// SPDX-License-Identifier: LGPL-3.0-or-later
/// The text shown in place of a redacted secret.
pub const REDACTED: &str = "[REDACTED]";
/// Replaces every occurrence of each of `secrets` in `text` with `[REDACTED]`.
///
/// Meant for text that is about to be logged or shown (a command line, an HTTP dump, an error
/// message) when you know the secret values. Longer secrets are replaced first, so a secret that
/// contains another one is hidden whole. Empty secrets are ignored. Only exact occurrences are
/// found: a secret that was transformed (encoded, split over lines) is not.
///
/// # Arguments
///
/// - `text` - The text to clean.
/// - `secrets` - The values to hide.
///
/// # Returns
///
/// The text with every occurrence replaced.
///
/// # Examples
///
/// ```
/// use helpers4::secret::redact;
///
/// let line = "curl -H 'Authorization: Bearer abc123' https://x.org?key=abc123";
/// assert_eq!(
/// redact(line, &["abc123"]),
/// "curl -H 'Authorization: Bearer [REDACTED]' https://x.org?key=[REDACTED]"
/// );
/// ```