github.com/kubeshop/testkube@v1.17.23/pkg/utils/text/obfuscate.go (about)

     1  package text
     2  
     3  import "strings"
     4  
     5  const (
     6  	defaultLeftCharsShow  = 2
     7  	defaultRightCharsShow = 2
     8  )
     9  
    10  // Obfuscate string leave default characters count on left and right
    11  func Obfuscate(in string) string {
    12  	return ObfuscateLR(in, defaultLeftCharsShow, defaultRightCharsShow)
    13  }
    14  
    15  func ObfuscateLR(in string, keepLeft, keepRight int) (out string) {
    16  	if len(in) <= 0 {
    17  		return ""
    18  	}
    19  	if keepLeft > len(in) {
    20  		return strings.Repeat("*", len(in))
    21  	}
    22  	if keepRight > len(in) {
    23  		return strings.Repeat("*", len(in))
    24  	}
    25  
    26  	if keepLeft+keepRight > len(in) {
    27  		return strings.Repeat("*", len(in))
    28  	}
    29  
    30  	repeatCount := len(in) - keepLeft - keepRight
    31  	if repeatCount > 0 {
    32  		return in[:keepLeft] + strings.Repeat("*", repeatCount) + in[len(in)-keepRight:]
    33  	}
    34  
    35  	return "***"
    36  }