wrapper-box.cpp 795 B

12345678910111213141516171819
  1. #include <string>
  2. using std::string;
  3. #include "crypto_secretbox.h"
  4. string crypto_secretbox(const string &m,const string &n,const string &k)
  5. {
  6. if (k.size() != crypto_secretbox_KEYBYTES) throw "incorrect key length";
  7. if (n.size() != crypto_secretbox_NONCEBYTES) throw "incorrect nonce length";
  8. size_t mlen = m.size() + crypto_secretbox_ZEROBYTES;
  9. unsigned char mpad[mlen];
  10. for (int i = 0;i < crypto_secretbox_ZEROBYTES;++i) mpad[i] = 0;
  11. for (int i = crypto_secretbox_ZEROBYTES;i < mlen;++i) mpad[i] = m[i - crypto_secretbox_ZEROBYTES];
  12. unsigned char cpad[mlen];
  13. crypto_secretbox(cpad,mpad,mlen,(const unsigned char *) n.c_str(),(const unsigned char *) k.c_str());
  14. return string(
  15. (char *) cpad + crypto_secretbox_BOXZEROBYTES,
  16. mlen - crypto_secretbox_BOXZEROBYTES
  17. );
  18. }