uvAuth21.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #include "uvAuth21.h"
  2. uvAuth21::uvAuth21(void) {
  3. }
  4. uvAuth21::~uvAuth21(void) {
  5. }
  6. string uvAuth21::encrypt(string user,string pass,string key) {
  7. string blob;
  8. uint8_t* username = (uint8_t*)user.data();
  9. uint8_t* password = (uint8_t*)pass.data();
  10. uint8_t* cipherkey = (uint8_t*)key.data();
  11. blob = XTEA_encipher(username,user.length(),cipherkey,key.length());
  12. blob += ':';
  13. blob += XTEA_encipher(password,pass.length(),cipherkey,key.length());
  14. return blob;
  15. }
  16. const char * uvAuth21::encrypt(char * user,char * pass,char * key) {
  17. static string blob;
  18. uint8_t* username = (uint8_t*)user;
  19. uint8_t* password = (uint8_t*)pass;
  20. uint8_t* cipherkey = (uint8_t*)key;
  21. blob = XTEA_encipher(username,sizeof(user),cipherkey,sizeof(key));
  22. blob += ':';
  23. blob += XTEA_encipher(password,sizeof(pass),cipherkey,sizeof(key));
  24. return blob.c_str();
  25. }
  26. // from wikipedia. Slightly modified to be 32/64 bit clean
  27. void uvAuth21::XTEA_encipher(__uint32* v, __uint32* k) {
  28. unsigned int num_rounds = 32;
  29. __uint32 v0=v[0], v1=v[1], i;
  30. __uint32 sum=0, delta=0x9E3779B9;
  31. for(i=0; i<num_rounds; i++) {
  32. v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
  33. sum += delta;
  34. v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]);
  35. }
  36. v[0]=v0; v[1]=v1;
  37. }
  38. __uint32 uvAuth21::fourCharsToLong(__uint8 *s) {
  39. __uint32 l = 0;
  40. l |= s[0]; l <<= 8;
  41. l |= s[1]; l <<= 8;
  42. l |= s[2]; l <<= 8;
  43. l |= s[3];
  44. return l;
  45. }
  46. std::vector<std::string> &uvAuth21::split(const std::string &s, char delim, std::vector<std::string> &elems) {
  47. std::stringstream ss(s);
  48. std::string item;
  49. while(std::getline(ss, item, delim)) {
  50. elems.push_back(item);
  51. }
  52. return elems;
  53. }
  54. std::vector<std::string> uvAuth21::dosplit(const std::string &s, char delim) {
  55. std::vector<std::string> elems;
  56. return split(s, delim, elems);
  57. }
  58. std::string uvAuth21::XTEA_encipher(const __uint8* c_data,size_t c_data_cnt, const __uint8* c_key,size_t c_key_cnt) {
  59. std::ostringstream oss;
  60. std::vector<__uint8> key(c_key,c_key + c_key_cnt);
  61. std::vector<__uint8> data(c_data,c_data + c_data_cnt);
  62. // key is always 128 bits
  63. while(key.size() < 16) key.push_back(XTEA_KEY_PAD); // pad key with zero
  64. __uint32 k[4] = { fourCharsToLong(&key[0]),fourCharsToLong(&key[4]),fourCharsToLong(&key[8]),fourCharsToLong(&key[12])};
  65. // data is multiple of 64 bits
  66. size_t siz = data.size();
  67. while(siz % 8) { siz+=1; data.push_back(XTEA_DATA_PAD);} // pad data with zero
  68. for(size_t x = 0; x < siz; x+=8) {
  69. __uint32 v[2];
  70. v[0] = fourCharsToLong(&data[x]);
  71. v[1] = fourCharsToLong(&data[x+4]);
  72. XTEA_encipher(v,k);
  73. oss << setw(8) << setfill('0') << hex << v[0];
  74. oss << setw(8) << setfill('0') << hex << v[1]; // hex values. uvox uses colon as seperator so we can't use chars for
  75. // fear of collision
  76. }
  77. return oss.str();
  78. }