redact static method

String redact(
  1. String? value, {
  2. int visibleStart = 0,
  3. int visibleEnd = 0,
  4. String mask = '****',
})

Masks a sensitive value so it can be logged safely.

Keeps at most visibleStart leading and visibleEnd trailing characters and replaces the rest with mask. Values that are too short to be partially shown without leaking most of their content (less than twice the visible length) are fully replaced by redactedPlaceholder.

SecurityLogger.redact('eyJhbGciOiJIUzI1NiJ9.payload.sig', visibleEnd: 4);
// => '****.sig'

Implementation

static String redact(
  String? value, {
  int visibleStart = 0,
  int visibleEnd = 0,
  String mask = '****',
}) {
  if (value == null) return 'null';
  if (value.isEmpty) return '';
  final start = visibleStart < 0 ? 0 : visibleStart;
  final end = visibleEnd < 0 ? 0 : visibleEnd;
  final visible = start + end;
  if (visible == 0 || value.length < visible * 2) {
    return redactedPlaceholder;
  }
  return '${value.substring(0, start)}$mask'
      '${value.substring(value.length - end)}';
}