kunt

golang IRC bot
git clone git://git.2f30.org/kunt
Log | Files | Refs | LICENSE

commit a62a7eed8f8c931d4eacaec24c6943b04ee6539d
parent ff887e2e7e0d87451090e8dfd7f2e71f60ffc06f
Author: sin <sin@2f30.org>
Date:   Tue, 30 Apr 2013 16:28:40 +0100

update benc/bdec

Diffstat:
Msrc/bdec/bdec.go | 28++++++++++++++++++++++++++--
Msrc/benc/benc.go | 17++++++++++++++++-
2 files changed, 42 insertions(+), 3 deletions(-)

diff --git a/src/bdec/bdec.go b/src/bdec/bdec.go @@ -1,7 +1,9 @@ package main import ( + "bufio" "code.google.com/p/go.crypto/blowfish" + "encoding/gob" "flag" "fmt" "io/ioutil" @@ -24,6 +26,11 @@ func decryptBuf(c *blowfish.Cipher, a []byte) []byte { return pt } +type hdr struct { + Magic string + Len uint64 +} + func main() { log.SetPrefix("bdec: ") flag.Usage = usage @@ -39,10 +46,27 @@ func main() { log.Fatal(err) } - b, err := ioutil.ReadFile(args[1]) + fi, err := os.Open(args[1]) + if err != nil { + log.Fatal(err) + } + defer fi.Close() + + r := bufio.NewReader(fi) + g := gob.NewDecoder(r) + + // read header + h := new(hdr) + err = g.Decode(h) + if err != nil { + log.Fatal(err) + } + + // read body + buf, err := ioutil.ReadAll(r) if err != nil { log.Fatal(err) } - fmt.Fprintf(os.Stdout, "%s", decryptBuf(c, b)) + fmt.Printf("%s", decryptBuf(c, buf)[0:h.Len]) } diff --git a/src/benc/benc.go b/src/benc/benc.go @@ -2,6 +2,7 @@ package main import ( "code.google.com/p/go.crypto/blowfish" + "encoding/gob" "flag" "fmt" "io/ioutil" @@ -36,6 +37,11 @@ func encryptBuf(c *blowfish.Cipher, a []byte) []byte { return ct } +type hdr struct { + Magic string + Len uint64 +} + func main() { log.SetPrefix("benc: ") flag.Usage = usage @@ -56,5 +62,14 @@ func main() { log.Fatal(err) } - fmt.Fprintf(os.Stdout, "%s", encryptBuf(c, b)) + // Write header out + h := &hdr{"benc", uint64(len(b))} + g := gob.NewEncoder(os.Stdout) + err = g.Encode(*h) + if err != nil { + log.Fatal(err) + } + + // Write body + fmt.Printf("%s", encryptBuf(c, b)) }