aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2016-09-18 13:23:50 +0200
committerDimitri Sokolyuk <demon@dim13.org>2016-09-18 13:23:50 +0200
commitab444fef442808f7ff41f70992f19f4b2417841d (patch)
tree7202ce2b0e4ca9ea52eaed908a3eb4e512592017
parent753387164ce9d0b610ceae93830cfe1f26f0fdd2 (diff)
ReadFull
-rw-r--r--enc.go49
-rw-r--r--enc_test.go11
-rw-r--r--main.go38
-rw-r--r--pub.go34
-rw-r--r--pub_test.go11
-rw-r--r--sig.go34
-rw-r--r--sig_test.go11
7 files changed, 163 insertions, 25 deletions
diff --git a/enc.go b/enc.go
new file mode 100644
index 0000000..32d9aa3
--- /dev/null
+++ b/enc.go
@@ -0,0 +1,49 @@
+package main
+
+import (
+ "bytes"
+ "io"
+ //"encoding/base64"
+ "encoding/binary"
+
+ "dim13.org/signify/base64"
+
+ "golang.org/x/crypto/ed25519"
+)
+
+type EncKey struct {
+ PKAlg [2]byte
+ KDFAlg [2]byte
+ KDFRounds uint32 // network byte order
+ Salt [16]byte
+ Checksum [8]byte
+ KeyNum [KeyNumLen]byte
+ SecKey [ed25519.PrivateKeySize]byte
+}
+
+func (v *EncKey) Unmarshal(b []byte) error {
+ buf := bytes.NewBuffer(b)
+ dec := base64.NewDecoder(base64.StdEncoding, buf)
+ io.ReadFull(dec, v.PKAlg[:])
+ io.ReadFull(dec, v.KDFAlg[:])
+ binary.Read(dec, binary.BigEndian, &v.KDFRounds)
+ io.ReadFull(dec, v.Salt[:])
+ io.ReadFull(dec, v.Checksum[:])
+ io.ReadFull(dec, v.KeyNum[:])
+ io.ReadFull(dec, v.SecKey[:])
+ return nil
+}
+
+func (v *EncKey) Marshal() ([]byte, error) {
+ buf := new(bytes.Buffer)
+ enc := base64.NewEncoder(base64.StdEncoding, buf)
+ enc.Write(v.PKAlg[:])
+ enc.Write(v.KDFAlg[:])
+ binary.Write(enc, binary.BigEndian, v.KDFRounds)
+ enc.Write(v.Salt[:])
+ enc.Write(v.Checksum[:])
+ enc.Write(v.KeyNum[:])
+ enc.Write(v.SecKey[:])
+ enc.Close()
+ return buf.Bytes(), nil
+}
diff --git a/enc_test.go b/enc_test.go
new file mode 100644
index 0000000..6cbdb67
--- /dev/null
+++ b/enc_test.go
@@ -0,0 +1,11 @@
+package main
+
+import "testing"
+
+var b64enc = []byte(`RWRCSwAAAACzJBN2gC5//jVvDiV76rs4m2aKXkljqDpbOC0bBf7abZhV/Zygr6b0KIbSI56JQutwzsQeouxnnHuVTZp3IW4M9qdpe5Nh8Jrr5g7r0rHLPxEPmcv/dNru6ZjqI7CcGsY=`)
+
+func TestUnmarshalEnc(t *testing.T) {
+ v := new(EncKey)
+ v.Unmarshal(b64enc)
+ t.Logf("%+v", v)
+}
diff --git a/main.go b/main.go
index 16b1c18..47a636c 100644
--- a/main.go
+++ b/main.go
@@ -14,9 +14,12 @@ import (
"golang.org/x/crypto/ed25519"
)
+var (
+ PKAlg = [2]byte{'E', 'd'}
+ KDFAlg = [2]byte{'B', 'K'}
+)
+
const (
- PKAlg = "Ed"
- KDFAlg = "BK"
commentHdr = "untrusted comment: "
verifyWith = "verify with "
pubKey = "%s public key"
@@ -63,34 +66,19 @@ func main() {
}
*/
- log.Println(parseFile("test.sig"))
+ log.Println(parseFile("testcases/test.sig"))
s, _ := base64.StdEncoding.DecodeString("RWRCSwAAAACzJBN2gC5//jVvDiV76rs4m2aKXkljqDpbOC0bBf7abZhV/Zygr6b0KIbSI56JQutwzsQeouxnnHuVTZp3IW4M9qdpe5Nh8Jrr5g7r0rHLPxEPmcv/dNru6ZjqI7CcGsY=")
fmt.Printf("%v\n", s)
-}
-
-const KeyNumLen = 8
-type EncKey struct {
- PKAlg [2]byte
- KDFAlg [2]byte
- KDFRounds uint32 // network byte order
- Salt [16]byte
- Checksum [8]byte
- KeyNum [KeyNumLen]byte
- SecKey [ed25519.PrivateKeySize]byte
-}
-
-type PubKey struct {
- PKAlg [2]byte
- KeyNum [KeyNumLen]byte
- PubKey [ed25519.PublicKeySize]byte
+ ss := &Sig{
+ PKAlg: PKAlg,
+ }
+ rand.Read(ss.KeyNum[:])
+ ms, _ := ss.Marshal()
+ fmt.Println(string(ms))
}
-type Sig struct {
- PKAlg [2]byte
- KeyNum [KeyNumLen]byte
- Sig [ed25519.SignatureSize]byte
-}
+const KeyNumLen = 8
type File struct {
Comment string
diff --git a/pub.go b/pub.go
new file mode 100644
index 0000000..f94faf4
--- /dev/null
+++ b/pub.go
@@ -0,0 +1,34 @@
+package main
+
+import (
+ "bytes"
+ "encoding/base64"
+ "io"
+
+ "golang.org/x/crypto/ed25519"
+)
+
+type PubKey struct {
+ PKAlg [2]byte
+ KeyNum [KeyNumLen]byte
+ PubKey [ed25519.PublicKeySize]byte
+}
+
+func (v *PubKey) Unmarshal(b []byte) error {
+ buf := bytes.NewBuffer(b)
+ dec := base64.NewDecoder(base64.StdEncoding, buf)
+ io.ReadFull(dec, v.PKAlg[:])
+ io.ReadFull(dec, v.KeyNum[:])
+ io.ReadFull(dec, v.PubKey[:])
+ return nil
+}
+
+func (v *PubKey) Marshal() ([]byte, error) {
+ buf := new(bytes.Buffer)
+ enc := base64.NewEncoder(base64.StdEncoding, buf)
+ enc.Write(v.PKAlg[:])
+ enc.Write(v.KeyNum[:])
+ enc.Write(v.PubKey[:])
+ enc.Close()
+ return buf.Bytes(), nil
+}
diff --git a/pub_test.go b/pub_test.go
new file mode 100644
index 0000000..a0a37b9
--- /dev/null
+++ b/pub_test.go
@@ -0,0 +1,11 @@
+package main
+
+import "testing"
+
+var b64pub = []byte(`RWRbOC0bBf7abfanaXuTYfCa6+YO69Kxyz8RD5nL/3Ta7umY6iOwnBrG`)
+
+func TestUnmarshalPub(t *testing.T) {
+ v := new(PubKey)
+ v.Unmarshal(b64pub)
+ t.Logf("%+v", v)
+}
diff --git a/sig.go b/sig.go
new file mode 100644
index 0000000..5abea82
--- /dev/null
+++ b/sig.go
@@ -0,0 +1,34 @@
+package main
+
+import (
+ "bytes"
+ "encoding/base64"
+ "io"
+
+ "golang.org/x/crypto/ed25519"
+)
+
+type Sig struct {
+ PKAlg [2]byte
+ KeyNum [KeyNumLen]byte
+ Sig [ed25519.SignatureSize]byte
+}
+
+func (v *Sig) Unmarshal(b []byte) error {
+ buf := bytes.NewBuffer(b)
+ dec := base64.NewDecoder(base64.StdEncoding, buf)
+ io.ReadFull(dec, v.PKAlg[:])
+ io.ReadFull(dec, v.KeyNum[:])
+ io.ReadFull(dec, v.Sig[:])
+ return nil
+}
+
+func (v *Sig) Marshal() ([]byte, error) {
+ buf := new(bytes.Buffer)
+ enc := base64.NewEncoder(base64.StdEncoding, buf)
+ enc.Write(v.PKAlg[:])
+ enc.Write(v.KeyNum[:])
+ enc.Write(v.Sig[:])
+ enc.Close()
+ return buf.Bytes(), nil
+}
diff --git a/sig_test.go b/sig_test.go
new file mode 100644
index 0000000..ff6d5ec
--- /dev/null
+++ b/sig_test.go
@@ -0,0 +1,11 @@
+package main
+
+import "testing"
+
+var b64sig = []byte(`RWRbOC0bBf7abaGwGtq45KLDK63tgcF7CO4qTZSlTKCSbZTYlDfFm/DISQ60u+/jEzrk22suvXXAEsxQTe2xUOfV90get1YRGQo=`)
+
+func TestUnmarshalSig(t *testing.T) {
+ v := new(Sig)
+ v.Unmarshal(b64sig)
+ t.Logf("%+v", v)
+}