aboutsummaryrefslogtreecommitdiff
path: root/key/pub.go
diff options
context:
space:
mode:
Diffstat (limited to 'key/pub.go')
-rw-r--r--key/pub.go38
1 files changed, 38 insertions, 0 deletions
diff --git a/key/pub.go b/key/pub.go
new file mode 100644
index 0000000..bc94e30
--- /dev/null
+++ b/key/pub.go
@@ -0,0 +1,38 @@
+package key
+
+import "golang.org/x/crypto/ed25519"
+
+type Pub struct {
+ PKAlg [2]byte
+ KeyNum [8]byte
+ Key [ed25519.PublicKeySize]byte
+}
+
+func (v *Pub) Check() error {
+ if v.PKAlg != pkAlg {
+ return ErrInvalidPK
+ }
+ return nil
+}
+
+func (v *Pub) Verify(message []byte, sig *Sig) error {
+ if v.KeyNum != sig.KeyNum {
+ return ErrKeyNum
+ }
+ if !ed25519.Verify(ed25519.PublicKey(v.Key[:]), message, sig.Sig[:]) {
+ return ErrInvalidSig
+ }
+ return nil
+}
+
+func (v *Pub) MarshalBinary() ([]byte, error) {
+ return Marshal(v)
+}
+
+func ParsePub(data []byte) (*Pub, error) {
+ var pub *Pub
+ if err := Unmarshal(data, pub); err != nil {
+ return nil, err
+ }
+ return pub, pub.Check()
+}