/
ecc.go
60 lines (45 loc) · 1.44 KB
/
ecc.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
// Package ecc implements common support for error correction in sharded blob providers
package ecc
import (
"sort"
"github.com/pkg/errors"
"github.com/kopia/kopia/repo/encryption"
)
// CreateECCFunc creates an ECC for given parameters.
type CreateECCFunc func(opts *Options) (encryption.Encryptor, error)
//nolint:gochecknoglobals
var factories = map[string]CreateECCFunc{}
// RegisterAlgorithm registers new ecc algorithm.
func RegisterAlgorithm(name string, createFunc CreateECCFunc) {
factories[name] = createFunc
}
// SupportedAlgorithms returns the names of the supported ecc
// methods.
func SupportedAlgorithms() []string {
var result []string
for k := range factories {
result = append(result, k)
}
sort.Strings(result)
return result
}
// CreateAlgorithm returns new encryption.Encryptor with error correction.
func CreateAlgorithm(opts *Options) (encryption.Encryptor, error) {
factory, exists := factories[opts.Algorithm]
if !exists {
return nil, errors.New("Unknown ECC algorithm: " + opts.Algorithm)
}
return factory(opts)
}
// Parameters encapsulates all ECC parameters.
type Parameters interface {
GetECCAlgorithm() string
GetECCOverheadPercent() int
}
// CreateEncryptor returns new encryption.Encryptor with error correction.
func CreateEncryptor(p Parameters) (encryption.Encryptor, error) {
return CreateAlgorithm(&Options{
Algorithm: p.GetECCAlgorithm(),
OverheadPercent: p.GetECCOverheadPercent(),
})
}