forked from tongxin97/noise
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.go
43 lines (33 loc) · 1.19 KB
/
mod.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
package eddsa
import (
"github.com/guyu96/noise/crypto/edwards25519"
"github.com/guyu96/noise/signature"
"github.com/pkg/errors"
)
var _ signature.Scheme = (*policy)(nil)
type policy struct{}
func (p policy) Sign(privateKey, messageBuf []byte) ([]byte, error) {
return Sign(privateKey, messageBuf)
}
func (p policy) Verify(publicKeyBuf, messageBuf, signatureBuf []byte) error {
return Verify(publicKeyBuf, messageBuf, signatureBuf)
}
func New() *policy {
return new(policy)
}
func Sign(privateKeyBuf, messageBuf []byte) ([]byte, error) {
if len(privateKeyBuf) != edwards25519.PrivateKeySize {
return nil, errors.Errorf("edwards25519: private key expected to be %d bytes, but is %d bytes", edwards25519.PrivateKeySize, len(privateKeyBuf))
}
return edwards25519.Sign(privateKeyBuf, messageBuf), nil
}
func Verify(publicKeyBuf, messageBuf, signature []byte) error {
if len(publicKeyBuf) != edwards25519.PublicKeySize {
return errors.Errorf("edwards25519: public key expected to be %d bytes, but is %d bytes", edwards25519.PublicKeySize, len(publicKeyBuf))
}
if edwards25519.Verify(publicKeyBuf, messageBuf, signature) {
return nil
} else {
return errors.New("unable to verify signature")
}
}