-
Notifications
You must be signed in to change notification settings - Fork 2
/
encrypt.go
77 lines (64 loc) · 1.79 KB
/
encrypt.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
// vault encrypt is an example of how to use the encrypt/decrypt methods
package main
import (
"context"
"encoding/base64"
"fmt"
"log"
"os"
"time"
"github.com/pangeacyber/pangea-go/pangea-sdk/v4/pangea"
"github.com/pangeacyber/pangea-go/pangea-sdk/v4/service/vault"
)
func main() {
token := os.Getenv("PANGEA_VAULT_TOKEN")
if token == "" {
log.Fatal("Unauthorized: No token present")
}
vaultcli := vault.New(&pangea.Config{
Token: token,
Domain: os.Getenv("PANGEA_DOMAIN"),
})
ctx := context.Background()
fmt.Println("Generate key...")
name := "Go encrypt example " + time.Now().Format(time.RFC3339)
generateInput := &vault.SymmetricGenerateRequest{
Algorithm: vault.SYAaes128_cfb,
Purpose: vault.KPencryption,
CommonGenerateRequest: vault.CommonGenerateRequest{
Name: name,
},
}
generateResponse, err := vaultcli.SymmetricGenerate(ctx, generateInput)
if err != nil {
log.Fatal(err)
}
fmt.Println(pangea.Stringify(generateResponse.Result))
fmt.Println("Encrypt...")
message := "messagetoencrypt"
data := base64.StdEncoding.EncodeToString([]byte(message))
encryptInput := &vault.EncryptRequest{
ID: generateResponse.Result.ID,
PlainText: data,
}
encryptResponse, err := vaultcli.Encrypt(ctx, encryptInput)
if err != nil {
log.Fatal(err)
}
fmt.Println(pangea.Stringify(encryptResponse.Result))
fmt.Println("Decrypt...")
decryptInput := &vault.DecryptRequest{
ID: generateResponse.Result.ID,
CipherText: encryptResponse.Result.CipherText,
}
decryptResponse, err := vaultcli.Decrypt(ctx, decryptInput)
if err != nil {
log.Fatal(err)
}
fmt.Println(pangea.Stringify(decryptResponse.Result))
if decryptResponse.Result.PlainText == data {
fmt.Println("Encrypt/Decrypt success")
} else {
fmt.Println("Encrypt/Decrypt failed")
}
}