Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi Von C# nach Delphi (https://www.delphipraxis.net/145760-von-c-nach-delphi.html)

cookie22 7. Jan 2010 23:28


Von C# nach Delphi
 
hallo,

ich muss folgenden code von c# nach delphi bekommen, lieder hab ich in c# absolut keinen durchblick. kann mir da jemand weiter helfen?

Code:
public static uint EstimatePasswordBits(char[] vPasswordChars)
      {
         bool bChLower = false, bChUpper = false, bChNumber = false;
         bool bChSimpleSpecial = false, bChExtSpecial = false, bChHigh = false;
         bool bChEscape = false;
         Dictionary<char, uint> vCharCounts = new Dictionary<char, uint>();
         Dictionary<int, uint> vDifferences = new Dictionary<int, uint>();
         double dblEffectiveLength = 0.0;

         Debug.Assert(vPasswordChars != null); if(vPasswordChars == null) throw new ArgumentNullException("vPasswordChars");

         for(int i = 0; i < vPasswordChars.Length; i++) // Get character types
         {
            char tch = vPasswordChars[i];

            if(tch < ' ') bChEscape = true;
            if((tch >= 'A') && (tch <= 'Z')) bChUpper = true;
            if((tch >= 'a') && (tch <= 'z')) bChLower = true;
            if((tch >= '0') && (tch <= '9')) bChNumber = true;
            if((tch >= ' ') && (tch <= '/')) bChSimpleSpecial = true;
            if((tch >= ':') && (tch <= '@')) bChExtSpecial = true;
            if((tch >= '[') && (tch <= '`')) bChExtSpecial = true;
            if((tch >= '{') && (tch <= '~')) bChExtSpecial = true;
            if(tch > '~') bChHigh = true;

            double dblDiffFactor = 1.0;
            if(i >= 1)
            {
               int iDiff = (int)tch - (int)vPasswordChars[i - 1];

               if(vDifferences.ContainsKey(iDiff) == false)
                  vDifferences.Add(iDiff, 1);
               else
               {
                  vDifferences[iDiff] = vDifferences[iDiff] + 1;
                  dblDiffFactor /= (double)vDifferences[iDiff];
               }
            }

            if(vCharCounts.ContainsKey(tch) == false)
            {
               vCharCounts.Add(tch, 1);
               dblEffectiveLength += dblDiffFactor;
            }
            else
            {
               vCharCounts[tch] = vCharCounts[tch] + 1;
               dblEffectiveLength += dblDiffFactor * (1.0 / (double)vCharCounts[tch]);
            }
         }

         uint charSpace = 0;
         if(bChEscape) charSpace += (uint)CharSpaceBits.Escape;
         if(bChUpper) charSpace += (uint)CharSpaceBits.Alpha;
         if(bChLower) charSpace += (uint)CharSpaceBits.Alpha;
         if(bChNumber) charSpace += (uint)CharSpaceBits.Number;
         if(bChSimpleSpecial) charSpace += (uint)CharSpaceBits.SimpleSpecial;
         if(bChExtSpecial) charSpace += (uint)CharSpaceBits.ExtendedSpecial;
         if(bChHigh) charSpace += (uint)CharSpaceBits.High;

         if(charSpace == 0) return 0;

         double dblBitsPerChar = Math.Log((double)charSpace) / Math.Log(2.0);

         return (uint)Math.Ceiling(dblBitsPerChar * dblEffectiveLength);
      }

public static uint EstimatePasswordBits(byte[] pbUnprotectedUtf8)
      {
         if(pbUnprotectedUtf8 == null) { Debug.Assert(false); return 0; }

         UTF8Encoding utf8 = new UTF8Encoding();
         char[] vChars = utf8.GetChars(pbUnprotectedUtf8);

         uint uResult = EstimatePasswordBits(vChars);
         Array.Clear(vChars, 0, vChars.Length);

         return uResult;
      }
bis jetzt bin ich nicht weiter als was reflector ausgespuckt hat.

Delphi-Quellcode:
function QualityEstimation.EstimatePasswordBits(vPasswordChars: Char[]): Cardinal;
begin
    flag := false;
    flag2 := false;
    flag3 := false;
    flag4 := false;
    flag5 := false;
    flag6 := false;
    flag7 := false;
    dictionary := Dictionary<Char; Cardinal>.Create;
    dictionary2 := Dictionary<Integer; Cardinal>.Create;
    num := 0;
    if (vPasswordChars = nil) then
        raise ArgumentNullException.Create('vPasswordChars');
    i := 0;
    while ((i < vPasswordChars.Length)) do
    begin
        key := vPasswordChars[i];
        if (key < ' ') then
            flag7 := true;
        if ((key >= 'A') and (key <= 'Z')) then
            flag2 := true;
        if ((key >= 'a') and (key <= 'z')) then
            flag := true;
        if ((key >= '0') and (key <= '9')) then
            flag3 := true;
        if ((key >= ' ') and (key <= '/')) then
            flag4 := true;
        if ((key >= ':') and (key <= '@')) then
            flag5 := true;
        if ((key >= '[') and (key <= '`')) then
            flag5 := true;
        if ((key >= '{') and (key <= '~')) then
            flag5 := true;
        if (key > '~') then
            flag6 := true;
        num3 := 1;
        if (i >= 1) then
        begin
            num4 := (key - vPasswordChars[(i - 1)]);
            if (not dictionary2.ContainsKey(num4)) then
                dictionary2.Add(num4, 1)
            else
            begin
                inc(dictionary2.Item[num4], 1);
                num3 := (num3 div (dictionary2.Item[num4] as Double))
            end
        end;
        if (not dictionary.ContainsKey(key)) then
        begin
            dictionary.Add(key, 1);
            inc(num, num3)
        end
        else
        begin
            inc(dictionary.Item[key], 1);
            inc(num, (num3 * (1 div (dictionary.Item[key] as Double))))
        end;
        inc(i)
    end;
    num5 := 0;
    if (flag7) then
        inc(num5, 60);
    if (flag2) then
        inc(num5, $1a);
    if (flag) then
        inc(num5, $1a);
    if (flag3) then
        inc(num5, 10);
    if (flag4) then
        inc(num5, $10);
    if (flag5) then
        inc(num5, $11);
    if (flag6) then
        inc(num5, $70);
    if (num5 = 0) then
        begin
            Result := 0;
            exit
        end;
    num6 := (Math.Log((num5 as Double)) div Math.Log(2));
    begin
        Result := (Math.Ceiling(((num6 * num) as Double)) as Cardinal);
        exit
    end
end;
was sind diese dictionarys? :gruebel:

gruß,
cookie

Butch87 8. Jan 2010 06:35

Re: Von C# nach Delphi
 
Hi,
na das sieht doch schon mal ganz gut aus. Was .Net angeht ist die MSDN ne gute hilfe.
Schaust du hier Dictionary

Gruß Butche


Alle Zeitangaben in WEZ +1. Es ist jetzt 05:14 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz