]> git.za3k.com Git - cryptopals.git/commitdiff
Solve Set 1, Problem 2
authorZachary Vance <za3k@za3k.com>
Sun, 14 May 2017 07:13:27 +0000 (00:13 -0700)
committerZachary Vance <za3k@za3k.com>
Sun, 14 May 2017 07:13:27 +0000 (00:13 -0700)
Makefile
set1p2.test.c [new file with mode: 0644]
xor.c [new file with mode: 0644]
xor.h [new file with mode: 0644]

index f11b1389247e46a35be1524ba62a8221459684ed..5bbfa628e4e07e3d7eded898da9298345b818ef6 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -2,11 +2,15 @@ CC=gcc
 ODIR=obj
 CFLAGS=
 
-all: io.o io.test.o
-%.test: %.o %.test.c
+all: libs
+libs: io.o xor.o
+set1p1.test: set1p1.test.c io.o
        $(CC) -o $@ $? $(CFLAGS)
-test: set1p2.test
-       ./set1p2.test
+set1p2.test: set1p2.test.c io.o xor.o
+       $(CC) -o $@ $? $(CFLAGS)
+test: set1p1.test set1p2.test
+       ./set1p1.test >/dev/null || echo "Set 1, Problem 1: Failed"
+       ./set1p2.test >/dev/null || echo "Set 1, Problem 2: Failed"
 clean:
        rm -f *.o a.out *.test
 %.o: %.c
diff --git a/set1p2.test.c b/set1p2.test.c
new file mode 100644 (file)
index 0000000..1097a80
--- /dev/null
@@ -0,0 +1,29 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include "io.h"
+#include "xor.h"
+
+int main() {
+  char a[] = "1c0111001f010100061a024b53535009181c";
+  char b[] = "686974207468652062756c6c277320657965";
+  char expected_a_xor_b[] = "746865206b696420646f6e277420706c6179";
+
+  char a_xor_b[37];
+  char a_bytes[18], b_bytes[18], ab_bytes[18];
+  int a_bytes_len, b_bytes_len;
+  decode_hex(a, a_bytes, &a_bytes_len);
+  decode_hex(b, b_bytes, &b_bytes_len);
+  if (a_bytes_len != 18 || b_bytes_len != 18) {
+    printf("Wrong length."); exit(1);
+  }
+  xor(a_bytes, b_bytes, ab_bytes, a_bytes_len);
+  encode_hex(ab_bytes, a_bytes_len, a_xor_b);
+  if (strcmp(a_xor_b, expected_a_xor_b)==0) {
+    printf("Xor succeeded.\n");
+  } else {
+    printf("Xor failed.\nExpected: %s\n     Got: %s\n", expected_a_xor_b, a_xor_b);
+    exit(1);
+  }
+  exit(0);
+}
diff --git a/xor.c b/xor.c
new file mode 100644 (file)
index 0000000..ad84b89
--- /dev/null
+++ b/xor.c
@@ -0,0 +1,5 @@
+xor(char* buf1, char* buf2, char* buf3, int length) {
+  int i;
+  for(i=0; i<length; i++)
+    buf3[i] = buf1[i] ^ buf2[i];
+}
diff --git a/xor.h b/xor.h
new file mode 100644 (file)
index 0000000..b4c7ba4
--- /dev/null
+++ b/xor.h
@@ -0,0 +1 @@
+void xor(char* buf1, char* buf2, char* buf3, int length);