unit MainU;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, XPMan;

type
  TfrmMain = class(TForm)
    lblName: TLabel;
    lblKey: TLabel;
    edtName: TEdit;
    edtKey: TEdit;
    btnGenerate: TButton;
    btnExit: TButton;
    XPManifest1: TXPManifest;
    procedure btnExitClick(Sender: TObject);
    procedure btnGenerateClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmMain: TfrmMain;

implementation

{$R *.dfm}

function RandomPassword(PLen: Integer): string;   
var
 str: string;
begin
  Randomize;
  str    := '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
   Result := '';
   repeat
     Result := Result + str[Random(Length(str)) + 1];
   until (Length(Result) = PLen)
end;

function CalcHash(Data : pointer; DataSize : integer) : integer; register;
asm  
  push ebx   
  push esi   
  push edi   
  mov esi, Data   
  xor ebx, ebx   
  or esi, esi   
  jz @@Exit   
  mov edx, DataSize   
  or edx,edx   
  jz @@Exit   
  xor ecx,ecx   
    
@@Cycle:   
  xor eax,eax   
  mov al,[esi]   
  inc esi   
  rol eax,cl   
  xor ebx,eax   
  inc ecx   
  dec edx   
  jnz @@Cycle   
    
@@Exit:   
  mov eax,ebx   
  pop edi   
  pop esi   
  pop ebx   
end;

procedure TfrmMain.btnExitClick(Sender: TObject);
begin
  Close;
end;

procedure TfrmMain.btnGenerateClick(Sender: TObject);
var
  TempCode: array [0..4] of string;
  Hash: String;
begin
  FillChar(TempCode, SizeOf(TempCode),0);
  if Length(edtName.Text) >= 4 then
  begin
    TempCode[0] := RandomPassword(2) + edtName.Text[1] + RandomPassword(1)+ '-';
    TempCode[1] := RandomPassword(2) + edtName.Text[3] + RandomPassword(1)+ '-';
    TempCode[2] := IntToStr(CalcHash(pointer(edtName.Text), Length(edtName.Text))) + '-';
    TempCode[3] := RandomPassword(4) + '-';
    Hash := UpperCase(TempCode[0]+TempCode[1]+TempCode[2]+TempCode[3]);
    TempCode[4] := IntToStr(CalcHash(pointer(Hash), Length(Hash)));
    edtKey.Text := UpperCase(TempCode[0]+TempCode[1]+TempCode[2]+TempCode[3]+ TempCode[4]);
  end
  else begin
    MessageBox(Handle, 'Enter minimum 4 characters!', 'Error', MB_OK+MB_ICONERROR);
    edtName.SetFocus;
  end;
end;

end.
