MCPcopy Create free account
hub / github.com/Do0ks/GSpots / calculateEntropy

Function calculateEntropy

EncryptionDetection/EncryptionDetection.cpp:7–20  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

5#include <vector>
6
7double calculateEntropy(const std::vector<Byte>& data) {
8 std::vector<double> freq(256, 0.0);
9 for (Byte b : data) {
10 freq[b]++;
11 }
12 double entropy = 0.0;
13 for (double count : freq) {
14 if (count > 0) {
15 double p = count / static_cast<double>(data.size());
16 entropy -= p * log2(p);
17 }
18 }
19 return entropy;
20}
21
22bool IsFileEncrypted(const std::vector<Byte>& data) {
23 double entropy = calculateEntropy(data);

Callers 1

IsFileEncryptedFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected