Quellcode durchsuchen

add - byte overload for rotateleft and rotateright

Ugochukwu Mmaduekwe vor 7 Jahren
Ursprung
Commit
e963c7fb94
2 geänderte Dateien mit 34 neuen und 2 gelöschten Zeilen
  1. 2 2
      src/core/URandomHash.pas
  2. 32 0
      src/libraries/hashlib4pascal/HlpBits.pas

+ 2 - 2
src/core/URandomHash.pas

@@ -404,7 +404,7 @@ begin
   LChunkLength := System.Length(AChunk);
   System.SetLength(Result, LChunkLength);
   for i := System.Low(AChunk) to System.High(AChunk) do
-    Result[i] := Byte(TBits.RotateLeft32(AChunk[i], i));
+    Result[i] := TBits.RotateLeft8(AChunk[i], i);
 end;
 
 function TRandomHash.MemTransform8(const AChunk: TBytes): TBytes;
@@ -414,7 +414,7 @@ begin
   LChunkLength := System.Length(AChunk);
   System.SetLength(Result, LChunkLength);
   for i := System.Low(AChunk) to System.High(AChunk) do
-    Result[i] := Byte(TBits.RotateRight32(AChunk[i], i));
+    Result[i] := TBits.RotateRight8(AChunk[i], i);
 end;
 
 function TRandomHash.Expand(const AInput: TBytes; AExpansionFactor: Int32): TBytes;

+ 32 - 0
src/libraries/hashlib4pascal/HlpBits.pas

@@ -45,10 +45,14 @@ type
 
     class function Asr64(Value: int64; ShiftBits: Int32): int64; static; inline;
 
+    class function RotateLeft8(a_value: Byte; a_n: Int32): Byte; overload;
+      static; inline;
     class function RotateLeft32(a_value: UInt32; a_n: Int32): UInt32; overload;
       static; inline;
     class function RotateLeft64(a_value: UInt64; a_n: Int32): UInt64; overload;
       static; inline;
+    class function RotateRight8(a_value: Byte; a_n: Int32): Byte; overload;
+      static; inline;
     class function RotateRight32(a_value: UInt32; a_n: Int32): UInt32; overload;
       static; inline;
     class function RotateRight64(a_value: UInt64; a_n: Int32): UInt64; overload;
@@ -174,6 +178,20 @@ begin
 {$ENDIF FPC}
 end;
 
+class function TBits.RotateLeft8(a_value: Byte; a_n: Int32): Byte;
+begin
+{$IFDEF DEBUG}
+  System.Assert(a_n >= 0);
+{$ENDIF DEBUG}
+{$IFDEF FPC}
+  Result := RolByte(a_value, a_n);
+{$ELSE}
+  a_n := a_n and 7;
+
+  Result := (a_value shl a_n) or (a_value shr (8 - a_n));
+{$ENDIF FPC}
+end;
+
 class function TBits.RotateLeft32(a_value: UInt32; a_n: Int32): UInt32;
 begin
 {$IFDEF DEBUG}
@@ -202,6 +220,20 @@ begin
 {$ENDIF FPC}
 end;
 
+class function TBits.RotateRight8(a_value: Byte; a_n: Int32): Byte;
+begin
+{$IFDEF DEBUG}
+  System.Assert(a_n >= 0);
+{$ENDIF DEBUG}
+{$IFDEF FPC}
+  Result := RorByte(a_value, a_n);
+{$ELSE}
+  a_n := a_n and 7;
+
+  Result := (a_value shr a_n) or (a_value shl (8 - a_n));
+{$ENDIF FPC}
+end;
+
 class function TBits.RotateRight32(a_value: UInt32; a_n: Int32): UInt32;
 begin
 {$IFDEF DEBUG}