This repository has been archived by the owner on Mar 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 158
/
base64formatter.go
109 lines (87 loc) · 3.49 KB
/
base64formatter.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package exampleformatters
import (
"encoding/base64"
"github.com/google/uuid"
spi "github.com/hyperledger/aries-framework-go/spi/storage"
)
// Base64Formatter is a simple formatter that formats and deformats data between a plaintext format and base64 format.
// It's intended for demonstrating formattedstore functionality. It's not intended for production usage.
type Base64Formatter struct {
useDeterministicKeyFormatting bool
keyMap map[string]string // Only used if useDeterministicKeyFormatting is true.
}
// NewBase64Formatter creates a new Base64Formatter. If useDeterministicKeyFormatting is set to true, then the
// formatted keys output by the Base64Formatter will simply be base64-encoded versions of the unformatted keys.
// If useDeterministicKeyFormatting is set to false, then the formatted keys will instead be base64-formatted random
// UUIDs, with no relation to the unformatted key. This can be used to simulate the method by which the
// EDV Encrypted Formatter generates its formatted keys by default, which works in a similar way. An in-memory map
// is used to map between the random formatted keys and unformatted keys.
func NewBase64Formatter(useDeterministicKeyFormatting bool) *Base64Formatter {
return &Base64Formatter{
useDeterministicKeyFormatting: useDeterministicKeyFormatting,
keyMap: make(map[string]string),
}
}
// Format returns base64-encoded versions of key, value, and tags.
func (b *Base64Formatter) Format(key string, value []byte, tags ...spi.Tag) (string, []byte, []spi.Tag,
error) {
formattedTags := make([]spi.Tag, len(tags))
for i, tag := range tags {
formattedTags[i] = spi.Tag{
Name: base64.StdEncoding.EncodeToString([]byte(tag.Name)),
Value: base64.StdEncoding.EncodeToString([]byte(tag.Value)),
}
}
var formattedKey string
if b.useDeterministicKeyFormatting {
formattedKey = base64.StdEncoding.EncodeToString([]byte(key))
} else {
formattedKey = base64.StdEncoding.EncodeToString([]byte(uuid.New().String()))
b.keyMap[formattedKey] = key
}
return formattedKey, []byte(base64.StdEncoding.EncodeToString(value)),
formattedTags, nil
}
// Deformat returns base64-decoded versions of formattedKey, formattedValue, and formattedTags.
func (b *Base64Formatter) Deformat(formattedKey string, formattedValue []byte,
formattedTags ...spi.Tag) (string, []byte, []spi.Tag, error) {
var key string
if b.useDeterministicKeyFormatting {
keyBytes, err := base64.StdEncoding.DecodeString(formattedKey)
if err != nil {
return "", nil, nil, err
}
key = string(keyBytes)
} else {
key = b.keyMap[formattedKey]
}
value, err := base64.StdEncoding.DecodeString(string(formattedValue))
if err != nil {
return "", nil, nil, err
}
tags := make([]spi.Tag, len(formattedTags))
for i, formattedTag := range formattedTags {
tagName, err := base64.StdEncoding.DecodeString(formattedTag.Name)
if err != nil {
return "", nil, nil, err
}
tagValue, err := base64.StdEncoding.DecodeString(formattedTag.Value)
if err != nil {
return "", nil, nil, err
}
tags[i] = spi.Tag{
Name: string(tagName),
Value: string(tagValue),
}
}
return key, value, tags, nil
}
// UsesDeterministicKeyFormatting indicates whether this Base64Formatter has been set up to use deterministic key
// generation.
func (b *Base64Formatter) UsesDeterministicKeyFormatting() bool {
return b.useDeterministicKeyFormatting
}