Commits

Snake Doctor committed 7f140cf

Version has been tested and supposedly is correct
Finished port of Keccak Reference implementation to Delphi

Comments (0)

Files changed (1)

   writeln;
 end;
 
+function LengthInBits( val: AnsiString ): UInt64; inline;
+begin
+	Result := (sizeof(val[1]) * 8) * Length(val);
+end;
+
 procedure HashString_224A( val: AnsiString );
 var
   ctx: TSpongeState;
   Assert( Keccak_Init( @ctx, 224 ) = SUCCESS );
   if length(val) > 0 then
   begin
-    Assert( Keccak_Update( @ctx, @val[1], length(val) ) = SUCCESS );
+    Assert( Keccak_Update( @ctx, @val[1], LengthInBits(val) ) = SUCCESS );
   end;
   Assert( Keccak_Final( @ctx, @HashVal ) = SUCCESS );
 
     PrintData( 'Input:  ', nil, 0 );
   PrintData( 'Hash:   ', @HashVal, SizeOf(HashVal) );
   writeln; writeln; writeln;
-
-  readln;
 end;
 
 procedure HashString_256A( val: AnsiString );
   Assert( Keccak_Init( @ctx, 256 ) = SUCCESS );
   if length(val) > 0 then
   begin
-    Assert( Keccak_Update( @ctx, @val[1], length(val) ) = SUCCESS );
+    Assert( Keccak_Update( @ctx, @val[1], LengthInBits(val) ) = SUCCESS );
   end;
   Assert( Keccak_Final( @ctx, @HashVal ) = SUCCESS );
 
     PrintData( 'Input:  ', nil, 0 );
   PrintData( 'Hash:   ', @HashVal, SizeOf(HashVal) );
   writeln; writeln; writeln;
-
-  readln;
 end;
 
 procedure HashString_384A( val: AnsiString );
   Assert( Keccak_Init( @ctx, 384 ) = SUCCESS );
   if length(val) > 0 then
   begin
-    Assert( Keccak_Update( @ctx, @val[1], length(val) ) = SUCCESS );
+    Assert( Keccak_Update( @ctx, @val[1], LengthInBits(val) ) = SUCCESS );
   end;
   Assert( Keccak_Final( @ctx, @HashVal ) = SUCCESS );
 
     PrintData( 'Input:  ', nil, 0 );
   PrintData( 'Hash:   ', @HashVal, SizeOf(HashVal) );
   writeln; writeln; writeln;
-
-  readln;
 end;
 
 procedure HashString_512A( val: AnsiString );
   Assert( Keccak_Init( @ctx, 512 ) = SUCCESS );
   if length(val) > 0 then
   begin
-    Assert( Keccak_Update( @ctx, @val[1], length(val) ) = SUCCESS );
+    Assert( Keccak_Update( @ctx, @val[1], LengthInBits(val) ) = SUCCESS );
   end;
   Assert( Keccak_Final( @ctx, @HashVal ) = SUCCESS );
 
     PrintData( 'Input:  ', nil, 0 );
   PrintData( 'Hash:   ', @HashVal, SizeOf(HashVal) );
   writeln; writeln; writeln;
-
-  readln;
 end;
 
 
 var
-  vals: array[0..3] of AnsiString = (
-  	   '',
-       #$C0,
+  vals: array[0..4] of AnsiString = (
+  	   #$C6#$F5#$0B#$B7#$4E#$29,
+  	   #$C1#$EC#$FD#$FC,
+  	   #$41#$FB,
        #$CC,
-       #$4A#$4F#$20#$24#$84#$51#$25#$26
+       ''
     );
 {  vals: array[0..3] of AnsiString = (
        #$FF,
 
 
 //  fncs: array[0..1] of procedure( val: AnsiString ) = ( HashString_256A, HashString_256B{, HashString_512} );
-  fncs: array[0..0] of procedure( val: AnsiString ) = ( HashString_256A );
+  fncs: array[0..3] of procedure( val: AnsiString ) = ( HashString_224A, HashString_256A, HashString_384A, HashString_512A );
 
 var
   i, j: UInt32;
       fncs[i]( vals[j] );
     end;
 
+    readln;
+
     writeln;
     writeln('----------');
     writeln; writeln;
-
   end;
 
    readln;