From c642a88fe64593799d35db3bbc55ab5659ec6734 Mon Sep 17 00:00:00 2001 From: shockrah Date: Sun, 15 Sep 2019 23:45:47 -0700 Subject: [PATCH 1/4] base for h3 --- 334/homework/3/chap5.txt | 14 +++++++++++++ 334/homework/3/chap6.txt | 14 +++++++++++++ 334/homework/3/msh.c | 43 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+) create mode 100644 334/homework/3/chap5.txt create mode 100644 334/homework/3/chap6.txt create mode 100644 334/homework/3/msh.c diff --git a/334/homework/3/chap5.txt b/334/homework/3/chap5.txt new file mode 100644 index 0000000..a98d582 --- /dev/null +++ b/334/homework/3/chap5.txt @@ -0,0 +1,14 @@ +# +# Problem lines begin with "#@". +# For each problem line, enter your answer on the following line. +# Do not modify this file in any way except to enter your answers. +# + +#@ 1 -- enter 'a', 'b', or 'c' (don't use quotes in any answer) +b +#@ 2 -- enter 'a', 'b', or 'c' +a +#@ 3 -- enter 'a', 'b', or 'c' +b +#@ 4 -- enter 'a', 'b', or 'c' +a diff --git a/334/homework/3/chap6.txt b/334/homework/3/chap6.txt new file mode 100644 index 0000000..c874cce --- /dev/null +++ b/334/homework/3/chap6.txt @@ -0,0 +1,14 @@ +# +# Problem lines begin with "#@". +# For each problem line, enter your answer on the following line. +# Do not modify this file in any way except to enter your answers. +# + +#@ 1 -- enter 'a' or 'b' (don't use quotes in any answer) +b +#@ 2 -- enter 'a', 'b', or 'c' +a +#@ 3 -- enter 'T' or 'F' +F +#@ 4 -- enter 'T' or 'F' +F diff --git a/334/homework/3/msh.c b/334/homework/3/msh.c new file mode 100644 index 0000000..956d3de --- /dev/null +++ b/334/homework/3/msh.c @@ -0,0 +1,43 @@ +#include +#include +#include + +#define PROMPT "msh> " +#define EXIT_CMD "exit" +#define MAX_BUF 121 + +void echo(const char*); +void remove_newline(char*); +void exit_branch(const char*); + +int main(void) { + char buffer[MAX_BUF]; + while(1) { + printf("%s", PROMPT); + fgets(buffer, MAX_BUF, stdin); + // process the input + remove_newline(buffer); + exit_branch(buffer); + echo(buffer); + } + return 0; +} + +void echo(const char* buf) { + printf("%s\n", buf); +} + + +void remove_newline(char* buf) { + char* c = buf; + while(*c != '\n') { c++; } + *c='\0'; +} + +void exit_branch(const char* buf) { + if(strcmp(buf, EXIT_CMD)) { + return; + } + exit(0); + return; // just for gcc but exit shouldn't fail +} From 61cd0af471b018e6b4627f37897ad02823470928 Mon Sep 17 00:00:00 2001 From: shockrah Date: Mon, 16 Sep 2019 02:07:28 -0700 Subject: [PATCH 2/4] couple things missing but meh its there mostly --- 334/homework/3/msh.c | 79 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 72 insertions(+), 7 deletions(-) diff --git a/334/homework/3/msh.c b/334/homework/3/msh.c index 956d3de..6ad650d 100644 --- a/334/homework/3/msh.c +++ b/334/homework/3/msh.c @@ -1,24 +1,47 @@ #include #include #include +#include +#include +#include #define PROMPT "msh> " #define EXIT_CMD "exit" #define MAX_BUF 121 +// Statuses from builtin responses +#define RESP_HELP 1 +#define RESP_DATE 2 +#define RESP_ECHO 3 +#define RESP_SHELL 4 + void echo(const char*); void remove_newline(char*); void exit_branch(const char*); +int builtin_response(char*); + int main(void) { char buffer[MAX_BUF]; while(1) { printf("%s", PROMPT); fgets(buffer, MAX_BUF, stdin); - // process the input - remove_newline(buffer); + // Deal with EOF input + if(!strlen(buffer)) { + exit(0); + } exit_branch(buffer); - echo(buffer); + + remove_newline(buffer); + int status = builtin_response(buffer); + // the switch in case we need to add more response flags later + switch(status) { + case RESP_ECHO: + echo(buffer); + case RESP_SHELL: // shell command handler was used + continue; + } + memset(buffer, 0x00, MAX_BUF); // reset buffer after each usage } return 0; } @@ -35,9 +58,51 @@ void remove_newline(char* buf) { } void exit_branch(const char* buf) { - if(strcmp(buf, EXIT_CMD)) { - return; + if(!strcmp(buf, EXIT_CMD)) { + exit(0); } - exit(0); - return; // just for gcc but exit shouldn't fail +} + +int builtin_response(char* buffer) { +#define STRING_DELIMITER " " +#define TIME_FMT "%D" + exit_branch(buffer); // exit is builtin so here we are + if(!strcmp(buffer, "help")) { + echo("enter Linux commands, or ‘exit’ to exit"); + return RESP_HELP; + } + + // Printout the date today + if(!strcmp(buffer, "date")) { + static char date[64]; + time_t now = time(0); + strftime(date, sizeof(date), TIME_FMT, localtime(&now)); + echo(date); + return RESP_DATE; + } + + char *tokens[64]; + int idx = 0; + for(char* cur = strtok(buffer, STRING_DELIMITER); cur != NULL; cur=strtok(NULL, STRING_DELIMITER)) { +#ifdef DBG_LINE + printf("%s ", cur); +#endif + tokens[idx] = cur; + idx++; + } + + // Execute child process + pid_t child = fork(); + if(child) { + int status_child = wait(NULL); + if(status_child == 127) { + echo("command not found: oh btw remember to not print this string kek"); + } + } + execvp(tokens[0], tokens); + // Cleanup pointers + for(int i = 0; i< 64; i++) { + tokens[i] = NULL; + } + } From 374022ee7abb50c41c0a88a40b5bebe5d8bb7cd6 Mon Sep 17 00:00:00 2001 From: shockrah Date: Tue, 17 Sep 2019 00:25:55 -0700 Subject: [PATCH 3/4] homework 3 i think --- 334/homework/3/msh.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/334/homework/3/msh.c b/334/homework/3/msh.c index 6ad650d..73e6511 100644 --- a/334/homework/3/msh.c +++ b/334/homework/3/msh.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include @@ -93,13 +94,15 @@ int builtin_response(char* buffer) { // Execute child process pid_t child = fork(); - if(child) { + if(child != 0) { int status_child = wait(NULL); if(status_child == 127) { - echo("command not found: oh btw remember to not print this string kek"); + printf("msh: %s: %d", tokens[0], status_child); } } - execvp(tokens[0], tokens); + else { + execvp(tokens[0], tokens); + } // Cleanup pointers for(int i = 0; i< 64; i++) { tokens[i] = NULL; From b1e2526fbb1659621ecbaf3368e8c2c9a56ff976 Mon Sep 17 00:00:00 2001 From: shockrah Date: Thu, 19 Sep 2019 00:12:58 -0700 Subject: [PATCH 4/4] writing things down to never see this class again --- 312/notes/ciphers.md | 50 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 312/notes/ciphers.md diff --git a/312/notes/ciphers.md b/312/notes/ciphers.md new file mode 100644 index 0000000..49fa7b5 --- /dev/null +++ b/312/notes/ciphers.md @@ -0,0 +1,50 @@ +# Block Ciphers + +The main concept here is twofold : + +* we take _blocks_ of data and cipher the _blocks_ +* A given key is actually used to generate recursive keys to be further used on the data itself + + +_bs example ahead_ + +Say we have a key 7 and some data 123456. +We take the whole data set and chunk it into blocks(for example): 12 34 56. + +Let's say our function here is to just add 7 to each block so we do the first step: + +``` +12 + 7 = 19 +Unlike other ciphers we don't reuse 7; instead we use the new thing as both the new key and part of our cipher text + +19 + 34 = 53 +Cipher: 1953.. + +53 + 56 = 109 <= let's pretend that this rolls over 99 and back to 00 + 09 <= like this + +Final cipher: 195309 +``` + +_It should be noted that in practice these functions usually take in huge keys and blocks_. + +# Feistal Cipher + +Two main components: + +1. each _thing_ in the data to cipher is replaced by a _ciphered thing_ + +2. nothing is added or deleted or replaced in sequence, instead the order of _things_ is changed. + +Basically imagine that every _type of thing_ in our data maps to some other _type of thing/thing_ in the data and thus become swapped/reordered. + +# DES - Data Encryption Standard + +Widely used until about 2001 when AES surpassed it as the newer(ish(kinda)) standard. + +DEA was the actual algorithm tho: + +* 64 bit blocks +* 56 bit keys +* turns a 64-bit input into a 64-bit output (wew) +* Steps in reverse also reverse the encryption itself