-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
94 lines (77 loc) · 3.25 KB
/
service.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
82
83
84
85
86
87
88
89
90
91
92
93
94
//
// Copyright 2019 Insolar Technologies GmbH
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package cryptography
import (
"crypto"
"github.com/insolar/insolar/component"
"github.com/insolar/insolar/insolar"
"github.com/insolar/insolar/keystore"
"github.com/insolar/insolar/platformpolicy"
"github.com/pkg/errors"
)
type NodeCryptographyService struct {
KeyStore insolar.KeyStore `inject:""`
PlatformCryptographyScheme insolar.PlatformCryptographyScheme `inject:""`
KeyProcessor insolar.KeyProcessor `inject:""`
}
func (cs *NodeCryptographyService) GetPublicKey() (crypto.PublicKey, error) {
privateKey, err := cs.KeyStore.GetPrivateKey("")
if err != nil {
return nil, errors.Wrap(err, "[ Sign ] Failed to get private privateKey")
}
return cs.KeyProcessor.ExtractPublicKey(privateKey), nil
}
func (cs *NodeCryptographyService) Sign(payload []byte) (*insolar.Signature, error) {
privateKey, err := cs.KeyStore.GetPrivateKey("")
if err != nil {
return nil, errors.Wrap(err, "[ Sign ] Failed to get private privateKey")
}
signer := cs.PlatformCryptographyScheme.DataSigner(privateKey, cs.PlatformCryptographyScheme.IntegrityHasher())
signature, err := signer.Sign(payload)
if err != nil {
return nil, errors.Wrap(err, "[ Sign ] Failed to sign payload")
}
return signature, nil
}
func (cs *NodeCryptographyService) Verify(publicKey crypto.PublicKey, signature insolar.Signature, payload []byte) bool {
return cs.PlatformCryptographyScheme.DataVerifier(publicKey, cs.PlatformCryptographyScheme.IntegrityHasher()).Verify(signature, payload)
}
func NewCryptographyService() insolar.CryptographyService {
return &NodeCryptographyService{}
}
func NewKeyBoundCryptographyService(privateKey crypto.PrivateKey) insolar.CryptographyService {
platformCryptographyScheme := platformpolicy.NewPlatformCryptographyScheme()
keyStore := keystore.NewInplaceKeyStore(privateKey)
keyProcessor := platformpolicy.NewKeyProcessor()
cryptographyService := NewCryptographyService()
cm := component.Manager{}
cm.Register(platformCryptographyScheme)
cm.Inject(keyStore, cryptographyService, keyProcessor)
return cryptographyService
}
func NewStorageBoundCryptographyService(path string) (insolar.CryptographyService, error) {
platformCryptographyScheme := platformpolicy.NewPlatformCryptographyScheme()
keyStore, err := keystore.NewKeyStore(path)
if err != nil {
return nil, errors.Wrap(err, "[ NewStorageBoundCryptographyService ] Failed to create KeyStore")
}
keyProcessor := platformpolicy.NewKeyProcessor()
cryptographyService := NewCryptographyService()
cm := component.Manager{}
cm.Register(platformCryptographyScheme, keyStore)
cm.Inject(cryptographyService, keyProcessor)
return cryptographyService, nil
}