From d7cd50d491a484d2f5b499e6b02fbc4d34b50e71 Mon Sep 17 00:00:00 2001 From: nsz Date: Tue, 13 Dec 2011 21:01:20 +0100 Subject: [PATCH] key.Id +fmt fixes --- pkg/Makefile | 6 ++++++ pkg/document/document.go | 19 +++++++++++-------- pkg/key/key.go | 7 ++++++- pkg/key/key_test.go | 13 +++++++++++++ pkg/server/server.go | 8 ++++---- pkg/store/store.go | 2 +- 6 files changed, 41 insertions(+), 14 deletions(-) diff --git a/pkg/Makefile b/pkg/Makefile index 4917a98..896c219 100644 --- a/pkg/Makefile +++ b/pkg/Makefile @@ -11,12 +11,16 @@ DIRS=\ ../cmd/epoint-client\ ../cmd/epoint-server\ +fmt.dirs: $(addsuffix .fmt, $(DIRS)) clean.dirs: $(addsuffix .clean, $(DIRS)) install.dirs: $(addsuffix .install, $(DIRS)) nuke.dirs: $(addsuffix .nuke, $(DIRS)) test.dirs: $(addsuffix .test, $(DIRS)) testshort.dirs: $(addsuffix .testshort, $(DIRS)) +%.fmt: + gofmt -w $*/*.go + %.clean: +$(MAKE) -C $* clean @@ -31,6 +35,8 @@ testshort.dirs: $(addsuffix .testshort, $(DIRS)) +@echo test $* +@$(MAKE) -C $* test.clean >$*/test.out 2>&1 || (echo TEST FAIL $*; cat $*/test.out; exit 1) +fmt: fmt.dirs + clean: clean.dirs install: install.dirs diff --git a/pkg/document/document.go b/pkg/document/document.go index 22065fc..eecd68a 100644 --- a/pkg/document/document.go +++ b/pkg/document/document.go @@ -160,13 +160,13 @@ type Cert struct { type DebitCert struct { Cert - Beneficiary string + Beneficiary string } type CreditCert struct { Cert - Drawer string - DebitCert string + Drawer string + DebitCert string } type BounceCert struct { @@ -180,6 +180,7 @@ type BounceCert struct { References []string } +// Common cert part of a debit or credit cert func ToCert(v interface{}) (cert *Cert, err error) { cert = new(Cert) switch x := v.(type) { @@ -209,7 +210,7 @@ func Id(c *Signed) string { return fmt.Sprintf("%040X", h.Sum()) } -// parse an epoint document without checking the signature and format details +// Parse an epoint document without checking the signature and format details func Parse(s []byte) (iv interface{}, c *Signed, err error) { c, err = ParseSigned(s) if err != nil { @@ -223,7 +224,7 @@ func Parse(s []byte) (iv interface{}, c *Signed, err error) { return } -// format and sign an epoint document +// Format and sign an epoint document func Format(iv interface{}, key *openpgp.Entity) (s []byte, c *Signed, err error) { doc, err := FormatStruct(iv) if err != nil { @@ -241,7 +242,7 @@ func Format(iv interface{}, key *openpgp.Entity) (s []byte, c *Signed, err error return } -// verify an epoint document, return the cleaned version as well +// Verify an epoint document, return the cleaned version as well func Verify(c *Signed, key openpgp.KeyRing) (err error) { msg := bytes.NewBuffer(c.Body) sig := bytes.NewBuffer(c.Signature) @@ -251,7 +252,7 @@ func Verify(c *Signed, key openpgp.KeyRing) (err error) { return } -// sign body with given secret key +// Sign body with given secret key func Sign(body []byte, key *openpgp.Entity) (c *Signed, err error) { c = new(Signed) c.Hash = "SHA256" @@ -494,6 +495,7 @@ func parseStruct(v reflect.Value, fields map[string]string, seen map[string]bool return } +// ParseStruct parses an epoint document and returns a struct representation func ParseStruct(doc *Document) (iv interface{}, err error) { switch doc.Type { case "Draft": @@ -580,7 +582,7 @@ func formatStruct(v reflect.Value, doc *Document) (err error) { return } -// turn a struct into a document +// FormatStruct turns a struct into a document func FormatStruct(iv interface{}) (doc *Document, err error) { v := reflect.ValueOf(iv) if v.Kind() != reflect.Ptr || v.IsNil() || v.Elem().Kind() != reflect.Struct { @@ -593,6 +595,7 @@ func FormatStruct(iv interface{}) (doc *Document, err error) { return } +// ParseFields parses a key value sequence into a fields map func ParseFields(s []byte) (fields map[string]string, rest []byte, err error) { rest = s fields = make(map[string]string) diff --git a/pkg/key/key.go b/pkg/key/key.go index f5f9143..29e92bd 100644 --- a/pkg/key/key.go +++ b/pkg/key/key.go @@ -109,6 +109,11 @@ func Holder(r []byte, issuer, denomination string) (e *openpgp.Entity, err error return New(DsaKey(r), 0, "Holder of "+issuer, denomination, "") } +// Key id (fingerprint) +func Id(e *openpgp.Entity) string { + return fmt.Sprintf("%X", e.PrimaryKey.Fingerprint) +} + // Check the issuer and denomination associated with the given pgp key func Check(e *openpgp.Entity) (isIssuer bool, issuer, denomination string, err error) { // allow multiple identities, use the first one that looks like an epoint uid @@ -116,7 +121,7 @@ func Check(e *openpgp.Entity) (isIssuer bool, issuer, denomination string, err e denomination = id.UserId.Comment if id.UserId.Name == "Issuer" { isIssuer = true - issuer = fmt.Sprintf("%X", e.PrimaryKey.Fingerprint) + issuer = Id(e) return } const prefix = "Holder of " diff --git a/pkg/key/key_test.go b/pkg/key/key_test.go index 1e594fe..0226d12 100644 --- a/pkg/key/key_test.go +++ b/pkg/key/key_test.go @@ -37,6 +37,19 @@ func TestKey(t *testing.T) { } } +func TestId(t *testing.T) { + idwant := "E51F405B809FA2DEA760603F9D33F730611CBCD9" + key, err := Issuer([]byte("rand"), "") + if err != nil { + t.Errorf("Issuer failed: %s", err) + return + } + id := Id(key) + if id != idwant { + t.Errorf("Id failed: expected %s, got %s", idwant, id) + } +} + func TestIssuerHolder(t *testing.T) { denomination := "1/100 EUR" priv, err := Issuer([]byte("issuer-rand"), denomination) diff --git a/pkg/server/server.go b/pkg/server/server.go index 71bd3fc..614c256 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -24,7 +24,7 @@ func StoreSk(sk *openpgp.Entity) (err error) { if err != nil { return } - return db.Set("key", fmt.Sprintf("%X", sk.PrimaryKey.Fingerprint), b.Bytes()) + return db.Set("key", key.Id(sk), b.Bytes()) } func GetKeys(fpr string) (es openpgp.EntityList, err error) { @@ -75,7 +75,7 @@ func AddKeys(d []byte) (err error) { if err != nil { return } - fpr := fmt.Sprintf("%X", e.PrimaryKey.Fingerprint) + fpr := key.Id(e) err = db.Set("key", fpr, b.Bytes()) if err != nil { return @@ -259,7 +259,7 @@ func NewDebitCert(draftid string, draft *document.Draft) (*document.DebitCert, e } cert.LastDebitSerial = oldcert.LastDebitSerial cert.LastCreditSerial = oldcert.LastCreditSerial - if _,ok := iv.(*document.DebitCert); ok { + if _, ok := iv.(*document.DebitCert); ok { cert.LastDebitSerial = oldcert.Serial } else { cert.LastCreditSerial = oldcert.Serial @@ -315,7 +315,7 @@ func NewCreditCert(draftid string, draft *document.Draft, dcertid string, dcert } cert.LastDebitSerial = oldcert.LastDebitSerial cert.LastCreditSerial = oldcert.LastCreditSerial - if _,ok := iv.(*document.DebitCert); ok { + if _, ok := iv.(*document.DebitCert); ok { cert.LastDebitSerial = oldcert.Serial } else { cert.LastCreditSerial = oldcert.Serial diff --git a/pkg/store/store.go b/pkg/store/store.go index 2a6d2f8..a27935c 100644 --- a/pkg/store/store.go +++ b/pkg/store/store.go @@ -42,7 +42,7 @@ func (c *Conn) Get(name, k string) (v []byte, err error) { v, err = ioutil.ReadFile(filepath.Join(c.path, name, k)) if err != nil { if p, ok := err.(*os.PathError); ok && p.Err == os.ENOENT { - err = NotFoundError{name+"/"+k} + err = NotFoundError{name + "/" + k} } } return -- 2.20.1