Coder Social home page Coder Social logo

xor-el / hashlib4pascal Goto Github PK

View Code? Open in Web Editor NEW
215.0 38.0 77.0 2.46 MB

Hashing for Modern Object Pascal

License: MIT License

Pascal 99.39% PHP 0.23% Python 0.37%
delphi fpc checksum stream encoding byte-array computing-hashes untyped-data hash pascal

hashlib4pascal's Introduction

HashLib4Pascal: Hashing for Modern Object Pascal License

HashLib4Pascal is an Object Pascal hashing library released under the permissive MIT License which provides an easy to use interface for computing hashes and checksums of data. It also supports state based (incremental) hashing.

HashLib4Pascal's goal is to be the best option for hashing in Object Pascal by offering various hashing primitives via an easy to use API to Object Pascal developers.

Development is coordinated on GitHub and contributions are welcome. If you need help, please open an issue here.

Build Status Build Status

Available Algorithms

Hashes


Cyclic Redundancy Checks
  • All CRC Variants from CRC3 to CRC64
Checksums
  • Adler32
Non-Cryptographic Hash Functions

32 bit hashes
  • AP BKDR Bernstein Bernstein1 DEK DJB ELF FNV

  • FNV1a JS Jenkins3 Murmur2 MurmurHash3_x86_32 OneAtTime

  • PJW RS Rotating SDBM ShiftAndXor SuperFast XXHash32

64 bit hashes
  • FNV64 FNV1a64 Murmur2_64 SipHash2_4 XXHash64
128 bit hashes
  • MurmurHash3_x86_128 MurmurHash3_x64_128
Cryptographic Hash Functions

  • MD2

  • MD4

  • MD5

  • SHA-0

  • SHA-1

  • SHA-2 (224, 256, 384, 512, 512-224, 512-256)

  • GOST 34.11-94

  • GOST R 34.11-2012 (AKA Streebog) (256, 512)

  • Grindahl (256, 512)

  • Has160

  • RIPEMD (128, 160, 256, 256, 320)

  • Tiger (128, 160, 192 (Rounds 3, 4, 5))

  • Tiger2 (128, 160, 192 (Rounds 3, 4, 5))

  • Snefru (128, 256)

  • Haval (128, 160, 192, 224, 256 (Rounds 3, 4, 5))

  • Panama

  • RadioGatun (RadioGatun32, RadioGatun64)

  • WhirlPool

  • Blake2B (160, 256, 384, 512)

  • Blake2S (128, 160, 224, 256)

  • SHA-3 (224, 256, 384, 512)

  • Keccak (224, 256, 288, 384, 512)

  • Blake2BP

  • Blake2SP

  • Blake3

Key Derivation Functions


Password Hashing Schemes (Password Based Key Derivation Functions)

  • PBKDF2

  • Argon2 (2i, 2d and 2id variants)

  • Scrypt

MAC


  • HMAC (all supported hashes)

  • KMAC (KMAC128, KMAC256)

  • Blake2MAC (Blake2BMAC, Blake2SMAC)

XOF (Extendable Output Function)


  • Shake (Shake-128, Shake-256)

  • CShake (CShake-128, CShake-256)

  • Blake2X (Blake2XS, Blake2XB)

  • KMACXOF (KMAC128XOF, KMAC256XOF)

  • Blake3XOF

Supported Compilers


  • FreePascal 3.0.0+

  • Delphi 2010+

Other Implementations


If you want implementations in other languages, you can check out these

Tip Jar


  • 💵 Bitcoin: 1MhFfW7tDuEHQSgie65uJcAfJgCNchGeKf
  • 💶 Ethereum: 0x6c1DC21aeC49A822A4f1E3bf07c623C2C1978a98
  • 💷 Pascalcoin: 345367-40

hashlib4pascal's People

Contributors

emmanuelezenwere avatar maelh avatar xor-el avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

hashlib4pascal's Issues

Quick Start and Document needed

Is your feature request related to a problem? Please describe.
I cannot quickly know how to use this library.

Describe the solution you'd like
Beg a wiki or a quick start document.

Describe alternatives you've considered

Additional context

Custom Stop processing

Hi,
Is there feature to stop hashing process if user wants to abort process on long files ?

Processing data in chunks (Are my assumptions correct)?

I use code of the form below:

  FHasher := THashFactory.TCrypto.CreateSHA3_256();
  FHasher.Initialize;

  // repeat for each block of data (P^) where Count may vary
  FHasher.TransformUntyped(P^, Count);

  FHashResult := FHasher.TransformFinal;

