-
Notifications
You must be signed in to change notification settings - Fork 2
/
ed25519.go
46 lines (34 loc) · 1.07 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
package crypto
import (
"crypto/ed25519"
"encoding/hex"
log "github.com/sirupsen/logrus"
"strings"
)
type ed25519Scheme struct {
}
const Ed25519 SchemeName = "ed25519"
func (e *ed25519Scheme) Name() SchemeName {
return Ed25519
}
func (e *ed25519Scheme) Verify(appPubKey string, content string, signature string) bool {
hexSignature, err := hex.DecodeString(strings.TrimPrefix(signature, "0x"))
if err != nil {
log.WithError(err).WithField("signature", signature).Info("Can't decode signature to hex")
return false
}
publicKey, err := hex.DecodeString(strings.TrimPrefix(appPubKey, "0x"))
if err != nil {
log.WithError(err).WithField("appPubKey", appPubKey).Info("Can't decode app pub key (without 0x prefix) to hex")
return false
}
verified := ed25519.Verify(publicKey, []byte(content), hexSignature)
if !verified {
wrappedContent := "<Bytes>" + content + "</Bytes>"
verified = ed25519.Verify(publicKey, []byte(wrappedContent), hexSignature)
}
if !verified {
log.WithField("appPubKey", appPubKey).Info("Invalid content signature")
}
return verified
}