Browse Source

* PChar -> PAnsiChar

Michaël Van Canneyt 2 years ago
parent
commit
3180a5e83a

+ 54 - 36
packages/fcl-hash/src/fpasn.pp

@@ -35,7 +35,7 @@ const
   // ASN_organizationName = '2.5.4.10';
   // ASN_organizationName = '2.5.4.10';
   // ASN_organizationalUnitName = '2.5.4.11';
   // ASN_organizationalUnitName = '2.5.4.11';
   // ASN_countryName = '2.5.4.6';
   // ASN_countryName = '2.5.4.6';
-  // ASN_stateOrProvinceName = '2.5.4.8';
+  // ASN_stateOrProvince Name = '2.5.4.8';
   // ASN_localityName = '2.5.4.7';
   // ASN_localityName = '2.5.4.7';
   ASN_ecPublicKey = '1.2.840.10045.2.1';
   ASN_ecPublicKey = '1.2.840.10045.2.1';
   // ASN_prime256v1 = '1.2.840.10045.3.1.7';
   // ASN_prime256v1 = '1.2.840.10045.3.1.7';
@@ -50,8 +50,8 @@ const
 //------------------------------------------------------------------------------
 //------------------------------------------------------------------------------
 // ASN
 // ASN
 //------------------------------------------------------------------------------
 //------------------------------------------------------------------------------
-procedure ASNEncodeOID(const Value: Int64; var Result: String);
-function ASNDecodeOID(var Start: Integer; const S: String): Int64; overload;
+procedure ASNEncodeOID(const Value: Int64; var Result: AnsiString);
+function ASNDecodeOID(var Start: Integer; const S: AnsiString): Int64; overload;
 function ASNDecodeOID(var Buffer: PByte; BufferEnd: PByte): Int64; overload;
 function ASNDecodeOID(var Buffer: PByte; BufferEnd: PByte): Int64; overload;
 function ASNGetEncodedLen(const Len: Integer): Integer;
 function ASNGetEncodedLen(const Len: Integer): Integer;
 procedure ASNEncodeLen(const Len: Integer; var Buffer: TBytes);
 procedure ASNEncodeLen(const Len: Integer; var Buffer: TBytes);
@@ -69,11 +69,11 @@ function ASNWriteBitStrBegin(s: TMemoryStream): int64;
 procedure ASNWriteBitStrEnd(BitStrBegin: int64; s: TMemoryStream);
 procedure ASNWriteBitStrEnd(BitStrBegin: int64; s: TMemoryStream);
 
 
 // Encodes ASN.1 object to binary form
 // Encodes ASN.1 object to binary form
-procedure ASNObject(const Data: String; const ASNType: Integer; var Buffer: TBytes);
+procedure ASNObject(const Data: AnsiString; const ASNType: Integer; var Buffer: TBytes);
 // Encodes an MIB OID String to binary form
 // Encodes an MIB OID String to binary form
-procedure MibToId(Mib: String; var Result: String);
+procedure MibToId(Mib: AnsiString; var Result: AnsiString);
 // Decodes MIB OID from binary form to String form.
 // Decodes MIB OID from binary form to String form.
-procedure IdToMib(const Id: String; var Result: String); overload;
+procedure IdToMib(const Id: AnsiString; var Result: AnsiString); overload;
 function IdToMib(Buffer, BufferEnd: PByte): string; overload;
 function IdToMib(Buffer, BufferEnd: PByte): string; overload;
 
 
 procedure ASNDebug(const Buffer: TBytes; var Output: TBytes);
 procedure ASNDebug(const Buffer: TBytes; var Output: TBytes);
@@ -83,6 +83,7 @@ procedure ASNParse_GetItem(List: TStrings; Index: integer; out ASNType, ASNSize:
 function ASNParse_GetIntBytes(List: TStrings; ListIndex: integer; ID: int64): TBytes;
 function ASNParse_GetIntBytes(List: TStrings; ListIndex: integer; ID: int64): TBytes;
 function ASNFetch(var Buffer: PByte; BufferEnd: PByte; Out ASNType, ASNSize: Int32): Boolean; overload;
 function ASNFetch(var Buffer: PByte; BufferEnd: PByte; Out ASNType, ASNSize: Int32): Boolean; overload;
 function ASNFetchOID(var Buffer: PByte; BufferEnd: PByte; out OID: String): Boolean; overload;
 function ASNFetchOID(var Buffer: PByte; BufferEnd: PByte; out OID: String): Boolean; overload;
+function ASNFetchOID(var Buffer: PByte; BufferEnd: PByte; out OID: AnsiString): Boolean; overload;
 
 
 implementation
 implementation
 
 
@@ -90,13 +91,13 @@ implementation
 // ASN
 // ASN
 //------------------------------------------------------------------------------
 //------------------------------------------------------------------------------
 
 
-procedure ASNEncodeOID(const Value: Int64; var Result: String);
+procedure ASNEncodeOID(const Value: Int64; var Result: AnsiString);
 var
 var
   B: Boolean;
   B: Boolean;
   I: Integer;
   I: Integer;
   x: Int64;
   x: Int64;
   Modulo: Byte;
   Modulo: Byte;
-  S: String;
+  S: AnsiString;
 
 
 begin
 begin
   S:='';
   S:='';
@@ -109,14 +110,14 @@ begin
       Modulo := Modulo or $80;
       Modulo := Modulo or $80;
     if x > 0 then
     if x > 0 then
       B := True;
       B := True;
-    S:=S+Char(Modulo);
+    S:=S+AnsiChar(Modulo);
   until x = 0;
   until x = 0;
   for I:=Length(S) downto 1 do
   for I:=Length(S) downto 1 do
     Result:=Result+S[I];
     Result:=Result+S[I];
 end;
 end;
 
 
 // @Start=0
 // @Start=0
-function ASNDecodeOID(var Start: Integer; const S: String): Int64;
+function ASNDecodeOID(var Start: Integer; const S: AnsiString): Int64;
 var
 var
   x: Integer;
   x: Integer;
 begin
 begin
@@ -160,11 +161,11 @@ begin
   repeat
   repeat
     y := x mod 256;
     y := x mod 256;
     x := x div 256;
     x := x div 256;
-    S:=S+Char(y);
+    S:=S+AnsiChar(y);
   until x = 0;
   until x = 0;
   y := Length(S);
   y := Length(S);
   y := y or $80;
   y := y or $80;
-  S:=S+Char(y);
+  S:=S+AnsiChar(y);
   for x := Length(S) downto 1 do
   for x := Length(S) downto 1 do
     Buffer:=Concat(Buffer,[Ord(S[x])]);
     Buffer:=Concat(Buffer,[Ord(S[x])]);
 end;
 end;
@@ -212,7 +213,7 @@ var
   x: Int64;
   x: Int64;
   y: byte;
   y: byte;
   neg: Boolean;
   neg: Boolean;
-  S : String;
+  S : AnsiString;
 begin
 begin
   S:='';
   S:='';
   neg := Value < 0;
   neg := Value < 0;
@@ -309,7 +310,7 @@ end;
 
 
 procedure ASNWriteObjID(const ObjID: string; s: TStream);
 procedure ASNWriteObjID(const ObjID: string; s: TStream);
 var
 var
-  Mib: string;
+  Mib: Ansistring;
   aLen: TBytes;
   aLen: TBytes;
 begin
 begin
   Mib:='';
   Mib:='';
@@ -363,7 +364,7 @@ begin
   ASNWriteSequenceEnd(BitStrBegin,s);
   ASNWriteSequenceEnd(BitStrBegin,s);
 end;
 end;
 
 
-Procedure AppendStringToBuffer(var Buffer: TBytes; const aString : String);
+Procedure AppendStringToBuffer(var Buffer: TBytes; const aString : AnsiString);
 Var
 Var
   Buflen,sLen : integer;
   Buflen,sLen : integer;
 begin
 begin
@@ -374,7 +375,7 @@ begin
     Move(aString[1],Buffer[Buflen],sLen);
     Move(aString[1],Buffer[Buflen],sLen);
 end;
 end;
 
 
-procedure ASNObject(const Data: String; const ASNType: Integer; var Buffer: TBytes);
+procedure ASNObject(const Data: AnsiString; const ASNType: Integer; var Buffer: TBytes);
 begin
 begin
   Buffer:=Concat(Buffer,[ASNType]);
   Buffer:=Concat(Buffer,[ASNType]);
   ASNEncodeLen(Length(Data), Buffer);
   ASNEncodeLen(Length(Data), Buffer);
@@ -401,14 +402,14 @@ begin
   end;
   end;
 end;
 end;
 
 
-procedure OutputHexa(var Output: TBytes; const S: String);
+procedure OutputHexa(var Output: TBytes; const S: AnsiString);
 
 
 var
 var
   I: Integer;
   I: Integer;
   P: PByte;
   P: PByte;
 
 
 begin
 begin
-  P := PByte(PChar(S));
+  P := PByte(PAnsiChar(S));
   for I := 1 to Length(S) do
   for I := 1 to Length(S) do
   begin
   begin
     AppendStringToBuffer(Output, HexStr(P^,2));
     AppendStringToBuffer(Output, HexStr(P^,2));
@@ -416,9 +417,9 @@ begin
   end;
   end;
 end;
 end;
 
 
-procedure MibToId(Mib: String; var Result: String);
+procedure MibToId(Mib: AnsiString; var Result: AnsiString);
 
 
-  function WalkInt(var S: String): Integer;
+  function WalkInt(var S: AnsiString): Integer;
   var
   var
     P : Integer;
     P : Integer;
   begin
   begin
@@ -442,7 +443,7 @@ begin
   end;
   end;
 end;
 end;
 
 
-procedure IdToMib(const Id: String; var Result: String);
+procedure IdToMib(const Id: AnsiString; var Result: AnsiString);
 var
 var
   x, y, Index: Integer;
   x, y, Index: Integer;
 begin
 begin
@@ -506,21 +507,24 @@ end;
 // Decode the ASN.1 item of the next element in @Buffer. Type of item is stored in @ASNType
 // Decode the ASN.1 item of the next element in @Buffer. Type of item is stored in @ASNType
 procedure ASNDebugItem(var Buffer: PByte; BufferEnd: PByte; Out ASNType, ASNSize: Integer; var Output: TBytes);
 procedure ASNDebugItem(var Buffer: PByte; BufferEnd: PByte; Out ASNType, ASNSize: Integer; var Output: TBytes);
 
 
-  procedure BufToString(out S : String);
+  procedure BufToString(out S : AnsiString);
+  
+  var
+    SA : AnsiString;
 
 
   begin
   begin
-    S:='';
-    SetLength(S,ASNSize);
+    SetLength(SA,ASNSize);
     if ASNSize>0 then
     if ASNSize>0 then
     begin
     begin
-      Move(Buffer^,S[1],ASNSize);
+      Move(Buffer^,SA[1],ASNSize);
       inc(Buffer,ASNSize);
       inc(Buffer,ASNSize);
     end;
     end;
+    S:=SA;
   end;
   end;
 
 
 var
 var
   n: Integer;
   n: Integer;
-  S, S2: String;
+  S, S2: AnsiString;
   y: Int64;
   y: Int64;
   OldBuffer: PByte;
   OldBuffer: PByte;
 begin
 begin
@@ -689,7 +693,7 @@ end;
 procedure ASNDebug(const Buffer: TBytes; var Output: TBytes);
 procedure ASNDebug(const Buffer: TBytes; var Output: TBytes);
 
 
 const
 const
-  SSpaces: String = '                                                                     ';
+  SSpaces: AnsiString = '                                                                     ';
 
 
 var
 var
   ASNSize, ASNType, n: Integer;
   ASNSize, ASNType, n: Integer;
@@ -706,7 +710,7 @@ begin
   EndP:=StartP+length(Buffer);
   EndP:=StartP+length(Buffer);
   while p<EndP do
   while p<EndP do
   begin
   begin
-    //writeln('ASNDebug p=',p-StartP,' Type=',hexstr(p^,2),' Indent=',length(IndentList));
+    writeln('ASNDebug p=',p-StartP,' Type=',hexstr(p^,2),' Indent=',length(IndentList));
     // check if any sequence/set has ended and unindent
     // check if any sequence/set has ended and unindent
     for n := Length(IndentList)-1 downto 0 do
     for n := Length(IndentList)-1 downto 0 do
     begin
     begin
@@ -740,18 +744,19 @@ end;
 
 
 procedure ASNParseAddInt(var Buffer: PByte; BufferEnd: PByte; List: TStrings; const ASNType, ASNSize: Integer; Signed: boolean);
 procedure ASNParseAddInt(var Buffer: PByte; BufferEnd: PByte; List: TStrings; const ASNType, ASNSize: Integer; Signed: boolean);
 
 
-  procedure BufToString(var S : String);
+  procedure BufToString(var S : AnsiString);
 
 
   begin
   begin
     SetLength(S,ASNSize);
     SetLength(S,ASNSize);
-    if ASNSize=0 then exit;
+    if ASNSize=0 then
+      exit;
     Move(Buffer^,S[1],ASNSize);
     Move(Buffer^,S[1],ASNSize);
     inc(Buffer, ASNSize);
     inc(Buffer, ASNSize);
   end;
   end;
 
 
 var
 var
-  S, S2: String;
-  y: Int64;
+  S, S2: AnsiString;
+  y:  Int64;
 begin
 begin
   S:='';
   S:='';
   S2:='';
   S2:='';
@@ -765,7 +770,7 @@ begin
   end else
   end else
   begin
   begin
     BufToString(S2);
     BufToString(S2);
-    if S2[1] = Char(#00) then
+    if S2[1] = AnsiChar(#00) then
       Delete(S2,1,1);
       Delete(S2,1,1);
     BytesToHexStr(S,GetRawStringBytes(S2));
     BytesToHexStr(S,GetRawStringBytes(S2));
   end;
   end;
@@ -799,6 +804,16 @@ begin
   Result:=true;
   Result:=true;
 end;
 end;
 
 
+function ASNFetchOID(var Buffer: PByte; BufferEnd: PByte; out OID: AnsiString): Boolean; overload;
+
+Var
+  OIDS : String;
+
+begin
+  Result:=ASNFetchOID(Buffer,BufferEnd,OIDS);
+  OID:=OIDS;
+end;
+
 function ASNFetchOID(var Buffer: PByte; BufferEnd: PByte; out OID: String): Boolean;
 function ASNFetchOID(var Buffer: PByte; BufferEnd: PByte; out OID: String): Boolean;
 var
 var
   ASNType, ASNSize: Int32;
   ASNType, ASNSize: Int32;
@@ -828,18 +843,18 @@ end;
 
 
 function ASNParseItem(var Buffer: PByte; BufferEnd: PByte; List: TStrings): boolean;
 function ASNParseItem(var Buffer: PByte; BufferEnd: PByte; List: TStrings): boolean;
 
 
-  function BufToString(Len : Integer): String;
+  function BufToString(Len : Integer): AnsiString;
   begin
   begin
     SetLength(Result{%H-},Len);
     SetLength(Result{%H-},Len);
     if Len=0 then exit;
     if Len=0 then exit;
-    Move(Buffer^,Result[1],Len);
+      Move(Buffer^,Result[1],Len);
     inc(Buffer, Len);
     inc(Buffer, Len);
   end;
   end;
 
 
 var
 var
   ASNType, ASNSize: Integer;
   ASNType, ASNSize: Integer;
   n: Integer;
   n: Integer;
-  S, S2: String;
+  S,S2: AnsiString;
   y: Int64;
   y: Int64;
   OldBuffer: PByte;
   OldBuffer: PByte;
 
 
@@ -937,7 +952,10 @@ end;
 procedure ASNParse(const Buffer: TBytes; List: TStrings);
 procedure ASNParse(const Buffer: TBytes; List: TStrings);
 var
 var
   P, EndP: PByte;
   P, EndP: PByte;
+  O : Tbytes;
 begin
 begin
+  ASNDebug(Buffer,O);
+  Writeln(TEncoding.UTF8.GetAnsiString(O));
   if length(Buffer)=0 then exit;
   if length(Buffer)=0 then exit;
   P:=@Buffer[0];
   P:=@Buffer[0];
   EndP:=P+length(Buffer);
   EndP:=P+length(Buffer);

+ 40 - 8
packages/fcl-hash/src/fphashutils.pp

@@ -33,8 +33,9 @@ Procedure BytesToVar(const aBytes : TBytes; out aLocation; aSize : Integer);
 Procedure BytesToVar(const aBytes : TBytes; Out aLocation : Pointer);
 Procedure BytesToVar(const aBytes : TBytes; Out aLocation : Pointer);
 
 
 Procedure HexStrToBytes(Const aHexStr : String; out aBytes : TBytes); overload;
 Procedure HexStrToBytes(Const aHexStr : String; out aBytes : TBytes); overload;
+Procedure HexStrToBytes(Const aHexStr : AnsiString; out aBytes : TBytes); overload;
 Function HexStrToBytes(Const aHexStr : String) : TBytes; overload;
 Function HexStrToBytes(Const aHexStr : String) : TBytes; overload;
-Function HexStrToString(Const aHexStr : String) : String; overload;
+Function HexStrToString(Const aHexStr :  String) : AnsiString; overload;
 
 
 Function BytesToHexStr(Const aSource : AnsiString) : Ansistring; overload;
 Function BytesToHexStr(Const aSource : AnsiString) : Ansistring; overload;
 Procedure BytesToHexStr(out aHexStr : AnsiString;Const aSource : AnsiString); overload;
 Procedure BytesToHexStr(out aHexStr : AnsiString;Const aSource : AnsiString); overload;
@@ -42,7 +43,7 @@ Procedure BytesToHexStr(out aHexStr : AnsiString; aBytes : PByte; aSize : Intege
 Procedure BytesToHexStr(out aHexStr : AnsiString; aBytes : TBytes); overload;
 Procedure BytesToHexStr(out aHexStr : AnsiString; aBytes : TBytes); overload;
 Function BytesToHexStr(aBytes : TBytes) : AnsiString; overload;
 Function BytesToHexStr(aBytes : TBytes) : AnsiString; overload;
 Procedure BytesToHexStrAppend(aBytes : TBytes;var aHexStr : AnsiString);
 Procedure BytesToHexStrAppend(aBytes : TBytes;var aHexStr : AnsiString);
-Function StringToHex(const s: string): string; overload;
+Function StringToHex(const s: ansistring): ansistring; overload;
 
 
 Procedure BytesEncodeBase64(Source: Tbytes; out Dest: AnsiString; const IsURL, MultiLines, Padding: Boolean);
 Procedure BytesEncodeBase64(Source: Tbytes; out Dest: AnsiString; const IsURL, MultiLines, Padding: Boolean);
 Function BytesEncodeBase64(Source: Tbytes; const IsURL, MultiLines, Padding: Boolean) : AnsiString;
 Function BytesEncodeBase64(Source: Tbytes; const IsURL, MultiLines, Padding: Boolean) : AnsiString;
@@ -99,7 +100,7 @@ begin
   HexStrToBytes(aHexStr,Result);
   HexStrToBytes(aHexStr,Result);
 end;
 end;
 
 
-function HexStrToString(const aHexStr: String): String;
+function HexStrToString(const aHexStr: String): AnsiString;
 var
 var
   aBytes: TBytes;
   aBytes: TBytes;
   l: SizeInt;
   l: SizeInt;
@@ -112,7 +113,7 @@ begin
   Move(aBytes[0],Result[1],l);
   Move(aBytes[0],Result[1],l);
 end;
 end;
 
 
-procedure HexStrToBytes(const aHexStr: String; out aBytes: TBytes);
+Procedure HexStrToBytes(Const aHexStr : AnsiString; out aBytes : TBytes); overload;
 
 
 const
 const
   Convert: array['0'..'f'] of SmallInt =
   Convert: array['0'..'f'] of SmallInt =
@@ -142,6 +143,37 @@ begin
   end;
   end;
 end;
 end;
 
 
+
+procedure HexStrToBytes(const aHexStr: String; out aBytes: TBytes);
+
+const
+  Convert: array['0'..'f'] of SmallInt =
+    ( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1,-1,-1,-1,-1,
+     -1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1,
+     -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
+     -1,10,11,12,13,14,15);
+
+Var
+  Len : LongInt;
+  P: PChar;
+  PResult: PByte;
+
+begin
+  Len:=Length(aHexStr);
+  aBytes:=[];
+  SetLength(aBytes, (Len+1) div 2);
+  if Len=0 then Exit;
+  P := PChar(aHexStr);
+  PResult := PByte(aBytes);
+  while Len > 0 do
+  begin
+    PResult^ := (Convert[P[0]] shl 4) + Convert[P[1]];
+    Inc(PResult);
+    Inc(P, 2);
+    Dec(Len, 2);
+  end;
+end;
+
 function BytesToHexStr(const aSource: AnsiString): Ansistring;
 function BytesToHexStr(const aSource: AnsiString): Ansistring;
 
 
 begin
 begin
@@ -151,7 +183,7 @@ end;
 procedure BytesToHexStr(out aHexStr: AnsiString; const aSource: AnsiString);
 procedure BytesToHexStr(out aHexStr: AnsiString; const aSource: AnsiString);
 
 
 begin
 begin
-  BytesToHexStr(aHexStr,PByte(PChar(aSource)),Length(aSource))
+  BytesToHexStr(aHexStr,PByte(PAnsiChar(aSource)),Length(aSource))
 end;
 end;
 
 
 procedure BytesToHexStr(out aHexStr : AnsiString; aBytes : PByte; aSize : Integer);
 procedure BytesToHexStr(out aHexStr : AnsiString; aBytes : PByte; aSize : Integer);
@@ -166,7 +198,7 @@ begin
     exit;
     exit;
   SetLength(aHexStr,aSize*2);
   SetLength(aHexStr,aSize*2);
   PB:=aBytes;
   PB:=aBytes;
-  PC:=PChar(aHexStr);
+  PC:=PAnsiChar(aHexStr);
   for I:=0 to aSize-1 do
   for I:=0 to aSize-1 do
     begin
     begin
     PC^:=HexDigits[PB^ shr 4];
     PC^:=HexDigits[PB^ shr 4];
@@ -195,7 +227,7 @@ begin
   aHexStr:=aHexStr+BytesToHexStr(aBytes);
   aHexStr:=aHexStr+BytesToHexStr(aBytes);
 end;
 end;
 
 
-function StringToHex(const s: string): string;
+function StringToHex(const s: ansistring): ansistring;
 begin
 begin
   if s='' then exit;
   if s='' then exit;
   BytesToHexStr(Result,@s[1],length(s));
   BytesToHexStr(Result,@s[1],length(s));
@@ -325,7 +357,7 @@ begin
       DestBuf[0] := Ord('=');
       DestBuf[0] := Ord('=');
       Inc(DestSize);
       Inc(DestSize);
     end;
     end;
-  end else if Index = BufSize-1 then // Last remaining char
+  end else if Index = BufSize-1 then // Last remaining AnsiChar
   begin
   begin
     Ch1 := Source[Index];
     Ch1 := Source[Index];
     XBufferEncode64_1(DestBuf, Ch1, IsURL);
     XBufferEncode64_1(DestBuf, Ch1, IsURL);

+ 4 - 2
packages/fcl-hash/src/fppem.pp

@@ -65,7 +65,7 @@ end;
 procedure PemLoadPublicKey64FromList(List: TStrings; out PrivateKey: TEccPrivateKey; out PublicKey: TEccPublicKey; out PublicKeyX64, PublicKeyY64: AnsiString);
 procedure PemLoadPublicKey64FromList(List: TStrings; out PrivateKey: TEccPrivateKey; out PublicKey: TEccPublicKey; out PublicKeyX64, PublicKeyY64: AnsiString);
 
 
 var
 var
-  SPrivateKeyHexa, SPublicKeyHexa: String;
+  SPrivateKeyHexa, SPublicKeyHexa: AnsiString;
   DecompressedPublicKey, SPrivateKey : TBytes;
   DecompressedPublicKey, SPrivateKey : TBytes;
 
 
 begin
 begin
@@ -149,6 +149,7 @@ begin
   ASNParsePemSection(Buffer, List, _BEGIN_EC_PRIVATE_KEY, _END_EC_PRIVATE_KEY);
   ASNParsePemSection(Buffer, List, _BEGIN_EC_PRIVATE_KEY, _END_EC_PRIVATE_KEY);
   if List.Count < 7 then
   if List.Count < 7 then
     Exit;
     Exit;
+  Writeln(List.Text);
   CurveOID := List.Strings[4];
   CurveOID := List.Strings[4];
   Result := (CurveOID=ASN_secp256r1);
   Result := (CurveOID=ASN_secp256r1);
 end;
 end;
@@ -320,7 +321,8 @@ end;
 function PemToDER(const PEM, BeginTag, EndTag: String): TBytes;
 function PemToDER(const PEM, BeginTag, EndTag: String): TBytes;
 var
 var
   sl: TStringList;
   sl: TStringList;
-  Line, TxtBase64: String;
+  Line :  AnsiString;
+  TxtBase64: AnsiString;
   StartIndex, EndIndex, i: Integer;
   StartIndex, EndIndex, i: Integer;
 begin
 begin
   Result:=[];
   Result:=[];

+ 22 - 22
packages/fcl-hash/src/fprsa.pas

@@ -52,8 +52,8 @@ type
     Exponent1,           // dp
     Exponent1,           // dp
     Exponent2,           // dq
     Exponent2,           // dq
     Coefficient: TBytes; // qi
     Coefficient: TBytes; // qi
-    procedure InitWithHexStrings(const n, e, d, p, q, dp, dq, qi: string);
-    procedure InitWithBase64UrlEncoded(const n, e, d, p, q, dp, dq, qi: string);
+    procedure InitWithHexStrings(const n, e, d, p, q, dp, dq, qi: ansistring);
+    procedure InitWithBase64UrlEncoded(const n, e, d, p, q, dp, dq, qi: ansistring);
     procedure WriteASN(ms: TMemoryStream);
     procedure WriteASN(ms: TMemoryStream);
     function AsDER: TBytes;
     function AsDER: TBytes;
   end;
   end;
@@ -63,8 +63,8 @@ type
   TX509RSAPublicKey = record
   TX509RSAPublicKey = record
     Modulus: TBytes; // m or n
     Modulus: TBytes; // m or n
     Exponent: TBytes; // e
     Exponent: TBytes; // e
-    procedure InitWithHexStrings(const n, e: string);
-    procedure InitWithBase64UrlEncoded(const n, e: string);
+    procedure InitWithHexStrings(const n, e: AnsiString);
+    procedure InitWithBase64UrlEncoded(const n, e: ansistring);
     procedure WriteASN(ms: TMemoryStream);
     procedure WriteASN(ms: TMemoryStream);
     function AsDER: TBytes;
     function AsDER: TBytes;
   end;
   end;
@@ -72,9 +72,9 @@ type
 procedure RSACreate(out RSA: TRSA);
 procedure RSACreate(out RSA: TRSA);
 procedure RSAFree(var RSA: TRSA);
 procedure RSAFree(var RSA: TRSA);
 
 
-procedure RsaPublicKeyToHexa(const Modulus, Exponent: String; var PublicKeyHexa: String);
-procedure RsaPublicKeyFromHexa(const PublicKeyHexa: String; out Modulus, Exponent: String);
-procedure RsaInitFromPublicKey(var RSA: TRSA; const Modulus, Exponent: String); overload;
+procedure RsaPublicKeyToHexa(const Modulus, Exponent: AnsiString; var PublicKeyHexa: AnsiString);
+procedure RsaPublicKeyFromHexa(const PublicKeyHexa: AnsiString; out Modulus, Exponent: AnsiString);
+procedure RsaInitFromPublicKey(var RSA: TRSA; const Modulus, Exponent: AnsiString); overload;
 procedure RSAInitFromPublicKey(var RSA: TRSA; const RSAPublicKey: TX509RSAPublicKey); overload;
 procedure RSAInitFromPublicKey(var RSA: TRSA; const RSAPublicKey: TX509RSAPublicKey); overload;
 procedure RSAInitFromPublicKeyDER(var RSA: TRSA; const PublicKeyDER: TBytes);
 procedure RSAInitFromPublicKeyDER(var RSA: TRSA; const PublicKeyDER: TBytes);
 procedure X509RsaPublicKeyInitFromDER(out RSA: TX509RSAPublicKey; const PublicKeyDER: TBytes);
 procedure X509RsaPublicKeyInitFromDER(out RSA: TX509RSAPublicKey; const PublicKeyDER: TBytes);
@@ -195,14 +195,13 @@ begin
   BITerminate(RSA.Context);
   BITerminate(RSA.Context);
 end;
 end;
 
 
-procedure RsaPublicKeyToHexa(const Modulus, Exponent: String;
-  var PublicKeyHexa: String);
+procedure RsaPublicKeyToHexa(const Modulus, Exponent: AnsiString;
+  var PublicKeyHexa: AnsiString);
 begin
 begin
   PublicKeyHexa:=PublicKeyHexa+BytesToHexStr(Exponent)+BytesToHexStr(Modulus);
   PublicKeyHexa:=PublicKeyHexa+BytesToHexStr(Exponent)+BytesToHexStr(Modulus);
 end;
 end;
 
 
-procedure RsaPublicKeyFromHexa(const PublicKeyHexa: String; out Modulus,
-  Exponent: String);
+procedure RsaPublicKeyFromHexa(const PublicKeyHexa: AnsiString; out Modulus, Exponent: AnsiString);
 var
 var
   aBytes: TBytes;
   aBytes: TBytes;
 begin
 begin
@@ -215,7 +214,7 @@ begin
   Move(aBytes[3],Modulus[1],length(Modulus));
   Move(aBytes[3],Modulus[1],length(Modulus));
 end;
 end;
 
 
-procedure RsaInitFromPublicKey(var RSA: TRSA; const Modulus, Exponent: String);
+procedure RsaInitFromPublicKey(var RSA: TRSA; const Modulus, Exponent: AnsiString);
 begin
 begin
   RSA.ModulusLen := length(Modulus);
   RSA.ModulusLen := length(Modulus);
   RSA.M := BIImport(RSA.Context, Modulus);
   RSA.M := BIImport(RSA.Context, Modulus);
@@ -635,13 +634,13 @@ begin
   end;
   end;
 end;
 end;
 
 
-function RsaVerify(const Modulus, Exponent, Hash, Signature: String): Boolean;
+function RsaVerify(const Modulus, Exponent, Hash, Signature: AnsiString): Boolean;
 var
 var
   ASNType, ASNSize: Int32;
   ASNType, ASNSize: Int32;
   Data: array[0..4095] of byte;
   Data: array[0..4095] of byte;
-  Digest: String;
+  Digest: AnsiString;
   DataP, DataEnd: PByte;
   DataP, DataEnd: PByte;
-  OID: String;
+  OID: AnsiString;
   RSA: TRSA;
   RSA: TRSA;
   Size: Integer;
   Size: Integer;
 begin
 begin
@@ -718,7 +717,7 @@ end;
 function RS256VerifyFromPublicKeyHexa(const PublicKeyHexa, SignatureBaseHash,
 function RS256VerifyFromPublicKeyHexa(const PublicKeyHexa, SignatureBaseHash,
   Signature: String): Boolean;
   Signature: String): Boolean;
 var
 var
-  Modulus, Exponent: String;
+  Modulus, Exponent: AnsiString;
 begin
 begin
   RsaPublicKeyFromHexa(PublicKeyHexa, Modulus, Exponent);
   RsaPublicKeyFromHexa(PublicKeyHexa, Modulus, Exponent);
   Result := RsaVerify(Modulus, Exponent, SignatureBaseHash, Signature);
   Result := RsaVerify(Modulus, Exponent, SignatureBaseHash, Signature);
@@ -737,7 +736,8 @@ const
                +'18BFB311B8377C0FACDED4CD2B1E2692E480BE260BE355F050EBABF89E24F2833F56F0A74C185225DB3B47B63612FB9BDEE1E1B8707807093E1551F24527A763'
                +'18BFB311B8377C0FACDED4CD2B1E2692E480BE260BE355F050EBABF89E24F2833F56F0A74C185225DB3B47B63612FB9BDEE1E1B8707807093E1551F24527A763'
                +'1947D033ED7052C439E50B8A46E4D0C06DBC38AF1D64B49766A5CF9A82644650FFD733B61942DB0BD8D47C8EF24A02DC9FD2EF557B12DED804519F2B2B6C284D';
                +'1947D033ED7052C439E50B8A46E4D0C06DBC38AF1D64B49766A5CF9A82644650FFD733B61942DB0BD8D47C8EF24A02DC9FD2EF557B12DED804519F2B2B6C284D';
 var
 var
-  Exponent, Modulus, Hash, Signature: string;
+  Exponent, Modulus, Hash, Signature: Ansistring;
+
 begin
 begin
   Exponent:=HexStrToString(_Exponent);
   Exponent:=HexStrToString(_Exponent);
   Modulus:=HexStrToString(_Modulus);
   Modulus:=HexStrToString(_Modulus);
@@ -1060,7 +1060,7 @@ function MGF1(const InputStr: string; HashFunc: PRSAHashFuncInfo; Len: integer):
 begin
 begin
   SetLength(Result{%H-},Len);
   SetLength(Result{%H-},Len);
   if Len=0 then exit;
   if Len=0 then exit;
-  MGF1(PByte(PChar(InputStr)){InputStr might be empty!},length(InputStr), HashFunc, @Result[1], Len);
+  MGF1(PByte(PAnsiChar(InputStr)){InputStr might be empty!},length(InputStr), HashFunc, @Result[1], Len);
 end;
 end;
 
 
 procedure MGF1(Input: PByte; InLen: Integer; HashFunc: PRSAHashFuncInfo;
 procedure MGF1(Input: PByte; InLen: Integer; HashFunc: PRSAHashFuncInfo;
@@ -1182,7 +1182,7 @@ end;
 
 
 { TX509RSAPrivateKey }
 { TX509RSAPrivateKey }
 
 
-procedure TX509RSAPrivateKey.InitWithHexStrings(const n, e, d, p, q, dp, dq, qi: string
+procedure TX509RSAPrivateKey.InitWithHexStrings(const n, e, d, p, q, dp, dq, qi: Ansistring
   );
   );
 begin
 begin
   Version:=0;
   Version:=0;
@@ -1197,7 +1197,7 @@ begin
 end;
 end;
 
 
 procedure TX509RSAPrivateKey.InitWithBase64UrlEncoded(const n, e, d, p, q, dp,
 procedure TX509RSAPrivateKey.InitWithBase64UrlEncoded(const n, e, d, p, q, dp,
-  dq, qi: string);
+  dq, qi: Ansistring);
 begin
 begin
   Version:=0;
   Version:=0;
   Modulus:=Base64URL.Decode(n,false);
   Modulus:=Base64URL.Decode(n,false);
@@ -1244,13 +1244,13 @@ end;
 
 
 { TX509RSAPublicKey }
 { TX509RSAPublicKey }
 
 
-procedure TX509RSAPublicKey.InitWithHexStrings(const n, e: string);
+procedure TX509RSAPublicKey.InitWithHexStrings(const n, e: ansistring);
 begin
 begin
   Modulus:=HexStrToBytes(n);
   Modulus:=HexStrToBytes(n);
   Exponent:=HexStrToBytes(e);
   Exponent:=HexStrToBytes(e);
 end;
 end;
 
 
-procedure TX509RSAPublicKey.InitWithBase64UrlEncoded(const n, e: string);
+procedure TX509RSAPublicKey.InitWithBase64UrlEncoded(const n, e: ansistring);
 begin
 begin
   Modulus:=Base64URL.Decode(n,false);
   Modulus:=Base64URL.Decode(n,false);
   Exponent:=Base64URL.Decode(e,false);
   Exponent:=Base64URL.Decode(e,false);

+ 1 - 1
packages/fcl-hash/src/fptlsbigint.pas

@@ -1176,7 +1176,7 @@ end;
 
 
 function BIToString(BI: PBigInt): AnsiString;
 function BIToString(BI: PBigInt): AnsiString;
 const
 const
-  Digits: Array[0..15] of char = ('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');
+  Digits: Array[0..15] of AnsiChar = ('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');
 var
 var
   I,J,K: Integer;
   I,J,K: Integer;
   Num: TBIComponent;
   Num: TBIComponent;

+ 8 - 7
packages/fcl-hash/src/onetimepass.pp

@@ -32,7 +32,7 @@ function HOTPCalculateToken(const aSecret: AnsiString; const Counter: LongInt):
 function TOTPCalculateToken(const aSecret: AnsiString): LongInt;
 function TOTPCalculateToken(const aSecret: AnsiString): LongInt;
 function TOTPGenerateToken(const aSecret: AnsiString): LongInt;
 function TOTPGenerateToken(const aSecret: AnsiString): LongInt;
 function TOTPValidate(const aSecret: AnsiString; const Token: LongInt; const WindowSize: LongInt; var Counter: LongInt): Boolean;
 function TOTPValidate(const aSecret: AnsiString; const Token: LongInt; const WindowSize: LongInt; var Counter: LongInt): Boolean;
-Function TOTPSharedSecret(aRandom : TRandomBytes = Nil) : String;
+Function TOTPSharedSecret(aRandom : TRandomBytes = Nil) : AnsiString;
 
 
 implementation
 implementation
 
 
@@ -51,13 +51,13 @@ begin
     Result:=Result+AnsiChar(B[I]);
     Result:=Result+AnsiChar(B[I]);
 end;
 end;
 
 
-function TOTPCalculateToken(const aSecret: String): Longint;
+function TOTPCalculateToken(const aSecret: AnsiString): Longint;
 
 
 begin
 begin
   Result:=HOTPCalculateToken(aSecret,-1);
   Result:=HOTPCalculateToken(aSecret,-1);
 end;
 end;
 
 
-function HOTPCalculateToken(const aSecret: String; const Counter: Longint): Longint;
+function HOTPCalculateToken(const aSecret: AnsiString; const Counter: Longint): Longint;
 
 
 var
 var
   Digest: TSHA1Digest;
   Digest: TSHA1Digest;
@@ -65,16 +65,17 @@ var
   Offset: Longint;
   Offset: Longint;
   Part1, Part2, Part3, Part4: UInt32;
   Part1, Part2, Part3, Part4: UInt32;
   SecretBinBuf: TBytes;
   SecretBinBuf: TBytes;
-  STime, SSecretBin: String;
+  STime, SSecretBin: RawbyteString;
   Time: Longint;
   Time: Longint;
 
 
 begin
 begin
   Time := Counter;
   Time := Counter;
   if Time=-1 then
   if Time=-1 then
     Time := DateTimeToUnix(Now,False) div TOTP_KeyRegeneration;
     Time := DateTimeToUnix(Now,False) div TOTP_KeyRegeneration;
+  SSecretBin:='';
   SecretBinBuf:=Base32.Decode(aSecret);
   SecretBinBuf:=Base32.Decode(aSecret);
   STime:=Int64ToRawString(Time);
   STime:=Int64ToRawString(Time);
-  SetLength(SSecretBin{%H-},length(SecretBinBuf));
+  SetLength(SSecretBin,length(SecretBinBuf));
   Move(SecretBinBuf[0],SSecretBin[1],length(SecretBinBuf));
   Move(SecretBinBuf[0],SSecretBin[1],length(SecretBinBuf));
   Digest:=HMACSHA1Digest(SSecretBin, STime);
   Digest:=HMACSHA1Digest(SSecretBin, STime);
   Offset := Digest[19] and $0F;
   Offset := Digest[19] and $0F;
@@ -91,7 +92,7 @@ begin
   Result := HOTPCalculateToken(aSecret, -1);
   Result := HOTPCalculateToken(aSecret, -1);
 end;
 end;
 
 
-Function TOTPSharedSecret(aRandom : TRandomBytes = Nil) : String;
+Function TOTPSharedSecret(aRandom : TRandomBytes = Nil) : AnsiString;
 
 
 var
 var
   RandomKey: TByteDynArray;
   RandomKey: TByteDynArray;
@@ -109,7 +110,7 @@ begin
 end;
 end;
 
 
 // @Secret Base32 encoded, @WindowSize=1
 // @Secret Base32 encoded, @WindowSize=1
-function TOTPValidate(const aSecret: String; const Token: LongInt; const WindowSize: LongInt; var Counter: LongInt): Boolean;
+function TOTPValidate(const aSecret: AnsiString; const Token: LongInt; const WindowSize: LongInt; var Counter: LongInt): Boolean;
 var
 var
   TimeStamp: Longint;
   TimeStamp: Longint;
   UnixTime: Longint;
   UnixTime: Longint;

+ 4 - 0
packages/fcl-hash/tests/testhash.lpi

@@ -81,9 +81,13 @@
       <Filename Value="testhash"/>
       <Filename Value="testhash"/>
     </Target>
     </Target>
     <SearchPaths>
     <SearchPaths>
+      <IncludeFiles Value="$(ProjOutDir)"/>
       <OtherUnitFiles Value="../src"/>
       <OtherUnitFiles Value="../src"/>
       <UnitOutputDirectory Value="lib/$(TargetCPU)-$(TargetOS)"/>
       <UnitOutputDirectory Value="lib/$(TargetCPU)-$(TargetOS)"/>
     </SearchPaths>
     </SearchPaths>
+    <Other>
+      <CustomOptions Value="-tunicodertl"/>
+    </Other>
   </CompilerOptions>
   </CompilerOptions>
   <Debugging>
   <Debugging>
     <Exceptions>
     <Exceptions>

+ 3 - 0
packages/fcl-hash/tests/testhash.pp

@@ -5,6 +5,9 @@ program testhash;
 {$mode objfpc}
 {$mode objfpc}
 
 
 uses
 uses
+{$ifdef unix}
+  cwstring,
+{$endif}
   consoletestrunner, utestsha256, utestonetimepass, utestsha512, utestpem, fpECC, fphashutils, fpsha256;
   consoletestrunner, utestsha256, utestonetimepass, utestsha512, utestpem, fpECC, fphashutils, fpsha256;
 
 
 var
 var

+ 6 - 3
packages/fcl-hash/tests/utestpem.pp

@@ -14,7 +14,7 @@ Type
 
 
   TTestPEM = Class(TTestCase)
   TTestPEM = Class(TTestCase)
   protected
   protected
-    procedure GetPEM_PrivateKeyRSA2048bit(out PrivatePEM, PublicPEM: string);
+    procedure GetPEM_PrivateKeyRSA2048bit(out PrivatePEM, PublicPEM: Ansistring);
   Published
   Published
     Procedure TestECC_Load;
     Procedure TestECC_Load;
     Procedure TestRSA_RS256Verify;
     Procedure TestRSA_RS256Verify;
@@ -36,7 +36,7 @@ end;
 Const
 Const
   PrivateKeyFile = 'private-key.pem';
   PrivateKeyFile = 'private-key.pem';
 
 
-procedure TTestPEM.GetPEM_PrivateKeyRSA2048bit(out PrivatePEM, PublicPEM: string
+procedure TTestPEM.GetPEM_PrivateKeyRSA2048bit(out PrivatePEM, PublicPEM: Ansistring
   );
   );
 begin
 begin
   // generated with
   // generated with
@@ -112,7 +112,10 @@ var
 begin
 begin
   List := TStringList.Create;
   List := TStringList.Create;
   try
   try
+    PrivateKey:=Default(TEccPrivateKey);
+    PublicKey:=Default(TEccPublicKey);
     Res:=FileExists(PrivateKeyFile) and PemIsECDSA(PrivateKeyFile, List);
     Res:=FileExists(PrivateKeyFile) and PemIsECDSA(PrivateKeyFile, List);
+    AssertTrue('Have file with Key',Res);
     if Res then
     if Res then
       PemLoadPublicKey64FromList(List, PrivateKey, PublicKey, PublicKeyX64, PublicKeyY64);
       PemLoadPublicKey64FromList(List, PrivateKey, PublicKey, PublicKeyX64, PublicKeyY64);
     AssertEquals('Private key',resprivatekey,BytesToHexStr(BytesFromVar(@PrivateKey,Sizeof(PrivateKey))));
     AssertEquals('Private key',resprivatekey,BytesToHexStr(BytesFromVar(@PrivateKey,Sizeof(PrivateKey))));
@@ -140,7 +143,7 @@ procedure TTestPEM.TestRSA_PrivatePublicPEM_NoPassphrase;
 const
 const
   SecretMsg = 'FreePascal RSA Test';
   SecretMsg = 'FreePascal RSA Test';
 var
 var
-  PrivatePEM, PublicPEM, Original, Encrypted, Decrypted: string;
+  PrivatePEM, PublicPEM, Original, Encrypted, Decrypted: Ansistring;
   DER: TBytes;
   DER: TBytes;
   PrivateRSA, PublicRSA: TRSA;
   PrivateRSA, PublicRSA: TRSA;
   EncryptedLen, DecryptedLen: Integer;
   EncryptedLen, DecryptedLen: Integer;

+ 6 - 6
packages/fcl-hash/tests/utestsha512.pp

@@ -112,7 +112,7 @@ end;
 procedure TTestSHA512.TestSmallString;
 procedure TTestSHA512.TestSmallString;
 
 
 Var
 Var
-  S : String;
+  S : AnsiString;
 begin
 begin
   BytesToHexStr(S,@Test1Out,SizeOf(Test1Out));
   BytesToHexStr(S,@Test1Out,SizeOf(Test1Out));
   TestHexString('abc',S);
   TestHexString('abc',S);
@@ -120,7 +120,7 @@ end;
 
 
 procedure TTestSHA512.TestLargeString;
 procedure TTestSHA512.TestLargeString;
 Var
 Var
-  S : String;
+  S : AnsiString;
 begin
 begin
   BytesToHexStr(S,@Test2Out,SizeOf(Test2Out));
   BytesToHexStr(S,@Test2Out,SizeOf(Test2Out));
   TestHexString('abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu',S);
   TestHexString('abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu',S);
@@ -153,7 +153,7 @@ procedure TTestSHA512.TestHMACStream;
 
 
 Var
 Var
   S : TStringStream;
   S : TStringStream;
-  res : String;
+  res : AnsiString;
 
 
 begin
 begin
   BytesToHexStr(Res,@Test1Out,SizeOf(Test1Out));
   BytesToHexStr(Res,@Test1Out,SizeOf(Test1Out));
@@ -223,7 +223,7 @@ end;
 
 
 procedure TTestSHA384.TestSmallString;
 procedure TTestSHA384.TestSmallString;
 Var
 Var
-  S : String;
+  S : AnsiString;
 begin
 begin
   BytesToHexStr(S,@Test1Out384,SizeOf(Test1Out384));
   BytesToHexStr(S,@Test1Out384,SizeOf(Test1Out384));
   TestHexString('abc',S);
   TestHexString('abc',S);
@@ -231,7 +231,7 @@ end;
 
 
 procedure TTestSHA384.TestLargeString;
 procedure TTestSHA384.TestLargeString;
 Var
 Var
-  S : String;
+  S : AnsiString;
 begin
 begin
   BytesToHexStr(S,@Test2Out384,SizeOf(Test2Out384));
   BytesToHexStr(S,@Test2Out384,SizeOf(Test2Out384));
   TestHexString('abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu',S);
   TestHexString('abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu',S);
@@ -264,7 +264,7 @@ procedure TTestSHA384.TestHMACStream;
 
 
 Var
 Var
   S : TStringStream;
   S : TStringStream;
-  res : String;
+  res : AnsiString;
 
 
 begin
 begin
   BytesToHexStr(Res,@Test1Out384,SizeOf(Test1Out384));
   BytesToHexStr(Res,@Test1Out384,SizeOf(Test1Out384));