The SWORD Project  1.9.0.svnversion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SWCipher Class Reference

#include <swcipher.h>

+ Collaboration diagram for SWCipher:

Public Member Functions

virtual void decode (void)
 
virtual void encode (void)
 
virtual char * getCipheredBuf (unsigned long *len=0)
 
virtual char * getUncipheredBuf ()
 
virtual void setCipheredBuf (unsigned long *len, const char *buf=0)
 
virtual void setCipherKey (const char *key)
 
virtual void setUncipheredBuf (const char *buf=0, unsigned long len=0)
 
 SWCipher (unsigned char *key)
 
virtual ~SWCipher ()
 

Static Public Member Functions

static SWBuf personalize (const SWBuf &buf, bool encode)
 

Private Attributes

char * buf
 
bool cipher
 
unsigned long len
 
Sapphire master
 
Sapphire work
 

Detailed Description

Definition at line 33 of file swcipher.h.

Constructor & Destructor Documentation

SWORD_NAMESPACE_START SWCipher::SWCipher ( unsigned char *  key)

Definition at line 49 of file swcipher.cpp.

49  {
50  SWBuf cipherKey = personalize((const char *)key, false);
51  master.initialize((unsigned char *)(const char *)cipherKey, cipherKey.size());
52  buf = 0;
53 }
Sapphire master
Definition: swcipher.h:36
Definition: swbuf.h:47
char * buf
Definition: swcipher.h:39
void initialize(unsigned char *key, unsigned char keysize)
Definition: sapphire.cpp:75
unsigned long size() const
Definition: swbuf.h:185
static SWBuf personalize(const SWBuf &buf, bool encode)
Definition: swcipher.cpp:179
SWCipher::~SWCipher ( )
virtual

Definition at line 60 of file swcipher.cpp.

61 {
62  if (buf)
63  free(buf);
64 }
char * buf
Definition: swcipher.h:39
free(preg->fastmap)

Member Function Documentation

void SWCipher::decode ( void  )
virtual

Definition at line 149 of file swcipher.cpp.

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 }
Sapphire work
Definition: swcipher.h:37
Sapphire master
Definition: swcipher.h:36
char * buf
Definition: swcipher.h:39
bool cipher
Definition: swcipher.h:40
unsigned long len
Definition: swcipher.h:41
unsigned char decrypt(unsigned char b)
Definition: sapphire.cpp:200
void SWCipher::encode ( void  )
virtual

Definition at line 130 of file swcipher.cpp.

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 }
Sapphire work
Definition: swcipher.h:37
Sapphire master
Definition: swcipher.h:36
unsigned char encrypt(unsigned char b=0)
Definition: sapphire.cpp:169
char * buf
Definition: swcipher.h:39
bool cipher
Definition: swcipher.h:40
unsigned long len
Definition: swcipher.h:41
char * SWCipher::getCipheredBuf ( unsigned long *  len = 0)
virtual

Definition at line 112 of file swcipher.cpp.

112  {
113 
114  encode();
115 
116  if (ilen) *ilen = len;
117 
118  return buf;
119 }
virtual void encode(void)
Definition: swcipher.cpp:130
char * buf
Definition: swcipher.h:39
unsigned long len
Definition: swcipher.h:41
char * SWCipher::getUncipheredBuf ( )
virtual

Definition at line 87 of file swcipher.cpp.

87  {
88 
89  decode();
90 
91  return buf;
92 }
virtual void decode(void)
Definition: swcipher.cpp:149
char * buf
Definition: swcipher.h:39
SWBuf SWCipher::personalize ( const SWBuf buf,
bool  encode 
)
static

Definition at line 179 of file swcipher.cpp.

179  {
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 }
Definition: swbuf.h:47
virtual void encode(void)
Definition: swcipher.cpp:130
SWBuf & append(const char *str, long max=-1)
Definition: swbuf.h:274
unsigned long size() const
Definition: swbuf.h:185
int size
Definition: regex.c:5043
result
Definition: regex.c:5545
void SWCipher::setCipheredBuf ( unsigned long *  len,
const char *  buf = 0 
)
virtual

Definition at line 95 of file swcipher.cpp.

95  {
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 }
virtual void encode(void)
Definition: swcipher.cpp:130
char * buf
Definition: swcipher.h:39
char * malloc()
free(preg->fastmap)
bool cipher
Definition: swcipher.h:40
unsigned long len
Definition: swcipher.h:41
void SWCipher::setCipherKey ( const char *  key)
virtual

Definition at line 167 of file swcipher.cpp.

167  {
168  SWBuf cipherKey = personalize(ikey, false);
169  master.initialize((unsigned char *)(const char *)cipherKey, cipherKey.size());
170 }
Sapphire master
Definition: swcipher.h:36
Definition: swbuf.h:47
void initialize(unsigned char *key, unsigned char keysize)
Definition: sapphire.cpp:75
unsigned long size() const
Definition: swbuf.h:185
static SWBuf personalize(const SWBuf &buf, bool encode)
Definition: swcipher.cpp:179
void SWCipher::setUncipheredBuf ( const char *  buf = 0,
unsigned long  len = 0 
)
virtual

Definition at line 67 of file swcipher.cpp.

67  {
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 }
virtual void decode(void)
Definition: swcipher.cpp:149
char * buf
Definition: swcipher.h:39
char * malloc()
free(preg->fastmap)
bool cipher
Definition: swcipher.h:40
unsigned long len
Definition: swcipher.h:41

Member Data Documentation

char* SWCipher::buf
private

Definition at line 39 of file swcipher.h.

bool SWCipher::cipher
private

Definition at line 40 of file swcipher.h.

unsigned long SWCipher::len
private

Definition at line 41 of file swcipher.h.

Sapphire SWCipher::master
private

Definition at line 36 of file swcipher.h.

Sapphire SWCipher::work
private

Definition at line 37 of file swcipher.h.


The documentation for this class was generated from the following files: