-
Notifications
You must be signed in to change notification settings - Fork 28
/
keys.go
64 lines (54 loc) · 1.83 KB
/
keys.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
package crypto
import (
"bufio"
"encoding/hex"
"io"
"os"
"github.com/0chain/gosdk/core/zcncrypto"
"github.com/0chain/gosdk/zmagmacore/errors"
)
// ReadKeysFile reads file existing in keysFile dir and parses public and private keys from file.
func ReadKeysFile(keysFile string) (publicKey, privateKey []byte, err error) {
const errCode = "read_keys"
reader, err := os.Open(keysFile)
if err != nil {
return nil, nil, errors.Wrap(errCode, "error while open keys file", err)
}
publicKeyHex, privateKeyHex := readKeys(reader)
err = reader.Close()
if err != nil {
return nil, nil, errors.Wrap(errCode, "error while close keys file", err)
}
publicKey, err = hex.DecodeString(publicKeyHex)
if err != nil {
return nil, nil, errors.Wrap(errCode, "error while decoding public key", err)
}
privateKey, err = hex.DecodeString(privateKeyHex)
if err != nil {
return nil, nil, errors.Wrap(errCode, "error while decoding private key", err)
}
return publicKey, privateKey, nil
}
// readKeys reads a publicKey and a privateKey from a io.Reader passed in args.
// They are assumed to be in two separate lines one followed by the other.
func readKeys(reader io.Reader) (publicKey string, privateKey string) {
scanner := bufio.NewScanner(reader)
scanner.Scan()
publicKey = scanner.Text()
scanner.Scan()
privateKey = scanner.Text()
scanner.Scan()
return publicKey, privateKey
}
// Verify verifies passed signature of the passed hash with passed public key using the signature scheme.
func Verify(publicKey, signature, hash, scheme string) (bool, error) {
signScheme := zcncrypto.NewSignatureScheme(scheme)
if signScheme != nil {
err := signScheme.SetPublicKey(publicKey)
if err != nil {
return false, err
}
return signScheme.Verify(signature, hash)
}
return false, errors.New("invalid_signature_scheme", "invalid signature scheme")
}