summaryrefslogtreecommitdiff
path: root/nacl/crypto_box/wrapper-box.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'nacl/crypto_box/wrapper-box.cpp')
-rw-r--r--nacl/crypto_box/wrapper-box.cpp24
1 files changed, 24 insertions, 0 deletions
diff --git a/nacl/crypto_box/wrapper-box.cpp b/nacl/crypto_box/wrapper-box.cpp
new file mode 100644
index 00000000..f0429295
--- /dev/null
+++ b/nacl/crypto_box/wrapper-box.cpp
@@ -0,0 +1,24 @@
1#include <string>
2using std::string;
3#include "crypto_box.h"
4
5string crypto_box(const string &m,const string &n,const string &pk,const string &sk)
6{
7 if (pk.size() != crypto_box_PUBLICKEYBYTES) throw "incorrect public-key length";
8 if (sk.size() != crypto_box_SECRETKEYBYTES) throw "incorrect secret-key length";
9 if (n.size() != crypto_box_NONCEBYTES) throw "incorrect nonce length";
10 size_t mlen = m.size() + crypto_box_ZEROBYTES;
11 unsigned char mpad[mlen];
12 for (int i = 0;i < crypto_box_ZEROBYTES;++i) mpad[i] = 0;
13 for (int i = crypto_box_ZEROBYTES;i < mlen;++i) mpad[i] = m[i - crypto_box_ZEROBYTES];
14 unsigned char cpad[mlen];
15 crypto_box(cpad,mpad,mlen,
16 (const unsigned char *) n.c_str(),
17 (const unsigned char *) pk.c_str(),
18 (const unsigned char *) sk.c_str()
19 );
20 return string(
21 (char *) cpad + crypto_box_BOXZEROBYTES,
22 mlen - crypto_box_BOXZEROBYTES
23 );
24}