The SWORD Project  1.9.0.svnversion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
swcipher.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  *
3  * swcipher.cpp - code for class 'SWCipher'- a driver class that
4  * provides cipher utilities
5  *
6  * $Id: swcipher.cpp 3755 2020-07-19 18:43:07Z scribe $
7  *
8  * Copyright 1999-2013 CrossWire Bible Society (http://www.crosswire.org)
9  * CrossWire Bible Society
10  * P. O. Box 2528
11  * Tempe, AZ 85280-2528
12  *
13  * This program is free software; you can redistribute it and/or modify it
14  * under the terms of the GNU General Public License as published by the
15  * Free Software Foundation version 2.
16  *
17  * This program is distributed in the hope that it will be useful, but
18  * WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20  * General Public License for more details.
21  *
22  */
23 
24 
25 #include <stdlib.h>
26 #include <string.h>
27 #include <swcipher.h>
28 #include <map>
29 
30 namespace {
31  char lats[] = {
32  'b', 'c', 'e', 'a', 'f', 'g', 'i', 'j', 'k', 'l',
33  'h', 'm', 'p', 'q', 'B', 'r', 'H', 'o', 's', 't',
34  'T', 'u', 'w', 'x', 'y', 'A', 'd', 'C', 'D', 'z',
35  'E', 'F', 'I', 'J', 'K', 'G', 'L', 'N', 'O', '7',
36  'P', 'Q', 'M', 'R', 'S', 'U', 'V', 'W', 'X', 'Y',
37  '9', '0', '1', '2', 'Z', '3', '6', '4', 'n', '8',
38  'v', '5'
39  };
40 }
41 
43 
44 /******************************************************************************
45  * SWCipher Constructor - Initializes data for instance of SWCipher
46  *
47  */
48 
49 SWCipher::SWCipher(unsigned char *key) {
50  SWBuf cipherKey = personalize((const char *)key, false);
51  master.initialize((unsigned char *)(const char *)cipherKey, cipherKey.size());
52  buf = 0;
53 }
54 
55 
56 /******************************************************************************
57  * SWCipher Destructor - Cleans up instance of SWCipher
58  */
59 
61 {
62  if (buf)
63  free(buf);
64 }
65 
66 
67 void SWCipher::setUncipheredBuf(const char *ibuf, unsigned long ilen) {
68  if (ibuf) {
69 
70  if (buf)
71  free(buf);
72 
73  if (!ilen) {
74  len = strlen(buf);
75  ilen = len + 1;
76  }
77  else len = ilen;
78 
79  buf = (char *) malloc(ilen);
80  memcpy(buf, ibuf, ilen);
81  cipher = false;
82  }
83 
84  decode();
85 }
86 
88 
89  decode();
90 
91  return buf;
92 }
93 
94 
95 void SWCipher::setCipheredBuf(unsigned long *ilen, const char *ibuf) {
96  if (ibuf) {
97 
98  if (buf)
99  free(buf);
100 
101  buf = (char *) malloc(*ilen+1);
102  memcpy(buf, ibuf, *ilen);
103  len = *ilen;
104  cipher = true;
105  }
106 
107  encode();
108 
109  *ilen = len;
110 }
111 
112 char *SWCipher::getCipheredBuf(unsigned long *ilen) {
113 
114  encode();
115 
116  if (ilen) *ilen = len;
117 
118  return buf;
119 }
120 
121 
122 /******************************************************************************
123  * SWCipher::encode - This function "encodes" the input stream into the
124  * output stream.
125  * The GetChars() and SendChars() functions are
126  * used to separate this method from the actual
127  * i/o.
128  */
129 
131 {
132  if (!cipher) {
133  work = master;
134  for (unsigned long i = 0; i < len; i++)
135  buf[i] = work.encrypt(buf[i]);
136  cipher = true;
137  }
138 }
139 
140 
141 /******************************************************************************
142  * SWCipher::decode - This function "decodes" the input stream into the
143  * output stream.
144  * The GetChars() and SendChars() functions are
145  * used to separate this method from the actual
146  * i/o.
147  */
148 
150 {
151  if (cipher) {
152  work = master;
153  unsigned long i;
154  for (i = 0; i < len; i++)
155  buf[i] = work.decrypt(buf[i]);
156  buf[i] = 0;
157  cipher = false;
158  }
159 }
160 
161 
162 /******************************************************************************
163  * SWCipher::setCipherKey - setter for a new CipherKey
164  *
165  */
166 
167 void SWCipher::setCipherKey(const char *ikey) {
168  SWBuf cipherKey = personalize(ikey, false);
169  master.initialize((unsigned char *)(const char *)cipherKey, cipherKey.size());
170 }
171 
172 
173 /******************************************************************************
174  * SWCipher::personalize - a simple personalization encoding
175  *
176  * encode - whether to encode or decode
177  *
178  */
179 SWBuf SWCipher::personalize(const SWBuf &buf, bool encode) {
180 
181  std::map<char, int> charHash;
182  for (int i = 0; i < 62; ++i) charHash[lats[i]] = i;
183 
184  SWBuf segs[5];
185  int segn = 0;
186  for (unsigned int i = 0; i < buf.size() && segn < 5; ++i) {
187  if (buf[i] == '-') ++segn;
188  else segs[segn].append(buf[i]);
189  }
190  SWBuf result;
191  SWBuf chkSum = segs[4];
192  if (segs[4].size() < 5) segs[4].size(4);
193  for (int i = 0; i < 4; ++i) {
194  int csum = 0;
195  for (unsigned int j = 0; j < segs[i].size() && j < segs[0].size(); ++j) {
196  char hash = charHash[segs[i][j]];
197  char obfusHash = charHash[segs[0][j%segs[0].size()]];
198  if (encode) {
199  obfusHash = hash - (i ? obfusHash : 0);
200  if (obfusHash < 0) obfusHash = (62 + obfusHash);
201  }
202  else {
203  obfusHash = hash + (i ? obfusHash : 0);
204  obfusHash %= 62;
205  }
206  if (i) segs[i][j] = lats[(long)obfusHash];
207  csum += (encode ? obfusHash : hash);
208  }
209  segs[4][i] = lats[csum%62];
210  if (result.size()) result += "-";
211  result += (!encode && !i ? "" : segs[i].c_str());
212  }
213  if (encode) {
214  result += "-";
215  result += segs[4];
216  }
217  return (!encode && chkSum != segs[4]) ? buf : result;
218 }
219 
Sapphire work
Definition: swcipher.h:37
#define SWORD_NAMESPACE_START
Definition: defs.h:39
Sapphire master
Definition: swcipher.h:36
SWCipher(unsigned char *key)
Definition: swcipher.cpp:49
virtual void decode(void)
Definition: swcipher.cpp:149
Definition: swbuf.h:47
unsigned char encrypt(unsigned char b=0)
Definition: sapphire.cpp:169
virtual void setCipherKey(const char *key)
Definition: swcipher.cpp:167
virtual ~SWCipher()
Definition: swcipher.cpp:60
virtual void encode(void)
Definition: swcipher.cpp:130
virtual void setCipheredBuf(unsigned long *len, const char *buf=0)
Definition: swcipher.cpp:95
char * buf
Definition: swcipher.h:39
char * malloc()
free(preg->fastmap)
const char * c_str() const
Definition: swbuf.h:158
SWBuf & append(const char *str, long max=-1)
Definition: swbuf.h:274
void initialize(unsigned char *key, unsigned char keysize)
Definition: sapphire.cpp:75
virtual char * getCipheredBuf(unsigned long *len=0)
Definition: swcipher.cpp:112
bool cipher
Definition: swcipher.h:40
virtual void setUncipheredBuf(const char *buf=0, unsigned long len=0)
Definition: swcipher.cpp:67
unsigned long size() const
Definition: swbuf.h:185
unsigned long len
Definition: swcipher.h:41
int size
Definition: regex.c:5043
static SWBuf personalize(const SWBuf &buf, bool encode)
Definition: swcipher.cpp:179
unsigned char decrypt(unsigned char b)
Definition: sapphire.cpp:200
virtual char * getUncipheredBuf()
Definition: swcipher.cpp:87
result
Definition: regex.c:5545
#define SWORD_NAMESPACE_END
Definition: defs.h:40