-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNaClInterface.cpp
More file actions
127 lines (111 loc) · 2.4 KB
/
NaClInterface.cpp
File metadata and controls
127 lines (111 loc) · 2.4 KB
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
#include "NaClInterface.h"
NaClInterface::NaClInterface()
{
gen_new_nonce();
gen_new_keys();
gen_new_secret();
}
std::string NaClInterface::nonce()
{
return my_nonce;
}
void NaClInterface::set_remote_nonce(std::string nonce)
{
remote_nonce = nonce;
}
void NaClInterface::gen_new_nonce()
{
char nonce_array[crypto_box_NONCEBYTES+1];
for (int i = 0; i < crypto_box_NONCEBYTES; ++i)
{
nonce_array[i] = (rand() % 254) + 1;
}
nonce_array[crypto_box_NONCEBYTES] = 0;
my_nonce = nonce_array;
}
std::string NaClInterface::public_key()
{
return my_pk;
}
std::string NaClInterface::secret_key()
{
return my_sk;
}
void NaClInterface::set_remote_key(std::string rk)
{
remote_pk = rk;
}
void NaClInterface::gen_new_keys()
{
my_pk = crypto_box_keypair(&my_sk);
}
std::string NaClInterface::secret()
{
return my_secret;
}
void NaClInterface::set_secret(std::string new_secret)
{
my_secret = new_secret;
}
void NaClInterface::gen_new_secret()
{
char secret_array[crypto_secretbox_KEYBYTES+1];
for (int i = 0; i < crypto_secretbox_KEYBYTES; ++i)
{
secret_array[i] = (rand() % 254) + 1;
}
secret_array[crypto_secretbox_KEYBYTES] = 0;
my_secret = secret_array;
}
std::string NaClInterface::public_decrypt(std::string enc_msg)
{
try
{
std::string encrypted_message = enc_msg;
std::string message = crypto_box_open(encrypted_message, my_nonce, remote_pk, my_sk);
return message;
}
catch (int e)
{
return "";
}
}
std::string NaClInterface::public_encrypt(std::string msg)
{
try
{
std::string message = msg;
std::string encrypted_message = crypto_box(message, remote_nonce, remote_pk, my_sk);
return encrypted_message;
}
catch (int e)
{
return "";
}
}
std::string NaClInterface::secret_decrypt(std::string enc_msg)
{
try
{
std::string encrypted_message = enc_msg;
std::string message = crypto_secretbox_open(encrypted_message, my_nonce, my_secret);
return message;
}
catch (int e)
{
return "";
}
}
std::string NaClInterface::secret_encrypt(std::string msg)
{
try
{
std::string message = msg;
std::string encrypted_message = crypto_secretbox(message, remote_nonce, my_secret);
return encrypted_message;
}
catch (int e)
{
return "";
}
}