Repository navigation
Expand file tree
/
Copy pathpolymorph_crypto.d
More file actions
213 lines (169 loc) · 6.23 KB
/
Copy pathpolymorph_crypto.d
File metadata and controls
213 lines (169 loc) · 6.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/++
Author: Era Scarecrow <rtcvb32@yahoo.com>
Date: 23 Oct 2012
Project: Polymorphic Structs mini-crypto
Licence: GPLv3
Description: Mini crypto with the sole purpose of testing the PolyMorphicStructs
Here's a stripped version for basic layout of how the structs are constructed.
---
struct PolyCryptoBase {
///Enum, _prefix and _types don't exist until this calls.
mixin(expandTypes("Data", "Poly_", "CryptoBase,CryptoROT13"));
struct Data {
Enum polyMorphType;
char[] crypted;
}
Data data;
mixin PolyMorphic!("smartmerged.polymorphicstruct_crypto", PolyCryptoBase, Data, "data");
}
struct CryptoBase {
mixin PolyMorphicInclude!(PolyCryptoBase);
///Does basic xor enciphering, individual letters only
static void Poly_cipherChar(ref char c) {c ^= 0x7f;}
///set the plaintext (resetting crypted text as well)
void setString(string input) { crypted = input.dup; }
///encrypt cipher string (which done twice will decrypt it)
void encipher() @property {
foreach(ref ch; data.crypted) {
//calls polymorphed version from opDispatch
this.cipherChar(ch);
}
}
}
struct CryptoROT13 {
mixin PolyMorphicInclude!(PolyCryptoBase);
void cipherChar(ref char c);
}
---
+/
module smartmerged.polymorphicstruct_crypto;
import smartmerged.polymorphicstruct;
/** This is not intended as a serious crypto library, just enough to test several factors to ensure the polymorphic
nature is being done. As other items need to be tested this crypto will be improved to handle those tests.*/
struct PolyCryptoBase {
///Enum, _prefix and _types don't exist until this calls.
mixin(expandTypes("Data", "Poly_", "CryptoBase,CryptoXOR13,CryptoROT13"));
///
struct Data {
Enum polyMorphType; ///
string original; ///
char[] crypted; ///mutable copy
}
Data data; ///
///Seems opCmp opEquals and similar functions go in the poly base. That's livable.
///if not, forward reference to Poly_opCmp
mixin PolyMorphicCompare!(PolyCryptoBase);
mixin PolyMorphic!("smartmerged.polymorphicstruct_crypto", PolyCryptoBase, Data, "data"); ///
}
///
struct CryptoBase {
mixin PolyMorphicInclude!(PolyCryptoBase); ///
///Seems opCmp opEquals and similar functions go in the poly base. That's livable.
///if not, forward reference to Poly_opCmp
int Poly_opCmp(ref const PolyCryptoBase rhs) const {
return data.original.length - rhs.data.original.length;
}
///if it matches the original string
bool isCrypted() @property pure @safe nothrow{
return data.original != data.crypted;
}
///Does basic enciphering, individual letters only
static void Poly_cipherChar(ref char c) pure @safe nothrow {
c ^= 127;
}
///set the plaintext (resetting crypted text as well)
void setString(string input) @property {
data.original = input;
data.crypted = input.dup;
}
///encrypt cipher string (which done twice will decrypt it)
void encipher() @property {
foreach(ref ch; data.crypted) {
this.cipherChar(ch);
}
}
}
///
struct CryptoXOR13 {
mixin PolyMorphicInclude!(PolyCryptoBase);///
/**non matching signatures, but still compatible
requires 'static' or const to work, otherwise
potential for only calling a different version
may apply making some bugs hard to find.*/
static void cipherChar(ref char c) pure @safe {
c ^= 13;
}
}
///
struct CryptoROT13 {
mixin PolyMorphicInclude!(PolyCryptoBase);///
///ditto, our const-only version
void cipherChar(ref char c) const {
if (c >= 'a' && c <= 'z') {
c += 13;
if (c > 'z')
c -= 26;
}
if (c >= 'A' && c <= 'Z') {
c += 13;
if (c > 'Z')
c -= 26;
}
}
}
unittest {
PolyCryptoBase crypto;
crypto.setString = "Hello world!";
char[] str = "Hello world!".dup;
str[] ^= 127;
assert(!crypto.isCrypted);
crypto.encipher;
assert(crypto.isCrypted);
assert(crypto.data.crypted == str);
crypto.encipher;
assert(!crypto.isCrypted);
str[] ^= 127;
assert(crypto.data.crypted == str);
//k XOR13
crypto.setPolyMorphState("CryptoXOR13");
str[] ^= 13;
crypto.encipher;
assert(crypto.data.crypted == str);
crypto.encipher;
assert(!crypto.isCrypted);
str[] ^= 13;
assert(crypto.data.crypted == str);
//k ROT13.
crypto.setPolyMorphState(crypto.Enum.CryptoROT13);
crypto.setString("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
crypto.encipher;
assert(crypto.data.crypted == "nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM");
//const/immutable tests.
const PolyCryptoBase c_crypto = crypto;
auto i_crypto = cast(immutable PolyCryptoBase) crypto;
//compile time checks for incompatibility
assert(!is(typeof(c_crypto.setString("break!"))), "const/mutable compile-time checks failed!");
assert(!is(typeof(c_crypto.encipher)), "const/mutable compile-time checks failed!");
//check if ref is working, plus const version
char c = 'A';
c_crypto.cipherChar(c);
assert(c == 'N');
i_crypto.cipherChar(c); //static so immutable is happy
assert(c == 'A');
crypto = PolyCryptoBase.factory("CryptoROT13");
crypto.setString("(Whfg n yvggyr pelcgb grfg)");
crypto.encipher;
assert(crypto.data.crypted == "(Just a little crypto test)");
//compare for equality
assert(crypto !is c_crypto, "opCmp of previous version broken");
assert(crypto != c_crypto, "opCmp of previous version broken");
assert(crypto is crypto, "opCmp of same object broken");
assert(crypto == crypto, "opCmp of same object broken");
CryptoROT13 rot13 = cast(CryptoROT13) crypto;
assert(crypto == rot13, "contents not equal and should be"); //alias this should make them work still.
assert(rot13 == crypto, "contents not equal and should be");
assert(rot13 == rot13, "contents not equal and should be");
assert(rot13 != c_crypto); //alias this should make them work still. Crypto string is different.
assert(c_crypto > crypto); //test polymorphic opCmp
}
void main(){}