Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / test / testcrypto.cc
1 #include "auth/Crypto.h"
2 #include "common/Clock.h"
3
4 #include "common/config.h"
5 #include "common/debug.h"
6
7 #define dout_subsys ceph_subsys_auth
8
9 #define AES_KEY_LEN     16
10
11 #define dout_context g_ceph_context
12
13 int main(int argc, char *argv[])
14 {
15   char aes_key[AES_KEY_LEN];
16   memset(aes_key, 0x77, sizeof(aes_key));
17   bufferptr keybuf(aes_key, sizeof(aes_key));
18   CryptoKey key(CEPH_CRYPTO_AES, ceph_clock_now(), keybuf);
19
20   const char *msg="hello! this is a message\n";
21   char pad[16];
22   memset(pad, 0, 16);
23   bufferptr ptr(msg, strlen(msg));
24   bufferlist enc_in;
25   enc_in.append(ptr);
26   enc_in.append(msg, strlen(msg));
27
28   bufferlist enc_out;
29   std::string error;
30   if (key.encrypt(g_ceph_context, enc_in, enc_out, &error) < 0) {
31     assert(!error.empty());
32     dout(0) << "couldn't encode! error " << error << dendl;
33     exit(1);
34   }
35
36   const char *enc_buf = enc_out.c_str();
37   for (unsigned i=0; i<enc_out.length(); i++) {
38     std::cout << hex << (int)(unsigned char)enc_buf[i] << dec << " ";
39     if (i && !(i%16))
40       std::cout << std::endl;
41   }
42
43   bufferlist dec_in, dec_out;
44
45   dec_in = enc_out;
46
47   if (key.decrypt(g_ceph_context, dec_in, dec_out, &error) < 0) {
48     assert(!error.empty());
49     dout(0) << "couldn't decode! error " << error << dendl;
50     exit(1);
51   }
52
53   dout(0) << "decoded len: " << dec_out.length() << dendl;
54   dout(0) << "decoded msg: " << dec_out.c_str() << dendl;
55
56   return 0;
57 }
58