-
Notifications
You must be signed in to change notification settings - Fork 9
/
encrypt_event_test.go
69 lines (61 loc) · 2.03 KB
/
encrypt_event_test.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
package encryptedstore_test
import (
"context"
"fmt"
"math/rand"
"github.com/inklabs/rangedb"
"github.com/inklabs/rangedb/pkg/crypto/aes"
"github.com/inklabs/rangedb/pkg/crypto/cryptotest"
"github.com/inklabs/rangedb/pkg/crypto/eventencryptor"
"github.com/inklabs/rangedb/pkg/crypto/provider/inmemorykeystore"
"github.com/inklabs/rangedb/provider/encryptedstore"
"github.com/inklabs/rangedb/provider/inmemorystore"
)
func ExampleNew_automatically_encrypt_decrypt() {
// Given
seededRandReader := rand.New(rand.NewSource(100))
aesEncryptor := aes.NewGCM()
aesEncryptor.SetRandReader(seededRandReader)
keyStore := inmemorykeystore.New()
eventEncryptor := eventencryptor.New(keyStore, aesEncryptor)
eventEncryptor.SetRandReader(seededRandReader)
event := &cryptotest.CustomerSignedUp{
ID: "fe65ac8d8c3a45e9b3cee407f10ee518",
Name: "John Doe",
Email: "[email protected]",
Status: "active",
}
inMemoryStore := inmemorystore.New()
encryptedStore := encryptedstore.New(inMemoryStore, eventEncryptor)
encryptedStore.Bind(&cryptotest.CustomerSignedUp{})
streamName := rangedb.GetEventStream(event)
ctx := context.Background()
// When
_, err := encryptedStore.Save(ctx, streamName, &rangedb.EventRecord{Event: event})
PrintError(err)
fmt.Println("Auto Decrypted Event:")
recordIterator := encryptedStore.Events(ctx, 0)
for recordIterator.Next() {
PrintEvent(recordIterator.Record().Data.(rangedb.Event))
}
fmt.Println("Raw Encrypted Event:")
rawRecordIterator := inMemoryStore.Events(ctx, 0)
for rawRecordIterator.Next() {
PrintEvent(rawRecordIterator.Record().Data.(rangedb.Event))
}
// Output:
// Auto Decrypted Event:
// {
// "ID": "fe65ac8d8c3a45e9b3cee407f10ee518",
// "Name": "John Doe",
// "Email": "[email protected]",
// "Status": "active"
// }
// Raw Encrypted Event:
// {
// "ID": "fe65ac8d8c3a45e9b3cee407f10ee518",
// "Name": "Lp5pGK8QGYw3NJyJVBsW49HESSf+NEraAQoBmpLXboZvsN/L",
// "Email": "o1H9t1BClYc5UcyUV+Roe3wz5gwRZRjgBI/xzwZs8ueQGQ5L8uGnbrTGrh8=",
// "Status": "active"
// }
}