summaryrefslogtreecommitdiff
path: root/src/XOR.c
diff options
context:
space:
mode:
authorakuchling <akuchling@rivest.dlitz.net>2002-05-16 16:30:50 -0700
committerakuchling <akuchling@rivest.dlitz.net>2002-05-16 16:30:50 -0700
commit88f1eaa5bf36fd5268ac67d581c44d9da329c8d8 (patch)
tree567b9b9ee473715ef48cc16adf43ff7643234368 /src/XOR.c
parentcb4ace279e321a014ff8085efcf32ef32ff859cb (diff)
downloadpycrypto-88f1eaa5bf36fd5268ac67d581c44d9da329c8d8.tar.gz
[project @ akuchling-20020516233050-85ce258c1e144af3]
[project @ 2002-05-16 16:30:50 by akuchling] Move C implementation files into src/
Diffstat (limited to 'src/XOR.c')
-rw-r--r--src/XOR.c52
1 files changed, 52 insertions, 0 deletions
diff --git a/src/XOR.c b/src/XOR.c
new file mode 100644
index 0000000..57af911
--- /dev/null
+++ b/src/XOR.c
@@ -0,0 +1,52 @@
+/*
+ * xor.c : Source for the trivial cipher which XORs the message with the key.
+ * The key can be up to 32 bytes long.
+ *
+ * Part of the Python Cryptography Toolkit
+ *
+ * Distribute and use freely; there are no restrictions on further
+ * dissemination and usage except those imposed by the laws of your
+ * country of residence.
+ *
+ */
+
+#define MODULE_NAME XOR
+#define BLOCK_SIZE 1
+#define KEY_SIZE 0
+
+typedef struct
+{
+ unsigned char key[32];
+ int keylen, last_pos;
+} stream_state;
+
+static void
+stream_init(stream_state *self, unsigned char *key, int len)
+{
+ int i;
+
+ if (32 <= len) len=32;
+ self->keylen = len;
+ self->last_pos = 0;
+
+ for(i=0; i<len; i++)
+ {
+ self->key[i] = key[i];
+ }
+}
+
+/* Encryption and decryption are symmetric */
+#define stream_decrypt stream_encrypt
+
+static void stream_encrypt(stream_state *self, unsigned char *block,
+ int len)
+{
+ int i, j = self->last_pos;
+ for(i=0; i<len; i++, j=(j+1) % self->keylen)
+ {
+ block[i] ^= self->key[j];
+ }
+ self->last_pos = j;
+}
+
+#include "stream_template.c"