-
Notifications
You must be signed in to change notification settings - Fork 0
/
signature.go
87 lines (76 loc) · 2.18 KB
/
signature.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
78
79
80
81
82
83
84
85
86
87
// Copyright (C) 2018 go-gamc authors
//
// This file is part of the go-gamc library.
//
// the go-gamc library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// the go-gamc library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with the go-gamc library. If not, see <http://www.gnu.org/licenses/>.
//
package ed25519
import (
"gamc.pro/gamcio/go-gamc/crypto/keystore"
"errors"
)
type Message struct {
signer []byte
data []byte
}
func (m *Message) GetData() []byte {
return m.data
}
func (m *Message) GetSigner() []byte {
return m.signer
}
// Signature signature ecdsa
type Signature struct {
privateKey *PrivateKey
publicKey *PublicKey
}
// Algorithm secp256k1 algorithm
func (s *Signature) Algorithm() keystore.Algorithm {
return keystore.ED25519
}
// InitSign ed25519 init sign
func (s *Signature) InitSign(priv keystore.PrivateKey) error {
s.privateKey = priv.(*PrivateKey)
return nil
}
// Sign ed25519 sign
func (s *Signature) Sign(data []byte) (out keystore.SignResult, err error) {
if s.privateKey == nil {
return nil, errors.New("please get private key first")
}
signature := s.privateKey.Sign(data)
signer, err := s.privateKey.PublicKey().Encoded()
if err != nil {
return nil, err
}
return &Message{
signer,
signature,
}, nil
}
// InitVerify ed25519 verify init
func (s *Signature) InitVerify(pub keystore.PublicKey) error {
s.publicKey = pub.(*PublicKey)
return nil
}
// Verify ed25519 verify
func (s *Signature) Verify(data []byte, signature keystore.SignResult) (bool, error) {
if signature.GetSigner() != nil {
s.publicKey = NewPublicKey(signature.GetSigner())
}
if s.publicKey == nil {
return false, errors.New("please give public key first")
}
return s.publicKey.Verify(data, signature.GetData()), nil
}