serval-dna/nacl/nacl-20110221/crypto_secretbox/wrapper-box.cpp
Daniel O'Connor bf9710fd5a Unpacked nacl-20110221 after processing by nacl-prepare-sources.
This only affects build_android, if nacl-gcc-prep is run then build/`uname -s` will be created.
2012-02-27 12:40:14 +10:30

20 lines
795 B
C++

#include <string>
using std::string;
#include "crypto_secretbox.h"
string crypto_secretbox(const string &m,const string &n,const string &k)
{
if (k.size() != crypto_secretbox_KEYBYTES) throw "incorrect key length";
if (n.size() != crypto_secretbox_NONCEBYTES) throw "incorrect nonce length";
size_t mlen = m.size() + crypto_secretbox_ZEROBYTES;
unsigned char mpad[mlen];
for (int i = 0;i < crypto_secretbox_ZEROBYTES;++i) mpad[i] = 0;
for (int i = crypto_secretbox_ZEROBYTES;i < mlen;++i) mpad[i] = m[i - crypto_secretbox_ZEROBYTES];
unsigned char cpad[mlen];
crypto_secretbox(cpad,mpad,mlen,(const unsigned char *) n.c_str(),(const unsigned char *) k.c_str());
return string(
(char *) cpad + crypto_secretbox_BOXZEROBYTES,
mlen - crypto_secretbox_BOXZEROBYTES
);
}