ChipMaster's trial hacks on C++CMS starting with v1.2.1. Not sure I'll follow on with the v2 since it looks to be breaking and mostly frivolous.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

75 lines
1.7 KiB

  1. #define CPPCMS_SOURCE
  2. #include "hmac_encryptor.h"
  3. #include "md5.h"
  4. #include <time.h>
  5. using namespace std;
  6. namespace cppcms {
  7. namespace sessions {
  8. namespace impl {
  9. hmac_cipher::hmac_cipher(string key) :
  10. base_encryptor(key)
  11. {
  12. }
  13. void hmac_cipher::hash(unsigned char const *data,size_t size,unsigned char md5[16])
  14. {
  15. vector<unsigned char> ipad(16,0),opad(32,0);
  16. for(unsigned i=0;i<16;i++) {
  17. ipad[i]=0x36 ^ key[i];
  18. opad[i]=0x5c ^ key[i];
  19. }
  20. using namespace cppcms::impl;
  21. md5_state_t state;
  22. md5_init(&state);
  23. md5_append(&state,&ipad.front(),16);
  24. md5_append(&state,data,size);
  25. md5_finish(&state,&opad.front()+16);
  26. md5_init(&state);
  27. md5_append(&state,&opad.front(),32);
  28. md5_finish(&state,md5);
  29. }
  30. string hmac_cipher::encrypt(string const &plain,time_t timeout)
  31. {
  32. vector<unsigned char> data(16+sizeof(info)+plain.size(),0);
  33. info &header=*(info *)(&data.front()+16);
  34. header.timeout=timeout;
  35. header.size=plain.size();
  36. salt(header.salt);
  37. copy(plain.begin(),plain.end(),data.begin()+16+sizeof(info));
  38. hash(&data.front()+16,data.size()-16,&data.front());
  39. return base64_enc(data);
  40. }
  41. bool hmac_cipher::decrypt(string const &cipher,string &plain,time_t *timeout)
  42. {
  43. vector<unsigned char> data;
  44. base64_dec(cipher,data);
  45. const unsigned offset=16+sizeof(info);
  46. if(data.size()<offset)
  47. return false;
  48. info &header=*(info *)(&data.front()+16);
  49. if(header.size!=data.size()-offset)
  50. return false;
  51. unsigned char md5[16];
  52. hash(&data.front()+16,data.size()-16,md5);
  53. if(!equal(data.begin(),data.begin()+16,md5))
  54. return false;
  55. time_t now;
  56. time(&now);
  57. if(now>header.timeout)
  58. return false;
  59. if(timeout)
  60. *timeout=header.timeout;
  61. plain.assign(data.begin()+offset,data.end());
  62. return true;
  63. }
  64. } // impl
  65. } // sessions
  66. } // cppcms