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)
| 57 | // The implementation follows the RFC4648 standard, which is documented |
| 58 | // at https://datatracker.ietf.org/doc/html/rfc4648#section-4 |
| 59 | func 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 | } |