diff dcrypt/crypto/hashes/MD5.d @ 27:8b5eaf3c2979

Fixed error in hash message padding reported by Glenn Haecker.
author Thomas Dixon <reikon@reikon.us>
date Sat, 09 May 2009 23:29:20 -0400
parents 176c933827a8
children ad687db713a4
line wrap: on
line diff
--- a/dcrypt/crypto/hashes/MD5.d	Sun Mar 01 13:06:48 2009 -0500
+++ b/dcrypt/crypto/hashes/MD5.d	Sat May 09 23:29:20 2009 -0400
@@ -17,11 +17,13 @@
  * References: http://www.faqs.org/rfcs/rfc1321.html
  * Bugs: MD5 is not cryptographically secure.
  */
-class MD5 : Hash {
+class MD5 : Hash
+{
 	private uint h0, h1, h2, h3;
     
     // Shift amounts
-    private enum {
+    private enum
+    {
         S11 =  7,
         S12 = 12,
         S13 = 17,
@@ -43,24 +45,29 @@
         S44 = 21
     };
     
-    this (void[] input_=null) {
+    this (void[] input_=null)
+    {
         reset();
         super(input_);
     }
 
-    uint blockSize() {
+    uint blockSize()
+    {
         return 64;
     }
     
-    uint digestSize() {
+    uint digestSize()
+    {
         return 16;
     }
     
-    char[] name() {
+    char[] name()
+    {
         return "MD5";
     }
     
-    void transform(ubyte[] input) {
+    void transform(ubyte[] input)
+    {
         uint[] w = new uint[16];
         
         for (int i = 0, j = 0; i < 16; i++,j+=int.sizeof)
@@ -151,51 +158,56 @@
         // FATALITY! \o/
     }
     
-    private uint f(uint x, uint y, uint z) {
+    private uint f(uint x, uint y, uint z)
+    {
         return (x&y)|(~x&z);
     }
 
-    private uint h(uint x, uint y, uint z) {
+    private uint h(uint x, uint y, uint z)
+    {
         return x^y^z;
     }
     
-    private uint g(uint x, uint y, uint z) {
+    private uint g(uint x, uint y, uint z)
+    {
         return (x&z)|(y&~z);
     }
 
-    private uint i(uint x, uint y, uint z) {
+    private uint i(uint x, uint y, uint z)
+    {
         return y^(x|~z);
     }
 
-    private void ff(ref uint a, uint b, uint c,
-                        uint d, uint x, uint s, uint ac) {
+    private void ff(ref uint a, uint b, uint c, uint d, uint x, uint s, uint ac)
+    {
         a += f(b, c, d) + x + ac;
         a = Bitwise.rotateLeft(a, s);
         a += b;
     }
 
-    private void gg(ref uint a, uint b, uint c,
-                        uint d, uint x, uint s, uint ac) {
+    private void gg(ref uint a, uint b, uint c, uint d, uint x, uint s, uint ac)
+    {
         a += g(b, c, d) + x + ac;
         a = Bitwise.rotateLeft(a, s);
         a += b;
     }
 
-    private void hh(ref uint a, uint b, uint c,
-                        uint d, uint x, uint s, uint ac) {
+    private void hh(ref uint a, uint b, uint c, uint d, uint x, uint s, uint ac)
+    {
         a += h(b, c, d) + x + ac;
         a = Bitwise.rotateLeft(a, s);
         a += b;
     }
 
-    private void ii(ref uint a, uint b, uint c,
-                        uint d, uint x, uint s, uint ac) {
+    private void ii(ref uint a, uint b, uint c, uint d, uint x, uint s, uint ac)
+    {
         a += i(b, c, d) + x + ac;
         a = Bitwise.rotateLeft(a, s);
         a += b;
     }
     
-    ubyte[] digest() {
+    ubyte[] digest()
+    {
     	padMessage(MODE_MD);
         ubyte[] result = new ubyte[digestSize];
         
@@ -208,7 +220,8 @@
         return result;
     }
 
-    void reset() {
+    void reset()
+    {
         super.reset();
         h0 = 0x67452301u;
         h1 = 0xefcdab89u;
@@ -216,7 +229,8 @@
         h3 = 0x10325476u;
     }
     
-    MD5 copy() {
+    MD5 copy()
+    {
         MD5 h = new MD5(buffer[0..index]);
         h.bytes = bytes;
         h.h0 = h0;
@@ -226,9 +240,11 @@
         return h;
     }
     
-    debug (UnitTest) {
+    debug (UnitTest)
+    {
         // Found in Tango <3
-        unittest {
+        unittest
+        {
             static const char[][] test_inputs = [
                 "",
                 "a",
@@ -250,7 +266,8 @@
             ];
             
             MD5 h = new MD5();
-            foreach (uint i, char[] input; test_inputs) {
+            foreach (uint i, char[] input; test_inputs)
+            {
                 h.update(input);
                 char[] digest = h.hexDigest();
                 assert(digest == test_results[i],