tanszek:oktatas:techcomm:information
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
tanszek:oktatas:techcomm:information [2024/10/13 12:19] – [Information] kissa | tanszek:oktatas:techcomm:information [2024/10/15 06:30] (current) – [Example of Entropy calculation] knehez | ||
---|---|---|---|
Line 99: | Line 99: | ||
which means the redundancy of the event system is approximately 16%. | which means the redundancy of the event system is approximately 16%. | ||
+ | |||
+ | ==== Example of Entropy calculation ==== | ||
+ | |||
+ | Why do not store raw password strings in compiled code? | ||
+ | |||
+ | <sxh c> | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | float calculateEntropy(unsigned int bytes[], int length); | ||
+ | |||
+ | char sample[] = "Some poetry types are unique to particular cultures and genres and respond to yQ%v? | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | unsigned int byteCounter[256]; | ||
+ | const int windowWidth = 20; | ||
+ | |||
+ | for(int i = 0; i < sizeof(sample) - windowWidth; | ||
+ | { | ||
+ | memset(byteCounter, | ||
+ | |||
+ | char *p = & | ||
+ | char *end = & | ||
+ | |||
+ | while(p != end) | ||
+ | { | ||
+ | byteCounter[(unsigned char)(*p++)]++; | ||
+ | } | ||
+ | float entropy = calculateEntropy(byteCounter, | ||
+ | printf(" | ||
+ | } | ||
+ | |||
+ | |||
+ | } | ||
+ | |||
+ | |||
+ | float calculateEntropy(unsigned int bytes[], int length) | ||
+ | { | ||
+ | float entropy = 0.0f; | ||
+ | |||
+ | for (int i = 0; i < 256; i++) | ||
+ | { | ||
+ | if (bytes[i] != 0) | ||
+ | { | ||
+ | float freq = (float) bytes[i] / (float) length; | ||
+ | entropy += -freq * log2f(freq); | ||
+ | } | ||
+ | } | ||
+ | return entropy; | ||
+ | } | ||
+ | </ | ||
tanszek/oktatas/techcomm/information.1728821970.txt.gz · Last modified: 2024/10/13 12:19 by kissa