From c61e5359cb454ae20c0b7b057c3a7b53e2beefd6 Mon Sep 17 00:00:00 2001 From: Mark Haines Date: Thu, 26 Feb 2015 16:43:06 +0000 Subject: Squashed 'lib/crypto-algorithms/' content from commit 100f4ff git-subtree-dir: lib/crypto-algorithms git-subtree-split: 100f4ff91b5a5b31a84b3999365c3058df6251ea --- rot-13.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 rot-13.c (limited to 'rot-13.c') diff --git a/rot-13.c b/rot-13.c new file mode 100644 index 0000000..0ab8497 --- /dev/null +++ b/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 +#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; + } +} -- cgit v1.2.3