31 #ifndef WAMP_AUTH_UTILS_HPP 32 #define WAMP_AUTH_UTILS_HPP 46 virtual const char* what()
const throw()
48 return "Error occured when calulcate a derived key";
59 #include <openssl/evp.h> 60 #include <openssl/hmac.h> 61 #include <openssl/bio.h> 62 #include <openssl/buffer.h> 73 inline std::string base_64_encode(
const std::string & data )
78 b64 = BIO_new(BIO_f_base64());
79 bio = BIO_new(BIO_s_mem());
80 bio = BIO_push(b64, bio);
82 BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);
84 BIO_write(bio, (
const unsigned char *) data.c_str(), data.size());
87 BIO_get_mem_ptr(bio, &pBuf);
88 (void)BIO_set_close(bio, BIO_NOCLOSE);
91 str_out.assign( pBuf->data, pBuf->length );
108 inline std::string derive_key(
109 const std::string & passwd,
110 const std::string & salt,
116 int passwdLen = passwd.size();
117 const char * pwd = passwd.c_str();
119 int saltLen = salt.size();
120 unsigned char * salt_value = (
unsigned char * ) salt.c_str();
123 str_out.resize( keylen );
126 unsigned char * out = (
unsigned char *) str_out.c_str();
129 int result = PKCS5_PBKDF2_HMAC(
138 return base_64_encode( str_out );
154 inline std::string compute_wcs(
155 const std::string & key,
156 const std::string & challenge )
159 unsigned int len = 32;
160 unsigned char hash[32];
161 #if (OPENSSL_VERSION_NUMBER < 0x10100000L) 163 HMAC_CTX_init(&hmac);
164 HMAC_Init_ex(&hmac, key.data(), key.length(), EVP_sha256(), NULL);
165 HMAC_Update(&hmac, (
unsigned char*) challenge.data(), challenge.length());
166 HMAC_Final(&hmac, hash, &len);
167 HMAC_CTX_cleanup(&hmac);
169 HMAC_CTX *hmac = HMAC_CTX_new();
172 HMAC_Init_ex(hmac, key.data(), key.length(), EVP_sha256(), NULL);
173 HMAC_Update(hmac, (
unsigned char* ) challenge.data(), challenge.length());
174 HMAC_Final(hmac, hash, &len);
179 str_out.assign( (
char * ) &hash , 32 );
181 return base_64_encode( str_out );
198 inline std::string generate_wcs(
int length=14){
203 static const char WCS_SECRET_CHARSET[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
206 for (
int i = 0; i < length; ++i) {
207 s.push_back( WCS_SECRET_CHARSET[ rand() % (
sizeof(WCS_SECRET_CHARSET) - 1) ] );
214 #endif //WAMP_AUTH_UTILS_HPP