forked from sourcegraph/sourcegraph-public-snapshot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils_test.go
More file actions
52 lines (39 loc) · 1.4 KB
/
utils_test.go
File metadata and controls
52 lines (39 loc) · 1.4 KB
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
package encryption
import (
"context"
"encoding/base64"
"encoding/json"
"testing"
)
type base64Key struct{}
var base64KeyVersion = KeyVersion{Type: "base64"}
func (k base64Key) Encrypt(ctx context.Context, plaintext []byte) ([]byte, error) {
return []byte(base64.StdEncoding.EncodeToString(plaintext)), nil
}
func (k base64Key) Decrypt(ctx context.Context, ciphertext []byte) (*Secret, error) {
decoded, err := base64.StdEncoding.DecodeString(string(ciphertext))
s := NewSecret(string(decoded))
return &s, err
}
func (k base64Key) Version(ctx context.Context) (KeyVersion, error) {
return base64KeyVersion, nil
}
type base64PlusJunkKey struct{ base64Key }
var base64PlusJunkKeyVersion = KeyVersion{Type: "base64-plus-junk"}
func (k base64PlusJunkKey) Encrypt(ctx context.Context, plaintext []byte) ([]byte, error) {
encrypted, err := k.base64Key.Encrypt(ctx, plaintext)
return append([]byte(`!@#$`), encrypted...), err
}
func (k base64PlusJunkKey) Decrypt(ctx context.Context, ciphertext []byte) (*Secret, error) {
return k.base64Key.Decrypt(ctx, ciphertext[4:])
}
func (k base64PlusJunkKey) Version(ctx context.Context) (KeyVersion, error) {
return base64PlusJunkKeyVersion, nil
}
func keyType(t *testing.T, keyID string) string {
var key KeyVersion
if err := json.Unmarshal([]byte(keyID), &key); err != nil {
t.Fatalf("unexpected key identifier - not json: %s", err.Error())
}
return key.Type
}