HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_Obfuscator.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: UT_Obfuscator.h
7  *
8  * COMMENTS:
9  * This is a simple class that encodes and decodes ASCII
10  * strings. It uses a simple substitution cipher, so it
11  * is by no means "secure" in the same way true encryption
12  * is, and shouldn't be relied on exclusively for protection.
13  * While it uses a fixed table generated based on a seed,
14  * it can also take in a secondary key which it uses to permute
15  * the string to be encoded. Note that to decode the string back,
16  * the same key must be passed in.
17  *
18  * Please note that it currently only supports [A-Z][a-z][0-9] and
19  * '_' characters. Any other characters will not be substituted and
20  * will remain unchanged.
21  *
22  * The result of the encoding is guaranteed to be a valid ASCII string.
23  */
24 
25 
26 #ifndef __UT_Obfuscator_h__
27 #define __UT_Obfuscator_h__
28 
29 #include "UT_API.h"
30 
31 #define NUM_LETTERS 26
32 #define NUM_DIGITS 10
33 // +1 for underscore
34 #define TOTAL_MAP_LEN (NUM_LETTERS*2 + NUM_DIGITS + 1)
35 
36 
37 class UT_String;
38 class UT_WorkBuffer;
39 
40 
41 /*****************************************************************************/
43 {
44 public:
45  UT_Obfuscator();
46  ~UT_Obfuscator();
47 
48  void encode(UT_String& str_in_out, const char* extra_key_in = nullptr);
49 
50  void decode(UT_String& str_in_out, const char* extra_key_in = nullptr)
51  { decodeImpl(str_in_out, extra_key_in); }
52  void decode(UT_WorkBuffer& str_in_out, const char* extra_key_in = nullptr)
53  { decodeImpl(str_in_out, extra_key_in); }
54 
55 private:
56 
57  void reset();
58 
59  void generateMap();
60  int getIndexForChar(char chr);
61 
62  void factorInExtraKey(UT_String& str_in_out, const char* extra_key);
63 
64  template <typename StringT>
65  void factorOutExtraKey(StringT& str_in_out, const char* extra_key);
66  template <typename StringT>
67  void decodeImpl(StringT& str_in_out, const char* extra_key_in);
68 
69 private:
70 
71  char myMap[TOTAL_MAP_LEN];
72  char myRevMap[TOTAL_MAP_LEN];
73 };
74 /*****************************************************************************/
75 #endif
#define UT_API
Definition: UT_API.h:13
void decode(UT_String &str_in_out, const char *extra_key_in=nullptr)
Definition: UT_Obfuscator.h:50
#define TOTAL_MAP_LEN
Definition: UT_Obfuscator.h:34
void decode(UT_WorkBuffer &str_in_out, const char *extra_key_in=nullptr)
Definition: UT_Obfuscator.h:52