github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/internal/util/base64.go (about)

     1  package util
     2  
     3  import "encoding/base64"
     4  
     5  // TryDecodeBase64 attempts to base64 decode the input multiple times until
     6  // it is no longer base64 encoded. If the input is not base64 encoded, it
     7  // will return it as-is.
     8  func TryDecodeBase64(s string) []byte {
     9  	decoded, err := base64.StdEncoding.DecodeString(s)
    10  	if err != nil {
    11  		return []byte(s)
    12  	}
    13  
    14  	for {
    15  		encoded := decoded
    16  
    17  		buffer := make([]byte, base64.StdEncoding.DecodedLen(len(encoded)))
    18  		length, err := base64.StdEncoding.Decode(buffer, encoded)
    19  		if err != nil {
    20  			return decoded
    21  		}
    22  
    23  		decoded = buffer[:length]
    24  	}
    25  }