curl sha1

This commit is contained in:
zhulongjun 2018-11-22 10:23:36 +08:00
parent 1f3a5f7f32
commit e0cc2d0834
3 changed files with 25 additions and 1 deletions

View File

@ -22,12 +22,19 @@ namespace a8
curl_easy_setopt(curl, CURLOPT_URL, url); curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_HEADER, 0); curl_easy_setopt(curl, CURLOPT_HEADER, 0);
curl_easy_setopt(curl, CURLOPT_HTTPPOST, 1); curl_easy_setopt(curl, CURLOPT_HTTPPOST, 1);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); curl_slist* list = nullptr;
if (headers) {
list = curl_slist_append(list, headers);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
}
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, content.c_str()); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, content.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &_ReceiveResponse); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &_ReceiveResponse);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
bool ok = curl_easy_perform(curl) == CURLE_OK; bool ok = curl_easy_perform(curl) == CURLE_OK;
curl_easy_cleanup(curl); curl_easy_cleanup(curl);
if (list) {
curl_slist_free_all(list);
}
return ok; return ok;
} }

16
a8/openssl.cc Normal file → Executable file
View File

@ -134,5 +134,21 @@ namespace a8
return std::string((char*)md, SHA_DIGEST_LENGTH); return std::string((char*)md, SHA_DIGEST_LENGTH);
} }
std::string sha1(std::string& from)
{
unsigned char digest[SHA_DIGEST_LENGTH];
SHA_CTX ctx;
SHA1_Init(&ctx);
SHA1_Update(&ctx, from.data(), from.size());
SHA1_Final(digest, &ctx);
char mdString[SHA_DIGEST_LENGTH*2+1];
for (int i = 0; i < SHA_DIGEST_LENGTH; i++){
sprintf(&mdString[i*2], "%02x", (unsigned int)digest[i]);
}
return std::string(mdString);
}
} }
} }

1
a8/openssl.h Normal file → Executable file
View File

@ -16,6 +16,7 @@ namespace a8
void Base64Encode(const std::string& from, std::string& to); void Base64Encode(const std::string& from, std::string& to);
std::string Sha1Encode(std::string& from); std::string Sha1Encode(std::string& from);
std::string sha1(std::string& from);
} }
} }