The Road to Delphi

Delphi – Free Pascal – Oxygene


Leave a comment

Calculate the SHA1 hash of a file, using Delphi Prism

The next code show how calculate the SHA1 hash of a file, using Delphi Prism (Oxygene)

uses
    System.IO,
    System.Security,
    System.Security.Cryptography;

function CalcSHA1HashCode(oFile:String) : String;
var
 sh1Provider : SHA1CryptoServiceProvider;
 st          : FileStream;
 hash        : Array of Byte;
begin
st := New FileStream(oFile,System.IO.FileMode.Open, System.IO.FileAccess.Read,System.IO.FileShare.ReadWrite);
try
  sh1Provider:= New SHA1CryptoServiceProvider();
  hash       := sh1Provider.ComputeHash(st);
  Result     := Convert.ToBase64String(hash);
finally
   st.Close;
end;
end;


1 Comment

Detect if an antivirus software is installed in Windows using Delphi Prism

The next code show how detect if an antivirus software is installed in Windows using Delphi Prism (Oxygene) and the WMI.

uses
System,
System.Management,
System.Text;

type
  ConsoleApp = class
    private
      class method AntivirusInstalled() : Boolean;
    public
      class method Main;
  end;

implementation

class method ConsoleApp.Main;
begin
  var returnCode : Boolean := AntivirusInstalled();
  Console.WriteLine("Antivirus Installed " + returnCode.ToString());
  Console.WriteLine();
  Console.Read();
end;

class method ConsoleApp.AntivirusInstalled() : Boolean;
begin
   var wmipathstr :string  := "\\" + Environment.MachineName + "\root\SecurityCenter";//since windows vista you must use the SecurityCenter2 namespace
    try
       var searcher  : ManagementObjectSearcher    := New ManagementObjectSearcher(wmipathstr, "SELECT * FROM AntivirusProduct");
       var instances : ManagementObjectCollection  := searcher.Get();
       result:=(instances.Count > 0);
    except on E: Exception do
      begin
        Console.WriteLine(e.Message);
        Result:=False;
      end;
    end;
end;