#define AV_RB32(x) ((((const uint8_t*)(x))[0] << 24) | \
(((const uint8_t*)(x))[1] << 16) | \
(((const uint8_t*)(x))[2] << 8) | \
((const uint8_t*)(x))[3])
std::string Utils::MD5(std::string source) {
boost::uuids::detail::md5 hash;
hash.process_bytes(source.data(), source.size());
boost::uuids::detail::md5::digest_type result;
hash.get_digest(result);
boost::uuids::detail::md5::digest_type char_digest;
char_digest[0] = AV_RB32(&result[0]);
char_digest[1] = AV_RB32(&result[1]);
char_digest[2] = AV_RB32(&result[2]);
char_digest[3] = AV_RB32(&result[3]);
std::string md5 = "";
boost::algorithm::hex_lower(reinterpret_cast<char *>(&char_digest),
reinterpret_cast<char *>(&char_digest) + sizeof(boost::uuids::detail::md5::digest_type),
std::back_inserter(md5));
return md5;
}
std::string base64_encode(std::uint8_t const *data, std::size_t len) {
std::string dest;
dest.resize(boost::beast::detail::base64::encoded_size(len));
dest.resize(boost::beast::detail::base64::encode(&dest[0], data, len));
return dest;
}
std::string base64_encode(boost::string_view s) {
return base64_encode(reinterpret_cast <
std::uint8_t const *> (s.data()), s.size());
}
std::string base64_decode(boost::string_view data) {
std::string dest;
dest.resize(boost::beast::detail::base64::decoded_size(data.size()));
auto const result = boost::beast::detail::base64::decode(
&dest[0], data.data(), data.size());
dest.resize(result.first);
return dest;
}
std::string CreateUUID() {
boost::uuids::uuid a_uuid = boost::uuids::random_generator()();
return boost::uuids::to_string(a_uuid);
}
收到WWW-Authenticate: Digest realm=“Login to 5B037E4PAK1E725”,qop=“auth”,nonce=“b252aWYtZGlnZXN0OjQyOTUxNzk1MTUw”, opaque=“”, stale=“false”
摘要鉴权:
std::string cnonce = "1234" //随机
std::string ha1 = MD5(userName + ":" + realm + ":" + password);
std::string ha2 = MD5("GET:" + target);
std::string response = MD5(ha1 + ":" + nonce + ":" + "00000001" + ":" + cnonce + ":" + qop + ":" + ha2);
std::string authorization = "Digest username=\"" + userName + "\", realm=\"" + realm + "\", nonce=\"" + nonce
+ "\", uri=\"" + target + "\", response=\"" + response + "\", qop=\"" + qop + "\", nc=\"" + "00000001" + "\", cnonce=\"" + cnonce + "\"";
utf8 gb2312
bool IsUtf8(unsigned char *data, int len) {
int num = 0;
int i = 0;
while (i < len) {
if ((data[i] & 0x80) == 0x00) {
// 0XXX_XXXX
i++;
continue;
} else if ((num = preNUm(data[i])) > 2) {
// 110X_XXXX 10XX_XXXX
// 1110_XXXX 10XX_XXXX 10XX_XXXX
// 1111_0XXX 10XX_XXXX 10XX_XXXX 10XX_XXXX
// 1111_10XX 10XX_XXXX 10XX_XXXX 10XX_XXXX 10XX_XXXX
// 1111_110X 10XX_XXXX 10XX_XXXX 10XX_XXXX 10XX_XXXX 10XX_XXXX
// preNUm()
// 返回首个字节8个bits中首�?0bit前面1bit的个数,该数量也是该字符所使用的字节数
i++;
for (int j = 0; j < num - 1; j++) {
//判断后面num - 1 个字节是不是都是10开
if ((data[i] & 0xc0) != 0x80) {
return false;
}
i++;
}
} else {
//其他情况说明不是utf-8
return false;
}
}
return true;
}
int GB2312_to_utf8(std::string const gb, std::string &utf8) {
std::string const &from_encoding("gbk");
try {
utf8 = boost::locale::conv::to_utf<char>(gb, from_encoding);
} catch (std::exception &e) {
printf("error: %s", e.what());
return false;
}
return true;
}
int Utf8_to_GB2312(std::string const utf8, std::string &gb) {
std::string const &to_encoding("gb2312");
try {
gb = boost::locale::conv::from_utf(utf8, to_encoding);
} catch (std::exception &e) {
printf("error: %s", e.what());
return false;
}
return true;
}
收集文件夹下文件并排序
#ifndef __FILEPATH__
#define __FILEPATH__
#include <algorithm>
#include <boost/filesystem.hpp>
#include <mutex>
#include <string>
#include <vector>
static bool GreaterEqSort(std::string filePath1, std::string filePath2) {
int len1 = filePath1.length();
int len2 = filePath2.length();
// std::cout<<"len1:"<<len1<<" path:"<<filePath1<<std::endl;
// std::cout<<"len2:"<<len2<<" path:"<<filePath2<<std::endl;
if (len1 < len2) {
return false;
} else if (len1 > len2) {
return true;
} else {
int iter = 0;
while (iter < len1) {
if (filePath1.at(iter) < filePath2.at(iter)) {
return false;
} else if (filePath1.at(iter) > filePath2.at(iter)) {
return true;
}
++iter;
}
}
return true;
}
static bool LessSort(std::string filePath1, std::string filePath2) {
return (!GreaterEqSort(filePath1, filePath2));
}
class FilePath {
public:
static FilePath& Instance() {
static FilePath f;
return f;
}
bool researchAudioFileFromPath(const std::string& audioPath) {
boost::filesystem::path path(audioPath);
if (!boost::filesystem::exists(path)) {
return false;
}
if (boost::filesystem::is_directory(audioPath.c_str())) {
//
boost::filesystem::directory_iterator end_iter;
for (boost::filesystem::directory_iterator iter(path); iter != end_iter;
++iter) {
if (boost::filesystem::is_regular_file(iter->status())) {
if (strstr(iter->path().string().c_str(), ".wav")) {
_audioFiles.emplace_back(iter->path().string());
}
}
}
} else {
if (strstr(audioPath.c_str(), ".wav")) {
_audioFiles.emplace_back(audioPath);
}
}
if (_audioFiles.size()) {
std::sort(_audioFiles.begin(), _audioFiles.end(), LessSort);
for (int i = 0; i < _audioFiles.size(); i++) {
printf("filename =========%s\n", _audioFiles.at(i).c_str());
}
return true;
} else
return false;
}
std::vector<std::string> _audioFiles;
std::string getFile() {
std::lock_guard<std::mutex> lock(_mutex);
std::string file = "";
if (_audioFiles.size()) {
file = _audioFiles.front();
_audioFiles.erase(_audioFiles.begin());
}
return file;
}
std::mutex _mutex;
};
#endif