/
signature.go
46 lines (36 loc) · 1.05 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
// Copyright 2020 Condensat Tech. All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.
package security
import (
"github.com/condensat/bank-core/security/utils"
"golang.org/x/crypto/nacl/sign"
)
func Sign(secretKey SignatureSecretKey, data []byte) ([]byte, error) {
if len(data) == 0 {
return nil, ErrNoData
}
defer utils.Memzero(secretKey[:])
var privateKey [SignatureSecretKeySize]byte
defer utils.Memzero(privateKey[:])
copy(privateKey[:], secretKey[:])
s := sign.Sign(nil, data, &privateKey)
if len(s) == 0 {
return nil, ErrSignMessage
}
return s, nil
}
func VerifySignature(publicKey SignaturePublicKey, signedMessage []byte) (bool, error) {
if len(signedMessage) <= sign.Overhead {
return false, ErrNoSignature
}
defer utils.Memzero(publicKey[:])
var pubKey [SignaturePublicKeySize]byte
defer utils.Memzero(pubKey[:])
copy(pubKey[:], publicKey[:])
_, ok := sign.Open(nil, signedMessage, &pubKey)
if !ok {
return false, ErrVerifySignature
}
return true, nil
}