TEST_F(HashTests, test_algorithms) { const unsigned char buffer[1] = {'0'}; auto digest = hashFromBuffer(HASH_TYPE_MD5, buffer, 1); EXPECT_EQ(digest, "cfcd208495d565ef66e7dff9f98764da"); digest = hashFromBuffer(HASH_TYPE_SHA1, buffer, 1); EXPECT_EQ(digest, "b6589fc6ab0dc82cf12099d1c2d40ab994e8410c"); digest = hashFromBuffer(HASH_TYPE_SHA256, buffer, 1); EXPECT_EQ(digest, "5feceb66ffc86f38d952786c6d696c79c2dbc239dd4e91b46729d73a27fb57e9"); }
std::string genSHA1ForCertificate(const SecCertificateRef& ca) { CFDataRef ca_data; // Access raw data, hash and release. ca_data = SecCertificateCopyData(ca); auto digest = hashFromBuffer( HASH_TYPE_SHA1, CFDataGetBytePtr(ca_data), CFDataGetLength(ca_data)); CFRelease(ca_data); return digest; }
void genACPITable(const void* key, const void* value, void* results) { Row r; auto data = (CFDataRef)value; auto length = CFDataGetLength(data); r["name"] = stringFromCFString((CFStringRef)key); r["size"] = INTEGER(length); r["md5"] = hashFromBuffer(HASH_TYPE_MD5, CFDataGetBytePtr(data), length); ((QueryData*)results)->push_back(r); }
size_t getMachineShard(const std::string& hostname = "", bool force = false) { static size_t shard = 0; if (shard > 0 && !force) { return shard; } // An optional input hostname may override hostname detection for testing. auto hn = (hostname.empty()) ? getHostname() : hostname; auto hn_hash = hashFromBuffer(HASH_TYPE_MD5, hn.c_str(), hn.size()); if (hn_hash.size() >= 2) { long hn_char; if (safeStrtol(hn_hash.substr(0, 2), 16, hn_char)) { shard = (hn_char * 100) / 255; } } return shard; }
void genSMBIOSTables(const uint8_t* tables, size_t length, QueryData& results) { // Keep a pointer to the end of the SMBIOS data for comparison. auto tables_end = tables + length; auto table = tables; // Iterate through table structures within SMBIOS data range. size_t index = 0; while (table + sizeof(SMBStructHeader) <= tables_end) { auto header = (const SMBStructHeader*)table; if (table + header->length > tables_end) { // Invalid header, length must be within SMBIOS data range. break; } Row r; // The index is a supliment that keeps track of table order. r["number"] = INTEGER(index++); r["type"] = INTEGER((unsigned short)header->type); if (kSMBIOSTypeDescriptions.count(header->type) > 0) { r["description"] = kSMBIOSTypeDescriptions.at(header->type); } r["handle"] = BIGINT((unsigned long long)header->handle); r["header_size"] = INTEGER((unsigned short)header->length); // The SMBIOS structure may have unformatted, double-NULL delimited trailing // data, which are usually strings. auto next_table = table + header->length; for (; next_table + sizeof(SMBStructHeader) <= tables_end; next_table++) { if (next_table[0] == 0 && next_table[1] == 0) { next_table += 2; break; } } auto table_length = next_table - table; r["size"] = INTEGER(table_length); r["md5"] = hashFromBuffer(HASH_TYPE_MD5, table, table_length); table = next_table; results.push_back(r); } }
Status Config::getMD5(std::string& hash) { if (!valid_) { return Status(1, "Current config is not valid"); } WriteLock lock(config_hash_mutex_); std::vector<char> buffer; buffer.reserve(hash_.size() * 32); auto add = [&buffer](const std::string& text) { for (const auto& c : text) { buffer.push_back(c); } }; for (const auto& it : hash_) { add(it.second); } hash = hashFromBuffer(HASH_TYPE_MD5, &buffer[0], buffer.size()); return Status(0, "OK"); }
void genSMBIOSTable(size_t index, const SMBStructHeader* hdr, uint8_t* address, size_t size, QueryData& results) { Row r; // The index is a supliment that keeps track of table order. r["number"] = INTEGER(index++); r["type"] = INTEGER((unsigned short)hdr->type); if (kSMBIOSTypeDescriptions.count(hdr->type) > 0) { r["description"] = kSMBIOSTypeDescriptions.at(hdr->type); } else { r["description"] = "Unknown"; } r["handle"] = BIGINT((unsigned long long)hdr->handle); r["header_size"] = INTEGER((unsigned short)hdr->length); r["size"] = INTEGER(size); r["md5"] = hashFromBuffer(HASH_TYPE_MD5, address, size); results.push_back(r); }
void Config::hashSource(const std::string& source, const std::string& content) { WriteLock wlock(config_hash_mutex_); hash_[source] = hashFromBuffer(HASH_TYPE_MD5, &(content.c_str())[0], content.size()); }
std::string genSHA1ForCertificate(const CFDataRef& raw_cert) { return hashFromBuffer( HASH_TYPE_SHA1, CFDataGetBytePtr(raw_cert), CFDataGetLength(raw_cert)); }