serval-dna/nacl/nacl-20110221/crypto_box/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

25 lines
900 B
C++

#include <string>
using std::string;
#include "crypto_box.h"
string crypto_box(const string &m,const string &n,const string &pk,const string &sk)
{
if (pk.size() != crypto_box_PUBLICKEYBYTES) throw "incorrect public-key length";
if (sk.size() != crypto_box_SECRETKEYBYTES) throw "incorrect secret-key length";
if (n.size() != crypto_box_NONCEBYTES) throw "incorrect nonce length";
size_t mlen = m.size() + crypto_box_ZEROBYTES;
unsigned char mpad[mlen];
for (int i = 0;i < crypto_box_ZEROBYTES;++i) mpad[i] = 0;
for (int i = crypto_box_ZEROBYTES;i < mlen;++i) mpad[i] = m[i - crypto_box_ZEROBYTES];
unsigned char cpad[mlen];
crypto_box(cpad,mpad,mlen,
(const unsigned char *) n.c_str(),
(const unsigned char *) pk.c_str(),
(const unsigned char *) sk.c_str()
);
return string(
(char *) cpad + crypto_box_BOXZEROBYTES,
mlen - crypto_box_BOXZEROBYTES
);
}