aboutsummaryrefslogtreecommitdiff
path: root/lib/crypto-algorithms/rot-13.c
diff options
context:
space:
mode:
authorMark Haines <mjark@negativecurvature.net>2015-02-26 16:43:06 +0000
committerMark Haines <mjark@negativecurvature.net>2015-02-26 16:43:06 +0000
commit15bb93321fdf9c5ec2c75e03f9a1bb5fdf8b7e10 (patch)
treed754f9776a62095d5adfb257134f7bcff54c1157 /lib/crypto-algorithms/rot-13.c
parent6c56bcf2fd3db38c679b9cf9345051a7309fa02f (diff)
parentc61e5359cb454ae20c0b7b057c3a7b53e2beefd6 (diff)
Merge commit 'c61e5359cb454ae20c0b7b057c3a7b53e2beefd6' as 'lib/crypto-algorithms'
Diffstat (limited to 'lib/crypto-algorithms/rot-13.c')
-rw-r--r--lib/crypto-algorithms/rot-13.c35
1 files changed, 35 insertions, 0 deletions
diff --git a/lib/crypto-algorithms/rot-13.c b/lib/crypto-algorithms/rot-13.c
new file mode 100644
index 0000000..0ab8497
--- /dev/null
+++ b/lib/crypto-algorithms/rot-13.c
@@ -0,0 +1,35 @@
+/*********************************************************************
+* Filename: rot-13.c
+* Author: Brad Conte (brad AT bradconte.com)
+* Copyright:
+* Disclaimer: This code is presented "as is" without any guarantees.
+* Details: Implementation of the ROT-13 encryption algorithm.
+ Algorithm specification can be found here:
+ *
+ This implementation uses little endian byte order.
+*********************************************************************/
+
+/*************************** HEADER FILES ***************************/
+#include <string.h>
+#include "rot-13.h"
+
+/*********************** FUNCTION DEFINITIONS ***********************/
+void rot13(char str[])
+{
+ int case_type, idx, len;
+
+ for (idx = 0, len = strlen(str); idx < len; idx++) {
+ // Only process alphabetic characters.
+ if (str[idx] < 'A' || (str[idx] > 'Z' && str[idx] < 'a') || str[idx] > 'z')
+ continue;
+ // Determine if the char is upper or lower case.
+ if (str[idx] >= 'a')
+ case_type = 'a';
+ else
+ case_type = 'A';
+ // Rotate the char's value, ensuring it doesn't accidentally "fall off" the end.
+ str[idx] = (str[idx] + 13) % (case_type + 26);
+ if (str[idx] < 26)
+ str[idx] += case_type;
+ }
+}