-
Notifications
You must be signed in to change notification settings - Fork 3
/
key_provider.go
42 lines (34 loc) · 1.17 KB
/
key_provider.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
package superdog
import (
"errors"
"log"
"strconv"
)
var (
ErrKeyNotFound = errors.New("Key not found")
)
var _ KeyProvider = &DevKeyProvider{}
// KeyProvider is an interface that wraps the GetKey method, responsible for retrieving encryption keys at a specified version.
type KeyProvider interface {
GetKey(prefix string, version uint64) (*Key, error)
CurrentKeyVersion(prefix string) (uint64, error)
}
// DevKeyProvider is a KeyProvider used for development purposes only, and contains a hardcoded key.
type DevKeyProvider struct {
DisableWarn bool // Disable log messages whenever this provider is used.
KeyVersion uint64
}
// CurrentKeyVersion returns the version number of the latest key for a given prefix
func (kp *DevKeyProvider) CurrentKeyVersion(prefix string) (uint64, error) {
return kp.KeyVersion, nil
}
func (kp *DevKeyProvider) GetKey(prefix string, version uint64) (*Key, error) {
if !kp.DisableWarn {
log.Println("USING DEV KEY PROVIDER!")
}
if version == 1 {
return NewKey(version, AES, CFB, []byte("DEFAULT XOR KEY DEFAULT XOR KEY "))
}
v := strconv.FormatUint(version, 10)
return NewKey(version, AES, GCM, []byte("DEFAULT XOR KEY DEFAULT XOR KEY" v))
}