[sword-svn] r3614 - in trunk: include src/modules/common tests

scribe at crosswire.org scribe at crosswire.org
Sat Dec 29 14:23:25 MST 2018


Author: scribe
Date: 2018-12-29 14:23:25 -0700 (Sat, 29 Dec 2018)
New Revision: 3614

Modified:
   trunk/include/swcipher.h
   trunk/src/modules/common/swcipher.cpp
   trunk/tests/ciphertest.cpp
Log:
Added personalization mechanism for cipher keys

Modified: trunk/include/swcipher.h
===================================================================
--- trunk/include/swcipher.h	2018-12-29 21:23:01 UTC (rev 3613)
+++ trunk/include/swcipher.h	2018-12-29 21:23:25 UTC (rev 3614)
@@ -27,6 +27,7 @@
 #include <sapphire.h>
 
 #include <defs.h>
+#include <swbuf.h>
 
 SWORD_NAMESPACE_START
 
@@ -48,6 +49,7 @@
   virtual char *cipherBuf (unsigned long *len, const char *buf = 0);
   virtual void Encode (void);
   virtual void Decode (void);
+  static SWBuf personalize(const SWBuf &buf, bool encode);
 };
 
 SWORD_NAMESPACE_END

Modified: trunk/src/modules/common/swcipher.cpp
===================================================================
--- trunk/src/modules/common/swcipher.cpp	2018-12-29 21:23:01 UTC (rev 3613)
+++ trunk/src/modules/common/swcipher.cpp	2018-12-29 21:23:25 UTC (rev 3614)
@@ -25,7 +25,20 @@
 #include <stdlib.h>
 #include <string.h>
 #include <swcipher.h>
+#include <map>
 
+namespace {
+	char lats[] = {
+		'b', 'c', 'e', 'a', 'f', 'g', 'i', 'j', 'k', 'l',
+		'h', 'm', 'p', 'q', 'B', 'r', 'H', 'o', 's', 't',
+		'T', 'u', 'w', 'x', 'y', 'A', 'd', 'C', 'D', 'z',
+		'E', 'F', 'I', 'J', 'K', 'G', 'L', 'N', 'O', '7',
+		'P', 'Q', 'M', 'R', 'S', 'U', 'V', 'W', 'X', 'Y',
+		'9', '0', '1', '2', 'Z', '3', '6', '4', 'n', '8',
+		'v', '5'
+	};
+}
+
 SWORD_NAMESPACE_START
 
 /******************************************************************************
@@ -34,7 +47,8 @@
  */
 
 SWCipher::SWCipher(unsigned char *key) {
-	master.initialize(key, strlen((char *)key));
+	SWBuf cipherKey = personalize((const char *)key, false);
+	master.initialize((unsigned char *)(const char *)cipherKey, cipherKey.size());
 	buf = 0;
 }
 
@@ -140,8 +154,56 @@
  */
 
 void SWCipher::setCipherKey(const char *ikey) {
-	unsigned char *key = (unsigned char *)ikey;
-	master.initialize(key, strlen((char *)key));
+	SWBuf cipherKey = personalize(ikey, false);
+	master.initialize((unsigned char *)(const char *)cipherKey, cipherKey.size());
 }
 
+
+/******************************************************************************
+ * SWCipher::personalize	- a simple personalization encoding
+ *
+ * encode - whether to encode or decode
+ *
+ */
+SWBuf SWCipher::personalize(const SWBuf &buf, bool encode) {
+
+	std::map<char, int> charHash;
+	for (int i = 0; i < 62; ++i) charHash[lats[i]] = i;
+
+	SWBuf segs[5];
+	int segn = 0;
+	for (unsigned int i = 0; i < buf.size() && segn < 5; ++i) {
+		if (buf[i] == '-') ++segn;
+		else segs[segn].append(buf[i]);
+	}
+	SWBuf result;
+	SWBuf chkSum = segs[4];
+	if (segs[4].size() < 5) segs[4].size(4);
+	for (int i = 0; i < 4; ++i) {
+		int csum = 0;
+		for (unsigned int j = 0; j < segs[i].size(); ++j) {
+			char hash = charHash[segs[i][j]];
+			char obfusHash = charHash[segs[0][j%segs[0].size()]];
+			if (encode) {
+				obfusHash = hash - (i ? obfusHash : 0);
+				if (obfusHash < 0) obfusHash = (62 + obfusHash);
+			}
+			else {
+				obfusHash = hash + (i ? obfusHash : 0);
+				obfusHash %= 62;
+			}
+			if (i) segs[i][j] = lats[(long)obfusHash];
+			csum += (encode ? obfusHash : hash);
+		}
+		segs[4][i] = lats[csum%62];
+		if (result.size()) result += "-";
+		result += (!encode && !i ? "" : segs[i]);
+	}
+	if (encode) {
+		result += "-";
+		result += segs[4];
+	}
+	return (!encode && chkSum != segs[4]) ? buf : result;
+}
+
 SWORD_NAMESPACE_END

Modified: trunk/tests/ciphertest.cpp
===================================================================
--- trunk/tests/ciphertest.cpp	2018-12-29 21:23:01 UTC (rev 3613)
+++ trunk/tests/ciphertest.cpp	2018-12-29 21:23:25 UTC (rev 3614)
@@ -21,6 +21,7 @@
  */
 
 #include <cipherfil.h>
+#include <swcipher.h>
 #include <filemgr.h>
 #include <swbuf.h>
 #include <iostream>
@@ -30,7 +31,7 @@
 int main(int argc, char **argv) {
 	
 	if (argc != 3) {
-		std::cerr << "usage: " << *argv << " <key> <0-encipher|1-decipher>\n";
+		std::cerr << "usage: " << *argv << " <key> <0-encipher|1-decipher|2-personalize|3-de-personalize>\n";
 		return -1;
 	}
 
@@ -44,11 +45,16 @@
 	std::cin >> buf;
 	text = buf;
 
-	filter->processText(text, (SWKey *)encipher);
+	switch (encipher) {
+	case 2:
+	case 3:
+		text = SWCipher::personalize(text, encipher == 2);
+		break;
+	default:
+		filter->processText(text, (SWKey *)encipher);
+	}
 
 	std::cout << text;
 	
-
-	
 	return 0;
 }




More information about the sword-cvs mailing list