Registriert seit: 28. Jun 2006
Ort: Düsseldorf
936 Beiträge
Delphi XE2 Professional
|
Re: [Delphi 2010] Interner Fehler
9. Jan 2010, 13:16
also ich mache das folgendermassen.
hier das original in c#:
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);
}
das hab ich mir mit hilfe von refector daraus zusammen gereimt. meine c# kenntnisse sind = 0, das muss ich dazu sagen.
Delphi-Quellcode:
function EstimatePasswordBits(vPasswordChars: string): Cardinal;
var
flag, flag2, flag3, flag4, flag5, flag6, flag7: Boolean;
dictionary: TDictionary<Char, Cardinal>;
dictionary2: TDictionary<Integer, Cardinal>;
i, num, num3, num4, num5 : Integer;
num6 : Extended;
key: Char;
begin
flag := False;
flag2 := False;
flag3 := False;
flag4 := False;
flag5 := False;
flag6 := False;
flag7 := False;
num := 0;
dictionary := TDictionary<Char, Cardinal>.Create;
dictionary2 := TDictionary<Integer, Cardinal>.Create;
if (vPasswordChars = '') then
raise ArgumentNullException.Create('vPasswordChars');
i := 0;
while i < Length(vPasswordChars) 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 := (Ord(key) - Ord(vPasswordChars[i - 1]));
if (not dictionary2.ContainsKey(num4)) then
dictionary2.Add(num4, 1)
else
begin
dictionary2.Items[num4] := dictionary2.Items[num4] + 1;
num3 := num3 div (dictionary2.Items[num4])
end
end;
if (not dictionary.ContainsKey(key)) then
begin
dictionary.Add(key, 1);
inc(num, num3)
end
else
begin
dictionary.Items[key] := dictionary.Items[key] +1;
inc(num, (num3 * (1 div (dictionary.Items[key] ))))
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 := round(Math.LogN(1, (num5))) / round(Math.LogN(1, 2));
begin
Result := Math.Ceil(num6 * num);
exit
end
end;
Delphi-Quellcode:
...
var
s: string;
bits: cardinal;
begin
...
bits := EstimatePasswordBits(s);
...
end;
wie gesagt compilieren lässt sich das, nur nicht aufrufen.
|