aboutsummaryrefslogtreecommitdiff
path: root/key/pub.go
blob: 2d955d77431c6b73e1a8bb19d0fc8147852a4876 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package key

import "golang.org/x/crypto/ed25519"

type Pub struct {
	PKAlg  [2]byte
	KeyNum [8]byte
	Key    [ed25519.PublicKeySize]byte
}

func (v *Pub) Err() 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 (v *Pub) UnmarshalBinary(data []byte) error {
	return unmarshal(data, v)
}

func ParsePub(data []byte) (*Pub, error) {
	pub := new(Pub)
	if err := pub.UnmarshalBinary(data); err != nil {
		return nil, err
	}
	return pub, pub.Err()
}