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.
 
 
 
 
 
 

84 lines
1.8 KiB

  1. #define CPPCMS_SOURCE
  2. #include "base_encryptor.h"
  3. #include "base64.h"
  4. #include "cppcms_error.h"
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. using namespace std;
  8. namespace cppcms {
  9. namespace sessions {
  10. namespace impl {
  11. base_encryptor::~base_encryptor()
  12. {
  13. }
  14. base_encryptor::base_encryptor(string key_):
  15. key(16,0)
  16. {
  17. if(key_.size()!=32) {
  18. throw cppcms_error("Incorrect key length (32 expected)\n");
  19. }
  20. for(unsigned i=0;i<32;i+=2) {
  21. char buf[3];
  22. if(!isxdigit(key_[i]) || !isxdigit(key_[i+1])) {
  23. throw cppcms_error("Cipher should be encoded as hexadecimal 32 digits number");
  24. }
  25. buf[0]=key_[i];
  26. buf[1]=key_[i+1];
  27. buf[2]=0;
  28. unsigned v;
  29. sscanf(buf,"%x",&v);
  30. key[i/2]=v;
  31. }
  32. urandom_device dev;
  33. dev.generate(&seed,sizeof(seed));
  34. }
  35. unsigned base_encryptor::rand(unsigned max)
  36. {
  37. #ifdef CPPCMS_WIN_NATIVE
  38. unsigned res;
  39. rnd.generate(&res,sizeof(res));
  40. return (unsigned)(res/(RAND_MAX+1.0)*max);
  41. #else
  42. return (unsigned)(rand_r(&seed)/(RAND_MAX+1.0)*max);
  43. #endif
  44. }
  45. string base_encryptor::base64_enc(vector<unsigned char> const &data)
  46. {
  47. size_t size=b64url::encoded_size(data.size());
  48. vector<unsigned char> result(size,0);
  49. b64url::encode(&data.front(),&data.front()+data.size(),&result.front());
  50. return string(result.begin(),result.end());
  51. }
  52. void base_encryptor::base64_dec(std::string const &in,std::vector<unsigned char> &data)
  53. {
  54. int size=b64url::decoded_size(in.size());
  55. if(size<0) return;
  56. data.resize(size);
  57. unsigned char const *ptr=(unsigned char const *)in.data();
  58. b64url::decode((unsigned char const *)ptr,ptr+in.size(),&data.front());
  59. }
  60. void base_encryptor::salt(char *salt)
  61. {
  62. info dummy;
  63. #ifdef CPPCMS_WIN_NATIVE
  64. unsigned res;
  65. rnd.generate(salt,sizeof(dummy.salt));
  66. #else
  67. for(unsigned i=0;i<sizeof(dummy.salt);i++)
  68. salt[i]=rand(256);
  69. #endif
  70. }
  71. } // impl
  72. } // sessions
  73. } // cppcms