comparison dcrypt/crypto/Crypto.d @ 10:cd376996cdb3

Renamed SymmetricCipher back to Cipher (we don't support any other kind atm, I'll deal with it when we do.). Added BlockCipherWrapper for the encryption of arbitrary streams with or without padding. Removed hashByName, and replaced it with createHash. Re-did the high-level API, and filled out Crypto. Added cipher creation via createCipher. Added dsk to the CONTRIBUTORS file for helping with the design of the high-level API.
author Thomas Dixon <reikon@reikon.us>
date Wed, 20 Aug 2008 20:08:07 -0400
parents 29b910949588
children 02970e63257d
comparison
equal deleted inserted replaced
9:29b910949588 10:cd376996cdb3
6 * Authors: Thomas Dixon 6 * Authors: Thomas Dixon
7 */ 7 */
8 8
9 module dcrypt.crypto.Crypto; 9 module dcrypt.crypto.Crypto;
10 10
11 import dcrypt.crypto.SymmetricCipher;
12
13 import dcrypt.misc.Util;
14
15 // Hash functions 11 // Hash functions
16 public import dcrypt.crypto.Hash; 12 public import dcrypt.crypto.Hash;
17 import dcrypt.crypto.hashes.MD5; 13 import dcrypt.crypto.hashes.MD5;
18 import dcrypt.crypto.hashes.SHA1; 14 import dcrypt.crypto.hashes.SHA1;
19 import dcrypt.crypto.hashes.SHA224; 15 import dcrypt.crypto.hashes.SHA224;
20 import dcrypt.crypto.hashes.SHA256; 16 import dcrypt.crypto.hashes.SHA256;
21 import dcrypt.crypto.hashes.SHA384; 17 import dcrypt.crypto.hashes.SHA384;
22 import dcrypt.crypto.hashes.SHA512; 18 import dcrypt.crypto.hashes.SHA512;
23 19
20 // Params
21 public import dcrypt.crypto.params.SymmetricKey;
22 public import dcrypt.crypto.params.ParametersWithIV;
23
24
25 // Ciphers
26 public import dcrypt.crypto.Cipher;
27 public import dcrypt.crypto.BlockCipher;
28 public import dcrypt.crypto.StreamCipher;
29 public import dcrypt.crypto.BlockCipherWrapper;
30 import dcrypt.crypto.ciphers.Blowfish;
31 import dcrypt.crypto.ciphers.RC6;
32 import dcrypt.crypto.ciphers.TEA;
33 import dcrypt.crypto.ciphers.XTEA;
34 import dcrypt.crypto.ciphers.RC4;
35 import dcrypt.crypto.ciphers.AES;
36
37 // Block modes
38 import dcrypt.crypto.modes.CBC;
39 import dcrypt.crypto.modes.CTR;
40
41 // Block padding
42 public import dcrypt.crypto.BlockCipherPadding;
43 import dcrypt.crypto.padding.NullByte;
44 import dcrypt.crypto.padding.X923;
45 import dcrypt.crypto.padding.PKCS7;
46 import dcrypt.crypto.padding.RFC1321;
47
48 enum Hashes {
49 MD5=0,
50 SHA1,
51 SHA224,
52 SHA256,
53 SHA384,
54 SHA512
55 }
56
57 enum Ciphers {
58 // Block ciphers
59 Blowfish=0,
60 AES,
61 RC6,
62 TEA,
63 XTEA,
64
65 // Stream ciphers
66 RC4
67 }
68
69 enum Modes {
70 ECB=-1,
71 CTR,
72 CBC
73 }
74
75 enum Padding {
76 None=-1,
77 NullByte, // lol
78 X923,
79 PKCS7,
80 RFC1321
81 }
82
24 struct Crypto { 83 struct Crypto {
25 static Hash hashByName(char[] name) { 84 static Hash createHash(uint hash) {
26 switch (Util.stringToAlphanumeric(Util.stringToUpper(name))) { 85 switch (hash) {
27 case "MD5": 86 case Hashes.MD5:
28 return new MD5(); 87 return new MD5;
29 case "SHA1": 88 case Hashes.SHA1:
30 return new SHA1(); 89 return new SHA1;
31 case "SHA224": 90 case Hashes.SHA224:
32 return new SHA224(); 91 return new SHA224;
33 case "SHA256": 92 case Hashes.SHA256:
34 return new SHA256(); 93 return new SHA256;
35 case "SHA384": 94 case Hashes.SHA384:
36 return new SHA384(); 95 return new SHA384;
37 case "SHA512": 96 case Hashes.SHA512:
38 return new SHA512(); 97 return new SHA512;
39 default: 98 default:
40 throw new InvalidParameterError("Unknown hash function: "~name); 99 throw new InvalidParameterError("Unknown hash function passed to createHash()");
41 } 100 }
42 } 101 }
102
103 static Cipher createCipher(uint cipher, uint mode=Modes.ECB, uint padding=Padding.None) {
104 BlockCipher c = null,
105 m = null;
106 BlockCipherPadding p = null;
107 switch (cipher) {
108 case Ciphers.Blowfish:
109 c = new Blowfish;
110 break;
111 case Ciphers.AES:
112 c = new AES;
113 break;
114 case Ciphers.RC6:
115 c = new RC6;
116 break;
117 case Ciphers.TEA:
118 c = new TEA;
119 break;
120 case Ciphers.XTEA:
121 c = new XTEA;
122 break;
123 case Ciphers.RC4:
124 return new RC4; // Note the return
125 default:
126 throw new InvalidParameterError("Unknown cipher passed to createCipher()");
127 }
128
129 switch (mode) {
130 case Modes.ECB:
131 break;
132 case Modes.CTR:
133 m = new CTR(c);
134 break;
135 case Modes.CBC:
136 m = new CBC(c);
137 break;
138 default:
139 throw new InvalidParameterError("Unknown mode passed to createCipher()");
140 }
141
142 switch (padding) {
143 case Padding.None:
144 break;
145 case Padding.NullByte:
146 p = new NullByte;
147 break;
148 case Padding.X923:
149 p = new X923;
150 break;
151 case Padding.PKCS7:
152 p = new PKCS7;
153 break;
154 case Padding.RFC1321:
155 p = new RFC1321;
156 break;
157 default:
158 throw new InvalidParameterError("Unknown padding passed to createCipher()");
159 }
160
161 return new BlockCipherWrapper(((m is null) ? c : m), p);
162 }
43 } 163 }