Skip to content

Instantly share code, notes, and snippets.

@Hanan-Natan
Created December 14, 2018 01:28
Show Gist options
  • Save Hanan-Natan/69090449df42f13625a7270fff82ffb5 to your computer and use it in GitHub Desktop.
Save Hanan-Natan/69090449df42f13625a7270fff82ffb5 to your computer and use it in GitHub Desktop.
Golang - Base64 with different index table
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))
}
@Hanan-Natan
Copy link
Author

Hanan-Natan commented Dec 14, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment