-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
105 lines (96 loc) · 3.52 KB
/
main.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
package examples
import (
"bytes"
"fmt"
"os"
"sync"
"time"
"github.com/onqlavelabs/onqlave-go/onqlave/onqlaveconnection"
"github.com/onqlavelabs/onqlave-go/onqlave/onqlavecredentials"
"github.com/onqlavelabs/onqlave-go/onqlave/onqlaveencryption"
)
func encryption_cycle(opts ...onqlaveencryption.Option) {
service := onqlaveencryption.NewEncryption(opts...)
defer service.Close()
test := "This is a test plaintext"
cipher, err := service.Encrypt([]byte(test), nil)
if err != nil {
fmt.Printf("Error happend in encrypting: %s", err)
} else {
plainData, err := service.Decrypt(cipher, nil)
if err != nil || string(plainData) != test {
fmt.Printf("Error happend in decrypting: %s", err)
}
}
}
func encryption_cycle_file(opts ...onqlaveencryption.Option) {
service := onqlaveencryption.NewEncryption(opts...)
path, _ := os.Getwd()
defer service.Close()
//open your inpu file here - this is the file you would like to ecnrypt - it can be any implementation if <io.Reader> interface
plainStream, err := os.OpenFile(path+"/examples/data.in", os.O_RDONLY, 0666)
if err != nil {
fmt.Printf("Error happend in opening file: %s", err)
return
}
//this is the output stream where you would like to write the cipther to - it can be any implementation of <io.Writer> interface
cipherStream := bytes.NewBuffer(nil)
er := service.EncryptStream(plainStream, cipherStream, nil)
if er != nil {
fmt.Printf("Error happend in encrypting: %s", er)
} else {
replainStream, err := os.OpenFile(path+"/examples/data.o", os.O_WRONLY, 0666)
if err != nil {
fmt.Printf("Error happend in opening file: %s", err)
return
}
er = service.DecryptStream(cipherStream, replainStream, nil)
if er != nil {
fmt.Printf("Error happend in decrypting: %s", er)
}
replainStream.Close()
}
}
func Start() {
//This option lets you choose whether to start SDK operation in debug mode to info mode
debugOption := onqlaveencryption.WithDebug(true)
directory, _ := os.Getwd()
//This is just for testing purpose - for prodcution environment you must not store your Onqlave keys in file. Best way to handle keys is to keep them in secret manager of your choice!
credentials, err := LoadCredentials(directory + "/examples/credential.json")
if err != nil {
fmt.Printf("Error happend during loading credential file : %s", err)
return
}
options := make([][]onqlaveencryption.Option, len(credentials))
//This option lets you choose what strategy SDK uses whilst connecting to services in case there is a connectivity issue
retryOption := onqlaveencryption.WithRetry(onqlaveconnection.RetrySettings{
Count: 2,
WaitTime: 400 * time.Millisecond,
MaxWaitTime: 2 * time.Second,
})
//Set the options by credentials read from file/environment/secret manager
for i, credential := range credentials {
arxOption := onqlaveencryption.WithArx(credential.Arx)
credentialOption := onqlaveencryption.WithCredential(onqlavecredentials.Credential{
AccessKey: credential.AccessKey,
SigningKey: credential.SigningKey,
SecretKey: credential.SecretKey,
})
options[i] = make([]onqlaveencryption.Option, 0)
options[i] = append(options[i], arxOption, credentialOption, retryOption, debugOption)
}
//Concurrent calls to Onqlave services - using SDK
var wg sync.WaitGroup
for i := 0; i < len(credentials); i++ {
wg.Add(1)
go func(opts ...onqlaveencryption.Option) {
for {
encryption_cycle(opts...)
time.Sleep(300 * time.Millisecond)
encryption_cycle_file(opts...)
time.Sleep(30 * time.Millisecond)
}
}(options[i]...)
}
wg.Wait()
}