Advertisement
filhotecmail

Algoritmo Md5

Oct 24th, 2017
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 7.32 KB | None | 0 0
  1. unit mymd5unt;
  2.  
  3. interface
  4. function MyMD5(const InputString: UTF8String): UTF8String;
  5. function MD5Crypt(const Password: UnicodeString): UnicodeString; overload;
  6. function MD5Crypt(const Password: UnicodeString; Salt: UTF8String): UnicodeString; overload;
  7.  
  8. implementation
  9.  
  10. uses
  11.   SysUtils, ComObj, ActiveX, IdGlobal, IdHash, IdHashMessageDigest, IdCoderMIME;
  12.  
  13. const
  14.   L = UnicodeString('');
  15.   // to make strings Unicode
  16.   // in C++: L"abcd"
  17.   // in Delphi: L+'abcd'
  18.   // otherwise 'nonunicode literal' + UnicodeFunction(Arg1, Arg2) becomes
  19.   // converted to screwed ANSI
  20.  
  21. function RandomString: UnicodeString;
  22. var
  23.   GUID: TGUID;
  24. begin
  25.   OleCheck(CreateGUID(GUID));
  26.   Result := Copy(GUIDToString(GUID), 2, 36);
  27. end;
  28.  
  29. const
  30.   MD5CryptAlphabet : UnicodeString =
  31.     UnicodeString('./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz');
  32.  
  33. function MyMD5(const InputString: UTF8String): UTF8String;
  34. var
  35.   Input, Output: TIdBytes;
  36.   X_Hash_Alg: TIdHash;
  37. begin
  38.   SetLength(Input, Length(InputString));
  39.   Move(InputString[1], Input[0], Length(InputString));
  40.   X_Hash_Alg := nil;
  41.   try
  42.     X_Hash_Alg := TIdHashMessageDigest5.Create;
  43.     Output := X_Hash_Alg.HashBytes(Input);
  44.     SetString(Result, PAnsiChar(@Output[0]), Length(Output));
  45.   finally
  46.     FreeAndNil(X_Hash_Alg);
  47.   end;
  48. end;
  49.  
  50. function MD5Crypt(const Password: UnicodeString): UnicodeString; overload;
  51. var
  52.   Salt: UTF8String;
  53.   PasswordUTF8: UTF8String;
  54.   Len: Integer;
  55.   I: Integer;
  56.   FinalString: UTF8String;
  57.   Final: TIdBytes;
  58.   CTXString: UTF8String;
  59.   CTX1String: UTF8String;
  60.   procedure To64(Value, Len: Integer);
  61.   var
  62.     J: Integer;
  63.   begin
  64.     for J := 1 to Len do
  65.     begin
  66.       Result := Result + MD5CryptAlphabet[Value and $3f + 1];
  67.       Value := Value shr 6;
  68.     end;
  69.   end;
  70. begin
  71.   Salt := Copy(UTF8Encode(RandomString), 1, 8);
  72.   //Salt := 'GzQ4OT9U';
  73.   PasswordUTF8 := UTF8Encode(Password);
  74.  
  75.   CTXString := PasswordUTF8 + UTF8String('$1$') + Salt;
  76.  
  77.   FinalString := MyMD5(PasswordUTF8 + Salt + PasswordUTF8);
  78.  
  79.   // Add as many characters of Final to CTX
  80.   Len := Length(PasswordUTF8);
  81.   while Len > 0 do
  82.   begin
  83.     if Len >= Length(FinalString) then
  84.     begin
  85.       CTXString := CTXString + FinalString;
  86.       Dec(Len, Length(FinalString));
  87.     end
  88.     else
  89.     begin
  90.       CTXString := CTXString + Copy(FinalString, 1, Len);
  91.       Len := 0;
  92.     end;
  93.   end;
  94.  
  95.   // Then something really weird...
  96.   I := Length(PasswordUTF8);
  97.   while I > 0 do
  98.   begin
  99.     if I and 1 = 1 then
  100.     begin
  101.       CTXString := CTXString + UTF8String(#0);
  102.     end
  103.     else
  104.     begin
  105.       CTXString := CTXString + PasswordUTF8[1];
  106.     end;
  107.     I := I shr 1;
  108.   end;
  109.  
  110.   FinalString := MyMD5(CTXString);
  111.  
  112.   // Do additional mutations
  113.   for I := 0 to 999 do
  114.   begin
  115.     CTX1String := '';
  116.     if I and 1 > 0 then
  117.     begin
  118.       CTX1String := CTX1String + PasswordUTF8;
  119.     end
  120.     else
  121.     begin
  122.       CTX1String := CTX1String + FinalString;
  123.     end;
  124.  
  125.     if I mod 3 > 0 then
  126.     begin
  127.       CTX1String := CTX1String + Salt;
  128.     end;
  129.  
  130.     if I mod 7 > 0 then
  131.     begin
  132.       CTX1String := CTX1String + PasswordUTF8;
  133.     end;
  134.  
  135.     if I and 1 > 0 then
  136.     begin
  137.       CTX1String := CTX1String + FinalString;
  138.     end
  139.     else
  140.     begin
  141.       CTX1String := CTX1String + PasswordUTF8;
  142.     end;
  143.  
  144.     FinalString := MyMD5(CTX1String);
  145.   end;
  146.  
  147.   Result := L+'$1$' + UTF8ToUnicodeString(Salt) + '$';
  148.  
  149.   SetLength(Final, Length(FinalString));
  150.   Move(FinalString[1], Final[0], Length(FinalString));
  151.   To64(((Integer(Final[ 0]) and $FF) shl 16) or ((Integer(Final[ 6]) and $FF) shl 8) or (Integer(Final[12]) and $FF), 4);
  152.   To64(((Integer(Final[ 1]) and $FF) shl 16) or ((Integer(Final[ 7]) and $FF) shl 8) or (Integer(Final[13]) and $FF), 4);
  153.   To64(((Integer(Final[ 2]) and $FF) shl 16) or ((Integer(Final[ 8]) and $FF) shl 8) or (Integer(Final[14]) and $FF), 4);
  154.   To64(((Integer(Final[ 3]) and $FF) shl 16) or ((Integer(Final[ 9]) and $FF) shl 8) or (Integer(Final[15]) and $FF), 4);
  155.   To64(((Integer(Final[ 4]) and $FF) shl 16) or ((Integer(Final[10]) and $FF) shl 8) or (Integer(Final[ 5]) and $FF), 4);
  156.   To64(                                           Integer(Final[11]) and $FF                                        , 2);
  157. end;
  158.  
  159. function MD5Crypt(const Password: UnicodeString; Salt: UTF8String): UnicodeString; overload;
  160. var
  161.   PasswordUTF8: UTF8String;
  162.   Len: Integer;
  163.   I: Integer;
  164.   FinalString: UTF8String;
  165.   Final: TIdBytes;
  166.   CTXString: UTF8String;
  167.   CTX1String: UTF8String;
  168.   procedure To64(Value, Len: Integer);
  169.   var
  170.     J: Integer;
  171.   begin
  172.     for J := 1 to Len do
  173.     begin
  174.       Result := Result + MD5CryptAlphabet[Value and $3f + 1];
  175.       Value := Value shr 6;
  176.     end;
  177.   end;
  178. begin
  179.   PasswordUTF8 := UTF8Encode(Password);
  180.  
  181.   CTXString := PasswordUTF8 + UTF8String('$1$') + Salt;
  182.  
  183.   FinalString := MyMD5(PasswordUTF8 + Salt + PasswordUTF8);
  184.  
  185.   // Add as many characters of Final to CTX
  186.   Len := Length(PasswordUTF8);
  187.   while Len > 0 do
  188.   begin
  189.     if Len >= Length(FinalString) then
  190.     begin
  191.       CTXString := CTXString + FinalString;
  192.       Dec(Len, Length(FinalString));
  193.     end
  194.     else
  195.     begin
  196.       CTXString := CTXString + Copy(FinalString, 1, Len);
  197.       Len := 0;
  198.     end;
  199.   end;
  200.  
  201.   // Then something really weird...
  202.   I := Length(PasswordUTF8);
  203.   while I > 0 do
  204.   begin
  205.     if I and 1 = 1 then
  206.     begin
  207.       CTXString := CTXString + UTF8String(#0);
  208.     end
  209.     else
  210.     begin
  211.       CTXString := CTXString + PasswordUTF8[1];
  212.     end;
  213.     I := I shr 1;
  214.   end;
  215.  
  216.   FinalString := MyMD5(CTXString);
  217.  
  218.   // Do additional mutations
  219.   for I := 0 to 999 do
  220.   begin
  221.     CTX1String := '';
  222.     if I and 1 > 0 then
  223.     begin
  224.       CTX1String := CTX1String + PasswordUTF8;
  225.     end
  226.     else
  227.     begin
  228.       CTX1String := CTX1String + FinalString;
  229.     end;
  230.  
  231.     if I mod 3 > 0 then
  232.     begin
  233.       CTX1String := CTX1String + Salt;
  234.     end;
  235.  
  236.     if I mod 7 > 0 then
  237.     begin
  238.       CTX1String := CTX1String + PasswordUTF8;
  239.     end;
  240.  
  241.     if I and 1 > 0 then
  242.     begin
  243.       CTX1String := CTX1String + FinalString;
  244.     end
  245.     else
  246.     begin
  247.       CTX1String := CTX1String + PasswordUTF8;
  248.     end;
  249.  
  250.     FinalString := MyMD5(CTX1String);
  251.   end;
  252.  
  253.   Result := L+'$1$' + UTF8ToUnicodeString(Salt) + '$';
  254.  
  255.   SetLength(Final, Length(FinalString));
  256.   Move(FinalString[1], Final[0], Length(FinalString));
  257.   To64(((Integer(Final[ 0]) and $FF) shl 16) or ((Integer(Final[ 6]) and $FF) shl 8) or (Integer(Final[12]) and $FF), 4);
  258.   To64(((Integer(Final[ 1]) and $FF) shl 16) or ((Integer(Final[ 7]) and $FF) shl 8) or (Integer(Final[13]) and $FF), 4);
  259.   To64(((Integer(Final[ 2]) and $FF) shl 16) or ((Integer(Final[ 8]) and $FF) shl 8) or (Integer(Final[14]) and $FF), 4);
  260.   To64(((Integer(Final[ 3]) and $FF) shl 16) or ((Integer(Final[ 9]) and $FF) shl 8) or (Integer(Final[15]) and $FF), 4);
  261.   To64(((Integer(Final[ 4]) and $FF) shl 16) or ((Integer(Final[10]) and $FF) shl 8) or (Integer(Final[ 5]) and $FF), 4);
  262.   To64(                                           Integer(Final[11]) and $FF                                        , 2);
  263. end;
  264.  
  265.  
  266. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement