cryptopals_c

cryptopals crypto challenges solutions in pure c
git clone git://git.superpozycja.net/cryptopals_c
Log | Files | Refs | README

commit 95752fee2b8b9ee983d8cb79a7bc1c224aaefea9
parent 799a0de697d6b0ee0d999ca51f1ceb14354657ce
Author: superpozycja <anna@superpozycja.net>
Date:   Sun,  1 Sep 2024 18:49:15 +0200

fix style

Diffstat:
Msrc/s1/c1.c | 45+++++++++++++++++++++++++++++++--------------
Msrc/s1/c2.c | 6++++--
Msrc/s1/c3.c | 8+++++---
3 files changed, 40 insertions(+), 19 deletions(-)

diff --git a/src/s1/c1.c b/src/s1/c1.c @@ -7,31 +7,46 @@ static int hex_to_base64(const char* hex, char** b64) const char b64table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; - int length = strlen(hex); - int block_count = length / 6; - int res_length = block_count*4; - int leftover_length = length % 6; + int leftover_length; + int block_count; + int res_length; + int length; + char *res; + int i; + + length = strlen(hex); + block_count = length / 6; + leftover_length = length % 6; + res_length = block_count * 4; + if (leftover_length%2 == 1) { fprintf(stderr, "bad sequence length\n"); return -1; } - char * res = (char*)(malloc(sizeof(char)*res_length+2)); + res = (char *) malloc(sizeof(char) * res_length + 2); - for (int i = 0; i < block_count+(leftover_length != 0); i++) { - int end_ix = i < block_count ? 6 : leftover_length; + for (i = 0; i < block_count + (leftover_length != 0); i++) { char block[6+1]; + int block_hex; + int end_ix; + int j; + + end_ix = i < block_count ? 6 : leftover_length; memcpy(block, hex+(6*i), end_ix); block[end_ix] = '\0'; - int block_hex = strtol(block, NULL, 16); - block_hex <<= 4*(6-end_ix); + block_hex = strtol(block, NULL, 16); + block_hex <<= 4 * (6 - end_ix); + + for (j = 0; j < 4; j++) { + char b64; - for (int j = 0; j < 4; j++) { - char b64 = b64table[(block_hex >> 6*(3-j))& 0x3f]; - res[4*i+j] = j <= end_ix/2 ? b64 : '='; + b64 = b64table[(block_hex >> 6 * (3 - j)) & 0x3f]; + res[4 *i + j] = j <= end_ix / 2 ? b64 : '='; } } + *b64 = res; return 0; } @@ -39,8 +54,10 @@ static int hex_to_base64(const char* hex, char** b64) int main(int argc, char* argv[]) { char* b64; - int ret = hex_to_base64(argv[1], &b64); - if (ret == 0) + int ret; + + if (hex_to_base64(argv[1], &b64) == 0) printf("%s\n", b64); + return 0; } diff --git a/src/s1/c2.c b/src/s1/c2.c @@ -1,6 +1,7 @@ -#include <ba.h> /* see my tools repo for this */ +#include <ba.h> -int main(int argc, char* argv[]) { +int main(int argc, char* argv[]) +{ ba* a; ba* b; @@ -9,6 +10,7 @@ int main(int argc, char* argv[]) { a = ba_from_hex(argv[1]); b = ba_from_hex(argv[2]); + if (a == NULL || b == NULL) { return -1; } diff --git a/src/s1/c3.c b/src/s1/c3.c @@ -1,16 +1,18 @@ -#include <stdlib.h> -#include <ctype.h> #include <ba.h> #include <cryptanalysis.h> int main(int argc, char* argv[]) { + ba *ct; + if (argc != 2) return -1; - ba *ct; + ct = ba_from_hex(argv[1]); + if (!ct) return -1; + ct = decrypt_scxor(ct); ba_fprint_ascii(ct, stdout, 0); printf("\n");