add hamming_distance
This commit is contained in:
parent
c81c8bfa3c
commit
89a3585c7f
@ -92,8 +92,16 @@ const unsigned char *repeating_key_xor_s(const char* s, const char* key);
|
|||||||
|
|
||||||
### hamming_distance
|
### hamming_distance
|
||||||
|
|
||||||
|
Calculates the Hamming Distance (the number of differing bits) between two arrays of unsigned bytes
|
||||||
|
|
||||||
|
```c
|
||||||
|
unsigned int hamming_distance(unsigned char *a, unsigned char *b);
|
||||||
|
```
|
||||||
|
|
||||||
|
### hamming_distance_s
|
||||||
|
|
||||||
Calculates the Hamming Distance (the number of differing bits) between two strings
|
Calculates the Hamming Distance (the number of differing bits) between two strings
|
||||||
|
|
||||||
```c
|
```c
|
||||||
unsigned int hamming_distance(const char *a, const char *b);
|
unsigned int hamming_distance_s(const char *a, const char *b);
|
||||||
```
|
```
|
||||||
|
@ -13,6 +13,7 @@ const char *hex_to_str(const unsigned char *hex, size_t sz);
|
|||||||
const unsigned char* repeating_key_xor(const unsigned char* s, size_t s_sz, const unsigned char* key, size_t k_sz);
|
const unsigned char* repeating_key_xor(const unsigned char* s, size_t s_sz, const unsigned char* key, size_t k_sz);
|
||||||
const unsigned char *repeating_key_xor_s(const char* s, const char* key);
|
const unsigned char *repeating_key_xor_s(const char* s, const char* key);
|
||||||
|
|
||||||
unsigned int hamming_distance(const char *a, const char *b);
|
unsigned int hamming_distance_s(const char *a, const char *b);
|
||||||
|
unsigned int hamming_distance(unsigned char *a, unsigned char *b, size_t sz);
|
||||||
|
|
||||||
#endif // LIBFLINT_CRYPTO_H
|
#endif // LIBFLINT_CRYPTO_H
|
||||||
|
@ -272,12 +272,16 @@ const unsigned char *repeating_key_xor_s(const char* s, const char* key) {
|
|||||||
return repeating_key_xor((unsigned char*)s, strlen(s), (unsigned char*)key, strlen(key));
|
return repeating_key_xor((unsigned char*)s, strlen(s), (unsigned char*)key, strlen(key));
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int hamming_distance(const char *a, const char *b) {
|
unsigned int hamming_distance_s(const char *a, const char *b) {
|
||||||
size_t sz = strlen(a);
|
size_t sz = strlen(a);
|
||||||
if (sz != strlen(b)) {
|
if (sz != strlen(b)) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return hamming_distance((unsigned char *)a, (unsigned char *)b, sz);
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned int hamming_distance(unsigned char *a, unsigned char *b, size_t sz) {
|
||||||
unsigned int hamming = 0;
|
unsigned int hamming = 0;
|
||||||
for (size_t i = 0; i < sz; ++i) {
|
for (size_t i = 0; i < sz; ++i) {
|
||||||
if (a[i] == b[i]) {
|
if (a[i] == b[i]) {
|
||||||
|
@ -374,7 +374,12 @@ void test_crypto() {
|
|||||||
free(enc);
|
free(enc);
|
||||||
free(s);
|
free(s);
|
||||||
|
|
||||||
unsigned int hamming = hamming_distance("this is a test", "wokka wokka!!!");
|
unsigned char ua[2] = { 0x2, 0xF };
|
||||||
|
unsigned char ub[2] = { 0x4, 0xE };
|
||||||
|
unsigned int hamming = hamming_distance(ua, ub, 2);
|
||||||
|
assert(hamming == 3);
|
||||||
|
|
||||||
|
hamming = hamming_distance_s("this is a test", "wokka wokka!!!");
|
||||||
assert(hamming == 37);
|
assert(hamming == 37);
|
||||||
|
|
||||||
printf("Passes all crypto tests\n");
|
printf("Passes all crypto tests\n");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user