wrapper-box.cpp 900 B

123456789101112131415161718192021222324
  1. #include <string>
  2. using std::string;
  3. #include "crypto_box.h"
  4. string crypto_box(const string &m,const string &n,const string &pk,const string &sk)
  5. {
  6. if (pk.size() != crypto_box_PUBLICKEYBYTES) throw "incorrect public-key length";
  7. if (sk.size() != crypto_box_SECRETKEYBYTES) throw "incorrect secret-key length";
  8. if (n.size() != crypto_box_NONCEBYTES) throw "incorrect nonce length";
  9. size_t mlen = m.size() + crypto_box_ZEROBYTES;
  10. unsigned char mpad[mlen];
  11. for (int i = 0;i < crypto_box_ZEROBYTES;++i) mpad[i] = 0;
  12. for (int i = crypto_box_ZEROBYTES;i < mlen;++i) mpad[i] = m[i - crypto_box_ZEROBYTES];
  13. unsigned char cpad[mlen];
  14. crypto_box(cpad,mpad,mlen,
  15. (const unsigned char *) n.c_str(),
  16. (const unsigned char *) pk.c_str(),
  17. (const unsigned char *) sk.c_str()
  18. );
  19. return string(
  20. (char *) cpad + crypto_box_BOXZEROBYTES,
  21. mlen - crypto_box_BOXZEROBYTES
  22. );
  23. }