00001 // Credentials.cpp 00002 00003 00004 #include "Credentials.h" 00005 00006 #include <sstream> 00007 #include <string.h> 00008 00009 using namespace Yosokumo; 00010 00011 Credentials::Credentials() 00012 { 00013 userId = ""; 00014 key.clear(); 00015 } 00016 00017 Credentials::Credentials( 00018 const std::string &userId, 00019 const std::vector<uint8_t> &key) throw(ServiceException) : 00020 userId(userId), 00021 key(key) 00022 { 00023 if (key.size() != KEY_LEN) 00024 throw ServiceException("Key is not the correct length"); 00025 } 00026 00027 Credentials::Credentials(const Credentials &rhs) 00028 { 00029 operator=(rhs); 00030 } 00031 00032 Credentials::~Credentials() 00033 {} 00034 00035 Credentials &Credentials::operator=(const Credentials& rhs) 00036 { 00037 if (this == &rhs) 00038 return *this; 00039 00040 userId = rhs.userId ; 00041 key = rhs.key; 00042 00043 return *this; 00044 } 00045 00046 00047 // Equality operators 00048 00049 bool Credentials::operator==(const Credentials &rhs) const 00050 { 00051 return 00052 ( 00053 userId == rhs.userId && 00054 key.size() == rhs.key.size() && 00055 memcmp((void*)&key[0], (void*)&rhs.key[0], key.size()) == 0 00056 ); 00057 } 00058 00059 bool Credentials::operator!=(const Credentials &rhs) const 00060 { 00061 return !(*this == rhs); 00062 } 00063 00064 00065 std::string Credentials::getUserId() 00066 { 00067 return userId; 00068 } 00069 00070 void Credentials::getKey(std::vector<uint8_t> &theKey) 00071 { 00072 theKey = key; 00073 } 00074 00075 std::string Credentials::toString() 00076 { 00077 std::stringstream s; 00078 00079 s << 00080 "Credentials:" << "\n" << 00081 " userId = " << getUserId() << "\n" << 00082 keyToString(key) 00083 ; 00084 00085 return s.str(); 00086 } 00087 00088 std::string Credentials::keyToString(const std::vector<uint8_t> &key) 00089 { 00090 std::stringstream s; 00091 00092 s << " key = "; 00093 00094 if (key.empty()) 00095 { 00096 s << "empty" << "\n"; 00097 return s.str(); 00098 } 00099 00100 s << key.size() << " bytes"; 00101 00102 int numPerLine = 15; 00103 int i = 0; 00104 00105 while (i < Credentials::KEY_LEN) 00106 { 00107 uint8_t n = key.at(i); 00108 if (i++ % numPerLine == 0) 00109 s << "\n"; 00110 s << " " << unsigned(n); 00111 } 00112 s << "\n"; 00113 00114 return s.str(); 00115 } 00116 /* 00117 key = 64 bytes 00118 -xxx -xxx -xxx -xxx -xxx -xxx -xxx -xxx -xxx -xxx -xxx -xxx -xxx -xxx -xxx 00119 -xxx -xxx -xxx -xxx -xxx -xxx -xxx -xxx -xxx -xxx -xxx -xxx -xxx -xxx -xxx 00120 -xxx -xxx -xxx -xxx -xxx -xxx -xxx -xxx -xxx -xxx -xxx -xxx -xxx -xxx -xxx 00121 -xxx -xxx -xxx -xxx -xxx -xxx -xxx -xxx -xxx -xxx -xxx -xxx -xxx -xxx -xxx 00122 -xxx -xxx -xxx -xxx 00123 */ 00124 00125 // end Credentials.cpp