diff options
author | sss <sss@dark-alexandr.net> | 2023-01-17 00:38:19 +0300 |
---|---|---|
committer | sss <sss@dark-alexandr.net> | 2023-01-17 00:38:19 +0300 |
commit | cc3f33db7a8d3c4ad373e646b199808e01bc5d9b (patch) | |
tree | ec09d690c7656ab5f2cc72607e05fb359c24d8b2 /src/core/utilities.c |
added webrdp public code
Diffstat (limited to 'src/core/utilities.c')
-rw-r--r-- | src/core/utilities.c | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/src/core/utilities.c b/src/core/utilities.c new file mode 100644 index 0000000..969e36b --- /dev/null +++ b/src/core/utilities.c @@ -0,0 +1,115 @@ +/* BSD-2-Clause license + * + * Copyright (c) 2018-2023 NST <www.newinfosec.ru>, sss <sss at dark-alexandr dot net>. + * + */ + +#include <stdbool.h> +#include <stdint.h> +#include <sys/stat.h> + +#include <openssl/bio.h> +#include <openssl/buffer.h> +#include <openssl/evp.h> + +#include <openssl/sha.h> + +bool +is_regular_file(const char *path) +{ + struct stat path_stat; + stat(path, &path_stat); + return S_ISREG(path_stat.st_mode); +} + +bool +is_directory(const char *path) +{ + struct stat path_stat; + stat(path, &path_stat); + return S_ISDIR(path_stat.st_mode); +} + +bool +sha1(uint8_t *dst, const uint8_t *src, size_t src_length) +{ + SHA_CTX ctx = {0}; + if (!SHA1_Init(&ctx)) + { + return false; + } + if (!SHA1_Update(&ctx, src, src_length)) + { + return false; + } + if (!SHA1_Final(dst, &ctx)) + { + return false; + } + return true; +} + +void +hex_print(uint8_t *buf, size_t buf_len) +{ + size_t p; + for (p = 0; p < buf_len; ++p) + { + if (!p) + { + printf("00: "); + } + if (p && !(p % 8)) + { + printf("| "); + } + if (p && !(p % 16)) + { + size_t pp = p - 16; + for (; pp < p; ++pp) + { + printf("%c", ((char *)(buf))[pp]); + } + printf("\n%lx: ", p); + } + printf("%x ", ((char *)(buf))[p]); + } +} + +char +random_ascii_character() +{ + /* use whole ascii table of printable characters */ + char c; +get_new_char: + c = ((rand() % 94) + 32); + while (!c) + goto get_new_char; + return c; +} + +char * +random_ascii_string(char *buf, const size_t len) +{ + size_t i = 0; + if (!buf) + { + return 0; + } + for (; i < len; ++i) + { + buf[i] = random_ascii_character(); + } + buf[len] = 0; + return buf; +} + +void +random_bytes(uint8_t *buf, size_t buf_len) +{ + size_t i = 0; + for (; i < buf_len; ++i) + { + buf[i] = rand(); + } +} |