-
Notifications
You must be signed in to change notification settings - Fork 88
/
crypto.go
45 lines (36 loc) · 1016 Bytes
/
crypto.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
package main
import "C"
import (
"encoding/base64"
"fmt"
"github.com/replicatedhq/kots/pkg/crypto"
)
//export EncryptString
func EncryptString(cipherString string, message string) *C.char {
cipher, err := crypto.AESCipherFromString(cipherString)
if err != nil {
fmt.Printf("failed to create cipher: %v\n", err)
return nil
}
encrypted := cipher.Encrypt([]byte(message))
return C.CString(base64.StdEncoding.EncodeToString(encrypted))
}
//export DecryptString
func DecryptString(cipherString string, messageBase64 string) *C.char {
cipher, err := crypto.AESCipherFromString(cipherString)
if err != nil {
fmt.Printf("failed to create cipher: %v\n", err)
return nil
}
message, err := base64.StdEncoding.DecodeString(messageBase64)
if err != nil {
fmt.Printf("failed to decode message: %v\n", err)
return nil
}
decrypted, err := cipher.Decrypt([]byte(message))
if err != nil {
fmt.Printf("failed to decrypt message: %v\n", err)
return nil
}
return C.CString(string(decrypted))
}