Now I have several questions:

  • Is it necessary to inform the hasher/hashing object what block size it will receive at initialization? Does it expect a fixed size, or something like that? I noticed the properties BlockSize and BufferSize. Do I need to consider them somehow?
  • What if I don't call TransformFinal because the hash computation was stopped by the user. Is it enough to call FHasher.Initialize next time when computing another hash, or do I need to free and create FHasher again first?
  • Besides of these questions, anything else I would need to consider such that the code works for any hasher?

FPC 3.3.1: Doesn't compile

Describe the bug
When selecting [Package] ->[Open Package File (.lpk) ...] -> Select 'HashLib4PascalPackage.lpk' and click [Compile] I get the errors:

Compile package HashLib4PascalPackage 3.0: Exit code 1, Errors: 8
HlpAdler32.pas(16,14) Error: No matching implementation for interface method "GetBlockSize:LongInt;" found
HlpAdler32.pas(16,14) Error: No matching implementation for interface method "GetHashSize:LongInt;" found
HlpAdler32.pas(16,14) Error: No matching implementation for interface method "GetBufferSize:LongInt;" found
HlpAdler32.pas(16,14) Error: No matching implementation for interface method "SetBufferSize(LongInt);" found
HlpAdler32.pas(16,14) Error: No matching implementation for interface method "GetBlockSize:LongInt;" found
HlpAdler32.pas(16,14) Error: No matching implementation for interface method "GetHashSize:LongInt;" found
HlpAdler32.pas(16,14) Error: No matching implementation for interface method "GetBufferSize:LongInt;" found
HlpAdler32.pas(16,14) Error: No matching implementation for interface method "SetBufferSize(LongInt);" found

My Version:

Version #: 2.1.0
FPC Version: 3.3.1
SVN Revision 59976
x86_64-win64-win32/win64
Windows 10

Am I doing something wrong?

A little help needed please

HashLib4Pascal looks amazing, but without any help documentation I'm really struggling to work out how to use it.

I need to generate a 64-character Blake2SP checksum for any given file. This will match the Blake2 checksums created by 7z and Winrar.

Could you provide a small sample of code for how to do this please.

I have Delphi 10.3

Many thanks!

Edit: I managed to work it out from a previous reply:
MyResult := THashFactory.TCrypto.CreateBlake2SP(32, nil).ComputeFile('C:\somefile.zip').ToString();

Help for Key Derivation Functions "PBKDF2" in delphi

Hi,

I'm having trouble using your library. Can you help me?

I'd like create a key Derivation Functions "PBKDF2" like I do in .net core.

This is what I do in .net core

public static string HashUsingPbkdf2(string password, byte[] salt)
{
  byte[] derivedKey = KeyDerivation.Pbkdf2(password, salt, KeyDerivationPrf.HMACSHA256, iterationCount: 100000, 32);
  
  return Convert.ToBase64String(derivedKey);
 }

The "salt" is generated by RandomNumberGenerator.GetBytes(32);
And is saved in the db in Base64 : Convert.ToBase64String(salt).
When I need to retrieve it from the db and convert it to "byte[]", I use Convert.FromBase64String(StringSalt).

This is what I do with the library :

function TForm1.PBKDF2(Password: string; Salt: string): string;
var
  PBKDF2 : IPBKDF2_HMAC;
  PassBytes, SaltBytes, ResultBytes: TBytes;
  HashInstance : IHash;
  HMACInstance: IHMAC;
begin

  PassBytes := TBase64Encoding.Base64.DecodeStringToBytes(Password);
  SaltBytes := TBase64Encoding.Base64.DecodeStringToBytes(Salt);

  HashInstance := THashFactory.TCrypto.CreateSHA2_256();
  HMACInstance := THashFactory.THMAC.CreateHMAC(HashInstance);

  PBKDF2 := TKDF.TPBKDF2_HMAC.CreatePBKDF2_HMAC(HMACInstance, PassBytes, SaltBytes, 100000);

  ResultBytes := PBKDF2.GetBytes(32);

  Result := BytesToBase64String(ResultBytes);
end;

But when I try to do it with the same "password" and "salt" I don't get the same result.

Can you show me how to do it in Delphi?

Thank you in advance for your support

Help needed trying to figure hash method

Hello,

First of all thank you again for making this library MIT license and thank you to all who supported that.

