AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

Von C# nach Delphi

Ein Thema von cookie22 · begonnen am 7. Jan 2010 · letzter Beitrag vom 8. Jan 2010
Antwort Antwort
Benutzerbild von cookie22
cookie22

Registriert seit: 28. Jun 2006
Ort: Düsseldorf
936 Beiträge
 
Delphi XE2 Professional
 
#1

Von C# nach Delphi

  Alt 7. Jan 2010, 23:28
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?

gruß,
cookie
  Mit Zitat antworten Zitat
Butch87

Registriert seit: 3. Mai 2007
70 Beiträge
 
#2

Re: Von C# nach Delphi

  Alt 8. Jan 2010, 06:35
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
  Mit Zitat antworten Zitat
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 07:50 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