Created
December 14, 2018 01:28
-
-
Save Hanan-Natan/69090449df42f13625a7270fff82ffb5 to your computer and use it in GitHub Desktop.
Golang - Base64 with different index table
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/base64" | |
"fmt" | |
) | |
func base64Enc(buffer []byte, base64Table string) []byte { | |
// encode the buffer with the provided base64 index table | |
encoder := base64.NewEncoding(base64Table) | |
encodedBuffer := make([]byte, encoder.EncodedLen(len(buffer))) | |
encoder.Encode(encodedBuffer, buffer) | |
return encodedBuffer | |
} | |
func base64Dec(buffer []byte, base64Table string) []byte { | |
// decode the buffer with the procided base64 index table | |
decoder := base64.NewEncoding(base64Table) | |
decodedBuffer := make([]byte, decoder.DecodedLen(len(buffer))) | |
// ignoring CorruptInputError | |
realLength, _ := decoder.Decode(decodedBuffer, buffer) | |
return decodedBuffer[:realLength] | |
} | |
func main() { | |
base64Table := "HJIA/CB+FGKLNOP3RSlUVWXYZfbcdeaghi5kmn0pqrstuvwx89o12467MEDyzQjT" | |
encodedTest := base64Enc([]byte("testme"), base64Table) | |
decodedTest := base64Dec(encodedTest, base64Table) | |
fmt.Println("Encoded: ", string(encodedTest)) | |
fmt.Println("Decoded: ", string(decodedTest)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Test it here: https://play.golang.org/p/fFFlDsEsLGx