-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkey_pair.go
46 lines (36 loc) · 1.37 KB
/
key_pair.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 2018 ProximaX Limited. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
package crypto
import "errors"
// KeyPair represent the pair of keys - private & public
type KeyPair struct {
*PrivateKey
*PublicKey
}
// NewRandomKeyPair creates a random key pair.
func NewRandomKeyPair() (*KeyPair, error) {
return NewKeyPairByEngine(CryptoEngines.DefaultEngine)
}
// NewKeyPair The public key is calculated from the private key.
// The private key must by nil
// if crypto engine is nil - default Engine
func NewKeyPair(privateKey *PrivateKey, publicKey *PublicKey, engine CryptoEngine) (*KeyPair, error) {
if engine == nil {
engine = CryptoEngines.DefaultEngine
}
if publicKey == nil {
publicKey = engine.CreateKeyGenerator().DerivePublicKey(privateKey)
} else if !engine.CreateKeyAnalyzer().IsKeyCompressed(publicKey) {
return nil, errors.New("publicKey must be in compressed form")
}
return &KeyPair{privateKey, publicKey}, nil
}
// NewKeyPairByEngine creates a random key pair that is compatible with the specified engine.
func NewKeyPairByEngine(engine CryptoEngine) (*KeyPair, error) {
return engine.CreateKeyGenerator().GenerateKeyPair()
}
// HasPrivateKey Determines if the current key pair has a private key.
func (ref *KeyPair) HasPrivateKey() bool {
return ref.PrivateKey != nil
}