Skip to content

Instantly share code, notes, and snippets.

@mpfund
Created May 24, 2017 09:46
Show Gist options
  • Save mpfund/7f02f2190dfa59bfd48c466531aa8651 to your computer and use it in GitHub Desktop.
Save mpfund/7f02f2190dfa59bfd48c466531aa8651 to your computer and use it in GitHub Desktop.
encode/decode file base64
package main
import( b64 "encoding/base64"
"fmt"
"log"
"flag"
"io/ioutil"
)
func main() {
decFlag := flag.Bool("dec", false, "decode instead of encode base64")
fileName := flag.String("f", "" , "input file")
flag.Parse()
// Here's the `string` we'll encode/decode.
data,err := ioutil.ReadFile(*fileName)
if err != nil{
log.Fatal(err)
}
if(!*decFlag){
sEnc := b64.StdEncoding.EncodeToString(data)
fmt.Println(sEnc)
}else{
sDec, err := b64.StdEncoding.DecodeString(string(data))
if err != nil{
log.Fatal(err)
}
fmt.Println(string(sDec))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment