diff dcrypt/crypto/ciphers/Blowfish.d @ 23:4589f8c5eb3c

Replaced dcrypt.crypto.Util with dcrypt.misc.Bitwise and dcrypt.misc.ByteConverter. Altered all dependent files to reflect changes.
author Thomas Dixon <reikon@reikon.us>
date Sat, 14 Feb 2009 19:58:20 -0500
parents ec23779ee794
children 176c933827a8
line wrap: on
line diff
--- a/dcrypt/crypto/ciphers/Blowfish.d	Sat Jan 10 13:17:58 2009 -0500
+++ b/dcrypt/crypto/ciphers/Blowfish.d	Sat Feb 14 19:58:20 2009 -0500
@@ -10,7 +10,8 @@
 
 module dcrypt.crypto.ciphers.Blowfish;
 
-import dcrypt.misc.Util;
+import dcrypt.misc.ByteConverter;
+import dcrypt.misc.Bitwise;
 import dcrypt.crypto.BlockCipher;
 
 /** Implementation of the Blowfish cipher designed by Bruce Schneier. */
@@ -262,8 +263,8 @@
         if (output.length < BLOCK_SIZE)
             throw new ShortBufferError(name()~": Output buffer too short");
         
-        uint xl = Util.ubytesToUintBig(input, 0),
-             xr = Util.ubytesToUintBig(input, 4),
+        uint xl = ByteConverter.BigEndian.to!(uint)(input[0..4]),
+             xr = ByteConverter.BigEndian.to!(uint)(input[4..8]),
              i = 0;
         
         xl ^= P[i++];
@@ -273,8 +274,8 @@
         }
         xr ^= P[i];
   
-        Util.uintToUbytesBig(xr, output, 0);
-        Util.uintToUbytesBig(xl, output, 4);
+        output[0..4] = ByteConverter.BigEndian.from!(uint)(xr);
+        output[4..8] = ByteConverter.BigEndian.from!(uint)(xl);
         
         return BLOCK_SIZE;
     }
@@ -305,32 +306,32 @@
         ubyte[] t = new ubyte[BLOCK_SIZE]; // Initialized to 0's
         for (int i = 0; i < PBOX_SIZE;) {
             update(t, t);
-            P[i++] = Util.ubytesToUintBig(t, 0);
-            P[i++] = Util.ubytesToUintBig(t, 4);
+            P[i++] = ByteConverter.BigEndian.to!(uint)(t[0..4]);
+            P[i++] = ByteConverter.BigEndian.to!(uint)(t[4..8]);
         }
         
         for (int i = 0; i < SBOX_SIZE;) {
             update(t, t);
-            S0[i++] = Util.ubytesToUintBig(t, 0);
-            S0[i++] = Util.ubytesToUintBig(t, 4);
+            S0[i++] = ByteConverter.BigEndian.to!(uint)(t[0..4]);
+            S0[i++] = ByteConverter.BigEndian.to!(uint)(t[4..8]);
         }
         
         for (int i = 0; i < SBOX_SIZE;) {
             update(t, t);
-            S1[i++] = Util.ubytesToUintBig(t, 0);
-            S1[i++] = Util.ubytesToUintBig(t, 4);
+            S1[i++] = ByteConverter.BigEndian.to!(uint)(t[0..4]);
+            S1[i++] = ByteConverter.BigEndian.to!(uint)(t[4..8]);
         }
         
         for (int i = 0; i < SBOX_SIZE;) {
             update(t, t);
-            S2[i++] = Util.ubytesToUintBig(t, 0);
-            S2[i++] = Util.ubytesToUintBig(t, 4);
+            S2[i++] = ByteConverter.BigEndian.to!(uint)(t[0..4]);
+            S2[i++] = ByteConverter.BigEndian.to!(uint)(t[4..8]);
         }
         
         for (int i = 0; i < SBOX_SIZE;) {
             update(t, t);
-            S3[i++] = Util.ubytesToUintBig(t, 0);
-            S3[i++] = Util.ubytesToUintBig(t, 4);
+            S3[i++] = ByteConverter.BigEndian.to!(uint)(t[0..4]);
+            S3[i++] = ByteConverter.BigEndian.to!(uint)(t[4..8]);
         }
     }
     
@@ -371,19 +372,19 @@
             foreach (uint i, char[] test_key; test_keys) {
                 ubyte[] buffer = new ubyte[t.blockSize];
                 char[] result;
-                SymmetricKey key = new SymmetricKey(Util.hexToUbytes(test_key));
+                SymmetricKey key = new SymmetricKey(ByteConverter.hexDecode(test_key));
                 
                 // Encryption
                 t.init(true, key);
-                t.update(Util.hexToUbytes(test_plaintexts[i]), buffer);
-                result = Util.ubytesToHex(buffer);
+                t.update(ByteConverter.hexDecode(test_plaintexts[i]), buffer);
+                result = ByteConverter.hexEncode(buffer);
                 assert(result == test_ciphertexts[i],
                         t.name~": ("~result~") != ("~test_ciphertexts[i]~")");
             
                 // Decryption
                 t.init(false, key);
-                t.update(Util.hexToUbytes(test_ciphertexts[i]), buffer);
-                result = Util.ubytesToHex(buffer);
+                t.update(ByteConverter.hexDecode(test_ciphertexts[i]), buffer);
+                result = ByteConverter.hexEncode(buffer);
                 assert(result == test_plaintexts[i],
                         t.name~": ("~result~") != ("~test_plaintexts[i]~")");
             }