aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--file.go59
-rw-r--r--file_test.go19
-rw-r--r--main.go50
3 files changed, 81 insertions, 47 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
+}
diff --git a/file_test.go b/file_test.go
new file mode 100644
index 0000000..218da39
--- /dev/null
+++ b/file_test.go
@@ -0,0 +1,19 @@
+package main
+
+import "testing"
+
+var tc = []string{
+ "testcases/dim13.sec",
+ "testcases/dim13.pub",
+ "testcases/test.sig",
+}
+
+func TestParseFile(t *testing.T) {
+ for _, tf := range tc {
+ f, err := ParseFile(tf)
+ if err != nil {
+ t.Error(err)
+ }
+ t.Log(f)
+ }
+}
diff --git a/main.go b/main.go
index 47a636c..7e26857 100644
--- a/main.go
+++ b/main.go
@@ -1,15 +1,11 @@
package main
import (
- "bufio"
"crypto/rand"
"encoding/base64"
"flag"
"fmt"
- "io/ioutil"
- "log"
"os"
- "strings"
"golang.org/x/crypto/ed25519"
)
@@ -20,13 +16,9 @@ var (
)
const (
- commentHdr = "untrusted comment: "
- verifyWith = "verify with "
- pubKey = "%s public key"
- secKey = "%s secret key"
- sigFrom = "signature from %s"
- verFailed = "signature verfication failed"
- verOK = "Signature Verfied"
+ verFailed = "signature verfication failed"
+ verOK = "Signature Verfied"
+ KeyNumLen = 8
)
/*
@@ -66,7 +58,6 @@ func main() {
}
*/
- log.Println(parseFile("testcases/test.sig"))
s, _ := base64.StdEncoding.DecodeString("RWRCSwAAAACzJBN2gC5//jVvDiV76rs4m2aKXkljqDpbOC0bBf7abZhV/Zygr6b0KIbSI56JQutwzsQeouxnnHuVTZp3IW4M9qdpe5Nh8Jrr5g7r0rHLPxEPmcv/dNru6ZjqI7CcGsY=")
fmt.Printf("%v\n", s)
@@ -78,14 +69,6 @@ func main() {
fmt.Println(string(ms))
}
-const KeyNumLen = 8
-
-type File struct {
- Comment string
- Key string
- Body []byte
-}
-
func Generate(pubFile, secFile, comment string, rounds int) error {
pub, sec, err := ed25519.GenerateKey(rand.Reader)
if err != nil {
@@ -101,30 +84,3 @@ func Generate(pubFile, secFile, comment string, rounds int) error {
func Sign() {}
func Verify() {}
-
-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
- }
- comment = strings.TrimRight(comment, "\r\n")
- log.Println(comment)
-
- b64, err := buf.ReadString('\n')
- if err != nil {
- return File{}, err
- }
- b64 = strings.TrimRight(b64, "\r\n")
- body, err := ioutil.ReadAll(buf)
- return File{
- Comment: comment,
- Key: b64,
- Body: body,
- }, nil
-}