Why does k8s secrets need to be base64 encoded when configmaps does not?

Secrets can contain binary data (the type is map[string][]byte), and byte arrays are base64-encoded in JSON serialization. ConfigMaps only contain string data (the type is map[string]string), so the JSON serialization just outputs the string. In 1.10, ConfigMaps have a new binaryData field that allows storing binary data, which is base64-encoded, just like secrets. https://github.com/kubernetes/kubernetes/pull/57938

Encode/Decode base64

The len prefix is superficial and causes the invalid utf-8 error: package main import ( “encoding/base64” “fmt” “log” ) func main() { str := base64.StdEncoding.EncodeToString([]byte(“Hello, playground”)) fmt.Println(str) data, err := base64.StdEncoding.DecodeString(str) if err != nil { log.Fatal(“error:”, err) } fmt.Printf(“%q\n”, data) } (Also here) Output SGVsbG8sIHBsYXlncm91bmQ= “Hello, playground” EDIT: I read too fast, the len … Read more

Remove trailing “=” when base64 encoding

The = is padding. <!————> Wikipedia says An additional pad character is allocated which may be used to force the encoded output into an integer multiple of 4 characters (or equivalently when the unencoded binary text is not a multiple of 3 bytes) ; these padding characters must then be discarded when decoding but still … Read more

How to check whether a string is Base64 encoded or not

You can use the following regular expression to check if a string constitutes a valid base64 encoding: ^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)?$ In base64 encoding, the character set is [A-Z, a-z, 0-9, and + /]. If the rest length is less than 4, the string is padded with ‘=’ characters. ^([A-Za-z0-9+/]{4})* means the string starts with 0 or more … Read more