-
Notifications
You must be signed in to change notification settings - Fork 28
/
toxencryptsave.go
130 lines (107 loc) · 3.47 KB
/
toxencryptsave.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package tox
/*
#include <tox/toxencryptsave.h>
*/
import "C"
const PASS_KEY_LENGTH = int(C.TOX_PASS_KEY_LENGTH)
const PASS_ENCRYPTION_EXTRA_LENGTH = int(C.TOX_PASS_ENCRYPTION_EXTRA_LENGTH)
type ToxPassKey struct {
cpk *C.Tox_Pass_Key
}
func NewToxPassKey() *ToxPassKey {
this := &ToxPassKey{}
this.cpk = C.tox_pass_key_new()
return this
}
func (this *ToxPassKey) Free() {
C.tox_pass_key_free(this.cpk)
}
func (this *ToxPassKey) Derive(passphrase []byte) (bool, error) {
passphrase_ := (*C.uint8_t)(&passphrase[0])
var cerr C.TOX_ERR_KEY_DERIVATION
ok := C.tox_pass_key_derive(this.cpk, passphrase_, C.size_t(len(passphrase)), &cerr)
var err error
if !bool(ok) {
err = toxerr(cerr)
}
return bool(ok), err
}
func (this *ToxPassKey) DeriveWithSalt(passphrase []byte, salt []byte) (bool, error) {
passphrase_ := (*C.uint8_t)(&passphrase[0])
salt_ := (*C.uint8_t)(&salt[0])
var cerr C.TOX_ERR_KEY_DERIVATION
ok := C.tox_pass_key_derive_with_salt(this.cpk, passphrase_, C.size_t(len(passphrase)), salt_, &cerr)
var err error
if !bool(ok) {
err = toxerr(cerr)
}
return bool(ok), err
}
func (this *ToxPassKey) Encrypt(plaintext []byte) (bool, error, []byte) {
ciphertext := make([]byte, len(plaintext)+PASS_ENCRYPTION_EXTRA_LENGTH)
ciphertext_ := (*C.uint8_t)(&ciphertext[0])
plaintext_ := (*C.uint8_t)(&plaintext[0])
var cerr C.TOX_ERR_ENCRYPTION
ok := C.tox_pass_key_encrypt(this.cpk, plaintext_, C.size_t(len(plaintext)), ciphertext_, &cerr)
var err error
if !bool(ok) {
err = toxerr(err)
}
return bool(ok), err, ciphertext
}
func (this *ToxPassKey) Decrypt(ciphertext []byte) (bool, error, []byte) {
ciphertext_ := (*C.uint8_t)(&ciphertext[0])
plaintext := make([]byte, len(ciphertext)-PASS_ENCRYPTION_EXTRA_LENGTH)
plaintext_ := (*C.uint8_t)(&plaintext[0])
var cerr C.TOX_ERR_DECRYPTION
ok := C.tox_pass_key_decrypt(this.cpk, ciphertext_, C.size_t(len(ciphertext)), plaintext_, &cerr)
var err error
if !bool(ok) {
err = toxerr(cerr)
}
return bool(ok), err, plaintext
}
func GetSalt(ciphertext []byte) (bool, error, []byte) {
ciphertext_ := (*C.uint8_t)(&ciphertext[0])
salt := make([]byte, int(C.TOX_PASS_SALT_LENGTH))
salt_ := (*C.uint8_t)(&salt[0])
var cerr C.TOX_ERR_GET_SALT
ok := C.tox_get_salt(ciphertext_, salt_, &cerr)
var err error
if !bool(ok) {
err = toxerr(cerr)
}
return bool(ok), err, salt
}
func IsDataEncrypted(data []byte) bool {
data_ := (*C.uint8_t)(&data[0])
ok := C.tox_is_data_encrypted(data_)
if ok == C._Bool(false) {
return false
}
return true
}
func PassEncrypt(plaintext []byte, passphrase []byte) (ciphertext []byte, err error) {
ciphertext = make([]byte, len(plaintext)+PASS_ENCRYPTION_EXTRA_LENGTH)
ciphertext_ := (*C.uint8_t)(&ciphertext[0])
plaintext_ := (*C.uint8_t)(&plaintext[0])
passphrase_ := (*C.uint8_t)(&passphrase[0])
var cerr C.TOX_ERR_ENCRYPTION
ok := C.tox_pass_encrypt(plaintext_, C.size_t(len(plaintext)), passphrase_, C.size_t(len(passphrase)), ciphertext_, &cerr)
if !bool(ok) {
err = toxerr(cerr)
}
return
}
func PassDecrypt(ciphertext []byte, passphrase []byte) (plaintext []byte, err error) {
ciphertext_ := (*C.uint8_t)(&ciphertext[0])
plaintext = make([]byte, len(ciphertext)-PASS_ENCRYPTION_EXTRA_LENGTH)
plaintext_ := (*C.uint8_t)(&plaintext[0])
passphrase_ := (*C.uint8_t)(&plaintext[0])
var cerr C.TOX_ERR_DECRYPTION
ok := C.tox_pass_decrypt(ciphertext_, C.size_t(len(ciphertext)), passphrase_, C.size_t(len(passphrase)), plaintext_, &cerr)
if !bool(ok) {
err = toxerr(cerr)
}
return
}