Refactor frequency analysis
This commit is contained in:
parent
5a30f34558
commit
2503961778
4 changed files with 29 additions and 27 deletions
26
ex03/main.c
26
ex03/main.c
|
@ -1,33 +1,9 @@
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#include <math.h>
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <math.h>
|
||||||
double frequency_score(unsigned char *buf, size_t len) {
|
|
||||||
static const double english_freqs[26] = {
|
|
||||||
0.08167, 0.01492, 0.02782, 0.04253, 0.12702, 0.02228, 0.02015, // A-G
|
|
||||||
0.06094, 0.06966, 0.00153, 0.00772, 0.04025, 0.02406, 0.06749, // H-N
|
|
||||||
0.07507, 0.01929, 0.00095, 0.05987, 0.06327, 0.09056, 0.02758, // O-U
|
|
||||||
0.00978, 0.02360, 0.00150, 0.01974, 0.00074 // V-Z
|
|
||||||
};
|
|
||||||
|
|
||||||
unsigned int counts[26] = {0};
|
|
||||||
for (size_t i = 0; i < len; ++i) {
|
|
||||||
unsigned char c = tolower(buf[i]) - 'a';
|
|
||||||
if (c < 26) {
|
|
||||||
counts[c]++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
double chi2 = 0;
|
|
||||||
for (size_t i = 0; i < 26; ++i) {
|
|
||||||
double expected = len * english_freqs[i];
|
|
||||||
chi2 += pow(counts[i] - expected, 2) / expected;
|
|
||||||
}
|
|
||||||
return chi2;
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
if (argc < 2) {
|
if (argc < 2) {
|
||||||
|
|
|
@ -3,6 +3,6 @@ project('cryptopals', 'c')
|
||||||
cc = meson.get_compiler('c')
|
cc = meson.get_compiler('c')
|
||||||
m_dep = cc.find_library('m', required : false)
|
m_dep = cc.find_library('m', required : false)
|
||||||
|
|
||||||
executable('ex01', ['ex01/main.c', 'utils.c'])
|
executable('ex01', ['ex01/main.c', 'utils.c'], dependencies: m_dep)
|
||||||
executable('ex02', ['ex02/main.c', 'utils.c'])
|
executable('ex02', ['ex02/main.c', 'utils.c'], dependencies: m_dep)
|
||||||
executable('ex03', ['ex03/main.c', 'utils.c'], dependencies: m_dep)
|
executable('ex03', ['ex03/main.c', 'utils.c'], dependencies: m_dep)
|
||||||
|
|
25
utils.c
25
utils.c
|
@ -1,5 +1,6 @@
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
#include <math.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
unsigned char hex_to_byte(const char c) {
|
unsigned char hex_to_byte(const char c) {
|
||||||
|
@ -35,3 +36,27 @@ unsigned char *bytes_to_base64(unsigned char *out,
|
||||||
}
|
}
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
double frequency_score(unsigned char *buf, size_t len) {
|
||||||
|
static const double english_freqs[26] = {
|
||||||
|
0.08167, 0.01492, 0.02782, 0.04253, 0.12702, 0.02228, 0.02015, // A-G
|
||||||
|
0.06094, 0.06966, 0.00153, 0.00772, 0.04025, 0.02406, 0.06749, // H-N
|
||||||
|
0.07507, 0.01929, 0.00095, 0.05987, 0.06327, 0.09056, 0.02758, // O-U
|
||||||
|
0.00978, 0.02360, 0.00150, 0.01974, 0.00074 // V-Z
|
||||||
|
};
|
||||||
|
|
||||||
|
unsigned int counts[26] = {0};
|
||||||
|
for (size_t i = 0; i < len; ++i) {
|
||||||
|
unsigned char c = tolower(buf[i]) - 'a';
|
||||||
|
if (c < 26) {
|
||||||
|
counts[c]++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
double chi2 = 0;
|
||||||
|
for (size_t i = 0; i < 26; ++i) {
|
||||||
|
double expected = len * english_freqs[i];
|
||||||
|
chi2 += pow(counts[i] - expected, 2) / expected;
|
||||||
|
}
|
||||||
|
return chi2;
|
||||||
|
}
|
||||||
|
|
1
utils.h
1
utils.h
|
@ -7,5 +7,6 @@ unsigned char hex_to_byte(const char c);
|
||||||
size_t hex_to_bytes(unsigned char *out, const char hex[static 1]);
|
size_t hex_to_bytes(unsigned char *out, const char hex[static 1]);
|
||||||
unsigned char *bytes_to_base64(unsigned char *out,
|
unsigned char *bytes_to_base64(unsigned char *out,
|
||||||
const unsigned char in[static 1], size_t len);
|
const unsigned char in[static 1], size_t len);
|
||||||
|
double frequency_score(unsigned char *buf, size_t len);
|
||||||
|
|
||||||
#endif /* UTILS_H */
|
#endif /* UTILS_H */
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue