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