-
Notifications
You must be signed in to change notification settings - Fork 69
/
verifier.go
81 lines (72 loc) · 1.9 KB
/
verifier.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
package dongle
type Verifier struct {
dongle
sign []byte
}
// newVerifier returns a new Verifier instance.
func newVerifier() Verifier {
return Verifier{}
}
// FromRawString verify signature from raw string.
func (v Verifier) FromRawString(signature, message string) Verifier {
v.src = string2bytes(message)
v.sign = string2bytes(signature)
return v
}
// FromHexString decrypts from string with hex encoding.
func (v Verifier) FromHexString(signature, message string) Verifier {
d := Decode.FromString(signature).ByHex()
if d.Error != nil {
v.Error = NewDecodeError().ModeError("hex")
return v
}
v.sign = d.ToBytes()
v.src = string2bytes(message)
return v
}
// FromBase64String decrypts from string with base64 encoding.
func (v Verifier) FromBase64String(signature, message string) Verifier {
d := Decode.FromString(signature).ByBase64()
if d.Error != nil {
v.Error = NewDecodeError().ModeError("base64")
return v
}
v.sign = d.ToBytes()
v.src = string2bytes(message)
return v
}
// FromRawBytes verify signature from raw byte slice.
func (v Verifier) FromRawBytes(signature, message []byte) Verifier {
v.sign = signature
v.src = message
return v
}
// FromHexBytes verify from byte slice with hex encoding.
func (v Verifier) FromHexBytes(signature, message []byte) Verifier {
d := Decode.FromBytes(signature).ByHex()
if d.Error != nil {
v.Error = NewDecodeError().ModeError("hex")
return v
}
v.sign = d.ToBytes()
v.src = message
return v
}
// FromBase64Bytes verify from byte slice with base64 encoding.
func (v Verifier) FromBase64Bytes(signature, message []byte) Verifier {
d := Decode.FromBytes(signature).ByBase64()
if d.Error != nil {
v.Error = NewDecodeError().ModeError("base64")
return v
}
v.sign = d.ToBytes()
v.src = message
return v
}
// ToBool outputs as bool.
func (v Verifier) ToBool() bool {
if len(v.src) == 0 || len(v.sign) == 0 {
return false
}
return v.Error == nil
}