1. Home
  2. Computing & Technology
  3. Delphi Programming

Computer Locker
Fancy Delphi Application Contest Entry #47 by Dagan Hoover

By Zarko Gajic, About.com Guide

Computer Locker - Fancy Delphi Application Contest Entry #47

Computer Locker - Fancy Delphi Application Contest Entry #47

What happens with your machine when you need to move away from the computer for 10 or more minutes? How secure is your data? Can someone sit on your chair while you are at a coworkers office and play with your data?

Maybe you need a computer locking tool that is small and easy to use yet powerful enough to make sure your machine is locked when you need to go out for a break.

Computer Locker

When run the "Computer Locker" will ask you for a password that is then hashed for your safety. The locker will prevent a user from executing the Task Manager. Locker will also provide a way for a user to log off, restart or shut down the machine.

From a programmer point of interest the source code includes an Md5 hashing algoritham by using API calls from the ADVAPI32.DLL.

const
  ADVAPI32 = 'advapi32.dll';
  function CryptAcquireContext(phProv: PULONG; pszContainer: PAnsiChar; pszProvider: PAnsiChar; dwProvType: DWORD; dwFlags: DWORD): BOOL; stdcall; external ADVAPI32 name 'CryptAcquireContextA';
  function CryptCreateHash(hProv: ULONG; Algid: ULONG; hKey: ULONG; dwFlags: DWORD; phHash: PULONG): BOOL; stdcall; external ADVAPI32 name 'CryptCreateHash';
  function CryptHashData(hHash: ULONG; const pbData: PBYTE; dwDataLen: DWORD; dwFlags: DWORD): BOOL; stdcall; external ADVAPI32 name 'CryptHashData';
  function CryptGetHashParam(hHash: ULONG; dwParam: DWORD; pbData: PBYTE; pdwDataLen: PDWORD; dwFlags: DWORD): BOOL; stdcall; external ADVAPI32 name 'CryptGetHashParam';
  function CryptDestroyHash(hHash: ULONG): BOOL; stdcall; external ADVAPI32 name 'CryptDestroyHash';
  function CryptReleaseContext(hProv: ULONG; dwFlags: DWORD): BOOL; stdcall; external ADVAPI32 name 'CryptReleaseContext';

function MD5(const Input: String): String;
const
  HP_HASHVAL = $0002;
  PROV_RSA_FULL = 1;
  CRYPT_VERIFYCONTEXT = $F0000000;
  CRYPT_MACHINE_KEYSET = $00000020;
  ALG_CLASS_HASH = (4 SHL 13) ;
  ALG_TYPE_ANY = 0;
  ALG_SID_MD5 = 3;
  CALG_MD5 = (ALG_CLASS_HASH Or ALG_TYPE_ANY Or ALG_SID_MD5) ;
var
  hCryptProvider : ULONG;
  hHash : ULONG;
  bHash : Array[0..$7F] Of Byte;
  dwHashLen : DWORD;
  pbContent : PByte;
  cnt : Integer;
begin
  dwHashLen := 16;
  pbContent := Pointer(PChar(Input)) ;
  Result := '';
  If CryptAcquireContext(@hCryptProvider,nil,nil,PROV_RSA_FULL,CRYPT_VERIFYCONTEXT Or CRYPT_MACHINE_KEYSET) Then
    begin
    If CryptCreateHash(hCryptProvider,CALG_MD5,0,0,@hHash) Then
      begin
      If CryptHashData(hHash,pbContent,Length(Input),0) Then
        begin
        If CryptGetHashParam(hHash,HP_HASHVAL,@bHash[0],@dwHashLen,0) Then
          begin
          For cnt := 0 To dwHashLen - 1 Do
            begin
            Result := Result + Format('%.2x',[bHash[cnt]]) ;
          end;
        end;
      end;
      CryptDestroyHash(hHash) ;
    end;
    CryptReleaseContext(hCryptProvider, 0) ;
  end;
  Result := AnsiLowerCase(Result) ;
end;

"Computer Locker" was submitted by Dagan Hoover.

Do you have a FDA(C)? Submit your Delphi code to the Fancy Delphi Application Contest.

Explore Delphi Programming
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Source Code Projects
  5. Computer Locker - Fancy Delphi Application Contest Entry #47

©2009 About.com, a part of The New York Times Company.

All rights reserved.