aboutsummaryrefslogtreecommitdiff
path: root/file.go
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2016-09-18 14:14:27 +0200
committerDimitri Sokolyuk <demon@dim13.org>2016-09-18 14:14:27 +0200
commitf24d0f7933e6df09fb33bd65e2af85e6c2a23031 (patch)
treee0e24e0bcf62fe514097095e00b7b1de4bb7d2f7 /file.go
parent2f32015f940aa421eafbac41c6d8bdc5e227f190 (diff)
Split File
Diffstat (limited to 'file.go')
-rw-r--r--file.go59
1 files changed, 59 insertions, 0 deletions
diff --git a/file.go b/file.go
new file mode 100644
index 0000000..e321695
--- /dev/null
+++ b/file.go
@@ -0,0 +1,59 @@
+package main
+
+import (
+ "bufio"
+ "bytes"
+ "errors"
+ "io/ioutil"
+ "os"
+ "strings"
+)
+
+type B64 interface {
+ Marshal() ([]byte, error)
+ Unmarshal([]byte) error
+}
+
+type File struct {
+ Comment string
+ B64 []byte
+ Body []byte
+}
+
+const (
+ commentHdr = "untrusted comment: "
+ verifyWith = "verify with "
+ pubKey = "%s public key"
+ secKey = "%s secret key"
+ sigFrom = "signature from %s"
+)
+
+func ParseFile(fname string) (File, error) {
+ fd, err := os.Open(fname)
+ if err != nil {
+ return File{}, err
+ }
+ defer fd.Close()
+ buf := bufio.NewReader(fd)
+ comment, err := buf.ReadString('\n')
+ if err != nil {
+ return File{}, err
+ }
+ if !strings.HasPrefix(comment, commentHdr) {
+ return File{}, errors.New("expected untrusted header")
+ }
+ comment = comment[len(commentHdr):]
+ b64, err := buf.ReadBytes('\n')
+ if err != nil {
+ return File{}, err
+ }
+ body, err := ioutil.ReadAll(buf)
+ if err != nil {
+ return File{}, err
+ }
+ return File{
+ Comment: strings.TrimRight(comment, "\r\n"),
+ B64: bytes.TrimRight(b64, "\r\n"),
+ Body: body,
+ }, nil
+}