-
Notifications
You must be signed in to change notification settings - Fork 1
/
key_store_sss.go
41 lines (38 loc) · 1.07 KB
/
key_store_sss.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
package keycrypter
import (
"fmt"
"io/ioutil"
"os"
)
// LoadKeySSS loads key from the sharing key files
func LoadKeySSS(paths []string) ([]byte, error) {
shares := make([][]byte, len(paths))
for i, path := range paths {
s, err := ioutil.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("Read key file [%s] failed: %v", path, err)
}
shares[i] = s
}
return DecryptKeySSS(shares)
}
// StoreKeySSS encrypt the key using Shamir's Secret Sharing scheme, then write key shares to files.
// `k` is the threshold number of sharing key parts to reconstruct.
func StoreKeySSS(paths []string, k byte, key []byte) error {
n := byte(len(paths))
shares, err := EncryptKeySSS(key, n, k)
if err != nil {
return fmt.Errorf("Encrypt key failed: %v", err)
}
for i, path := range paths {
fd, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return fmt.Errorf("Open key file [%s] failed: %v", path, err)
}
_, err = fd.Write(shares[i])
if err != nil {
return fmt.Errorf("Write key file [%s] failed: %s", path, err)
}
}
return nil
}