I am provided a documentation for TLV communication with a device. There is no specific standard clearly indicated in the document. It says "CRC calculation". I assumed that is CRC16 since size in communication is 2 bytes only.

There is an example application provided. No SDK or code examples from the seller of the device, I am alone to solve this one.

Below is the data sent to the device by sample application. 02 at the beginning and 03 at the end should not be included in CRC calculation and F39A is the calculated CRC.
02 0017544553543030303032363036FF8A7507DF820803000001 F39A 03

I have following code that tries to calculate all supported CRC16 values for
0017544553543030303032363036FF8A7507DF820803000001 value

var
  LCRC: IHash;
  Bytes: TBytes;
  CRC: TBytes;
  I: Integer;
begin
  Bytes := string(Edit1.Text).StrToBytes(); // my own helper function

  for I := 49 to 77 do // loop all CRC16 standards
  begin
    LCRC := THashFactory.TChecksum.TCRC.CreateCRC(TCRCStandard(I));
    CRC := (LCRC as ICRC).ComputeBytes(Bytes).GetBytes;
    Memo1.Lines.Add(CRC.ToHex() + ' ' + LCRC.Name);
    CRC := [];
    LCRC := nil;
  end;
end;

My problem is output always adds to previous result as below

540D TCRC-16/AUG-CCITT
540D8FFD TCRC-16/BUYPASS
540D8FFDAA9F TCRC-16/CCITT-FALSE
540D8FFDAA9FA7CB TCRC-16/CDMA2000
540D8FFDAA9FA7CBE13A TCRC-16/CMS
540D8FFDAA9FA7CBE13A696A TCRC-16/DDS-110
540D8FFDAA9FA7CBE13A696A712C TCRC-16/DECT-R
540D8FFDAA9FA7CBE13A696A712C712D TCRC-16/DECT-X
540D8FFDAA9FA7CBE13A696A712C712DB27E TCRC-16/DNP
540D8FFDAA9FA7CBE13A696A712C712DB27ED177 TCRC-16/EN13757
540D8FFDAA9FA7CBE13A696A712C712DB27ED1775560 TCRC-16/GENIBUS
540D8FFDAA9FA7CBE13A696A712C712DB27ED1775560D3A1 TCRC-16/GSM
540D8FFDAA9FA7CBE13A696A712C712DB27ED1775560D3A192F8 TCRC-16/LJ1200
540D8FFDAA9FA7CBE13A696A712C712DB27ED1775560D3A192F8B33E TCRC-16/MAXIM
540D8FFDAA9FA7CBE13A696A712C712DB27ED1775560D3A192F8B33E6254 TCRC-16/MCRF4XX
540D8FFDAA9FA7CBE13A696A712C712DB27ED1775560D3A192F8B33E62545BC3 TCRC-16/OPENSAFETY-A
540D8FFDAA9FA7CBE13A696A712C712DB27ED1775560D3A192F8B33E62545BC38860 TCRC-16/OPENSAFETY-B
540D8FFDAA9FA7CBE13A696A712C712DB27ED1775560D3A192F8B33E62545BC3886051DA TCRC-16/PROFIBUS
540D8FFDAA9FA7CBE13A696A712C712DB27ED1775560D3A192F8B33E62545BC3886051DA038D TCRC-16/RIELLO
540D8FFDAA9FA7CBE13A696A712C712DB27ED1775560D3A192F8B33E62545BC3886051DA038D67EF TCRC-16/T10-DIF
540D8FFDAA9FA7CBE13A696A712C712DB27ED1775560D3A192F8B33E62545BC3886051DA038D67EF5BA8 TCRC-16/TELEDISK
540D8FFDAA9FA7CBE13A696A712C712DB27ED1775560D3A192F8B33E62545BC3886051DA038D67EF5BA8ABBF TCRC-16/TMS37157
540D8FFDAA9FA7CBE13A696A712C712DB27ED1775560D3A192F8B33E62545BC3886051DA038D67EF5BA8ABBF5048 TCRC-16/USB
540D8FFDAA9FA7CBE13A696A712C712DB27ED1775560D3A192F8B33E62545BC3886051DA038D67EF5BA8ABBF5048C988 TCRC-A
540D8FFDAA9FA7CBE13A696A712C712DB27ED1775560D3A192F8B33E62545BC3886051DA038D67EF5BA8ABBF5048C988E135 TKERMIT
540D8FFDAA9FA7CBE13A696A712C712DB27ED1775560D3A192F8B33E62545BC3886051DA038D67EF5BA8ABBF5048C988E135AFB7 TMODBUS
540D8FFDAA9FA7CBE13A696A712C712DB27ED1775560D3A192F8B33E62545BC3886051DA038D67EF5BA8ABBF5048C988E135AFB79DAB TX-25
540D8FFDAA9FA7CBE13A696A712C712DB27ED1775560D3A192F8B33E62545BC3886051DA038D67EF5BA8ABBF5048C988E135AFB79DAB2C5E TXMODEM
540D8FFDAA9FA7CBE13A696A712C712DB27ED1775560D3A192F8B33E62545BC3886051DA038D67EF5BA8ABBF5048C988E135AFB79DAB2C5E2CB1 TCRC-16/NRSC-5

I just cannot be sure that the calculation actually works fresh for each loop.

How can I clear buffer/data in the loop?

Thanks & Regards,
Ertan

Resourcestrings are in title caps

Several resourcestrings are in title caps, eventhough they are error messages and not meant to be shown in a caption/title.
Some would also benefit from added parameters instead of saying "current index" or "current datastructure".

Adding xxHash XXH128 (SSE2) and\or XXH3 (SSE2)

Describe the solution you'd like
xxHash, which I implement in the form of the standard 32 and 64 bit offerings, is super. It would be extra super if the newer variants could be added to the library. Namely, the super fast additions such as XXH128 (SSE2) and\or XXH3 (SSE2)

https://cyan4973.github.io/xxHash/

Thanks as ever for the continued amazing library.

Great library but Blake2SP is very slow

As I've said elsewhere, HashLib4Pascal is great but I don't use it to generate hash codes because my desired hash, Blake2SP, is very slow.

I actually shell-out to run 7zip.exe in a command line, copy the output to the clipboard, and then examine the clipboard to get the Blake2SP hash code for any given file. This is significantly quicker than using HashLib4Pascal. This is a shame as the following code from my program could be reduced to a single statement if HashLib4Pascal could match the speed. It's not even close, unfortunately, particularly on large files of 1GB or more.

function Blake2sp(AFilePath: string): string;
  var
    vSL: TStringList;
    StartInfo: TStartupInfo;
    ProcInfo: TProcessInformation;
    CreateOK: boolean;
    cmd: string;
    params: string;
    posSpace: integer;
  begin
    result := '';
    FillChar(StartInfo, SizeOf(TStartupInfo), #0);
    FillChar(ProcInfo, SizeOf(TProcessInformation), #0);
    StartInfo.cb := SizeOf(TStartupInfo);
    StartInfo.wShowWindow := SW_HIDE;
    StartInfo.dwFlags := STARTF_USESHOWWINDOW;

    cmd := 'c:\windows\system32\cmd.exe';
    params := '/c B:\Tools\7-Zip\7z.exe h -scrcBLAKE2sp "' + AFilePath  + '" | clip';

    CreateOk := CreateProcess(PWideChar(cmd), PWideChar(params), nil, nil, FALSE,
      CREATE_NEW_PROCESS_GROUP + NORMAL_PRIORITY_CLASS, nil, StrScratchFolder, StartInfo, ProcInfo);

    case CreateOK of TRUE: WaitForSingleObject(ProcInfo.hProcess, INFINITE); end;

    vSL := TStringList.Create;
    try
      vSL.Text := Clipboard.AsText;
      case vSL.Count > 7 of TRUE: begin
        result := LowerCase(vSL[8]);
        posSpace := pos(' ', result);
        SetLength(result, posSpace - 1);
      end; end;
    finally
      vSL.Free;
    end;
  end;

If HashLib4Pascal/Blake2SP could be made lightning fast, as it is in 7zip, I would prefer to be using HashLib4Pascal.

Regards,
Baz.

Line 156 of HlpHashResult - Missing ConvertBytesToString

Describe the bug
When installing the latest (as of 18/07/19) Freepascal package into Lazarus v2.0.2 (FPC 3.0.4) the compiler complains about line 156 of HlpHashResult in src\Base\HlpHashResult.pas stating : HlpHashResult.pas(156,18) Error: identifier idents no member "ConvertBytesToString"

On examination of TConverters, there is no ConvertBytesToString function or procedure, but there is "ConvertBytesToHexString" and "ConvertHexStringToBytes", but the parameters they expect also differ, requiring a Boolean value.

To Reproduce
Steps to reproduce the behavior:

  1. Download the latest code of master branch.
  2. Launch Lazarus v2.0.2 and use the menu to select Package --> Open Package File
  3. Select HashLib4Pascal\HashLib\src\Packages\FPC\HashLib4PascalPackage.lpk
  4. Click Compile. The error message appears.
    Untitled

Expected behavior
Successful compilation and then installation of the package into Lazarus.

Screenshots
If applicable, add screenshots to help explain your problem.

Environment (please complete the following information):

  • OS: Windows 10 64-bit
  • Freepascal v3.0.4
  • Package Version : Current master

Example needed

Hi!

I want a simple thing: to encode a string SHA3-512. Can you help me? Which unit, whose function knows this?
Thanks in advance!

SHA-256 With RSA and MGF1

I have a X509Certificate and I need to sign a token using the private key of the certificate. The signing algorithm requested is SHA-256 with RSA and MGF1. Is it possible to do that with HashLib4Pascal?

Encoding problems

Hi,

I tried to verify a Hash wich was made with the php_hash function. In the german language there are special characters. I used the encoding UTF8 and unicode, but the result is with asian characters.

The version I used was downloaded in September. The Delphi version is 10.4.2 with all patches applied.

procedure verify;
var
LGenerator: IPBKDF_Argon2;

mRes,
LActual,
FAdditional, FSecret, FSalt, FPassword: String;

LAdditional, LSecret, LSalt, LPassword: TBytes;

LArgon2Parameter: IArgon2Parameters;

Argon2ParametersBuilder: IArgon2ParametersBuilder;

begin
(*
php Result
"Knüllerß#" : $argon2i$v=19$m=65536,t=4,p=1$T2hqaURkbUI2SVVkeTZqaw$eT3Cr9d/PRpLS9aQViuSt0NYSgUjMzFJvnR8aWEC6T8
*)

FAdditional := '';
FSecret := '';
FSalt := 'T2hqaURkbUI2SVVkeTZqaw';
FPassword := 'Knüllerß#';

Argon2Version := TArgon2Version.a2vARGON2_VERSION_13;
Argon2ParametersBuilder := TArgon2dParametersBuilder.Builder();

LAdditional := TConverters.ConvertStringToBytes(FAdditional,TEncoding.UTF8);
LSecret := TConverters.ConvertStringToBytes(FSecret,TEncoding.UTF8);
LSalt := TConverters.ConvertStringToBytes(FSalt,TEncoding.UTF8);
LPassword := TConverters.ConvertStringToBytes(FPassword,TEncoding.UTF8);

Argon2ParametersBuilder
.WithVersion(Argon2Version)
.WithIterations(4)
.WithMemoryAsKB(65536)
.WithParallelism(1)
.WithAdditional(LAdditional)
.WithSecret(LSecret)
.WithSalt(LSalt);

LArgon2Parameter := Argon2ParametersBuilder.Build();
Argon2ParametersBuilder.Clear();

LGenerator := TKDF.TPBKDF_Argon2.CreatePBKDF_Argon2(LPassword, LArgon2Parameter);
mRes := TConverters.ConvertBytesToString(LGenerator.GetBytes(66), TEncoding.Unicode);

// mRes -> '밖씤섲䍺ﲿ'#$26CE'궛櫳ﺠ'#$0EA4'第墱'#$08BC'埐⺇'#$D978'찠쌿᧧颩ဢ⤔栅�梵鋕蜷'

LArgon2Parameter.Clear();
LGenerator.Clear();
end;

Code example?

Would it be possible to get an example of using murmur3 or another function in a simple program? I have tried to understand the code but it is non-trivial. A "Hello World" would help as in the following code where "HashFunction" can be replaced by a call to murmu3 hash function.

Program TryHash;
Var S : String;
hashValue : Cardinal;
begin
S := 'Hello World';
HashValue := HashFunction(S);
Writeln(S, hashValue);
end.

CRC8 Exception

Invoking this function generated an exception:

function CRC8(const Buffer;const Size:Integer):UInt8;
var
  CRC : IHash;
begin
  if @Buffer=nil then Exit(0);
  CRC    := THashFactory.TChecksum.TCRC.CreateCRC(HlpCRC.CRC8);
  Result := CRC.ComputeUntyped(Buffer,Size).GetUInt8;
end;

Exception class EInvalidOperationHashLibException with message 'The size of the byte array (2) and integer type (1) have to match.'.

Best regards.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.