forked from xbee/go-toxcore
-
Notifications
You must be signed in to change notification settings - Fork 24
/
toxencryptsave.go
126 lines (102 loc) · 3.4 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
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 (this *ToxPassKey) Free() {
C.tox_pass_key_free(this.cpk)
}
func Derive(passphrase []byte) (*ToxPassKey, error) {
this := &ToxPassKey{}
passphrase_ := (*C.uint8_t)(&passphrase[0])
var cerr C.Tox_Err_Key_Derivation
this.cpk = C.tox_pass_key_derive(passphrase_, C.size_t(len(passphrase)), &cerr)
if cerr != C.TOX_ERR_KEY_DERIVATION_OK {
return nil, toxerr(cerr)
}
return this, nil
}
func DeriveWithSalt(passphrase []byte, salt []byte) (*ToxPassKey, error) {
this := &ToxPassKey{}
passphrase_ := (*C.uint8_t)(&passphrase[0])
salt_ := (*C.uint8_t)(&salt[0])
var cerr C.Tox_Err_Key_Derivation
this.cpk = C.tox_pass_key_derive_with_salt(passphrase_, C.size_t(len(passphrase)), salt_, &cerr)
if cerr != C.TOX_ERR_KEY_DERIVATION_OK {
return nil, toxerr(cerr)
}
return this, nil
}
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
}