-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
102 lines (83 loc) · 3.31 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
95
96
97
98
99
100
101
102
/*
* Copyright 2018 Insolar
*
* 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/core"
"github.com/insolar/insolar/keystore"
"github.com/insolar/insolar/platformpolicy"
"github.com/pkg/errors"
)
type nodeCryptographyService struct {
KeyStore core.KeyStore `inject:""`
PlatformCryptographyScheme core.PlatformCryptographyScheme `inject:""`
KeyProcessor core.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) (*core.Signature, error) {
privateKey, err := cs.KeyStore.GetPrivateKey("")
if err != nil {
return nil, errors.Wrap(err, "[ Sign ] Failed to get private privateKey")
}
signer := cs.PlatformCryptographyScheme.Signer(privateKey)
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 core.Signature, payload []byte) bool {
return cs.PlatformCryptographyScheme.Verifier(publicKey).Verify(signature, payload)
}
func NewCryptographyService() core.CryptographyService {
return &nodeCryptographyService{}
}
type inPlaceKeyStore struct {
privateKey crypto.PrivateKey
}
func (ipks *inPlaceKeyStore) GetPrivateKey(string) (crypto.PrivateKey, error) {
return ipks.privateKey, nil
}
func NewKeyBoundCryptographyService(privateKey crypto.PrivateKey) core.CryptographyService {
platformCryptographyScheme := platformpolicy.NewPlatformCryptographyScheme()
keyStore := &inPlaceKeyStore{privateKey: privateKey}
keyProcessor := platformpolicy.NewKeyProcessor()
cryptographyService := NewCryptographyService()
cm := component.Manager{}
cm.Register(platformCryptographyScheme)
cm.Inject(keyStore, cryptographyService, keyProcessor)
return cryptographyService
}
func NewStorageBoundCryptographyService(path string) (core.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
}