summaryrefslogtreecommitdiff
path: root/src/XOR.c
diff options
context:
space:
mode:
authorakuchling <akuchling@rivest.dlitz.net>2009-08-02 13:36:46 -0400
committerDwayne C. Litzenberger <dlitz@dlitz.net>2009-08-02 13:41:58 -0400
commit670368aa1e5ef68f67651cd49cd62a8c33b4de75 (patch)
tree4c8f4c63987469dd55a235fd8f5432a95962847c /src/XOR.c
parenta6d55deddd457c4eee4fe5da591d6e2271d85992 (diff)
downloadpycrypto-670368aa1e5ef68f67651cd49cd62a8c33b4de75.tar.gz
Resurrect src/XOR.c
This partly reverts commit 5b5b496c0f81f3595d0aebb8da5196492abae429. --author='akuchling <akuchling@rivest.dlitz.net>' used so that "git blame" will show the true author of the source code. The author of this *commit* is Dwayne C. Litzenberger.
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..f2c74af
--- /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"