PascalVault / Lazarus_Hashing

Checksum & Hashing library for Lazarus
MIT License
18 stars 3 forks source link
crc crc-16 crc-32 crc-8 crc-algorithms crc-calculation crc16 crc32 crc64 crc8 hash hashing hashing-algorithm hashing-library md5 md5-hash md5hashing sha1 sha256

Checksum & Hashing library for Lazarus

The goal is the create a library which is modular, fast and portable. There is no platform-specific code, no Assembly, no includes, no conditinal defines. Every algorith has it's own separate file and if you need just one algorithm in your program you can just copy 2 files- the file with the algorithm you need and "HasherBase.pas". Optionally you can copy "Hasher.pas" but that's just a helper class.

Available algorithms

More soon...

PHP port available

https://github.com/PascalVault/PHP_Hashing

Usage examples

hashing a String

uses Hasher;

var Hasher: THasher;
    Hash: String;
begin
  try
    Hasher := THasher.Create('CRC-32 JAMCRC');
    Hasher.Update('123456789');
    Hash := Hasher.Final;
    Hasher.Free;

    Memo1.Lines.Add( Hash );
  finally
  end; 

hashing a Stream

uses Hasher;

var Hasher: THasher;
    Hash: String;
    Msg: TMemoryStream;
begin
  try
    Msg := TMemoryStream.Create;
    Hasher := THasher.Create('CRC-32 JAMCRC');
    Hasher.Update(Msg);
    Hash := Hasher.Final;
    Hasher.Free;

    Memo1.Lines.Add( Hash );
  finally
    Msg.Free;
  end; 

hashing a file

 uses Hasher;

var Hasher: THasher;
    Hash: String;
begin
  try       
    Hasher := THasher.Create('CRC-32 JAMCRC');
    Hasher.UpdateFile('directory/file.exe');
    Hash := Hasher.Final;
    Hasher.Free;

    Memo1.Lines.Add( Hash );
  finally
  end; 

Using classes directly- without THasher

hashing a String

uses CRC64;

var Hasher: THasherCRC64;
    Hash: String;
    Msg: String;
begin
  Msg := '123456789';
  Hasher := THasherCRC64.Create;
  Hasher.Update(@Msg[1], Length(Msg));
  Hash := Hasher.Final;
  Hasher.Free;

  Memo1.Lines.Add( Hash );
end;

hashing an Array

uses CRC64;

var Hasher: THasherCRC64;
    Hash: String;
    Msg: array of Byte;
    Len: Integer;
begin
  SetLength(Msg, Len);
  Hasher := THasherCRC64.Create;
  Hasher.Update(@Msg[0], Len);
  Hash := Hasher.Final;
  Hasher.Free;

  Memo1.Lines.Add( Hash );
end;