Răsfoiți Sursa

use non generic overload of AreEqual

Ugochukwu Mmaduekwe 4 ore în urmă
părinte
comite
249c65a153

+ 3 - 3
CryptoLib.Samples/src/UsageExamples.pas

@@ -300,7 +300,7 @@ begin
   // First read the magic text and the salt - if any
   Chopped := System.Copy(CipherText, 0, SALT_MAGIC_LEN);
   if (System.Length(CipherText) >= SALT_MAGIC_LEN) and
-    (TArrayUtilities.AreEqual<Byte>(Chopped, TConverters.ConvertStringToBytes(SALT_MAGIC,
+    (TArrayUtilities.AreEqual(Chopped, TConverters.ConvertStringToBytes(SALT_MAGIC,
     TEncoding.UTF8))) then
   begin
     System.Move(CipherText[SALT_MAGIC_LEN], SaltBytes[0], SALT_SIZE);
@@ -396,7 +396,7 @@ begin
   if TUsageExamples.AES256CBCPascalCoinDecrypt(CipherText, PasswordBytes,
     DecryptedCipherText) then
   begin
-    if TArrayUtilities.AreEqual<Byte>(PlainText, DecryptedCipherText) then
+    if TArrayUtilities.AreEqual(PlainText, DecryptedCipherText) then
     begin
       Writeln('AES_256_CBC PascalCoin Compatability Encrypt, Decrypt Was Successful '
         + sLineBreak);
@@ -553,7 +553,7 @@ begin
   if TUsageExamples.ECIESPascalCoinDecrypt(KeyPair.Private, CipherText,
     DecryptedCipherText) then
   begin
-    if TArrayUtilities.AreEqual<Byte>(PlainText, DecryptedCipherText) then
+    if TArrayUtilities.AreEqual(PlainText, DecryptedCipherText) then
     begin
       Writeln('ECIES PascalCoin Compatability Encrypt, Decrypt Was Successful '
         + sLineBreak);

+ 19 - 19
CryptoLib.Tests/src/CryptoLibTestBase.pas

@@ -30,12 +30,12 @@ type
   TCryptoLibAlgorithmTestCase = class abstract(TCryptoLibTestCase)
   protected
 
-    function DecodeHex(const data: String): TBytes;
-    function EncodeHex(const data: TBytes): String;
-    function DecodeBase64(const data: String): TBytes;
-    procedure ZeroFill(const data: TBytes);
-    function Prepend(const data: TBytes; b: Byte): TBytes;
-    function AreEqual(const A, b: TBytes): Boolean;
+    function DecodeHex(const AData: String): TBytes;
+    function EncodeHex(const AData: TBytes): String;
+    function DecodeBase64(const AData: String): TBytes;
+    procedure ZeroFill(const AData: TBytes);
+    function Prepend(const AData: TBytes; AValue: Byte): TBytes;
+    function AreEqual(const AA, AB: TBytes): Boolean;
     function CopyOfRange(const AData: TBytes; AFrom, ATo: Int32): TBytes;
 
   end;
@@ -44,35 +44,35 @@ implementation
 
 { TCryptoLibAlgorithmTestCase }
 
-function TCryptoLibAlgorithmTestCase.DecodeBase64(const data: String): TBytes;
+function TCryptoLibAlgorithmTestCase.DecodeBase64(const AData: String): TBytes;
 begin
-  result := TBase64Encoder.Decode(data);
+  Result := TBase64Encoder.Decode(AData);
 end;
 
-function TCryptoLibAlgorithmTestCase.DecodeHex(const data: String): TBytes;
+function TCryptoLibAlgorithmTestCase.DecodeHex(const AData: String): TBytes;
 begin
-  result := THexEncoder.Decode(data);
+  Result := THexEncoder.Decode(AData);
 end;
 
-function TCryptoLibAlgorithmTestCase.EncodeHex(const data: TBytes): String;
+function TCryptoLibAlgorithmTestCase.EncodeHex(const AData: TBytes): String;
 begin
-  result := THexEncoder.Encode(data);
+  Result := THexEncoder.Encode(AData);
 end;
 
-function TCryptoLibAlgorithmTestCase.Prepend(const data: TBytes;
-  b: Byte): TBytes;
+function TCryptoLibAlgorithmTestCase.Prepend(const AData: TBytes;
+  AValue: Byte): TBytes;
 begin
-  result := TArrayUtilities.Prepend<Byte>(data, b);
+  Result := TArrayUtilities.Prepend<Byte>(AData, AValue);
 end;
 
-procedure TCryptoLibAlgorithmTestCase.ZeroFill(const data: TBytes);
+procedure TCryptoLibAlgorithmTestCase.ZeroFill(const AData: TBytes);
 begin
-  TArrayUtilities.Fill<Byte>(data, 0, System.Length(data), Byte(0));
+  TArrayUtilities.Fill<Byte>(AData, 0, System.Length(AData), Byte(0));
 end;
 
-function TCryptoLibAlgorithmTestCase.AreEqual(const A, b: TBytes): Boolean;
+function TCryptoLibAlgorithmTestCase.AreEqual(const AA, AB: TBytes): Boolean;
 begin
-  result := TArrayUtilities.AreEqual<Byte>(A, b);
+  Result := TArrayUtilities.AreEqual(AA, AB);
 end;
 
 function TCryptoLibAlgorithmTestCase.CopyOfRange(const AData: TBytes; AFrom, ATo: Int32): TBytes;

+ 23 - 23
CryptoLib/src/Asn1/ClpAsn1Objects.pas

@@ -3241,7 +3241,7 @@ begin
     try
       LD1 := GetEncoded();
       LD2 := AAsn1Object.GetEncoded();
-      Result := TArrayUtilities.AreEqual<Byte>(LD1, LD2);
+      Result := TArrayUtilities.AreEqual(LD1, LD2);
       Exit;
     except
       on E: Exception do
@@ -5960,7 +5960,7 @@ begin
     Result := False;
     Exit;
   end;
-  Result := TArrayUtilities.AreEqual<Byte>(GetOctets(), LThat.GetOctets());
+  Result := TArrayUtilities.AreEqual(GetOctets(), LThat.GetOctets());
 end;
 
 function TAsn1OctetString.Asn1GetHashCode(): Int32;
@@ -7971,7 +7971,7 @@ begin
     Exit;
   end;
   LThatContents := LThat.Bytes;
-  Result := TArrayUtilities.AreEqual<Byte>(FContents, LThatContents);
+  Result := TArrayUtilities.AreEqual(FContents, LThatContents);
 end;
 
 function TDerEnumerated.Asn1GetHashCode(): Int32;
@@ -8230,7 +8230,7 @@ begin
     System.SetLength(FCache, 1024);
 
   LOriginalEntry := FCache[LIndex];
-  if (LOriginalEntry <> nil) and (TArrayUtilities.AreEqual<Byte>(AContents, LOriginalEntry.Contents)) then
+  if (LOriginalEntry <> nil) and (TArrayUtilities.AreEqual(AContents, LOriginalEntry.Contents)) then
   begin
     Result := LOriginalEntry;
     Exit;
@@ -8247,7 +8247,7 @@ begin
   LExchangedEntry := FCache[LIndex];
   if LExchangedEntry <> LOriginalEntry then
   begin
-    if (LExchangedEntry <> nil) and (TArrayUtilities.AreEqual<Byte>(AContents, LExchangedEntry.Contents)) then
+    if (LExchangedEntry <> nil) and (TArrayUtilities.AreEqual(AContents, LExchangedEntry.Contents)) then
     begin
       Result := LExchangedEntry;
       Exit;
@@ -8422,7 +8422,7 @@ begin
     Result := False;
     Exit;
   end;
-  Result := TArrayUtilities.AreEqual<Byte>(FContents, LThat.Contents);
+  Result := TArrayUtilities.AreEqual(FContents, LThat.Contents);
 end;
 
 function TDerObjectIdentifier.Asn1GetHashCode(): Int32;
@@ -8482,7 +8482,7 @@ begin
   LStemLength := System.Length(LStemContents);
   // Compare the first LStemLength bytes of both arrays
   Result := (System.Length(FContents) > LStemLength) and
-    (TArrayUtilities.AreEqual<Byte>(System.Copy(FContents, 0, LStemLength), System.Copy(LStemContents, 0, LStemLength)));
+    (TArrayUtilities.AreEqual(System.Copy(FContents, 0, LStemLength), System.Copy(LStemContents, 0, LStemLength)));
 end;
 
 function TDerObjectIdentifier.ToString(): String;
@@ -8737,7 +8737,7 @@ begin
     Result := False;
     Exit;
   end;
-  Result := TArrayUtilities.AreEqual<Byte>(FContents, LThat.Contents);
+  Result := TArrayUtilities.AreEqual(FContents, LThat.Contents);
 end;
 
 function TAsn1RelativeOid.Asn1GetHashCode(): Int32;
@@ -9196,7 +9196,7 @@ begin
     System.SetLength(FCache, 64);
 
   LOriginalEntry := FCache[LIndex];
-  if (LOriginalEntry <> nil) and (TArrayUtilities.AreEqual<Byte>(AContents, LOriginalEntry.Contents)) then
+  if (LOriginalEntry <> nil) and (TArrayUtilities.AreEqual(AContents, LOriginalEntry.Contents)) then
   begin
     Result := LOriginalEntry;
     Exit;
@@ -9214,7 +9214,7 @@ begin
   LExchangedEntry := FCache[LIndex];
   if LExchangedEntry <> LOriginalEntry then
   begin
-    if (LExchangedEntry <> nil) and (TArrayUtilities.AreEqual<Byte>(AContents, LExchangedEntry.Contents)) then
+    if (LExchangedEntry <> nil) and (TArrayUtilities.AreEqual(AContents, LExchangedEntry.Contents)) then
     begin
       Result := LExchangedEntry;
       Exit;
@@ -9381,7 +9381,7 @@ begin
   // TODO Performance
   LThisContents := GetContents(TAsn1OutputStream.EncodingDer);
   LThatContents := LThat.GetContents(TAsn1OutputStream.EncodingDer);
-  Result := TArrayUtilities.AreEqual<Byte>(LThisContents, LThatContents);
+  Result := TArrayUtilities.AreEqual(LThisContents, LThatContents);
 end;
 
 function TAsn1GeneralizedTime.Asn1GetHashCode(): Int32;
@@ -9765,7 +9765,7 @@ begin
   // TODO Performance
   LThisContents := GetContents(TAsn1OutputStream.EncodingDer);
   LThatContents := LThat.GetContents(TAsn1OutputStream.EncodingDer);
-  Result := TArrayUtilities.AreEqual<Byte>(LThisContents, LThatContents);
+  Result := TArrayUtilities.AreEqual(LThisContents, LThatContents);
 end;
 
 function TAsn1UtcTime.Asn1GetHashCode(): Int32;
@@ -10325,7 +10325,7 @@ begin
     Result := False;
     Exit;
   end;
-  Result := TArrayUtilities.AreEqual<Byte>(FContents, LThat.Contents);
+  Result := TArrayUtilities.AreEqual(FContents, LThat.Contents);
 end;
 
 function TDerUtf8String.Asn1GetHashCode(): Int32;
@@ -10492,7 +10492,7 @@ begin
     Result := False;
     Exit;
   end;
-  Result := TArrayUtilities.AreEqual<Byte>(FContents, LThat.Contents);
+  Result := TArrayUtilities.AreEqual(FContents, LThat.Contents);
 end;
 
 function TDerGeneralString.Asn1GetHashCode(): Int32;
@@ -10651,7 +10651,7 @@ begin
     Result := False;
     Exit;
   end;
-  Result := TArrayUtilities.AreEqual<Byte>(FContents, LThat.Contents);
+  Result := TArrayUtilities.AreEqual(FContents, LThat.Contents);
 end;
 
 function TDerGraphicString.Asn1GetHashCode(): Int32;
@@ -10842,7 +10842,7 @@ begin
     Result := False;
     Exit;
   end;
-  Result := TArrayUtilities.AreEqual<Byte>(FContents, LThat.Contents);
+  Result := TArrayUtilities.AreEqual(FContents, LThat.Contents);
 end;
 
 function TDerIA5String.Asn1GetHashCode(): Int32;
@@ -11055,7 +11055,7 @@ begin
     Result := False;
     Exit;
   end;
-  Result := TArrayUtilities.AreEqual<Byte>(FContents, LThat.Contents);
+  Result := TArrayUtilities.AreEqual(FContents, LThat.Contents);
 end;
 
 function TDerNumericString.Asn1GetHashCode(): Int32;
@@ -11222,7 +11222,7 @@ begin
     Result := False;
     Exit;
   end;
-  Result := TArrayUtilities.AreEqual<Byte>(FContents, LThat.Contents);
+  Result := TArrayUtilities.AreEqual(FContents, LThat.Contents);
 end;
 
 function TDerPrintableString.Asn1GetHashCode(): Int32;
@@ -11434,7 +11434,7 @@ begin
     Result := False;
     Exit;
   end;
-  Result := TArrayUtilities.AreEqual<Byte>(FContents, LThat.Contents);
+  Result := TArrayUtilities.AreEqual(FContents, LThat.Contents);
 end;
 
 function TDerT61String.Asn1GetHashCode(): Int32;
@@ -11609,7 +11609,7 @@ begin
     Result := False;
     Exit;
   end;
-  Result := TArrayUtilities.AreEqual<Byte>(FContents, LThat.Contents);
+  Result := TArrayUtilities.AreEqual(FContents, LThat.Contents);
 end;
 
 function TDerUniversalString.Asn1GetHashCode(): Int32;
@@ -11802,7 +11802,7 @@ begin
     Result := False;
     Exit;
   end;
-  Result := TArrayUtilities.AreEqual<Byte>(FContents, LThat.Contents);
+  Result := TArrayUtilities.AreEqual(FContents, LThat.Contents);
 end;
 
 function TDerVideotexString.Asn1GetHashCode(): Int32;
@@ -11969,7 +11969,7 @@ begin
     Result := False;
     Exit;
   end;
-  Result := TArrayUtilities.AreEqual<Byte>(FContents, LThat.Contents);
+  Result := TArrayUtilities.AreEqual(FContents, LThat.Contents);
 end;
 
 function TDerVisibleString.Asn1GetHashCode(): Int32;
@@ -12082,7 +12082,7 @@ begin
     Result := False;
     Exit;
   end;
-  Result := TArrayUtilities.AreEqual<Byte>(FBytes, LThat.Bytes);
+  Result := TArrayUtilities.AreEqual(FBytes, LThat.Bytes);
 end;
 
 function TDerInteger.Asn1GetHashCode(): Int32;

+ 1 - 1
CryptoLib/src/Crypto/Parameters/ClpDHParameters.pas

@@ -252,7 +252,7 @@ begin
     Result := True;
     Exit;
   end;
-  Result := (Counter = AOther.Counter) and (TArrayUtilities.AreEqual<Byte>(Seed,
+  Result := (Counter = AOther.Counter) and (TArrayUtilities.AreEqual(Seed,
     AOther.Seed));
 end;
 

+ 1 - 1
CryptoLib/src/Crypto/Parameters/ClpDsaParameters.pas

@@ -248,7 +248,7 @@ begin
     Result := True;
     Exit;
   end;
-  Result := (Counter = AOther.Counter) and (TArrayUtilities.AreEqual<Byte>(Seed,
+  Result := (Counter = AOther.Counter) and (TArrayUtilities.AreEqual(Seed,
     AOther.Seed));
 end;
 

+ 1 - 1
CryptoLib/src/Crypto/Signers/SignerEncodings/ClpStandardDsaEncoding.pas

@@ -82,7 +82,7 @@ begin
     LR := DecodeValue(AN, LSeq, 0);
     LS := DecodeValue(AN, LSeq, 1);
     LExpectedEncoding := Encode(AN, LR, LS);
-    if TArrayUtilities.AreEqual<Byte>(LExpectedEncoding, AEncoding) then
+    if TArrayUtilities.AreEqual(LExpectedEncoding, AEncoding) then
     begin
       Result := TCryptoLibGenericArray<TBigInteger>.Create(LR, LS);
       Exit;

+ 28 - 1
CryptoLib/src/GeneralUtilities/ClpArrayUtilities.pas

@@ -39,7 +39,10 @@ type
   public
 
     class function AreEqual<T>(const A, B: TCryptoLibGenericArray<T>;
-      const AComparer: IEqualityComparer<T> = nil): Boolean; static;
+      const AComparer: IEqualityComparer<T> = nil): Boolean; overload; static;
+
+    class function AreEqual(const A, B: TCryptoLibByteArray): Boolean; overload; static;
+    class function AreEqual(const A, B: TCryptoLibInt32Array): Boolean; overload; static;
 
     class function Concatenate<T>(const A, B: TCryptoLibGenericArray<T>)
       : TCryptoLibGenericArray<T>; static;
@@ -144,6 +147,30 @@ begin
   Result := True;
 end;
 
+class function TArrayUtilities.AreEqual(const A, B: TCryptoLibByteArray): Boolean;
+var
+  LLen: Int32;
+begin
+  LLen := System.Length(A);
+  if LLen <> System.Length(B) then
+    Exit(False);
+  if LLen = 0 then
+    Exit(True);
+  Result := CompareMem(@A[0], @B[0], LLen * System.SizeOf(Byte));
+end;
+
+class function TArrayUtilities.AreEqual(const A, B: TCryptoLibInt32Array): Boolean;
+var
+  LLen: Int32;
+begin
+  LLen := System.Length(A);
+  if LLen <> System.Length(B) then
+    Exit(False);
+  if LLen = 0 then
+    Exit(True);
+  Result := CompareMem(@A[0], @B[0], LLen * System.SizeOf(Int32));
+end;
+
 class function TArrayUtilities.Concatenate<T>(const A, B: TCryptoLibGenericArray<T>)
   : TCryptoLibGenericArray<T>;
 var

+ 1 - 1
CryptoLib/src/Math/Field/ClpGF2Polynomial.pas

@@ -69,7 +69,7 @@ begin
     Exit(False);
   if (Self as IGF2Polynomial) = (AOther as IGF2Polynomial) then
     Exit(True);
-  Result := TArrayUtilities.AreEqual<Int32>(FExponents, AOther.ExponentsPresent);
+  Result := TArrayUtilities.AreEqual(FExponents, AOther.ExponentsPresent);
 end;
 
 function TGF2Polynomial.GetHashCode: {$IFDEF DELPHI}Int32 {$ELSE}PtrInt {$ENDIF DELPHI};

+ 1 - 1
CryptoLib/src/X509/ClpX509Certificate.pas

@@ -698,7 +698,7 @@ begin
   LThatEncoding := LThat.GetCachedEncoding().GetEncoding();
 
   if (LThisEncoding <> nil) and (LThatEncoding <> nil) then
-    Result := TArrayUtilities.AreEqual<Byte>(LThisEncoding, LThatEncoding)
+    Result := TArrayUtilities.AreEqual(LThisEncoding, LThatEncoding)
   else
     Result := False;
 end;

+ 1 - 1
CryptoLib/src/X509/ClpX509Crl.pas

@@ -508,7 +508,7 @@ begin
 
   LThisEncoding := GetCachedEncoding().GetEncoding();
   LThatEncoding := LThatObj.GetCachedEncoding().GetEncoding();
-  Result := (LThisEncoding <> nil) and (LThatEncoding <> nil) and (TArrayUtilities.AreEqual<Byte>(LThisEncoding, LThatEncoding));
+  Result := (LThisEncoding <> nil) and (LThatEncoding <> nil) and (TArrayUtilities.AreEqual(LThisEncoding, LThatEncoding));
 end;
 
 function TX509Crl.GetHashCode: Int32;