-
Notifications
You must be signed in to change notification settings - Fork 6
/
signcheck.go
79 lines (71 loc) · 1.67 KB
/
signcheck.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
package fields
import (
"bytes"
"fmt"
"github.com/hacash/core/account"
)
// Signature verification data
type SignCheckData struct {
Signdata Sign
Stuffstr StringMax65535
}
func CreateSignCheckData(str string) SignCheckData {
l := len(str)
if l > 65535 {
l = 65535
}
return SignCheckData{
Signdata: CreateEmptySign(),
Stuffstr: CreateStringMax65535(str),
}
}
// Fill in signature
func (elm *SignCheckData) FillSign(acc *account.Account) error {
hash := CalculateHash([]byte(elm.Stuffstr.Value()))
signature, e := acc.Private.Sign(hash)
if e != nil {
return e
}
elm.Signdata = Sign{
PublicKey: acc.PublicKey,
Signature: signature.Serialize64(),
}
// fill sign success
return nil
}
func (elm SignCheckData) Size() uint32 {
return elm.Signdata.Size() + elm.Stuffstr.Size()
}
func (elm SignCheckData) Serialize() ([]byte, error) {
var buf = bytes.NewBuffer(nil)
bt1, _ := elm.Signdata.Serialize()
buf.Write(bt1)
bt2, _ := elm.Stuffstr.Serialize()
buf.Write(bt2)
return buf.Bytes(), nil
}
func (elm *SignCheckData) Parse(buf []byte, seek uint32) (uint32, error) {
var e error = nil
if seek >= uint32(len(buf)) {
return 0, fmt.Errorf("[SignCheckData.Parse] seek out of buf len.")
}
seek, e = elm.Signdata.Parse(buf, seek)
if e != nil {
return 0, e
}
seek, e = elm.Stuffstr.Parse(buf, seek)
if e != nil {
return 0, e
}
return seek, nil
}
// check sign
func (elm SignCheckData) VerifySign() (bool, Address, error) {
hash := CalculateHash([]byte(elm.Stuffstr.Value()))
addr := elm.Signdata.GetAddress()
ckok, e := account.CheckSignByHash32(hash, elm.Signdata.PublicKey, elm.Signdata.Signature)
if e != nil {
return false, nil, e
}
return ckok, addr, e
}