aboutsummaryrefslogtreecommitdiff
path: root/key/pub.go
blob: 6e3784ecc5df0d785612de75b632d53e87c163a4 (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) 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 (v *Pub) UnmarshalBinary(data []byte) error {
	return Unmarshal(data, v)
}

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