-
Notifications
You must be signed in to change notification settings - Fork 2
/
c_bridge.go
66 lines (53 loc) · 1.51 KB
/
c_bridge.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
package main
import "C"
import (
"math/rand"
"os"
"github.com/chayim/redicrypt/src/redicrypt"
)
//export Hash
func Hash(hashType, value *C.char) *C.char {
hashName := string(C.GoString(hashType))
hashVal := []byte(C.GoString(value))
return C.CString(redicrypt.Hash(hashName, hashVal))
}
//export Base64Encode
func Base64Encode(value *C.char) *C.char {
cstr := redicrypt.B64Encode([]byte(C.GoString(value)))
return C.CString(cstr)
}
//export Base64Decode
func Base64Decode(encodedText *C.char) *C.char {
encoded := string(C.GoString(encodedText))
cstr := redicrypt.B64Decode(encoded)
return C.CString(string(cstr))
}
//export Encrypt
func Encrypt(encKey *C.char, value *C.char) *C.char {
secret := []byte(C.GoString(encKey))
plaintext := []byte(C.GoString(value))
cstr := redicrypt.Encrypt(secret, plaintext)
return C.CString(cstr)
}
//export Decrypt
func Decrypt(encKey *C.char, value *C.char) *C.char {
secret := []byte(C.GoString(encKey))
encryptedText := []byte(C.GoString(value))
return C.CString(redicrypt.Decrypt(secret, encryptedText))
}
//export SetKey
func SetKey(secret *C.char) {
private := string(C.GoString(secret))
os.Setenv("REDICRYPT_KEY", private)
}
//export GenerateKey
func GenerateKey(bits *C.int) {
numchars := int(*bits)
var chars = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789`~!@#$%^&*()-_+={}[]|\\;:'\",<.>/?")
s := make([]rune, numchars)
for i := range s {
s[i] = chars[rand.Intn(len(chars))]
}
os.Setenv("REDICRYPT_KEY", string(s))
}
func main() {}