aboutsummaryrefslogtreecommitdiff
path: root/zsig
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2017-07-17 14:42:08 +0200
committerDimitri Sokolyuk <demon@dim13.org>2017-07-17 14:42:08 +0200
commita6ed0b51c8a04fbbc0bbf0aec4b0fdb20e52760d (patch)
tree7f02340f6629b575273fd4f02690792fe61810c2 /zsig
parent631d34ea85961e1b7a6eaa864ee2d91476cbe922 (diff)
Move
Diffstat (limited to 'zsig')
-rw-r--r--zsig/header.go71
-rw-r--r--zsig/header_test.go56
2 files changed, 127 insertions, 0 deletions
diff --git a/zsig/header.go b/zsig/header.go
new file mode 100644
index 0000000..aa1c13b
--- /dev/null
+++ b/zsig/header.go
@@ -0,0 +1,71 @@
+package zsig
+
+import (
+ "bufio"
+ "encoding/hex"
+ "fmt"
+ "io"
+ "strconv"
+ "strings"
+ "time"
+)
+
+type ZHeader struct {
+ Date time.Time
+ KeyFile string
+ Alg string
+ BlockSize int
+ Sums [][]byte
+}
+
+const (
+ DefaultAlg = "SHA512/256"
+ DefaultBlockSize = 65536
+)
+
+func (h ZHeader) Print(w io.Writer) error {
+ fmt.Fprintf(w, "date=%v\n", h.Date.Format(time.RFC3339))
+ fmt.Fprintf(w, "key=%v\n", h.KeyFile)
+ fmt.Fprintf(w, "algorithm=%v\n", h.Alg)
+ fmt.Fprintf(w, "blocksize=%v\n\n", h.BlockSize)
+ for _, sum := range h.Sums {
+ fmt.Fprintf(w, "%x\n", sum)
+ }
+ return nil
+}
+
+func Parse(r io.Reader) (ZHeader, error) {
+ var h ZHeader
+ s := bufio.NewScanner(r)
+ for s.Scan() {
+ line := s.Text()
+ switch {
+ case strings.HasPrefix(line, "date="):
+ t, err := time.Parse(time.RFC3339, line[5:])
+ if err != nil {
+ return ZHeader{}, err
+ }
+ h.Date = t
+ case strings.HasPrefix(line, "key="):
+ h.KeyFile = line[4:]
+ case strings.HasPrefix(line, "algorithm="):
+ h.Alg = line[10:]
+ case strings.HasPrefix(line, "blocksize="):
+ i, err := strconv.Atoi(line[10:])
+ if err != nil {
+ return ZHeader{}, err
+ }
+ h.BlockSize = i
+ case line == "":
+ for s.Scan() {
+ line = s.Text()
+ sum, err := hex.DecodeString(line)
+ if err != nil {
+ return ZHeader{}, err
+ }
+ h.Sums = append(h.Sums, sum)
+ }
+ }
+ }
+ return h, s.Err()
+}
diff --git a/zsig/header_test.go b/zsig/header_test.go
new file mode 100644
index 0000000..ca10b27
--- /dev/null
+++ b/zsig/header_test.go
@@ -0,0 +1,56 @@
+package zsig
+
+import (
+ "bytes"
+ "testing"
+ "time"
+)
+
+func TestZHeader(t *testing.T) {
+ date, err := time.Parse(time.UnixDate, "Mon Jan 2 15:04:05 UTC 2006")
+ if err != nil {
+ t.Fatal(err)
+ }
+ h := ZHeader{
+ Date: date,
+ KeyFile: "some.key",
+ Alg: DefaultAlg,
+ BlockSize: DefaultBlockSize,
+ Sums: [][]byte{
+ []byte{0, 1, 2, 3, 4, 5},
+ []byte{6, 7, 8, 9, 10},
+ },
+ }
+ buf := new(bytes.Buffer)
+ if err := h.Print(buf); err != nil {
+ t.Fatal(err)
+ }
+ head, err := Parse(bytes.NewReader(buf.Bytes()))
+ if err != nil {
+ t.Fatal(err)
+ }
+ cmp(t, head, h)
+}
+
+func cmp(t *testing.T, got, want ZHeader) {
+ if !got.Date.Equal(want.Date) {
+ t.Errorf("got %v; want %v", got.Date, want.Date)
+ }
+ if got.KeyFile != want.KeyFile {
+ t.Errorf("got %v; want %v", got.KeyFile, want.KeyFile)
+ }
+ if got.Alg != want.Alg {
+ t.Errorf("got %v; want %v", got.Alg, want.Alg)
+ }
+ if got.BlockSize != want.BlockSize {
+ t.Errorf("got %v; want %v", got.BlockSize, want.BlockSize)
+ }
+ if len(got.Sums) != len(want.Sums) {
+ t.Fatalf("got %v; want %v", len(got.Sums), len(want.Sums))
+ }
+ for i := 0; i < len(got.Sums); i++ {
+ if !bytes.Equal(got.Sums[i], want.Sums[i]) {
+ t.Errorf("got %v; want %v", got.Sums[i], want.Sums[i])
+ }
+ }
+}