Official Soldat Forums

Server Talk => Scripting Releases => Topic started by: DorkeyDear on September 17, 2008, 03:50:02 pm

Title: function MD5(const Str: string): string;
Post by: DorkeyDear on September 17, 2008, 03:50:02 pm
Function Name: MD5
Script Description: Basically sense I had issues using MD5String before, this tries to get a correct value out of it
Original Author: Curt (DorkeyDear)
Core Version: 2.2 (Server v2.6.3)
Code:
Code: [Select]
function MD5(const Str: string): string;
var
  i: byte;
begin
  while (true) do begin
    Result := MD5String(Str);
    for i := 0 to 11 do begin
      if (Result <> MD5String(Str)) then begin
        i := 0;
        break;
      end;
    end;
    if (i > 0) then break;
  end;
end;
Title: Re: function MD5(const Str: string): string;
Post by: zop on September 17, 2008, 07:14:39 pm
hmm?
Code: [Select]
while (true) do begin
    Result := MD5String(Str);
    for i := 0 to 11 do begin
      if (Result <> MD5String(Str)) then begin
.........
You already give "Result := MD5String(Str)",so "if (Result <> MD5String(Str))"  will never happen right?
And then if this is what you want then
Code: [Select]
for i := 0 to 11 do begin
      if (Result <> MD5String(Str)) then begin
        i := 0;
        break;
The "i" will always 0. Is it a always loop code?
Title: Re: function MD5(const Str: string): string;
Post by: iDante on September 17, 2008, 11:36:17 pm
He's doing this because evidently MD5String doesn't always respond with the same info... could be wrong though (correct me please).
Title: Re: function MD5(const Str: string): string;
Post by: DorkeyDear on September 18, 2008, 05:34:57 am
iDante is correct, i had issues with it before, so I created this

It does 12 checks, if any of them are different than the first set value of it, it will reset the return value and start the checks over

I probably could have made it with repeat until statement to make it slightly shorter, but it doesn't matter that much.

Code: [Select]
function MD5(const Str: string): string;
var
  i: byte;
begin
  repeat
    Result := MD5String(Str);
    for i := 0 to 11 do begin
      if (Result <> MD5String(Str)) then begin
        i := 0;
        break;
      end;
    end;
  until (i > 0);
end;
but untested, don't use repeat statement often, could be wrong
actually this may also work with a while (i > 0) sense i starts at 0, but wouldn't in other languages that don't initially set the value of numbers..
Title: Re: function MD5(const Str: string): string;
Post by: danmer on September 19, 2008, 01:15:40 pm
Code: [Select]
while not regexpmatch('^[0-9a-f]{32}$',result) do
  result := md5string(str);
Title: Re: function MD5(const Str: string): string;
Post by: DorkeyDear on September 19, 2008, 09:24:30 pm
Code: [Select]
while not regexpmatch('^[0-9a-f]{32}$',result) do
  result := md5string(str);
Nice thinking, didnt even think about that... although wouldn't it be possible for the "random" error to modify one of the 32 characters to be an invalid allowed character?

but then again i guess mine isn't perfect.. if x amount of the same error occurs in a row :P then it'll recognize it as correct, heh.
Title: Re: function MD5(const Str: string): string;
Post by: danmer on September 20, 2008, 04:39:33 am
but then again i guess mine isn't perfect.. if x amount of the same error occurs in a row :P then it'll recognize it as correct, heh.
yup, you can never be sure about that. Also it seems to be rare enough to be cared about.

And the while loop with a regex can hang your server if the error occurs too much, but it'd already mean that something is really wrong with the server so it doesnt matter either ;)
Title: Re: function MD5(const Str: string): string;
Post by: Rampage_Terranius on September 22, 2008, 11:43:03 am
yeah ive had problems with md5string adding extra letters or only coming back with just half the encryption before