-
Notifications
You must be signed in to change notification settings - Fork 20
/
ed25519.go
77 lines (63 loc) · 2.17 KB
/
ed25519.go
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package ed25519
import (
"encoding"
"github.com/oasisprotocol/oasis-core/go/common/crypto/signature"
sdkSignature "github.com/oasisprotocol/oasis-sdk/client-sdk/go/crypto/signature"
)
var (
_ encoding.BinaryMarshaler = PublicKey{}
_ encoding.BinaryUnmarshaler = (*PublicKey)(nil)
_ encoding.TextMarshaler = PublicKey{}
_ encoding.TextUnmarshaler = (*PublicKey)(nil)
)
// PublicKey is an Ed25519 public key.
type PublicKey signature.PublicKey
// MarshalBinary encodes a public key into binary form.
func (pk PublicKey) MarshalBinary() ([]byte, error) {
return (signature.PublicKey)(pk).MarshalBinary()
}
// UnmarshalBinary decodes a binary marshaled public key.
func (pk *PublicKey) UnmarshalBinary(data []byte) error {
return (*signature.PublicKey)(pk).UnmarshalBinary(data)
}
// MarshalText encodes a public key into text form.
func (pk PublicKey) MarshalText() ([]byte, error) {
return (signature.PublicKey)(pk).MarshalText()
}
// UnmarshalText decodes a text marshaled public key.
func (pk *PublicKey) UnmarshalText(text []byte) error {
return (*signature.PublicKey)(pk).UnmarshalText(text)
}
// String returns a string representation of the public key.
func (pk PublicKey) String() string {
return (signature.PublicKey)(pk).String()
}
// Equal compares vs another public key for equality.
func (pk PublicKey) Equal(other sdkSignature.PublicKey) bool {
var opk *PublicKey
switch otherPk := other.(type) {
case PublicKey:
opk = &otherPk
case *PublicKey:
opk = otherPk
default:
return false
}
return (signature.PublicKey)(pk).Equal((signature.PublicKey)(*opk))
}
// Verify returns true iff the signature is valid for the public key over the context and message.
func (pk PublicKey) Verify(context, message, sig []byte) bool {
return signature.PublicKey(pk).Verify(signature.Context(context), message, sig)
}
// NewPublicKey creates a new public key from the given Base64 representation or
// panics.
func NewPublicKey(text string) (pk PublicKey) {
if err := pk.UnmarshalText([]byte(text)); err != nil {
panic(err)
}
return
}
func init() {
// We need to allow unregistered contexts as contexts may be runtime-dependent.
signature.UnsafeAllowUnregisteredContexts()
}