forked from mattermost/mattermost-plugin-servicenow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hashed_key.go
56 lines (43 loc) · 1.3 KB
/
hashed_key.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
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
// See License for license information.
package kvstore
import (
"crypto/sha512"
"fmt"
"github.com/mattermost/mattermost-server/v5/model"
)
type hashedKeyStore struct {
store KVStore
prefix string
}
var _ KVStore = (*hashedKeyStore)(nil)
func NewHashedKeyStore(s KVStore, prefix string) KVStore {
return &hashedKeyStore{
store: s,
prefix: prefix,
}
}
func (s hashedKeyStore) Load(key string) ([]byte, error) {
return s.store.Load(hashKey(s.prefix, key))
}
func (s hashedKeyStore) Store(key string, data []byte) error {
return s.store.Store(hashKey(s.prefix, key), data)
}
func (s hashedKeyStore) StoreTTL(key string, data []byte, ttlSeconds int64) error {
return s.store.StoreTTL(hashKey(s.prefix, key), data, ttlSeconds)
}
func (s hashedKeyStore) StoreWithOptions(key string, value []byte, opts model.PluginKVSetOptions) (bool, error) {
return s.store.StoreWithOptions(hashKey(s.prefix, key), value, opts)
}
func (s hashedKeyStore) Delete(key string) error {
return s.store.Delete(hashKey(s.prefix, key))
}
func hashKey(prefix, hashableKey string) string {
if hashableKey == "" {
return prefix
}
h := sha512.New()
_, _ = h.Write([]byte(hashableKey))
hashedKey := fmt.Sprintf("%s%x", prefix, h.Sum(nil))
return hashedKey[:50]
}