Created
February 22, 2019 12:40
-
-
Save mjarkk/4f22f3fc4ebe12b179f04c64f79df2db to your computer and use it in GitHub Desktop.
Encode and compress a string to base64 and Decode it back to a string
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
func main() { | |
input := "KkktLgEEAAD//w==" | |
out, err := base64.StdEncoding.DecodeString(input) | |
if err != nil { | |
log.Panicln(err) | |
} | |
enflated, err := ioutil.ReadAll(flate.NewReader(bytes.NewReader(out))) | |
if err != nil { | |
log.Panicln(err) | |
} | |
content, err := url.QueryUnescape(string(enflated)) | |
if err != nil { | |
log.Panicln(err) | |
} | |
fmt.Println(content) | |
} |
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
func main() { | |
content := `test` | |
var buf bytes.Buffer | |
escapedURL := url.QueryEscape(content) | |
writter, err := flate.NewWriter(&buf, flate.BestCompression) | |
if err != nil { | |
log.Panicln(err) | |
} | |
writter.Write([]byte(escapedURL)) | |
writter.Close() | |
outData := base64.StdEncoding.EncodeToString(buf.Bytes()) | |
outData = outData + "" // make the go compiler happy | |
fmt.Println(outData) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment