MCPcopy Index your code
hub / github.com/TheAlgorithms/Go / Base64Decode

Function Base64Decode

conversion/base64.go:59–83  ·  view source on GitHub ↗

Base64Decode decodes the received input base64 string into a byte slice. The implementation follows the RFC4648 standard, which is documented at https://datatracker.ietf.org/doc/html/rfc4648#section-4

(input string)

Source from the content-addressed store, hash-verified

57// The implementation follows the RFC4648 standard, which is documented
58// at https://datatracker.ietf.org/doc/html/rfc4648#section-4
59func Base64Decode(input string) []byte {
60 padding := strings.Count(input, "=") // Number of bytes which will be ignored
61 var decoded []byte
62
63 // select 4 6-bit input groups, and re-arrange them into 3 8-bit groups
64 for i := 0; i < len(input); i += 4 {
65 // translate each group into a byte using the static map
66 byteInput := [4]byte{
67 byte(strings.IndexByte(Alphabet, input[i])),
68 byte(strings.IndexByte(Alphabet, input[i+1])),
69 byte(strings.IndexByte(Alphabet, input[i+2])),
70 byte(strings.IndexByte(Alphabet, input[i+3])),
71 }
72
73 group := [3]byte{
74 byteInput[0]<<2 + byteInput[1]>>4,
75 byteInput[1]<<4 + byteInput[2]>>2,
76 byteInput[2]<<6 + byteInput[3],
77 }
78
79 decoded = append(decoded, group[:]...)
80 }
81
82 return decoded[:len(decoded)-padding]
83}

Callers 4

TestBase64DecodeFunction · 0.85
BenchmarkBase64DecodeFunction · 0.85
FuzzBase64EncodeFunction · 0.85

Calls 1

CountMethod · 0.45

Tested by 4

TestBase64DecodeFunction · 0.68
BenchmarkBase64DecodeFunction · 0.68
FuzzBase64EncodeFunction · 0.68