├── .github ├── dependabot.yml ├── ISSUE_TEMPLATE │ ├── feature_request.md │ └── bug_report.md ├── FUNDING.yml └── workflows │ ├── make.yml │ └── make.pas ├── HashLib └── src │ ├── Base │ ├── HlpHashRounds.pas │ ├── HlpHashSize.pas │ ├── HlpKDF.pas │ ├── HlpMultipleTransformNonBlock.pas │ ├── HlpHashCryptoNotBuildIn.pas │ ├── HlpHMACNotBuildInAdapter.pas │ └── HlpHashBuffer.pas │ ├── KDF │ └── HlpArgon2TypeAndVersion.pas │ ├── Interfaces │ ├── HlpIHashResult.pas │ ├── HlpIKDF.pas │ ├── HlpICRC.pas │ ├── HlpIHash.pas │ ├── IBlake2BParams │ │ └── HlpIBlake2BParams.pas │ ├── IBlake2SParams │ │ └── HlpIBlake2SParams.pas │ └── HlpIHashInfo.pas │ ├── Include │ ├── HashLibHelper.inc │ └── HashLib.inc │ ├── Hash32 │ ├── HlpDEK.pas │ ├── HlpFNV.pas │ ├── HlpDJB.pas │ ├── HlpJS.pas │ ├── HlpFNV1a.pas │ ├── HlpBernstein.pas │ ├── HlpSDBM.pas │ ├── HlpBKDR.pas │ ├── HlpBernstein1.pas │ ├── HlpRotating.pas │ ├── HlpShiftAndXor.pas │ ├── HlpRS.pas │ ├── HlpELF.pas │ ├── HlpOneAtTime.pas │ ├── HlpAP.pas │ ├── HlpPJW.pas │ ├── HlpSuperFast.pas │ └── HlpMurmur2.pas │ ├── Hash128 │ └── HlpSipHash128.pas │ ├── Crypto │ ├── HlpSHA2_224.pas │ ├── HlpSHA2_256.pas │ ├── HlpSHA2_512.pas │ ├── HlpSHA2_384.pas │ ├── HlpSHA2_512_256.pas │ ├── HlpSHA2_512_224.pas │ ├── HlpMDBase.pas │ ├── HlpMD2.pas │ ├── HlpRadioGatun64.pas │ ├── HlpRadioGatun32.pas │ ├── HlpMD4.pas │ └── HlpHAS160.pas │ ├── Hash64 │ ├── HlpFNV64.pas │ └── HlpFNV1a64.pas │ ├── Packages │ ├── FPC │ │ └── HashLib4PascalPackage.pas │ └── Delphi │ │ └── HashLib4PascalPackage.dpk │ ├── Checksum │ ├── HlpCRC16.pas │ ├── HlpCRC64.pas │ ├── HlpCRC32.pas │ └── HlpAdler32.pas │ ├── NullDigest │ └── HlpNullDigest.pas │ ├── Utils │ └── HlpHashLibTypes.pas │ └── Nullable │ └── HlpNullable.pas ├── HashLib.Tests ├── FreePascal.Tests │ ├── HashLib.lpr │ ├── HashLibConsole.lpr │ ├── HashLib.Tests.lpi │ └── HashLibConsole.lpi └── src │ ├── ChecksumTests.pas │ ├── NullDigestTests.pas │ ├── PBKDF2_HMACTests.pas │ ├── Hash128Tests.pas │ ├── Hash64Tests.pas │ └── PBKDF_ScryptTests.pas ├── HashLib.Benchmark ├── project │ └── Lazarus │ │ ├── PerformanceBenchmarkConsole.lpr │ │ └── PerformanceBenchmarkConsole.lpi └── src │ ├── Forms │ └── FMX │ │ ├── fmxMainForm.fmx │ │ └── fmxMainForm.pas │ └── Core │ └── uPerformanceBenchmark.pas ├── LICENSE ├── .gitignore ├── .travis.yml └── README.md /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | --- 2 | version: 2 3 | updates: 4 | - package-ecosystem: "github-actions" 5 | directory: "/" 6 | schedule: 7 | interval: "monthly" 8 | -------------------------------------------------------------------------------- /HashLib/src/Base/HlpHashRounds.pas: -------------------------------------------------------------------------------- 1 | unit HlpHashRounds; 2 | 3 | {$I ..\Include\HashLib.inc} 4 | 5 | interface 6 | 7 | type 8 | {$SCOPEDENUMS ON} 9 | THashRounds = (hrRounds3 = 3, hrRounds4 = 4, hrRounds5 = 5, hrRounds8 = 8); 10 | {$SCOPEDENUMS OFF} 11 | 12 | implementation 13 | 14 | end. 15 | -------------------------------------------------------------------------------- /HashLib/src/KDF/HlpArgon2TypeAndVersion.pas: -------------------------------------------------------------------------------- 1 | unit HlpArgon2TypeAndVersion; 2 | 3 | {$I ..\Include\HashLib.inc} 4 | 5 | interface 6 | 7 | type 8 | {$SCOPEDENUMS ON} 9 | TArgon2Type = (a2tARGON2_d = $00, a2tARGON2_i = $01, a2tARGON2_id = $02); 10 | TArgon2Version = (a2vARGON2_VERSION_10 = $10, a2vARGON2_VERSION_13 = $13); 11 | {$SCOPEDENUMS OFF} 12 | 13 | implementation 14 | 15 | end. 16 | -------------------------------------------------------------------------------- /HashLib/src/Base/HlpHashSize.pas: -------------------------------------------------------------------------------- 1 | unit HlpHashSize; 2 | 3 | {$I ..\Include\HashLib.inc} 4 | 5 | interface 6 | 7 | type 8 | {$SCOPEDENUMS ON} 9 | THashSize = (hsHashSize128 = 16, hsHashSize160 = 20, hsHashSize192 = 24, 10 | hsHashSize224 = 28, hsHashSize256 = 32, hsHashSize288 = 36, 11 | hsHashSize384 = 48, hsHashSize512 = 64); 12 | {$SCOPEDENUMS OFF} 13 | 14 | implementation 15 | 16 | end. 17 | -------------------------------------------------------------------------------- /HashLib.Tests/FreePascal.Tests/HashLib.lpr: -------------------------------------------------------------------------------- 1 | program HashLib.Tests; 2 | 3 | {$mode objfpc}{$H+} 4 | 5 | uses 6 | Interfaces, 7 | Forms, 8 | GuiTestRunner, 9 | HashLibTestBase, 10 | TestVectors, 11 | ChecksumTests, 12 | NullDigestTests, 13 | Hash32Tests, 14 | Hash64Tests, 15 | Hash128Tests, 16 | CryptoTests, 17 | BitConverterTests, 18 | PBKDF2_HMACTests, 19 | PBKDF_Argon2Tests, 20 | PBKDF_ScryptTests, 21 | CRCTests; 22 | 23 | {$R *.res} 24 | 25 | begin 26 | Application.Initialize; 27 | Application.CreateForm(TGuiTestRunner, TestRunner); 28 | Application.Run; 29 | end. 30 | 31 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Feature request 3 | about: Suggest an idea for this project 4 | 5 | --- 6 | 7 | **Is your feature request related to a problem? Please describe.** 8 | A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] 9 | 10 | **Describe the solution you'd like** 11 | A clear and concise description of what you want to happen. 12 | 13 | **Describe alternatives you've considered** 14 | A clear and concise description of any alternative solutions or features you've considered. 15 | 16 | **Additional context** 17 | Add any other context or screenshots about the feature request here. 18 | -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2] 4 | patreon: xor_el # Replace with a single Patreon username 5 | open_collective: # Replace with a single Open Collective username 6 | ko_fi: # Replace with a single Ko-fi username 7 | tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel 8 | community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry 9 | liberapay: # Replace with a single Liberapay username 10 | issuehunt: # Replace with a single IssueHunt username 11 | otechie: # Replace with a single Otechie username 12 | custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2'] -------------------------------------------------------------------------------- /HashLib/src/Interfaces/HlpIHashResult.pas: -------------------------------------------------------------------------------- 1 | unit HlpIHashResult; 2 | 3 | {$I ..\Include\HashLib.inc} 4 | 5 | interface 6 | 7 | uses 8 | HlpHashLibTypes; 9 | 10 | type 11 | IHashResult = interface(IInterface) 12 | ['{A467E23D-DBC4-41CC-848D-7267476430C1}'] 13 | 14 | function GetBytes(): THashLibByteArray; 15 | function GetUInt8(): UInt8; 16 | function GetUInt16(): UInt16; 17 | function GetUInt32(): UInt32; 18 | function GetInt32(): Int32; 19 | function GetUInt64(): UInt64; 20 | function ToString(AGroup: Boolean = False): String; 21 | function Equals(const AHashResult: IHashResult): Boolean; overload; 22 | function GetHashCode(): {$IFDEF DELPHI}Int32; {$ELSE}PtrInt; 23 | {$ENDIF DELPHI} 24 | end; 25 | 26 | implementation 27 | 28 | end. 29 | -------------------------------------------------------------------------------- /HashLib.Benchmark/project/Lazarus/PerformanceBenchmarkConsole.lpr: -------------------------------------------------------------------------------- 1 | program PerformanceBenchmarkConsole; 2 | 3 | uses 4 | Classes, 5 | SysUtils, 6 | uPerformanceBenchmark; 7 | 8 | var 9 | StringList: TStringList; 10 | Log: String; 11 | 12 | begin 13 | try 14 | Writeln('Please be patient, this might take some time' + SLineBreak); 15 | StringList := TStringList.Create; 16 | try 17 | TPerformanceBenchmark.DoBenchmark(StringList); 18 | 19 | for Log in StringList do 20 | begin 21 | Writeln(Log); 22 | end; 23 | 24 | finally 25 | StringList.Free; 26 | end; 27 | Writeln(SLineBreak + 'Performance Benchmark Finished'); 28 | ReadLn; 29 | except 30 | on E: Exception do 31 | Writeln(E.ClassName, ': ', E.Message); 32 | end; 33 | 34 | end. 35 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve 4 | 5 | --- 6 | 7 | **Describe the bug** 8 | A clear and concise description of what the bug is. 9 | 10 | **To Reproduce** 11 | Steps to reproduce the behavior: 12 | 1. Go to '...' 13 | 2. Click on '....' 14 | 3. Scroll down to '....' 15 | 4. See error 16 | 17 | **Expected behavior** 18 | A clear and concise description of what you expected to happen. 19 | 20 | **Screenshots** 21 | If applicable, add screenshots to help explain your problem. 22 | 23 | **Environment (please complete the following information):** 24 | - OS: [e.g. Windows, Linux, Mac] 25 | - Compiler Version [e.g. FreePascal 3.0.0] 26 | - Package Version [e.g. 1.0] 27 | 28 | **Additional context** 29 | Add any other context about the problem here. 30 | -------------------------------------------------------------------------------- /HashLib.Tests/FreePascal.Tests/HashLibConsole.lpr: -------------------------------------------------------------------------------- 1 | program HashLib.Tests; 2 | 3 | {$mode objfpc}{$H+} 4 | 5 | uses 6 | consoletestrunner, 7 | HashLibTestBase, 8 | TestVectors, 9 | ChecksumTests, 10 | NullDigestTests, 11 | Hash32Tests, 12 | Hash64Tests, 13 | Hash128Tests, 14 | CryptoTests, 15 | BitConverterTests, 16 | PBKDF2_HMACTests, 17 | PBKDF_Argon2Tests, 18 | PBKDF_ScryptTests, 19 | CRCTests; 20 | 21 | type 22 | 23 | { THashLibConsoleTestRunner } 24 | 25 | THashLibConsoleTestRunner = class(TTestRunner) 26 | protected 27 | // override the protected methods of TTestRunner to customize its behaviour 28 | end; 29 | 30 | var 31 | Application: THashLibConsoleTestRunner; 32 | 33 | begin 34 | Application := THashLibConsoleTestRunner.Create(nil); 35 | Application.Initialize; 36 | Application.Run; 37 | Application.Free; 38 | end. 39 | 40 | -------------------------------------------------------------------------------- /HashLib/src/Interfaces/HlpIKDF.pas: -------------------------------------------------------------------------------- 1 | unit HlpIKDF; 2 | 3 | {$I ..\Include\HashLib.inc} 4 | 5 | interface 6 | 7 | uses 8 | HlpHashLibTypes; 9 | 10 | type 11 | IKDF = interface(IInterface) 12 | ['{4697798C-9DC2-476C-A6C2-2D633B74D3FC}'] 13 | 14 | procedure Clear(); 15 | /// 16 | /// Returns the pseudo-random bytes for this object. 17 | /// 18 | /// The number of pseudo-random key bytes to generate. 19 | /// A byte array filled with pseudo-random key bytes. 20 | /// AByteCount must be greater than zero. 21 | /// invalid start index or end index of internal buffer. 22 | function GetBytes(AByteCount: Int32): THashLibByteArray; 23 | 24 | end; 25 | 26 | implementation 27 | 28 | end. 29 | -------------------------------------------------------------------------------- /HashLib.Tests/src/ChecksumTests.pas: -------------------------------------------------------------------------------- 1 | unit ChecksumTests; 2 | 3 | interface 4 | 5 | uses 6 | {$IFDEF FPC} 7 | fpcunit, 8 | testregistry, 9 | {$ELSE} 10 | TestFramework, 11 | {$ENDIF FPC} 12 | HashLibTestBase, 13 | HlpHashFactory; 14 | 15 | type 16 | TTestAlder32 = class(THashAlgorithmTestCase) 17 | 18 | protected 19 | procedure SetUp; override; 20 | procedure TearDown; override; 21 | 22 | end; 23 | 24 | implementation 25 | 26 | { TTestAlder32 } 27 | 28 | procedure TTestAlder32.SetUp; 29 | begin 30 | inherited; 31 | HashInstance := THashFactory.TChecksum.CreateAdler32(); 32 | HashOfEmptyData := '00000001'; 33 | HashOfDefaultData := '25D40524'; 34 | HashOfOnetoNine := '091E01DE'; 35 | HashOfABCDE := '05C801F0'; 36 | end; 37 | 38 | procedure TTestAlder32.TearDown; 39 | begin 40 | HashInstance := Nil; 41 | inherited; 42 | end; 43 | 44 | initialization 45 | 46 | // Register any test cases with the test runner 47 | 48 | {$IFDEF FPC} 49 | RegisterTest(TTestAlder32); 50 | {$ELSE} 51 | RegisterTest(TTestAlder32.Suite); 52 | {$ENDIF FPC} 53 | 54 | end. 55 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 - 2019 Ugochukwu Mmaduekwe 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /HashLib/src/Interfaces/HlpICRC.pas: -------------------------------------------------------------------------------- 1 | unit HlpICRC; 2 | 3 | {$I ..\Include\HashLib.inc} 4 | 5 | interface 6 | 7 | uses 8 | HlpIHashInfo, 9 | HlpHashLibTypes; 10 | 11 | type 12 | 13 | ICRC = Interface(IChecksum) 14 | ['{44A105E5-6716-43C0-BE69-AE80F87FDC39}'] 15 | 16 | function GetNames: THashLibStringArray; 17 | property Names: THashLibStringArray read GetNames; 18 | function GetWidth: Int32; 19 | property Width: Int32 read GetWidth; 20 | function GetPolynomial: UInt64; 21 | property Polynomial: UInt64 read GetPolynomial; 22 | function GetInitialValue: UInt64; 23 | property InitialVlaue: UInt64 read GetInitialValue; 24 | function GetIsInputReflected: Boolean; 25 | property IsInputReflected: Boolean read GetIsInputReflected; 26 | function GetIsOutputReflected: Boolean; 27 | property IsOutputReflected: Boolean read GetIsOutputReflected; 28 | function GetOutputXor: UInt64; 29 | property OutputXor: UInt64 read GetOutputXor; 30 | function GetCheckValue: UInt64; 31 | property CheckValue: UInt64 read GetCheckValue; 32 | 33 | end; 34 | 35 | implementation 36 | 37 | end. 38 | -------------------------------------------------------------------------------- /HashLib.Benchmark/src/Forms/FMX/fmxMainForm.fmx: -------------------------------------------------------------------------------- 1 | object MainForm: TMainForm 2 | Left = 0 3 | Top = 0 4 | BorderIcons = [biSystemMenu, biMinimize] 5 | Caption = 'PerformanceBenchmarkFMX' 6 | ClientHeight = 480 7 | ClientWidth = 640 8 | Position = MainFormCenter 9 | FormFactor.Width = 320 10 | FormFactor.Height = 480 11 | FormFactor.Devices = [Desktop] 12 | DesignerMasterStyle = 0 13 | object mmoBenchmarkLog: TMemo 14 | Touch.InteractiveGestures = [Pan, LongTap, DoubleTap] 15 | DataDetectorTypes = [] 16 | Position.X = 24.000000000000000000 17 | Position.Y = 24.000000000000000000 18 | Size.Width = 593.000000000000000000 19 | Size.Height = 393.000000000000000000 20 | Size.PlatformDefault = False 21 | TabOrder = 0 22 | Viewport.Width = 589.000000000000000000 23 | Viewport.Height = 389.000000000000000000 24 | end 25 | object DoBenchmark: TButton 26 | Position.X = 168.000000000000000000 27 | Position.Y = 432.000000000000000000 28 | Size.Width = 305.000000000000000000 29 | Size.Height = 41.000000000000000000 30 | Size.PlatformDefault = False 31 | TabOrder = 1 32 | Text = 'Do Benchmark' 33 | OnClick = DoBenchmarkClick 34 | end 35 | end 36 | -------------------------------------------------------------------------------- /HashLib.Tests/src/NullDigestTests.pas: -------------------------------------------------------------------------------- 1 | unit NullDigestTests; 2 | 3 | interface 4 | 5 | {$IFDEF FPC} 6 | {$MODE DELPHI} 7 | {$ENDIF FPC} 8 | 9 | uses 10 | {$IFDEF FPC} 11 | fpcunit, 12 | testregistry, 13 | {$ELSE} 14 | TestFramework, 15 | {$ENDIF FPC} 16 | HashLibTestBase, 17 | HlpHashFactory; 18 | 19 | // NullDigest 20 | type 21 | TTestNullDigest = class(TNullDigestAlgorithmTestCase) 22 | 23 | protected 24 | procedure SetUp; override; 25 | procedure TearDown; override; 26 | 27 | end; 28 | 29 | implementation 30 | 31 | { TTestNullDigest } 32 | 33 | procedure TTestNullDigest.SetUp; 34 | begin 35 | inherited; 36 | HashInstance := THashFactory.TNullDigestFactory.CreateNullDigest(); 37 | FBlockSizeMethod := CallGetBlockSize; 38 | FHashSizeMethod := CallGetHashSize; 39 | end; 40 | 41 | procedure TTestNullDigest.TearDown; 42 | begin 43 | inherited; 44 | HashInstance := Nil; 45 | FBlockSizeMethod := Nil; 46 | FHashSizeMethod := Nil; 47 | end; 48 | 49 | initialization 50 | 51 | // Register any test cases with the test runner 52 | 53 | {$IFDEF FPC} 54 | // NullDigest 55 | RegisterTest(TTestNullDigest); 56 | {$ELSE} 57 | // NullDigest 58 | RegisterTest(TTestNullDigest.Suite); 59 | {$ENDIF FPC} 60 | 61 | end. 62 | -------------------------------------------------------------------------------- /HashLib/src/Include/HashLibHelper.inc: -------------------------------------------------------------------------------- 1 | { *********************************************************************************** } 2 | { * HashLib Library * } 3 | { * Copyright (c) 2016 - 2020 Ugochukwu Mmaduekwe * } 4 | { * Github Repository * } 5 | 6 | { * Distributed under the MIT software license, see the accompanying file LICENSE * } 7 | { * or visit http://www.opensource.org/licenses/mit-license.php. * } 8 | 9 | { * ******************************************************************************* * } 10 | 11 | (* &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& *) 12 | 13 | 14 | {$MACRO ON} 15 | {$IFDEF ENDIAN_BIG} 16 | {$MESSAGE FATAL 'This Library does not support "Big Endian" processors yet.'} 17 | {$ENDIF} 18 | // FPC 3.0.0 and Above 19 | // Had to Include this here since Delphi does not allow it Compile in "HashLib.inc". 20 | {$IF FPC_FULLVERSION < 30000} 21 | {$MESSAGE ERROR 'This Library requires FreePascal 3.0.0 or higher.'} 22 | {$IFEND} 23 | 24 | {$IF FPC_FULLVERSION > 30004} 25 | {$DEFINE FPC_GREATER_THAN_3.0.4} 26 | {$IFEND} 27 | -------------------------------------------------------------------------------- /HashLib/src/Base/HlpKDF.pas: -------------------------------------------------------------------------------- 1 | unit HlpKDF; 2 | 3 | {$I ..\Include\HashLib.inc} 4 | 5 | interface 6 | 7 | uses 8 | HlpIKDF, 9 | HlpHashLibTypes; 10 | 11 | type 12 | TKDF = class abstract(TInterfacedObject, IKDF) 13 | 14 | strict protected 15 | 16 | // Not really needed because there is an Intristic default constructor always 17 | // called for classes if none is defined by the developer but I just put it 18 | // for readability reasons. 19 | constructor Create(); 20 | 21 | public 22 | 23 | /// 24 | /// Clear sensitive materials from memory 25 | /// 26 | procedure Clear(); virtual; abstract; 27 | 28 | /// 29 | /// Returns the pseudo-random bytes for this object. 30 | /// 31 | /// The number of pseudo-random key bytes to generate. 32 | /// A byte array filled with pseudo-random key bytes. 33 | /// bc must be greater than zero. 34 | /// invalid start index or end index of internal buffer. 35 | function GetBytes(AByteCount: Int32): THashLibByteArray; virtual; abstract; 36 | 37 | end; 38 | 39 | implementation 40 | 41 | { TKDF } 42 | 43 | constructor TKDF.Create; 44 | begin 45 | Inherited Create(); 46 | end; 47 | 48 | end. 49 | -------------------------------------------------------------------------------- /HashLib.Benchmark/src/Forms/FMX/fmxMainForm.pas: -------------------------------------------------------------------------------- 1 | unit fmxMainForm; 2 | 3 | interface 4 | 5 | uses 6 | System.SysUtils, System.Types, System.UITypes, System.Classes, 7 | System.Variants, 8 | FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.StdCtrls, 9 | FMX.Controls.Presentation, FMX.ScrollBox, FMX.Memo, uPerformanceBenchmark; 10 | 11 | type 12 | TMainForm = class(TForm) 13 | mmoBenchmarkLog: TMemo; 14 | DoBenchmark: TButton; 15 | procedure DoBenchmarkClick(Sender: TObject); 16 | private 17 | { Private declarations } 18 | public 19 | { Public declarations } 20 | end; 21 | 22 | var 23 | MainForm: TMainForm; 24 | 25 | implementation 26 | 27 | {$R *.fmx} 28 | 29 | procedure TMainForm.DoBenchmarkClick(Sender: TObject); 30 | var 31 | StringList: TStringList; 32 | begin 33 | mmoBenchmarkLog.SelectAll; 34 | mmoBenchmarkLog.DeleteSelection; 35 | mmoBenchmarkLog.Lines.Add('Please be patient, this might take some time' + 36 | SLineBreak); 37 | StringList := TStringList.Create; 38 | try 39 | TPerformanceBenchmark.DoBenchmark(StringList); 40 | mmoBenchmarkLog.Lines.AddStrings(StringList); 41 | finally 42 | StringList.Free; 43 | end; 44 | mmoBenchmarkLog.Lines.Add(SLineBreak + 'Performance Benchmark Finished'); 45 | mmoBenchmarkLog.SelectAll; 46 | mmoBenchmarkLog.CopyToClipboard; 47 | mmoBenchmarkLog.Lines.Add(SLineBreak + 48 | 'Benchmark Results Copied to Clipboard'); 49 | end; 50 | 51 | end. 52 | -------------------------------------------------------------------------------- /HashLib/src/Hash32/HlpDEK.pas: -------------------------------------------------------------------------------- 1 | unit HlpDEK; 2 | 3 | {$I ..\Include\HashLib.inc} 4 | 5 | interface 6 | 7 | uses 8 | HlpHashLibTypes, 9 | {$IFDEF DELPHI} 10 | HlpHash, 11 | {$ENDIF DELPHI} 12 | HlpBits, 13 | HlpIHash, 14 | HlpIHashInfo, 15 | HlpHashResult, 16 | HlpIHashResult, 17 | HlpMultipleTransformNonBlock; 18 | 19 | type 20 | 21 | TDEK = class sealed(TMultipleTransformNonBlock, IHash32, ITransformBlock) 22 | 23 | strict protected 24 | function ComputeAggregatedBytes(const AData: THashLibByteArray) 25 | : IHashResult; override; 26 | public 27 | constructor Create(); 28 | function Clone(): IHash; override; 29 | 30 | end; 31 | 32 | implementation 33 | 34 | { TDEK } 35 | 36 | constructor TDEK.Create; 37 | begin 38 | Inherited Create(4, 1); 39 | end; 40 | 41 | function TDEK.Clone(): IHash; 42 | var 43 | LHashInstance: TDEK; 44 | begin 45 | LHashInstance := TDEK.Create(); 46 | FBuffer.Position := 0; 47 | LHashInstance.FBuffer.CopyFrom(FBuffer, FBuffer.Size); 48 | result := LHashInstance as IHash; 49 | result.BufferSize := BufferSize; 50 | end; 51 | 52 | function TDEK.ComputeAggregatedBytes(const AData: THashLibByteArray) 53 | : IHashResult; 54 | var 55 | LHash: UInt32; 56 | LIdx: Int32; 57 | begin 58 | LHash := UInt32(System.Length(AData)); 59 | for LIdx := 0 to System.Length(AData) - 1 do 60 | begin 61 | LHash := TBits.RotateLeft32(LHash, 5) xor AData[LIdx]; 62 | end; 63 | result := THashResult.Create(LHash); 64 | end; 65 | 66 | end. 67 | -------------------------------------------------------------------------------- /HashLib/src/Hash32/HlpFNV.pas: -------------------------------------------------------------------------------- 1 | unit HlpFNV; 2 | 3 | {$I ..\Include\HashLib.inc} 4 | 5 | interface 6 | 7 | uses 8 | HlpHashLibTypes, 9 | HlpHash, 10 | HlpIHash, 11 | HlpIHashInfo, 12 | HlpHashResult, 13 | HlpIHashResult; 14 | 15 | type 16 | TFNV = class sealed(THash, IHash32, ITransformBlock) 17 | strict private 18 | var 19 | FHash: UInt32; 20 | 21 | public 22 | constructor Create(); 23 | procedure Initialize(); override; 24 | procedure TransformBytes(const AData: THashLibByteArray; 25 | AIndex, ALength: Int32); override; 26 | function TransformFinal(): IHashResult; override; 27 | function Clone(): IHash; override; 28 | end; 29 | 30 | implementation 31 | 32 | { TFNV } 33 | 34 | function TFNV.Clone(): IHash; 35 | var 36 | LHashInstance: TFNV; 37 | begin 38 | LHashInstance := TFNV.Create(); 39 | LHashInstance.FHash := FHash; 40 | result := LHashInstance as IHash; 41 | result.BufferSize := BufferSize; 42 | end; 43 | 44 | constructor TFNV.Create; 45 | begin 46 | Inherited Create(4, 1); 47 | end; 48 | 49 | procedure TFNV.Initialize; 50 | begin 51 | FHash := 0; 52 | end; 53 | 54 | procedure TFNV.TransformBytes(const AData: THashLibByteArray; 55 | AIndex, ALength: Int32); 56 | var 57 | LIdx: Int32; 58 | begin 59 | {$IFDEF DEBUG} 60 | System.Assert(AIndex >= 0); 61 | System.Assert(ALength >= 0); 62 | System.Assert(AIndex + ALength <= System.Length(AData)); 63 | {$ENDIF DEBUG} 64 | LIdx := AIndex; 65 | while ALength > 0 do 66 | begin 67 | FHash := (FHash * 16777619) xor AData[LIdx]; 68 | System.Inc(LIdx); 69 | System.Dec(ALength); 70 | end; 71 | end; 72 | 73 | function TFNV.TransformFinal: IHashResult; 74 | begin 75 | result := THashResult.Create(FHash); 76 | Initialize(); 77 | end; 78 | 79 | end. 80 | -------------------------------------------------------------------------------- /HashLib/src/Hash32/HlpDJB.pas: -------------------------------------------------------------------------------- 1 | unit HlpDJB; 2 | 3 | {$I ..\Include\HashLib.inc} 4 | 5 | interface 6 | 7 | uses 8 | HlpHashLibTypes, 9 | HlpHash, 10 | HlpIHash, 11 | HlpIHashInfo, 12 | HlpHashResult, 13 | HlpIHashResult; 14 | 15 | type 16 | TDJB = class sealed(THash, IHash32, ITransformBlock) 17 | strict private 18 | var 19 | FHash: UInt32; 20 | 21 | public 22 | constructor Create(); 23 | procedure Initialize(); override; 24 | procedure TransformBytes(const AData: THashLibByteArray; 25 | AIndex, ALength: Int32); override; 26 | function TransformFinal(): IHashResult; override; 27 | function Clone(): IHash; override; 28 | end; 29 | 30 | implementation 31 | 32 | { TDJB } 33 | 34 | function TDJB.Clone(): IHash; 35 | var 36 | LHashInstance: TDJB; 37 | begin 38 | LHashInstance := TDJB.Create(); 39 | LHashInstance.FHash := FHash; 40 | result := LHashInstance as IHash; 41 | result.BufferSize := BufferSize; 42 | end; 43 | 44 | constructor TDJB.Create; 45 | begin 46 | Inherited Create(4, 1); 47 | end; 48 | 49 | procedure TDJB.Initialize; 50 | begin 51 | FHash := 5381; 52 | end; 53 | 54 | procedure TDJB.TransformBytes(const AData: THashLibByteArray; 55 | AIndex, ALength: Int32); 56 | var 57 | LIdx: Int32; 58 | begin 59 | {$IFDEF DEBUG} 60 | System.Assert(AIndex >= 0); 61 | System.Assert(ALength >= 0); 62 | System.Assert(AIndex + ALength <= System.Length(AData)); 63 | {$ENDIF DEBUG} 64 | LIdx := AIndex; 65 | while ALength > 0 do 66 | begin 67 | FHash := ((FHash shl 5) + FHash) + AData[LIdx]; 68 | System.Inc(LIdx); 69 | System.Dec(ALength); 70 | end; 71 | end; 72 | 73 | function TDJB.TransformFinal: IHashResult; 74 | begin 75 | result := THashResult.Create(FHash); 76 | Initialize(); 77 | end; 78 | 79 | end. 80 | -------------------------------------------------------------------------------- /HashLib/src/Hash32/HlpJS.pas: -------------------------------------------------------------------------------- 1 | unit HlpJS; 2 | 3 | {$I ..\Include\HashLib.inc} 4 | 5 | interface 6 | 7 | uses 8 | HlpHashLibTypes, 9 | HlpHash, 10 | HlpIHash, 11 | HlpIHashInfo, 12 | HlpHashResult, 13 | HlpIHashResult; 14 | 15 | type 16 | TJS = class sealed(THash, IHash32, ITransformBlock) 17 | strict private 18 | var 19 | FHash: UInt32; 20 | 21 | public 22 | constructor Create(); 23 | procedure Initialize(); override; 24 | procedure TransformBytes(const AData: THashLibByteArray; 25 | AIndex, ALength: Int32); override; 26 | function TransformFinal(): IHashResult; override; 27 | function Clone(): IHash; override; 28 | end; 29 | 30 | implementation 31 | 32 | { TJS } 33 | 34 | function TJS.Clone(): IHash; 35 | var 36 | LHashInstance: TJS; 37 | begin 38 | LHashInstance := TJS.Create(); 39 | LHashInstance.FHash := FHash; 40 | result := LHashInstance as IHash; 41 | result.BufferSize := BufferSize; 42 | end; 43 | 44 | constructor TJS.Create; 45 | begin 46 | Inherited Create(4, 1); 47 | end; 48 | 49 | procedure TJS.Initialize; 50 | begin 51 | FHash := 1315423911; 52 | end; 53 | 54 | procedure TJS.TransformBytes(const AData: THashLibByteArray; 55 | AIndex, ALength: Int32); 56 | var 57 | LIdx: Int32; 58 | begin 59 | {$IFDEF DEBUG} 60 | System.Assert(AIndex >= 0); 61 | System.Assert(ALength >= 0); 62 | System.Assert(AIndex + ALength <= System.Length(AData)); 63 | {$ENDIF DEBUG} 64 | LIdx := AIndex; 65 | while ALength > 0 do 66 | begin 67 | FHash := FHash xor ((FHash shl 5) + AData[LIdx] + (FHash shr 2)); 68 | System.Inc(LIdx); 69 | System.Dec(ALength); 70 | end; 71 | end; 72 | 73 | function TJS.TransformFinal: IHashResult; 74 | begin 75 | result := THashResult.Create(FHash); 76 | Initialize(); 77 | end; 78 | 79 | end. 80 | -------------------------------------------------------------------------------- /HashLib/src/Hash32/HlpFNV1a.pas: -------------------------------------------------------------------------------- 1 | unit HlpFNV1a; 2 | 3 | {$I ..\Include\HashLib.inc} 4 | 5 | interface 6 | 7 | uses 8 | HlpHashLibTypes, 9 | HlpHash, 10 | HlpIHash, 11 | HlpIHashInfo, 12 | HlpHashResult, 13 | HlpIHashResult; 14 | 15 | type 16 | TFNV1a = class sealed(THash, IHash32, ITransformBlock) 17 | strict private 18 | var 19 | FHash: UInt32; 20 | 21 | public 22 | constructor Create(); 23 | procedure Initialize(); override; 24 | procedure TransformBytes(const AData: THashLibByteArray; 25 | AIndex, ALength: Int32); override; 26 | function TransformFinal(): IHashResult; override; 27 | function Clone(): IHash; override; 28 | end; 29 | 30 | implementation 31 | 32 | { TFNV1a } 33 | 34 | function TFNV1a.Clone(): IHash; 35 | var 36 | LHashInstance: TFNV1a; 37 | begin 38 | LHashInstance := TFNV1a.Create(); 39 | LHashInstance.FHash := FHash; 40 | result := LHashInstance as IHash; 41 | result.BufferSize := BufferSize; 42 | end; 43 | 44 | constructor TFNV1a.Create; 45 | begin 46 | Inherited Create(4, 1); 47 | end; 48 | 49 | procedure TFNV1a.Initialize; 50 | begin 51 | FHash := 2166136261; 52 | end; 53 | 54 | procedure TFNV1a.TransformBytes(const AData: THashLibByteArray; 55 | AIndex, ALength: Int32); 56 | var 57 | LIdx: Int32; 58 | begin 59 | {$IFDEF DEBUG} 60 | System.Assert(AIndex >= 0); 61 | System.Assert(ALength >= 0); 62 | System.Assert(AIndex + ALength <= System.Length(AData)); 63 | {$ENDIF DEBUG} 64 | LIdx := AIndex; 65 | while ALength > 0 do 66 | begin 67 | FHash := (FHash xor AData[LIdx]) * 16777619; 68 | System.Inc(LIdx); 69 | System.Dec(ALength); 70 | end; 71 | end; 72 | 73 | function TFNV1a.TransformFinal: IHashResult; 74 | begin 75 | result := THashResult.Create(FHash); 76 | Initialize(); 77 | end; 78 | 79 | end. 80 | -------------------------------------------------------------------------------- /HashLib/src/Hash128/HlpSipHash128.pas: -------------------------------------------------------------------------------- 1 | unit HlpSipHash128; 2 | 3 | {$I ..\Include\HashLib.inc} 4 | 5 | interface 6 | 7 | uses 8 | {$IFDEF DELPHI} 9 | HlpHash, 10 | {$ENDIF DELPHI} 11 | HlpIHash, 12 | HlpSipHash; 13 | 14 | type 15 | /// 16 | /// SipHash128 2 - 4 algorithm. 17 | /// 18 | TSipHash128_2_4 = class sealed(TSipHash) 19 | 20 | strict protected 21 | function GetMagicXor(): Byte; override; 22 | 23 | public 24 | 25 | constructor Create(ACompressionRounds: Int32 = 2; 26 | AFinalizationRounds: Int32 = 4); 27 | function Clone(): IHash; override; 28 | 29 | end; 30 | 31 | implementation 32 | 33 | { TSipHash128_2_4 } 34 | 35 | function TSipHash128_2_4.GetMagicXor: Byte; 36 | begin 37 | Result := $EE; 38 | end; 39 | 40 | function TSipHash128_2_4.Clone: IHash; 41 | var 42 | LHashInstance: TSipHash128_2_4; 43 | begin 44 | LHashInstance := TSipHash128_2_4.Create(); 45 | LHashInstance.FV0 := FV0; 46 | LHashInstance.FV1 := FV1; 47 | LHashInstance.FV2 := FV2; 48 | LHashInstance.FV3 := FV3; 49 | LHashInstance.FKey0 := FKey0; 50 | LHashInstance.FKey1 := FKey1; 51 | LHashInstance.FPartA := FPartA; 52 | LHashInstance.FPartB := FPartB; 53 | LHashInstance.FTotalLength := FTotalLength; 54 | LHashInstance.FCompressionRounds := FCompressionRounds; 55 | LHashInstance.FFinalizationRounds := FFinalizationRounds; 56 | LHashInstance.FIdx := FIdx; 57 | LHashInstance.FBuffer := System.Copy(FBuffer); 58 | Result := LHashInstance as IHash; 59 | Result.BufferSize := BufferSize; 60 | end; 61 | 62 | constructor TSipHash128_2_4.Create(ACompressionRounds, 63 | AFinalizationRounds: Int32); 64 | begin 65 | Inherited Create(16, 8); 66 | FCompressionRounds := ACompressionRounds; 67 | FFinalizationRounds := AFinalizationRounds; 68 | end; 69 | 70 | end. 71 | -------------------------------------------------------------------------------- /HashLib/src/Hash32/HlpBernstein.pas: -------------------------------------------------------------------------------- 1 | unit HlpBernstein; 2 | 3 | {$I ..\Include\HashLib.inc} 4 | 5 | interface 6 | 7 | uses 8 | HlpHashLibTypes, 9 | HlpHash, 10 | HlpIHash, 11 | HlpIHashInfo, 12 | HlpHashResult, 13 | HlpIHashResult; 14 | 15 | type 16 | TBernstein = class sealed(THash, IHash32, ITransformBlock) 17 | strict private 18 | var 19 | FHash: UInt32; 20 | 21 | public 22 | constructor Create(); 23 | procedure Initialize(); override; 24 | procedure TransformBytes(const AData: THashLibByteArray; 25 | AIndex, ALength: Int32); override; 26 | function TransformFinal(): IHashResult; override; 27 | function Clone(): IHash; override; 28 | end; 29 | 30 | implementation 31 | 32 | { TBernstein } 33 | 34 | function TBernstein.Clone(): IHash; 35 | var 36 | LHashInstance: TBernstein; 37 | begin 38 | LHashInstance := TBernstein.Create(); 39 | LHashInstance.FHash := FHash; 40 | result := LHashInstance as IHash; 41 | result.BufferSize := BufferSize; 42 | end; 43 | 44 | constructor TBernstein.Create; 45 | begin 46 | Inherited Create(4, 1); 47 | end; 48 | 49 | procedure TBernstein.Initialize; 50 | begin 51 | FHash := 5381; 52 | end; 53 | 54 | procedure TBernstein.TransformBytes(const AData: THashLibByteArray; 55 | AIndex, ALength: Int32); 56 | var 57 | LIdx: Int32; 58 | begin 59 | {$IFDEF DEBUG} 60 | System.Assert(AIndex >= 0); 61 | System.Assert(ALength >= 0); 62 | System.Assert(AIndex + ALength <= System.Length(AData)); 63 | {$ENDIF DEBUG} 64 | LIdx := AIndex; 65 | while ALength > 0 do 66 | begin 67 | FHash := (FHash * 33) + AData[LIdx]; 68 | System.Inc(LIdx); 69 | System.Dec(ALength); 70 | end; 71 | end; 72 | 73 | function TBernstein.TransformFinal: IHashResult; 74 | begin 75 | result := THashResult.Create(FHash); 76 | Initialize(); 77 | end; 78 | 79 | end. 80 | -------------------------------------------------------------------------------- /HashLib/src/Hash32/HlpSDBM.pas: -------------------------------------------------------------------------------- 1 | unit HlpSDBM; 2 | 3 | {$I ..\Include\HashLib.inc} 4 | 5 | interface 6 | 7 | uses 8 | HlpHashLibTypes, 9 | HlpHash, 10 | HlpIHash, 11 | HlpIHashInfo, 12 | HlpHashResult, 13 | HlpIHashResult; 14 | 15 | type 16 | TSDBM = class sealed(THash, IHash32, ITransformBlock) 17 | strict private 18 | var 19 | FHash: UInt32; 20 | 21 | public 22 | constructor Create(); 23 | procedure Initialize(); override; 24 | procedure TransformBytes(const AData: THashLibByteArray; 25 | AIndex, ALength: Int32); override; 26 | function TransformFinal(): IHashResult; override; 27 | function Clone(): IHash; override; 28 | end; 29 | 30 | implementation 31 | 32 | { TSDBM } 33 | 34 | function TSDBM.Clone(): IHash; 35 | var 36 | LHashInstance: TSDBM; 37 | begin 38 | LHashInstance := TSDBM.Create(); 39 | LHashInstance.FHash := FHash; 40 | result := LHashInstance as IHash; 41 | result.BufferSize := BufferSize; 42 | end; 43 | 44 | constructor TSDBM.Create; 45 | begin 46 | Inherited Create(4, 1); 47 | end; 48 | 49 | procedure TSDBM.Initialize; 50 | begin 51 | FHash := 0; 52 | end; 53 | 54 | procedure TSDBM.TransformBytes(const AData: THashLibByteArray; 55 | AIndex, ALength: Int32); 56 | var 57 | LIdx: Int32; 58 | begin 59 | {$IFDEF DEBUG} 60 | System.Assert(AIndex >= 0); 61 | System.Assert(ALength >= 0); 62 | System.Assert(AIndex + ALength <= System.Length(AData)); 63 | {$ENDIF DEBUG} 64 | LIdx := AIndex; 65 | while ALength > 0 do 66 | begin 67 | FHash := UInt32(AData[LIdx] + Int64(FHash shl 6) + 68 | Int64(FHash shl 16) - FHash); 69 | System.Inc(LIdx); 70 | System.Dec(ALength); 71 | end; 72 | end; 73 | 74 | function TSDBM.TransformFinal: IHashResult; 75 | begin 76 | result := THashResult.Create(FHash); 77 | Initialize(); 78 | end; 79 | 80 | end. 81 | -------------------------------------------------------------------------------- /HashLib/src/Hash32/HlpBKDR.pas: -------------------------------------------------------------------------------- 1 | unit HlpBKDR; 2 | 3 | {$I ..\Include\HashLib.inc} 4 | 5 | interface 6 | 7 | uses 8 | HlpHashLibTypes, 9 | HlpHash, 10 | HlpIHash, 11 | HlpIHashInfo, 12 | HlpHashResult, 13 | HlpIHashResult; 14 | 15 | type 16 | 17 | TBKDR = class sealed(THash, IHash32, ITransformBlock) 18 | strict private 19 | var 20 | FHash: UInt32; 21 | 22 | const 23 | SEED = Int32(131); 24 | 25 | public 26 | constructor Create(); 27 | procedure Initialize(); override; 28 | procedure TransformBytes(const AData: THashLibByteArray; 29 | AIndex, ALength: Int32); override; 30 | function TransformFinal(): IHashResult; override; 31 | function Clone(): IHash; override; 32 | end; 33 | 34 | implementation 35 | 36 | { TBKDR } 37 | 38 | function TBKDR.Clone(): IHash; 39 | var 40 | LHashInstance: TBKDR; 41 | begin 42 | LHashInstance := TBKDR.Create(); 43 | LHashInstance.FHash := FHash; 44 | result := LHashInstance as IHash; 45 | result.BufferSize := BufferSize; 46 | end; 47 | 48 | constructor TBKDR.Create; 49 | begin 50 | Inherited Create(4, 1); 51 | end; 52 | 53 | procedure TBKDR.Initialize; 54 | begin 55 | FHash := 0; 56 | end; 57 | 58 | procedure TBKDR.TransformBytes(const AData: THashLibByteArray; 59 | AIndex, ALength: Int32); 60 | var 61 | LIdx: Int32; 62 | begin 63 | {$IFDEF DEBUG} 64 | System.Assert(AIndex >= 0); 65 | System.Assert(ALength >= 0); 66 | System.Assert(AIndex + ALength <= System.Length(AData)); 67 | {$ENDIF DEBUG} 68 | LIdx := AIndex; 69 | while ALength > 0 do 70 | begin 71 | FHash := (FHash * UInt32(SEED)) + AData[LIdx]; 72 | System.Inc(LIdx); 73 | System.Dec(ALength); 74 | end; 75 | end; 76 | 77 | function TBKDR.TransformFinal: IHashResult; 78 | begin 79 | result := THashResult.Create(FHash); 80 | Initialize(); 81 | end; 82 | 83 | end. 84 | -------------------------------------------------------------------------------- /HashLib/src/Hash32/HlpBernstein1.pas: -------------------------------------------------------------------------------- 1 | unit HlpBernstein1; 2 | 3 | {$I ..\Include\HashLib.inc} 4 | 5 | interface 6 | 7 | uses 8 | HlpHashLibTypes, 9 | HlpHash, 10 | HlpIHash, 11 | HlpIHashInfo, 12 | HlpHashResult, 13 | HlpIHashResult; 14 | 15 | type 16 | TBernstein1 = class sealed(THash, IHash32, ITransformBlock) 17 | strict private 18 | var 19 | FHash: UInt32; 20 | 21 | public 22 | constructor Create(); 23 | procedure Initialize(); override; 24 | procedure TransformBytes(const AData: THashLibByteArray; 25 | AIndex, ALength: Int32); override; 26 | function TransformFinal(): IHashResult; override; 27 | function Clone(): IHash; override; 28 | end; 29 | 30 | implementation 31 | 32 | { TBernstein1 } 33 | 34 | function TBernstein1.Clone(): IHash; 35 | var 36 | LHashInstance: TBernstein1; 37 | begin 38 | LHashInstance := TBernstein1.Create(); 39 | LHashInstance.FHash := FHash; 40 | result := LHashInstance as IHash; 41 | result.BufferSize := BufferSize; 42 | end; 43 | 44 | constructor TBernstein1.Create; 45 | begin 46 | Inherited Create(4, 1); 47 | end; 48 | 49 | procedure TBernstein1.Initialize; 50 | begin 51 | FHash := 5381; 52 | end; 53 | 54 | procedure TBernstein1.TransformBytes(const AData: THashLibByteArray; 55 | AIndex, ALength: Int32); 56 | var 57 | LIdx: Int32; 58 | begin 59 | {$IFDEF DEBUG} 60 | System.Assert(AIndex >= 0); 61 | System.Assert(ALength >= 0); 62 | System.Assert(AIndex + ALength <= System.Length(AData)); 63 | {$ENDIF DEBUG} 64 | LIdx := AIndex; 65 | while ALength > 0 do 66 | begin 67 | FHash := (FHash * 33) xor AData[LIdx]; 68 | System.Inc(LIdx); 69 | System.Dec(ALength); 70 | end; 71 | end; 72 | 73 | function TBernstein1.TransformFinal: IHashResult; 74 | begin 75 | result := THashResult.Create(FHash); 76 | Initialize(); 77 | end; 78 | 79 | end. 80 | -------------------------------------------------------------------------------- /HashLib/src/Hash32/HlpRotating.pas: -------------------------------------------------------------------------------- 1 | unit HlpRotating; 2 | 3 | {$I ..\Include\HashLib.inc} 4 | 5 | interface 6 | 7 | uses 8 | HlpHashLibTypes, 9 | HlpBits, 10 | HlpHash, 11 | HlpIHash, 12 | HlpIHashInfo, 13 | HlpHashResult, 14 | HlpIHashResult; 15 | 16 | type 17 | TRotating = class sealed(THash, IHash32, ITransformBlock) 18 | strict private 19 | var 20 | FHash: UInt32; 21 | 22 | public 23 | constructor Create(); 24 | procedure Initialize(); override; 25 | procedure TransformBytes(const AData: THashLibByteArray; 26 | AIndex, ALength: Int32); override; 27 | function TransformFinal(): IHashResult; override; 28 | function Clone(): IHash; override; 29 | end; 30 | 31 | implementation 32 | 33 | { TRotating } 34 | 35 | function TRotating.Clone(): IHash; 36 | var 37 | LHashInstance: TRotating; 38 | begin 39 | LHashInstance := TRotating.Create(); 40 | LHashInstance.FHash := FHash; 41 | result := LHashInstance as IHash; 42 | result.BufferSize := BufferSize; 43 | end; 44 | 45 | constructor TRotating.Create; 46 | begin 47 | Inherited Create(4, 1); 48 | end; 49 | 50 | procedure TRotating.Initialize; 51 | begin 52 | FHash := 0; 53 | end; 54 | 55 | procedure TRotating.TransformBytes(const AData: THashLibByteArray; 56 | AIndex, ALength: Int32); 57 | var 58 | LIdx: Int32; 59 | begin 60 | {$IFDEF DEBUG} 61 | System.Assert(AIndex >= 0); 62 | System.Assert(ALength >= 0); 63 | System.Assert(AIndex + ALength <= System.Length(AData)); 64 | {$ENDIF DEBUG} 65 | LIdx := AIndex; 66 | while ALength > 0 do 67 | begin 68 | FHash := TBits.RotateLeft32(FHash, 4) xor AData[LIdx]; 69 | System.Inc(LIdx); 70 | System.Dec(ALength); 71 | end; 72 | end; 73 | 74 | function TRotating.TransformFinal: IHashResult; 75 | begin 76 | result := THashResult.Create(FHash); 77 | Initialize(); 78 | end; 79 | 80 | end. 81 | -------------------------------------------------------------------------------- /HashLib/src/Hash32/HlpShiftAndXor.pas: -------------------------------------------------------------------------------- 1 | unit HlpShiftAndXor; 2 | 3 | {$I ..\Include\HashLib.inc} 4 | 5 | interface 6 | 7 | uses 8 | HlpHashLibTypes, 9 | HlpHash, 10 | HlpIHash, 11 | HlpIHashInfo, 12 | HlpHashResult, 13 | HlpIHashResult; 14 | 15 | type 16 | TShiftAndXor = class sealed(THash, IHash32, ITransformBlock) 17 | strict private 18 | var 19 | FHash: UInt32; 20 | 21 | public 22 | constructor Create(); 23 | procedure Initialize(); override; 24 | procedure TransformBytes(const AData: THashLibByteArray; 25 | AIndex, ALength: Int32); override; 26 | function TransformFinal(): IHashResult; override; 27 | function Clone(): IHash; override; 28 | end; 29 | 30 | implementation 31 | 32 | { TShiftAndXor } 33 | 34 | function TShiftAndXor.Clone(): IHash; 35 | var 36 | LHashInstance: TShiftAndXor; 37 | begin 38 | LHashInstance := TShiftAndXor.Create(); 39 | LHashInstance.FHash := FHash; 40 | result := LHashInstance as IHash; 41 | result.BufferSize := BufferSize; 42 | end; 43 | 44 | constructor TShiftAndXor.Create; 45 | begin 46 | Inherited Create(4, 1); 47 | end; 48 | 49 | procedure TShiftAndXor.Initialize; 50 | begin 51 | FHash := 0; 52 | end; 53 | 54 | procedure TShiftAndXor.TransformBytes(const AData: THashLibByteArray; 55 | AIndex, ALength: Int32); 56 | var 57 | LIdx: Int32; 58 | begin 59 | {$IFDEF DEBUG} 60 | System.Assert(AIndex >= 0); 61 | System.Assert(ALength >= 0); 62 | System.Assert(AIndex + ALength <= System.Length(AData)); 63 | {$ENDIF DEBUG} 64 | LIdx := AIndex; 65 | while ALength > 0 do 66 | begin 67 | FHash := FHash xor ((FHash shl 5) + (FHash shr 2) + AData[LIdx]); 68 | System.Inc(LIdx); 69 | System.Dec(ALength); 70 | end; 71 | end; 72 | 73 | function TShiftAndXor.TransformFinal: IHashResult; 74 | begin 75 | result := THashResult.Create(FHash); 76 | Initialize(); 77 | end; 78 | 79 | end. 80 | -------------------------------------------------------------------------------- /HashLib/src/Crypto/HlpSHA2_224.pas: -------------------------------------------------------------------------------- 1 | unit HlpSHA2_224; 2 | 3 | {$I ..\Include\HashLib.inc} 4 | 5 | interface 6 | 7 | uses 8 | {$IFDEF DELPHI2010} 9 | SysUtils, // to get rid of compiler hint "not inlined" on Delphi 2010. 10 | {$ENDIF DELPHI2010} 11 | HlpHashLibTypes, 12 | {$IFDEF DELPHI} 13 | HlpHashBuffer, 14 | HlpHash, 15 | {$ENDIF DELPHI} 16 | HlpSHA2_256Base, 17 | HlpIHash, 18 | HlpConverters; 19 | 20 | type 21 | TSHA2_224 = class sealed(TSHA2_256Base) 22 | 23 | strict protected 24 | function GetResult(): THashLibByteArray; override; 25 | 26 | public 27 | constructor Create(); 28 | procedure Initialize(); override; 29 | function Clone(): IHash; override; 30 | 31 | end; 32 | 33 | implementation 34 | 35 | { TSHA2_224 } 36 | 37 | function TSHA2_224.Clone(): IHash; 38 | var 39 | LHashInstance: TSHA2_224; 40 | begin 41 | LHashInstance := TSHA2_224.Create(); 42 | LHashInstance.FState := System.Copy(FState); 43 | LHashInstance.FBuffer := FBuffer.Clone(); 44 | LHashInstance.FProcessedBytesCount := FProcessedBytesCount; 45 | result := LHashInstance as IHash; 46 | result.BufferSize := BufferSize; 47 | end; 48 | 49 | constructor TSHA2_224.Create; 50 | begin 51 | Inherited Create(28); 52 | end; 53 | 54 | function TSHA2_224.GetResult: THashLibByteArray; 55 | begin 56 | System.SetLength(result, 7 * System.SizeOf(UInt32)); 57 | TConverters.be32_copy(PCardinal(FState), 0, PByte(result), 0, 58 | System.Length(result)); 59 | end; 60 | 61 | procedure TSHA2_224.Initialize; 62 | begin 63 | FState[0] := $C1059ED8; 64 | FState[1] := $367CD507; 65 | FState[2] := $3070DD17; 66 | FState[3] := $F70E5939; 67 | FState[4] := $FFC00B31; 68 | FState[5] := $68581511; 69 | FState[6] := $64F98FA7; 70 | FState[7] := $BEFA4FA4; 71 | Inherited Initialize(); 72 | end; 73 | 74 | end. 75 | -------------------------------------------------------------------------------- /HashLib/src/Crypto/HlpSHA2_256.pas: -------------------------------------------------------------------------------- 1 | unit HlpSHA2_256; 2 | 3 | {$I ..\Include\HashLib.inc} 4 | 5 | interface 6 | 7 | uses 8 | {$IFDEF DELPHI2010} 9 | SysUtils, // to get rid of compiler hint "not inlined" on Delphi 2010. 10 | {$ENDIF DELPHI2010} 11 | HlpHashLibTypes, 12 | {$IFDEF DELPHI} 13 | HlpHashBuffer, 14 | HlpHash, 15 | {$ENDIF DELPHI} 16 | HlpIHash, 17 | HlpSHA2_256Base, 18 | HlpConverters; 19 | 20 | type 21 | TSHA2_256 = class sealed(TSHA2_256Base) 22 | 23 | strict protected 24 | function GetResult(): THashLibByteArray; override; 25 | 26 | public 27 | constructor Create(); 28 | procedure Initialize(); override; 29 | function Clone(): IHash; override; 30 | 31 | end; 32 | 33 | implementation 34 | 35 | { TSHA2_256 } 36 | 37 | function TSHA2_256.Clone(): IHash; 38 | var 39 | LHashInstance: TSHA2_256; 40 | begin 41 | LHashInstance := TSHA2_256.Create(); 42 | LHashInstance.FState := System.Copy(FState); 43 | LHashInstance.FBuffer := FBuffer.Clone(); 44 | LHashInstance.FProcessedBytesCount := FProcessedBytesCount; 45 | result := LHashInstance as IHash; 46 | result.BufferSize := BufferSize; 47 | end; 48 | 49 | constructor TSHA2_256.Create; 50 | begin 51 | Inherited Create(32); 52 | end; 53 | 54 | function TSHA2_256.GetResult: THashLibByteArray; 55 | begin 56 | System.SetLength(result, 8 * System.SizeOf(UInt32)); 57 | TConverters.be32_copy(PCardinal(FState), 0, PByte(result), 0, 58 | System.Length(result)); 59 | end; 60 | 61 | procedure TSHA2_256.Initialize; 62 | begin 63 | FState[0] := $6A09E667; 64 | FState[1] := $BB67AE85; 65 | FState[2] := $3C6EF372; 66 | FState[3] := $A54FF53A; 67 | FState[4] := $510E527F; 68 | FState[5] := $9B05688C; 69 | FState[6] := $1F83D9AB; 70 | FState[7] := $5BE0CD19; 71 | Inherited Initialize(); 72 | end; 73 | 74 | end. 75 | -------------------------------------------------------------------------------- /HashLib/src/Hash32/HlpRS.pas: -------------------------------------------------------------------------------- 1 | unit HlpRS; 2 | 3 | {$I ..\Include\HashLib.inc} 4 | 5 | interface 6 | 7 | uses 8 | HlpHashLibTypes, 9 | HlpHash, 10 | HlpIHash, 11 | HlpIHashInfo, 12 | HlpHashResult, 13 | HlpIHashResult; 14 | 15 | type 16 | TRS = class sealed(THash, IHash32, ITransformBlock) 17 | strict private 18 | var 19 | FHash, FA: UInt32; 20 | 21 | const 22 | B = UInt32(378551); 23 | 24 | public 25 | constructor Create(); 26 | procedure Initialize(); override; 27 | procedure TransformBytes(const AData: THashLibByteArray; 28 | AIndex, ALength: Int32); override; 29 | function TransformFinal(): IHashResult; override; 30 | function Clone(): IHash; override; 31 | end; 32 | 33 | implementation 34 | 35 | { TRS } 36 | 37 | function TRS.Clone(): IHash; 38 | var 39 | LHashInstance: TRS; 40 | begin 41 | LHashInstance := TRS.Create(); 42 | LHashInstance.FHash := FHash; 43 | LHashInstance.FA := FA; 44 | result := LHashInstance as IHash; 45 | result.BufferSize := BufferSize; 46 | end; 47 | 48 | constructor TRS.Create; 49 | begin 50 | Inherited Create(4, 1); 51 | end; 52 | 53 | procedure TRS.Initialize; 54 | begin 55 | FHash := 0; 56 | FA := 63689; 57 | end; 58 | 59 | procedure TRS.TransformBytes(const AData: THashLibByteArray; 60 | AIndex, ALength: Int32); 61 | var 62 | LIdx: Int32; 63 | begin 64 | {$IFDEF DEBUG} 65 | System.Assert(AIndex >= 0); 66 | System.Assert(ALength >= 0); 67 | System.Assert(AIndex + ALength <= System.Length(AData)); 68 | {$ENDIF DEBUG} 69 | LIdx := AIndex; 70 | while ALength > 0 do 71 | begin 72 | FHash := (FHash * FA) + AData[LIdx]; 73 | FA := FA * B; 74 | System.Inc(LIdx); 75 | System.Dec(ALength); 76 | end; 77 | 78 | end; 79 | 80 | function TRS.TransformFinal: IHashResult; 81 | begin 82 | result := THashResult.Create(FHash); 83 | Initialize(); 84 | end; 85 | 86 | end. 87 | -------------------------------------------------------------------------------- /HashLib/src/Hash64/HlpFNV64.pas: -------------------------------------------------------------------------------- 1 | unit HlpFNV64; 2 | 3 | {$I ..\Include\HashLib.inc} 4 | 5 | interface 6 | 7 | uses 8 | HlpHashLibTypes, 9 | HlpHash, 10 | HlpIHash, 11 | HlpIHashInfo, 12 | HlpHashResult, 13 | HlpIHashResult; 14 | 15 | type 16 | TFNV64 = class sealed(THash, IHash64, ITransformBlock) 17 | strict private 18 | var 19 | FHash: UInt64; 20 | 21 | public 22 | constructor Create(); 23 | procedure Initialize(); override; 24 | procedure TransformBytes(const AData: THashLibByteArray; 25 | AIndex, ALength: Int32); override; 26 | function TransformFinal(): IHashResult; override; 27 | function Clone(): IHash; override; 28 | end; 29 | 30 | implementation 31 | 32 | { TFNV64 } 33 | 34 | function TFNV64.Clone(): IHash; 35 | var 36 | LHashInstance: TFNV64; 37 | begin 38 | LHashInstance := TFNV64.Create(); 39 | LHashInstance.FHash := FHash; 40 | result := LHashInstance as IHash; 41 | result.BufferSize := BufferSize; 42 | end; 43 | 44 | constructor TFNV64.Create; 45 | begin 46 | Inherited Create(8, 1); 47 | end; 48 | 49 | procedure TFNV64.Initialize; 50 | begin 51 | FHash := 0; 52 | end; 53 | 54 | procedure TFNV64.TransformBytes(const AData: THashLibByteArray; 55 | AIndex, ALength: Int32); 56 | var 57 | LIdx: Int32; 58 | begin 59 | {$IFDEF DEBUG} 60 | System.Assert(AIndex >= 0); 61 | System.Assert(ALength >= 0); 62 | System.Assert(AIndex + ALength <= System.Length(AData)); 63 | {$ENDIF DEBUG} 64 | LIdx := AIndex; 65 | while ALength > 0 do 66 | begin 67 | FHash := UInt64(FHash * UInt64(1099511628211)) xor AData[LIdx]; 68 | System.Inc(LIdx); 69 | System.Dec(ALength); 70 | end; 71 | 72 | end; 73 | 74 | function TFNV64.TransformFinal: IHashResult; 75 | begin 76 | result := THashResult.Create(FHash); 77 | Initialize(); 78 | end; 79 | 80 | end. 81 | -------------------------------------------------------------------------------- /HashLib/src/Packages/FPC/HashLib4PascalPackage.pas: -------------------------------------------------------------------------------- 1 | { This file was automatically created by Lazarus. Do not edit! 2 | This source is only used to compile and install the package. 3 | } 4 | 5 | unit HashLib4PascalPackage; 6 | 7 | {$warn 5023 off : no warning about unused units} 8 | interface 9 | 10 | uses 11 | HlpHash, HlpHashBuffer, HlpHashCryptoNotBuildIn, HlpHashFactory, 12 | HlpHashResult, HlpHashRounds, HlpHashSize, HlpHMACNotBuildInAdapter, 13 | HlpMultipleTransformNonBlock, HlpAdler32, HlpCRC, HlpCRC16, HlpCRC32, 14 | HlpCRC64, HlpGost, HlpGrindahl256, HlpGrindahl512, HlpHAS160, HlpHaval, 15 | HlpMD2, HlpMD4, HlpMD5, HlpMDBase, HlpPanama, HlpRadioGatun32, 16 | HlpRadioGatun64, HlpRIPEMD, HlpRIPEMD128, HlpRIPEMD160, HlpRIPEMD256, 17 | HlpRIPEMD320, HlpSHA0, HlpSHA1, HlpSHA2_224, HlpSHA2_256, HlpSHA2_256Base, 18 | HlpSHA2_384, HlpSHA2_512, HlpSHA2_512Base, HlpSHA2_512_224, HlpSHA2_512_256, 19 | HlpSHA3, HlpSnefru, HlpTiger, HlpTiger2, HlpWhirlPool, 20 | HlpMurmurHash3_x64_128, HlpNullDigest, HlpAP, HlpBernstein, HlpBernstein1, 21 | HlpBKDR, HlpDEK, HlpDJB, HlpELF, HlpFNV, HlpFNV1a, HlpJenkins3, HlpJS, 22 | HlpMurmur2, HlpMurmurHash3_x86_32, HlpOneAtTime, HlpPJW, HlpRotating, HlpRS, 23 | HlpSDBM, HlpShiftAndXor, HlpSuperFast, HlpXXHash32, HlpFNV1a64, HlpFNV64, 24 | HlpMurmur2_64, HlpSipHash, HlpXXHash64, HlpIHash, HlpIHashInfo, 25 | HlpIHashResult, HlpNullable, HlpConverters, HlpBitConverter, HlpBits, 26 | HlpHashLibTypes, HlpMurmurHash3_x86_128, HlpPBKDF2_HMACNotBuildInAdapter, 27 | HlpIKDF, HlpKDF, HlpICRC, HlpBlake2B, HlpBlake2S, HlpGOST3411_2012, 28 | HlpCRC32Fast, HlpArgon2TypeAndVersion, HlpPBKDF_Argon2NotBuildInAdapter, 29 | HlpPBKDF_ScryptNotBuildInAdapter, HlpArrayUtils, HlpBlake2BP, HlpBlake2SP, 30 | HlpSipHash128, HlpBlake2SParams, HlpBlake2BParams, HlpIBlake2SParams, 31 | HlpIBlake2BParams, HlpBlake3; 32 | 33 | implementation 34 | 35 | end. 36 | -------------------------------------------------------------------------------- /HashLib/src/Hash64/HlpFNV1a64.pas: -------------------------------------------------------------------------------- 1 | unit HlpFNV1a64; 2 | 3 | {$I ..\Include\HashLib.inc} 4 | 5 | interface 6 | 7 | uses 8 | HlpHashLibTypes, 9 | HlpHash, 10 | HlpIHash, 11 | HlpIHashInfo, 12 | HlpHashResult, 13 | HlpIHashResult; 14 | 15 | type 16 | TFNV1a64 = class sealed(THash, IHash64, ITransformBlock) 17 | strict private 18 | var 19 | FHash: UInt64; 20 | 21 | public 22 | constructor Create(); 23 | procedure Initialize(); override; 24 | procedure TransformBytes(const AData: THashLibByteArray; 25 | AIndex, ALength: Int32); override; 26 | function TransformFinal(): IHashResult; override; 27 | function Clone(): IHash; override; 28 | end; 29 | 30 | implementation 31 | 32 | { TFNV1a64 } 33 | 34 | function TFNV1a64.Clone(): IHash; 35 | var 36 | LHashInstance: TFNV1a64; 37 | begin 38 | LHashInstance := TFNV1a64.Create(); 39 | LHashInstance.FHash := FHash; 40 | result := LHashInstance as IHash; 41 | result.BufferSize := BufferSize; 42 | end; 43 | 44 | constructor TFNV1a64.Create; 45 | begin 46 | Inherited Create(8, 1); 47 | end; 48 | 49 | procedure TFNV1a64.Initialize; 50 | begin 51 | FHash := 14695981039346656037; 52 | end; 53 | 54 | procedure TFNV1a64.TransformBytes(const AData: THashLibByteArray; 55 | AIndex, ALength: Int32); 56 | var 57 | LIdx: Int32; 58 | begin 59 | {$IFDEF DEBUG} 60 | System.Assert(AIndex >= 0); 61 | System.Assert(ALength >= 0); 62 | System.Assert(AIndex + ALength <= System.Length(AData)); 63 | {$ENDIF DEBUG} 64 | LIdx := AIndex; 65 | while ALength > 0 do 66 | begin 67 | FHash := (FHash xor AData[LIdx]) * UInt64(1099511628211); 68 | System.Inc(LIdx); 69 | System.Dec(ALength); 70 | end; 71 | 72 | end; 73 | 74 | function TFNV1a64.TransformFinal: IHashResult; 75 | begin 76 | result := THashResult.Create(FHash); 77 | Initialize(); 78 | end; 79 | 80 | end. 81 | -------------------------------------------------------------------------------- /HashLib/src/Hash32/HlpELF.pas: -------------------------------------------------------------------------------- 1 | unit HlpELF; 2 | 3 | {$I ..\Include\HashLib.inc} 4 | 5 | interface 6 | 7 | uses 8 | HlpHashLibTypes, 9 | HlpHash, 10 | HlpIHash, 11 | HlpIHashInfo, 12 | HlpHashResult, 13 | HlpIHashResult; 14 | 15 | type 16 | TELF = class sealed(THash, IHash32, ITransformBlock) 17 | strict private 18 | var 19 | FHash: UInt32; 20 | 21 | public 22 | constructor Create(); 23 | procedure Initialize(); override; 24 | procedure TransformBytes(const AData: THashLibByteArray; 25 | AIndex, ALength: Int32); override; 26 | function TransformFinal(): IHashResult; override; 27 | function Clone(): IHash; override; 28 | end; 29 | 30 | implementation 31 | 32 | { TELF } 33 | 34 | function TELF.Clone(): IHash; 35 | var 36 | LHashInstance: TELF; 37 | begin 38 | LHashInstance := TELF.Create(); 39 | LHashInstance.FHash := FHash; 40 | result := LHashInstance as IHash; 41 | result.BufferSize := BufferSize; 42 | end; 43 | 44 | constructor TELF.Create; 45 | begin 46 | Inherited Create(4, 1); 47 | end; 48 | 49 | procedure TELF.Initialize; 50 | begin 51 | FHash := 0; 52 | end; 53 | 54 | procedure TELF.TransformBytes(const AData: THashLibByteArray; 55 | AIndex, ALength: Int32); 56 | var 57 | LIdx: Int32; 58 | LG: UInt32; 59 | begin 60 | {$IFDEF DEBUG} 61 | System.Assert(AIndex >= 0); 62 | System.Assert(ALength >= 0); 63 | System.Assert(AIndex + ALength <= System.Length(AData)); 64 | {$ENDIF DEBUG} 65 | LIdx := AIndex; 66 | while ALength > 0 do 67 | begin 68 | FHash := (FHash shl 4) + AData[LIdx]; 69 | LG := FHash and $F0000000; 70 | 71 | if (LG <> 0) then 72 | begin 73 | FHash := FHash xor (LG shr 24); 74 | end; 75 | 76 | FHash := FHash and (not LG); 77 | System.Inc(LIdx); 78 | System.Dec(ALength); 79 | end; 80 | end; 81 | 82 | function TELF.TransformFinal: IHashResult; 83 | begin 84 | result := THashResult.Create(FHash); 85 | Initialize(); 86 | end; 87 | 88 | end. 89 | -------------------------------------------------------------------------------- /HashLib/src/Crypto/HlpSHA2_512.pas: -------------------------------------------------------------------------------- 1 | unit HlpSHA2_512; 2 | 3 | {$I ..\Include\HashLib.inc} 4 | 5 | interface 6 | 7 | uses 8 | {$IFDEF DELPHI2010} 9 | SysUtils, // to get rid of compiler hint "not inlined" on Delphi 2010. 10 | {$ENDIF DELPHI2010} 11 | HlpHashLibTypes, 12 | {$IFDEF DELPHI} 13 | HlpHashBuffer, 14 | HlpHash, 15 | {$ENDIF DELPHI} 16 | HlpIHash, 17 | HlpSHA2_512Base, 18 | HlpConverters; 19 | 20 | type 21 | TSHA2_512 = class sealed(TSHA2_512Base) 22 | 23 | strict protected 24 | function GetResult(): THashLibByteArray; override; 25 | 26 | public 27 | constructor Create(); 28 | procedure Initialize(); override; 29 | function Clone(): IHash; override; 30 | 31 | end; 32 | 33 | implementation 34 | 35 | { TSHA2_512 } 36 | 37 | function TSHA2_512.Clone(): IHash; 38 | var 39 | LHashInstance: TSHA2_512; 40 | begin 41 | LHashInstance := TSHA2_512.Create(); 42 | LHashInstance.FState := System.Copy(FState); 43 | LHashInstance.FBuffer := FBuffer.Clone(); 44 | LHashInstance.FProcessedBytesCount := FProcessedBytesCount; 45 | result := LHashInstance as IHash; 46 | result.BufferSize := BufferSize; 47 | end; 48 | 49 | constructor TSHA2_512.Create; 50 | begin 51 | Inherited Create(64); 52 | end; 53 | 54 | function TSHA2_512.GetResult: THashLibByteArray; 55 | begin 56 | System.SetLength(result, 8 * System.SizeOf(UInt64)); 57 | TConverters.be64_copy(PUInt64(FState), 0, PByte(result), 0, 58 | System.Length(result)); 59 | end; 60 | 61 | procedure TSHA2_512.Initialize; 62 | begin 63 | FState[0] := $6A09E667F3BCC908; 64 | FState[1] := UInt64($BB67AE8584CAA73B); 65 | FState[2] := $3C6EF372FE94F82B; 66 | FState[3] := UInt64($A54FF53A5F1D36F1); 67 | FState[4] := $510E527FADE682D1; 68 | FState[5] := UInt64($9B05688C2B3E6C1F); 69 | FState[6] := $1F83D9ABFB41BD6B; 70 | FState[7] := $5BE0CD19137E2179; 71 | Inherited Initialize(); 72 | end; 73 | 74 | end. 75 | -------------------------------------------------------------------------------- /HashLib/src/Crypto/HlpSHA2_384.pas: -------------------------------------------------------------------------------- 1 | unit HlpSHA2_384; 2 | 3 | {$I ..\Include\HashLib.inc} 4 | 5 | interface 6 | 7 | uses 8 | {$IFDEF DELPHI2010} 9 | SysUtils, // to get rid of compiler hint "not inlined" on Delphi 2010. 10 | {$ENDIF DELPHI2010} 11 | HlpHashLibTypes, 12 | {$IFDEF DELPHI} 13 | HlpHashBuffer, 14 | HlpHash, 15 | {$ENDIF DELPHI} 16 | HlpIHash, 17 | HlpSHA2_512Base, 18 | HlpConverters; 19 | 20 | type 21 | TSHA2_384 = class sealed(TSHA2_512Base) 22 | 23 | strict protected 24 | function GetResult(): THashLibByteArray; override; 25 | 26 | public 27 | constructor Create(); 28 | procedure Initialize(); override; 29 | function Clone(): IHash; override; 30 | 31 | end; 32 | 33 | implementation 34 | 35 | { TSHA2_384 } 36 | 37 | function TSHA2_384.Clone(): IHash; 38 | var 39 | LHashInstance: TSHA2_384; 40 | begin 41 | LHashInstance := TSHA2_384.Create(); 42 | LHashInstance.FState := System.Copy(FState); 43 | LHashInstance.FBuffer := FBuffer.Clone(); 44 | LHashInstance.FProcessedBytesCount := FProcessedBytesCount; 45 | result := LHashInstance as IHash; 46 | result.BufferSize := BufferSize; 47 | end; 48 | 49 | constructor TSHA2_384.Create; 50 | begin 51 | Inherited Create(48); 52 | end; 53 | 54 | function TSHA2_384.GetResult: THashLibByteArray; 55 | begin 56 | System.SetLength(result, 6 * System.SizeOf(UInt64)); 57 | TConverters.be64_copy(PUInt64(FState), 0, PByte(result), 0, 58 | System.Length(result)); 59 | end; 60 | 61 | procedure TSHA2_384.Initialize; 62 | begin 63 | FState[0] := UInt64($CBBB9D5DC1059ED8); 64 | FState[1] := $629A292A367CD507; 65 | FState[2] := UInt64($9159015A3070DD17); 66 | FState[3] := $152FECD8F70E5939; 67 | FState[4] := $67332667FFC00B31; 68 | FState[5] := UInt64($8EB44A8768581511); 69 | FState[6] := UInt64($DB0C2E0D64F98FA7); 70 | FState[7] := $47B5481DBEFA4FA4; 71 | Inherited Initialize(); 72 | end; 73 | 74 | end. 75 | -------------------------------------------------------------------------------- /HashLib/src/Crypto/HlpSHA2_512_256.pas: -------------------------------------------------------------------------------- 1 | unit HlpSHA2_512_256; 2 | 3 | {$I ..\Include\HashLib.inc} 4 | 5 | interface 6 | 7 | uses 8 | {$IFDEF DELPHI2010} 9 | SysUtils, // to get rid of compiler hint "not inlined" on Delphi 2010. 10 | {$ENDIF DELPHI2010} 11 | HlpHashLibTypes, 12 | {$IFDEF DELPHI} 13 | HlpHashBuffer, 14 | HlpHash, 15 | {$ENDIF DELPHI} 16 | HlpIHash, 17 | HlpSHA2_512Base, 18 | HlpConverters; 19 | 20 | type 21 | TSHA2_512_256 = class sealed(TSHA2_512Base) 22 | 23 | strict protected 24 | function GetResult(): THashLibByteArray; override; 25 | 26 | public 27 | constructor Create(); 28 | procedure Initialize(); override; 29 | function Clone(): IHash; override; 30 | 31 | end; 32 | 33 | implementation 34 | 35 | { TSHA2_512_256 } 36 | 37 | function TSHA2_512_256.Clone(): IHash; 38 | var 39 | LHashInstance: TSHA2_512_256; 40 | begin 41 | LHashInstance := TSHA2_512_256.Create(); 42 | LHashInstance.FState := System.Copy(FState); 43 | LHashInstance.FBuffer := FBuffer.Clone(); 44 | LHashInstance.FProcessedBytesCount := FProcessedBytesCount; 45 | result := LHashInstance as IHash; 46 | result.BufferSize := BufferSize; 47 | end; 48 | 49 | constructor TSHA2_512_256.Create; 50 | begin 51 | Inherited Create(32); 52 | end; 53 | 54 | function TSHA2_512_256.GetResult: THashLibByteArray; 55 | begin 56 | System.SetLength(result, 4 * System.SizeOf(UInt64)); 57 | TConverters.be64_copy(PUInt64(FState), 0, PByte(result), 0, 58 | System.Length(result)); 59 | end; 60 | 61 | procedure TSHA2_512_256.Initialize; 62 | begin 63 | FState[0] := $22312194FC2BF72C; 64 | FState[1] := UInt64($9F555FA3C84C64C2); 65 | FState[2] := $2393B86B6F53B151; 66 | FState[3] := UInt64($963877195940EABD); 67 | FState[4] := UInt64($96283EE2A88EFFE3); 68 | FState[5] := UInt64($BE5E1E2553863992); 69 | FState[6] := $2B0199FC2C85B8AA; 70 | FState[7] := $0EB72DDC81C52CA2; 71 | Inherited Initialize(); 72 | end; 73 | 74 | end. 75 | -------------------------------------------------------------------------------- /HashLib/src/Hash32/HlpOneAtTime.pas: -------------------------------------------------------------------------------- 1 | unit HlpOneAtTime; 2 | 3 | {$I ..\Include\HashLib.inc} 4 | 5 | interface 6 | 7 | uses 8 | HlpHashLibTypes, 9 | HlpHash, 10 | HlpIHash, 11 | HlpIHashInfo, 12 | HlpHashResult, 13 | HlpIHashResult; 14 | 15 | type 16 | TOneAtTime = class sealed(THash, IHash32, ITransformBlock) 17 | strict private 18 | var 19 | FHash: UInt32; 20 | 21 | public 22 | constructor Create(); 23 | procedure Initialize(); override; 24 | procedure TransformBytes(const AData: THashLibByteArray; 25 | AIndex, ALength: Int32); override; 26 | function TransformFinal(): IHashResult; override; 27 | function Clone(): IHash; override; 28 | end; 29 | 30 | implementation 31 | 32 | { TOneAtTime } 33 | 34 | function TOneAtTime.Clone(): IHash; 35 | var 36 | LHashInstance: TOneAtTime; 37 | begin 38 | LHashInstance := TOneAtTime.Create(); 39 | LHashInstance.FHash := FHash; 40 | result := LHashInstance as IHash; 41 | result.BufferSize := BufferSize; 42 | end; 43 | 44 | constructor TOneAtTime.Create; 45 | begin 46 | Inherited Create(4, 1); 47 | end; 48 | 49 | procedure TOneAtTime.Initialize; 50 | begin 51 | FHash := 0; 52 | end; 53 | 54 | procedure TOneAtTime.TransformBytes(const AData: THashLibByteArray; 55 | AIndex, ALength: Int32); 56 | var 57 | LIdx: Int32; 58 | begin 59 | {$IFDEF DEBUG} 60 | System.Assert(AIndex >= 0); 61 | System.Assert(ALength >= 0); 62 | System.Assert(AIndex + ALength <= System.Length(AData)); 63 | {$ENDIF DEBUG} 64 | LIdx := AIndex; 65 | while ALength > 0 do 66 | begin 67 | FHash := FHash + AData[LIdx]; 68 | FHash := FHash + (FHash shl 10); 69 | FHash := FHash xor (FHash shr 6); 70 | System.Inc(LIdx); 71 | System.Dec(ALength); 72 | end; 73 | end; 74 | 75 | function TOneAtTime.TransformFinal: IHashResult; 76 | begin 77 | FHash := FHash + (FHash shl 3); 78 | FHash := FHash xor (FHash shr 11); 79 | FHash := FHash + (FHash shl 15); 80 | 81 | result := THashResult.Create(FHash); 82 | Initialize(); 83 | end; 84 | 85 | end. 86 | -------------------------------------------------------------------------------- /HashLib/src/Crypto/HlpSHA2_512_224.pas: -------------------------------------------------------------------------------- 1 | unit HlpSHA2_512_224; 2 | 3 | {$I ..\Include\HashLib.inc} 4 | 5 | interface 6 | 7 | uses 8 | {$IFDEF DELPHI2010} 9 | SysUtils, // to get rid of compiler hint "not inlined" on Delphi 2010. 10 | {$ENDIF DELPHI2010} 11 | HlpHashLibTypes, 12 | {$IFDEF DELPHI} 13 | HlpHashBuffer, 14 | HlpHash, 15 | {$ENDIF DELPHI} 16 | HlpIHash, 17 | HlpSHA2_512Base, 18 | HlpConverters; 19 | 20 | type 21 | TSHA2_512_224 = class sealed(TSHA2_512Base) 22 | 23 | strict protected 24 | function GetResult(): THashLibByteArray; override; 25 | 26 | public 27 | constructor Create(); 28 | procedure Initialize(); override; 29 | function Clone(): IHash; override; 30 | 31 | end; 32 | 33 | implementation 34 | 35 | { TSHA2_512_224 } 36 | 37 | function TSHA2_512_224.Clone(): IHash; 38 | var 39 | LHashInstance: TSHA2_512_224; 40 | begin 41 | LHashInstance := TSHA2_512_224.Create(); 42 | LHashInstance.FState := System.Copy(FState); 43 | LHashInstance.FBuffer := FBuffer.Clone(); 44 | LHashInstance.FProcessedBytesCount := FProcessedBytesCount; 45 | result := LHashInstance as IHash; 46 | result.BufferSize := BufferSize; 47 | end; 48 | 49 | constructor TSHA2_512_224.Create; 50 | begin 51 | Inherited Create(28); 52 | end; 53 | 54 | function TSHA2_512_224.GetResult: THashLibByteArray; 55 | begin 56 | System.SetLength(result, 4 * System.SizeOf(UInt64)); 57 | TConverters.be64_copy(PUInt64(FState), 0, PByte(result), 0, 58 | System.Length(result)); 59 | System.SetLength(result, HashSize * System.SizeOf(Byte)); 60 | end; 61 | 62 | procedure TSHA2_512_224.Initialize; 63 | begin 64 | FState[0] := UInt64($8C3D37C819544DA2); 65 | FState[1] := $73E1996689DCD4D6; 66 | FState[2] := $1DFAB7AE32FF9C82; 67 | FState[3] := $679DD514582F9FCF; 68 | FState[4] := $0F6D2B697BD44DA8; 69 | FState[5] := $77E36F7304C48942; 70 | FState[6] := $3F9D85A86A1D36C8; 71 | FState[7] := $1112E6AD91D692A1; 72 | Inherited Initialize(); 73 | end; 74 | 75 | end. 76 | -------------------------------------------------------------------------------- /.github/workflows/make.yml: -------------------------------------------------------------------------------- 1 | name: Make 2 | 3 | on: 4 | schedule: 5 | - cron: '0 0 1 * *' 6 | push: 7 | branches: 8 | - "**" 9 | pull_request: 10 | branches: 11 | - master 12 | - main 13 | 14 | concurrency: 15 | group: ${{ github.workflow }}-${{ github.ref }} 16 | cancel-in-progress: true 17 | 18 | jobs: 19 | build: 20 | runs-on: ${{ matrix.os }} 21 | timeout-minutes: 120 22 | strategy: 23 | matrix: 24 | os: 25 | - ubuntu-latest 26 | - windows-latest 27 | 28 | steps: 29 | - name: Checkout 30 | uses: actions/checkout@v4 31 | with: 32 | submodules: true 33 | 34 | - name: Build on Linux 35 | if: runner.os == 'Linux' 36 | shell: bash 37 | run: | 38 | set -xeuo pipefail 39 | sudo bash -c 'apt-get update; apt-get install -y lazarus' >/dev/null 40 | instantfpc -Fu/usr/lib/lazarus/*/components/lazutils .github/workflows/make.pas 41 | 42 | - name: Build on Windows 43 | if: runner.os == 'Windows' 44 | shell: powershell 45 | run: | 46 | $ErrorActionPreference = 'stop' 47 | Set-PSDebug -Strict 48 | 49 | Write-Host "Installing Lazarus and OpenSSL 1.1 via Chocolatey..." 50 | choco upgrade chocolatey -y 51 | choco install lazarus -y 52 | choco install openssl.light --version=1.1.1.20181020 -y 53 | 54 | Write-Host "Verifying installed packages..." 55 | choco list 56 | 57 | # Lazarus installs to C:\Lazarus by default 58 | # Add Lazarus and OpenSSL paths for instantfpc 59 | $env:Path += ';C:\Lazarus;C:\Lazarus\fpc\3.2.2\bin\x86_64-win64;C:\ProgramData\chocolatey\lib\openssl.light\tools' 60 | 61 | Write-Host "Checking lazbuild and instantfpc availability..." 62 | Get-Command lazbuild 63 | Get-Command instantfpc 64 | 65 | Write-Host "Building make.pas..." 66 | instantfpc '-FuC:\Lazarus\components\lazutils' .github/workflows/make.pas 67 | -------------------------------------------------------------------------------- /HashLib/src/Interfaces/HlpIHash.pas: -------------------------------------------------------------------------------- 1 | unit HlpIHash; 2 | 3 | {$I ..\Include\HashLib.inc} 4 | 5 | interface 6 | 7 | uses 8 | SysUtils, 9 | Classes, 10 | HlpHashLibTypes, 11 | HlpIHashResult; 12 | 13 | type 14 | IHash = interface(IInterface) 15 | ['{E91711E1-FD37-4AFD-875A-DD122470D269}'] 16 | 17 | function GetBlockSize: Int32; 18 | property BlockSize: Int32 read GetBlockSize; 19 | 20 | function GetHashSize: Int32; 21 | property HashSize: Int32 read GetHashSize; 22 | 23 | function GetBufferSize: Int32; 24 | procedure SetBufferSize(AValue: Int32); 25 | property BufferSize: Int32 read GetBufferSize write SetBufferSize; 26 | 27 | function GetName: String; 28 | property Name: String read GetName; 29 | 30 | procedure Initialize(); 31 | 32 | procedure TransformString(const AData: String; const AEncoding: TEncoding); 33 | procedure TransformBytes(const AData: THashLibByteArray); overload; 34 | procedure TransformBytes(const AData: THashLibByteArray; 35 | AIndex: Int32); overload; 36 | procedure TransformBytes(const AData: THashLibByteArray; AIndex: Int32; 37 | ALength: Int32); overload; 38 | procedure TransformUntyped(const AData; ALength: Int64); 39 | procedure TransformStream(const AStream: TStream; ALength: Int64 = -1); 40 | procedure TransformFile(const AFileName: String; AFrom: Int64 = 0; 41 | ALength: Int64 = -1); 42 | 43 | function TransformFinal(): IHashResult; 44 | 45 | function ComputeString(const AData: String; AEncoding: TEncoding) 46 | : IHashResult; 47 | function ComputeBytes(const AData: THashLibByteArray): IHashResult; 48 | function ComputeUntyped(const AData; ALength: Int64): IHashResult; 49 | function ComputeStream(const AStream: TStream; ALength: Int64 = -1) 50 | : IHashResult; 51 | function ComputeFile(const AFileName: String; AFrom: Int64 = 0; 52 | ALength: Int64 = -1): IHashResult; 53 | 54 | function Clone(): IHash; 55 | 56 | end; 57 | 58 | implementation 59 | 60 | end. 61 | -------------------------------------------------------------------------------- /HashLib/src/Hash32/HlpAP.pas: -------------------------------------------------------------------------------- 1 | unit HlpAP; 2 | 3 | {$I ..\Include\HashLib.inc} 4 | 5 | interface 6 | 7 | uses 8 | HlpHashLibTypes, 9 | HlpHash, 10 | HlpIHash, 11 | HlpIHashInfo, 12 | HlpHashResult, 13 | HlpIHashResult; 14 | 15 | type 16 | TAP = class sealed(THash, IHash32, ITransformBlock) 17 | strict private 18 | var 19 | FHash: UInt32; 20 | FIndex: Int32; 21 | 22 | public 23 | constructor Create(); 24 | procedure Initialize(); override; 25 | procedure TransformBytes(const AData: THashLibByteArray; 26 | AIndex, ALength: Int32); override; 27 | function TransformFinal(): IHashResult; override; 28 | function Clone(): IHash; override; 29 | 30 | end; 31 | 32 | implementation 33 | 34 | { TAP } 35 | 36 | function TAP.Clone(): IHash; 37 | var 38 | LHashInstance: TAP; 39 | begin 40 | LHashInstance := TAP.Create(); 41 | LHashInstance.FHash := FHash; 42 | LHashInstance.FIndex := FIndex; 43 | result := LHashInstance as IHash; 44 | result.BufferSize := BufferSize; 45 | end; 46 | 47 | constructor TAP.Create; 48 | begin 49 | Inherited Create(4, 1); 50 | end; 51 | 52 | procedure TAP.Initialize; 53 | begin 54 | FHash := $AAAAAAAA; 55 | FIndex := 0; 56 | end; 57 | 58 | procedure TAP.TransformBytes(const AData: THashLibByteArray; 59 | AIndex, ALength: Int32); 60 | var 61 | LIdx: Int32; 62 | begin 63 | {$IFDEF DEBUG} 64 | System.Assert(AIndex >= 0); 65 | System.Assert(ALength >= 0); 66 | System.Assert(AIndex + ALength <= System.Length(AData)); 67 | {$ENDIF DEBUG} 68 | LIdx := AIndex; 69 | while ALength > 0 do 70 | begin 71 | 72 | if (FIndex and 1) = 0 then 73 | begin 74 | FHash := FHash xor ((FHash shl 7) xor AData[LIdx] * (FHash shr 3)) 75 | end 76 | else 77 | begin 78 | FHash := FHash xor 79 | (not((FHash shl 11) xor AData[LIdx] xor (FHash shr 5))); 80 | end; 81 | 82 | System.Inc(FIndex); 83 | System.Inc(LIdx); 84 | System.Dec(ALength); 85 | end; 86 | end; 87 | 88 | function TAP.TransformFinal: IHashResult; 89 | begin 90 | result := THashResult.Create(FHash); 91 | Initialize(); 92 | end; 93 | 94 | end. 95 | -------------------------------------------------------------------------------- /HashLib/src/Hash32/HlpPJW.pas: -------------------------------------------------------------------------------- 1 | unit HlpPJW; 2 | 3 | {$I ..\Include\HashLib.inc} 4 | 5 | interface 6 | 7 | uses 8 | HlpHashLibTypes, 9 | HlpHash, 10 | HlpIHash, 11 | HlpIHashInfo, 12 | HlpHashResult, 13 | HlpIHashResult; 14 | 15 | type 16 | TPJW = class sealed(THash, IHash32, ITransformBlock) 17 | strict private 18 | var 19 | FHash: UInt32; 20 | 21 | const 22 | UInt32MaxValue = UInt32(4294967295); 23 | BitsInUnsignedInt = Int32(System.SizeOf(UInt32) * 8); 24 | ThreeQuarters = Int32(BitsInUnsignedInt * 3) shr 2; 25 | OneEighth = Int32(BitsInUnsignedInt shr 3); 26 | HighBits = UInt32(UInt32MaxValue shl (BitsInUnsignedInt - OneEighth)); 27 | 28 | public 29 | constructor Create(); 30 | procedure Initialize(); override; 31 | procedure TransformBytes(const AData: THashLibByteArray; 32 | AIndex, ALength: Int32); override; 33 | function TransformFinal(): IHashResult; override; 34 | function Clone(): IHash; override; 35 | end; 36 | 37 | implementation 38 | 39 | { TPJW } 40 | 41 | function TPJW.Clone(): IHash; 42 | var 43 | LHashInstance: TPJW; 44 | begin 45 | LHashInstance := TPJW.Create(); 46 | LHashInstance.FHash := FHash; 47 | result := LHashInstance as IHash; 48 | result.BufferSize := BufferSize; 49 | end; 50 | 51 | constructor TPJW.Create; 52 | begin 53 | Inherited Create(4, 1); 54 | end; 55 | 56 | procedure TPJW.Initialize; 57 | begin 58 | FHash := 0; 59 | end; 60 | 61 | procedure TPJW.TransformBytes(const AData: THashLibByteArray; 62 | AIndex, ALength: Int32); 63 | var 64 | LIdx: Int32; 65 | LTest: UInt32; 66 | begin 67 | {$IFDEF DEBUG} 68 | System.Assert(AIndex >= 0); 69 | System.Assert(ALength >= 0); 70 | System.Assert(AIndex + ALength <= System.Length(AData)); 71 | {$ENDIF DEBUG} 72 | LIdx := AIndex; 73 | while ALength > 0 do 74 | begin 75 | FHash := (FHash shl OneEighth) + AData[LIdx]; 76 | LTest := FHash and HighBits; 77 | if (LTest <> 0) then 78 | begin 79 | FHash := ((FHash xor (LTest shr ThreeQuarters)) and (not HighBits)); 80 | end; 81 | System.Inc(LIdx); 82 | System.Dec(ALength); 83 | end; 84 | end; 85 | 86 | function TPJW.TransformFinal: IHashResult; 87 | begin 88 | result := THashResult.Create(FHash); 89 | Initialize(); 90 | end; 91 | 92 | end. 93 | -------------------------------------------------------------------------------- /HashLib/src/Checksum/HlpCRC16.pas: -------------------------------------------------------------------------------- 1 | unit HlpCRC16; 2 | 3 | {$I ..\Include\HashLib.inc} 4 | 5 | interface 6 | 7 | uses 8 | HlpHashLibTypes, 9 | HlpHash, 10 | HlpICRC, 11 | HlpIHashResult, 12 | HlpIHashInfo, 13 | HlpCRC; 14 | 15 | type 16 | 17 | TCRC16Polynomials = class sealed(TObject) 18 | 19 | private 20 | 21 | const 22 | 23 | BUYPASS = UInt16($8005); 24 | 25 | end; 26 | 27 | TCRC16 = class(THash, IChecksum, IHash16, ITransformBlock) 28 | 29 | strict private 30 | var 31 | FCRCAlgorithm: ICRC; 32 | 33 | public 34 | constructor Create(APolynomial, AInitial: UInt64; 35 | AIsInputReflected, AIsOutputReflected: Boolean; 36 | AOutputXor, ACheckValue: UInt64; const ANames: THashLibStringArray); 37 | 38 | procedure Initialize(); override; 39 | procedure TransformBytes(const AData: THashLibByteArray; 40 | AIndex, ALength: Int32); override; 41 | function TransformFinal(): IHashResult; override; 42 | 43 | end; 44 | 45 | TCRC16_BUYPASS = class sealed(TCRC16) 46 | 47 | public 48 | constructor Create(); 49 | 50 | end; 51 | 52 | implementation 53 | 54 | { TCRC16 } 55 | 56 | constructor TCRC16.Create(APolynomial, AInitial: UInt64; 57 | AIsInputReflected, AIsOutputReflected: Boolean; 58 | AOutputXor, ACheckValue: UInt64; const ANames: THashLibStringArray); 59 | begin 60 | Inherited Create(2, 1); 61 | FCRCAlgorithm := TCRC.Create(16, APolynomial, AInitial, AIsInputReflected, 62 | AIsOutputReflected, AOutputXor, ACheckValue, ANames); 63 | end; 64 | 65 | procedure TCRC16.Initialize; 66 | begin 67 | FCRCAlgorithm.Initialize; 68 | end; 69 | 70 | procedure TCRC16.TransformBytes(const AData: THashLibByteArray; 71 | AIndex, ALength: Int32); 72 | begin 73 | FCRCAlgorithm.TransformBytes(AData, AIndex, ALength); 74 | end; 75 | 76 | function TCRC16.TransformFinal: IHashResult; 77 | begin 78 | result := FCRCAlgorithm.TransformFinal(); 79 | end; 80 | 81 | { TCRC16_BUYPASS } 82 | 83 | constructor TCRC16_BUYPASS.Create; 84 | begin 85 | Inherited Create(TCRC16Polynomials.BUYPASS, $0000, false, false, $0000, $FEE8, 86 | THashLibStringArray.Create('CRC-16/BUYPASS', 'CRC-16/VERIFONE', 87 | 'CRC-16/UMTS')); 88 | end; 89 | 90 | end. 91 | -------------------------------------------------------------------------------- /HashLib.Tests/src/PBKDF2_HMACTests.pas: -------------------------------------------------------------------------------- 1 | unit PBKDF2_HMACTests; 2 | 3 | interface 4 | 5 | uses 6 | SysUtils, 7 | {$IFDEF FPC} 8 | fpcunit, 9 | testregistry, 10 | {$ELSE} 11 | TestFramework, 12 | {$ENDIF FPC} 13 | HlpHashFactory, 14 | HlpConverters, 15 | HashLibTestBase; 16 | 17 | type 18 | 19 | TTestPBKDF2_HMACSHA1 = class(TPBKDF2_HMACTestCase) 20 | 21 | protected 22 | procedure SetUp; override; 23 | procedure TearDown; override; 24 | 25 | end; 26 | 27 | type 28 | 29 | TTestPBKDF2_HMACSHA2_256 = class(TPBKDF2_HMACTestCase) 30 | 31 | protected 32 | procedure SetUp; override; 33 | procedure TearDown; override; 34 | 35 | end; 36 | 37 | implementation 38 | 39 | { TTestPBKDF2_HMACSHA1 } 40 | 41 | procedure TTestPBKDF2_HMACSHA1.SetUp; 42 | var 43 | Password, Salt: TBytes; 44 | begin 45 | inherited; 46 | ExpectedString := 'BFDE6BE94DF7E11DD409BCE20A0255EC327CB936FFE93643'; 47 | Password := TBytes.Create($70, $61, $73, $73, $77, $6F, $72, $64); 48 | Salt := TBytes.Create($78, $57, $8E, $5A, $5D, $63, $CB, $06); 49 | ByteCount := 24; 50 | PBKDF2_HMACInstance := TKDF.TPBKDF2_HMAC.CreatePBKDF2_HMAC 51 | (THashFactory.TCrypto.CreateSHA1(), Password, Salt, 2048); 52 | end; 53 | 54 | procedure TTestPBKDF2_HMACSHA1.TearDown; 55 | begin 56 | PBKDF2_HMACInstance := Nil; 57 | inherited; 58 | end; 59 | 60 | { TTestPBKDF2_HMACSHA2_256 } 61 | 62 | procedure TTestPBKDF2_HMACSHA2_256.SetUp; 63 | var 64 | Password, Salt: TBytes; 65 | begin 66 | inherited; 67 | ExpectedString := 68 | '0394A2EDE332C9A13EB82E9B24631604C31DF978B4E2F0FBD2C549944F9D79A5'; 69 | Password := TConverters.ConvertStringToBytes('password', TEncoding.UTF8); 70 | Salt := TConverters.ConvertStringToBytes('salt', TEncoding.UTF8); 71 | ByteCount := 32; 72 | PBKDF2_HMACInstance := TKDF.TPBKDF2_HMAC.CreatePBKDF2_HMAC 73 | (THashFactory.TCrypto.CreateSHA2_256(), Password, Salt, 100000); 74 | end; 75 | 76 | procedure TTestPBKDF2_HMACSHA2_256.TearDown; 77 | begin 78 | PBKDF2_HMACInstance := Nil; 79 | inherited; 80 | end; 81 | 82 | initialization 83 | 84 | // Register any test cases with the test runner 85 | 86 | {$IFDEF FPC} 87 | RegisterTest(TTestPBKDF2_HMACSHA1); 88 | RegisterTest(TTestPBKDF2_HMACSHA2_256); 89 | {$ELSE} 90 | RegisterTest(TTestPBKDF2_HMACSHA1.Suite); 91 | RegisterTest(TTestPBKDF2_HMACSHA2_256.Suite); 92 | {$ENDIF FPC} 93 | 94 | end. 95 | -------------------------------------------------------------------------------- /HashLib/src/Interfaces/IBlake2BParams/HlpIBlake2BParams.pas: -------------------------------------------------------------------------------- 1 | unit HlpIBlake2BParams; 2 | 3 | {$I ..\..\Include\HashLib.inc} 4 | 5 | interface 6 | 7 | uses 8 | HlpHashLibTypes; 9 | 10 | type 11 | IBlake2BConfig = interface(IInterface) 12 | ['{176861A3-B06E-4CA3-A1BB-DDEAFF40BFE1}'] 13 | function GetPersonalisation: THashLibByteArray; 14 | procedure SetPersonalisation(const AValue: THashLibByteArray); 15 | property Personalisation: THashLibByteArray read GetPersonalisation 16 | write SetPersonalisation; 17 | function GetSalt: THashLibByteArray; 18 | procedure SetSalt(const AValue: THashLibByteArray); 19 | property Salt: THashLibByteArray read GetSalt write SetSalt; 20 | function GetKey: THashLibByteArray; 21 | procedure SetKey(const AValue: THashLibByteArray); 22 | property Key: THashLibByteArray read GetKey write SetKey; 23 | function GetHashSize: Int32; 24 | procedure SetHashSize(AValue: Int32); 25 | property HashSize: Int32 read GetHashSize write SetHashSize; 26 | 27 | function Clone(): IBlake2BConfig; 28 | 29 | procedure Clear(); 30 | 31 | end; 32 | 33 | type 34 | IBlake2BTreeConfig = interface(IInterface) 35 | ['{3EFB1A70-4478-4375-BAF6-EF17B3673DA8}'] 36 | 37 | function GetFanOut: Byte; 38 | procedure SetFanOut(AValue: Byte); 39 | property FanOut: Byte read GetFanOut write SetFanOut; 40 | 41 | function GetMaxDepth: Byte; 42 | procedure SetMaxDepth(AValue: Byte); 43 | property MaxDepth: Byte read GetMaxDepth write SetMaxDepth; 44 | 45 | function GetNodeDepth: Byte; 46 | procedure SetNodeDepth(AValue: Byte); 47 | property NodeDepth: Byte read GetNodeDepth write SetNodeDepth; 48 | 49 | function GetInnerHashSize: Byte; 50 | procedure SetInnerHashSize(AValue: Byte); 51 | property InnerHashSize: Byte read GetInnerHashSize write SetInnerHashSize; 52 | 53 | function GetLeafSize: UInt32; 54 | procedure SetLeafSize(AValue: UInt32); 55 | property LeafSize: UInt32 read GetLeafSize write SetLeafSize; 56 | 57 | function GetNodeOffset: UInt64; 58 | procedure SetNodeOffset(AValue: UInt64); 59 | property NodeOffset: UInt64 read GetNodeOffset write SetNodeOffset; 60 | 61 | function GetIsLastNode: Boolean; 62 | procedure SetIsLastNode(AValue: Boolean); 63 | property IsLastNode: Boolean read GetIsLastNode write SetIsLastNode; 64 | 65 | function Clone(): IBlake2BTreeConfig; 66 | 67 | end; 68 | 69 | implementation 70 | 71 | end. 72 | -------------------------------------------------------------------------------- /HashLib/src/Interfaces/IBlake2SParams/HlpIBlake2SParams.pas: -------------------------------------------------------------------------------- 1 | unit HlpIBlake2SParams; 2 | 3 | {$I ..\..\Include\HashLib.inc} 4 | 5 | interface 6 | 7 | uses 8 | HlpHashLibTypes; 9 | 10 | type 11 | IBlake2SConfig = interface(IInterface) 12 | ['{C78DE94A-0290-467D-BE26-D0AD1639076C}'] 13 | function GetPersonalisation: THashLibByteArray; 14 | procedure SetPersonalisation(const AValue: THashLibByteArray); 15 | property Personalisation: THashLibByteArray read GetPersonalisation 16 | write SetPersonalisation; 17 | function GetSalt: THashLibByteArray; 18 | procedure SetSalt(const AValue: THashLibByteArray); 19 | property Salt: THashLibByteArray read GetSalt write SetSalt; 20 | function GetKey: THashLibByteArray; 21 | procedure SetKey(const AValue: THashLibByteArray); 22 | property Key: THashLibByteArray read GetKey write SetKey; 23 | function GetHashSize: Int32; 24 | procedure SetHashSize(AValue: Int32); 25 | property HashSize: Int32 read GetHashSize write SetHashSize; 26 | 27 | function Clone(): IBlake2SConfig; 28 | 29 | procedure Clear(); 30 | 31 | end; 32 | 33 | type 34 | IBlake2STreeConfig = interface(IInterface) 35 | ['{93635D4F-7104-4E4A-BE9B-C608606F620F}'] 36 | 37 | function GetFanOut: Byte; 38 | procedure SetFanOut(AValue: Byte); 39 | property FanOut: Byte read GetFanOut write SetFanOut; 40 | 41 | function GetMaxDepth: Byte; 42 | procedure SetMaxDepth(AValue: Byte); 43 | property MaxDepth: Byte read GetMaxDepth write SetMaxDepth; 44 | 45 | function GetNodeDepth: Byte; 46 | procedure SetNodeDepth(AValue: Byte); 47 | property NodeDepth: Byte read GetNodeDepth write SetNodeDepth; 48 | 49 | function GetInnerHashSize: Byte; 50 | procedure SetInnerHashSize(AValue: Byte); 51 | property InnerHashSize: Byte read GetInnerHashSize write SetInnerHashSize; 52 | 53 | function GetLeafSize: UInt32; 54 | procedure SetLeafSize(AValue: UInt32); 55 | property LeafSize: UInt32 read GetLeafSize write SetLeafSize; 56 | 57 | function GetNodeOffset: UInt64; 58 | procedure SetNodeOffset(AValue: UInt64); 59 | property NodeOffset: UInt64 read GetNodeOffset write SetNodeOffset; 60 | 61 | function GetIsLastNode: Boolean; 62 | procedure SetIsLastNode(AValue: Boolean); 63 | property IsLastNode: Boolean read GetIsLastNode write SetIsLastNode; 64 | 65 | function Clone(): IBlake2STreeConfig; 66 | 67 | end; 68 | 69 | implementation 70 | 71 | end. 72 | -------------------------------------------------------------------------------- /HashLib/src/Checksum/HlpCRC64.pas: -------------------------------------------------------------------------------- 1 | unit HlpCRC64; 2 | 3 | {$I ..\Include\HashLib.inc} 4 | 5 | interface 6 | 7 | uses 8 | HlpHashLibTypes, 9 | HlpHash, 10 | HlpIHash, 11 | HlpICRC, 12 | HlpIHashResult, 13 | HlpIHashInfo, 14 | HlpCRC; 15 | 16 | type 17 | 18 | TCRC64Polynomials = class sealed(TObject) 19 | 20 | private 21 | 22 | const 23 | 24 | ECMA_182 = UInt64($42F0E1EBA9EA3693); 25 | 26 | end; 27 | 28 | TCRC64 = class(THash, IChecksum, IHash64, ITransformBlock) 29 | 30 | strict private 31 | var 32 | FCRCAlgorithm: ICRC; 33 | 34 | public 35 | constructor Create(APolynomial, AInitial: UInt64; 36 | AIsInputReflected, AIsOutputReflected: Boolean; 37 | AOutputXor, ACheckValue: UInt64; const ANames: THashLibStringArray); 38 | 39 | procedure Initialize(); override; 40 | procedure TransformBytes(const AData: THashLibByteArray; 41 | AIndex, ALength: Int32); override; 42 | function TransformFinal(): IHashResult; override; 43 | function Clone(): IHash; override; 44 | 45 | end; 46 | 47 | TCRC64_ECMA_182 = class sealed(TCRC64) 48 | 49 | public 50 | constructor Create(); 51 | 52 | end; 53 | 54 | implementation 55 | 56 | { TCRC64 } 57 | 58 | function TCRC64.Clone(): IHash; 59 | begin 60 | result := FCRCAlgorithm.Clone(); 61 | end; 62 | 63 | constructor TCRC64.Create(APolynomial, AInitial: UInt64; 64 | AIsInputReflected, AIsOutputReflected: Boolean; 65 | AOutputXor, ACheckValue: UInt64; const ANames: THashLibStringArray); 66 | begin 67 | Inherited Create(8, 1); 68 | FCRCAlgorithm := TCRC.Create(64, APolynomial, AInitial, AIsInputReflected, 69 | AIsOutputReflected, AOutputXor, ACheckValue, ANames); 70 | end; 71 | 72 | procedure TCRC64.Initialize; 73 | begin 74 | FCRCAlgorithm.Initialize; 75 | end; 76 | 77 | procedure TCRC64.TransformBytes(const AData: THashLibByteArray; 78 | AIndex, ALength: Int32); 79 | begin 80 | FCRCAlgorithm.TransformBytes(AData, AIndex, ALength); 81 | end; 82 | 83 | function TCRC64.TransformFinal: IHashResult; 84 | begin 85 | result := FCRCAlgorithm.TransformFinal(); 86 | end; 87 | 88 | { TCRC64_ECMA_182 } 89 | 90 | constructor TCRC64_ECMA_182.Create; 91 | begin 92 | Inherited Create(TCRC64Polynomials.ECMA_182, $0000000000000000, false, false, 93 | $0000000000000000, $6C40DF5F0B497347, THashLibStringArray.Create('CRC-64', 94 | 'CRC-64/ECMA-182')); 95 | end; 96 | 97 | end. 98 | -------------------------------------------------------------------------------- /HashLib/src/Crypto/HlpMDBase.pas: -------------------------------------------------------------------------------- 1 | unit HlpMDBase; 2 | 3 | {$I ..\Include\HashLib.inc} 4 | 5 | interface 6 | 7 | uses 8 | {$IFDEF DELPHI2010} 9 | SysUtils, // to get rid of compiler hint "not inlined" on Delphi 2010. 10 | {$ENDIF DELPHI2010} 11 | HlpHashLibTypes, 12 | {$IFDEF DELPHI} 13 | HlpHashBuffer, 14 | {$ENDIF DELPHI} 15 | HlpIHashInfo, 16 | HlpHashCryptoNotBuildIn, 17 | HlpConverters; 18 | 19 | type 20 | TMDBase = class abstract(TBlockHash, ICryptoNotBuildIn) 21 | 22 | strict protected 23 | var 24 | FState: THashLibUInt32Array; 25 | 26 | const 27 | 28 | C1 = UInt32($50A28BE6); 29 | C2 = UInt32($5A827999); 30 | C3 = UInt32($5C4DD124); 31 | C4 = UInt32($6ED9EBA1); 32 | C5 = UInt32($6D703EF3); 33 | C6 = UInt32($8F1BBCDC); 34 | C7 = UInt32($7A6D76E9); 35 | C8 = UInt32($A953FD4E); 36 | 37 | constructor Create(AStateLength, AHashSize: Int32); 38 | 39 | function GetResult(): THashLibByteArray; override; 40 | procedure Finish(); override; 41 | 42 | public 43 | procedure Initialize(); override; 44 | 45 | end; 46 | 47 | implementation 48 | 49 | { TMDBase } 50 | 51 | constructor TMDBase.Create(AStateLength, AHashSize: Int32); 52 | begin 53 | Inherited Create(AHashSize, 64); 54 | System.SetLength(FState, AStateLength); 55 | end; 56 | 57 | procedure TMDBase.Finish; 58 | var 59 | LBits: UInt64; 60 | LPadIndex: Int32; 61 | LPad: THashLibByteArray; 62 | begin 63 | LBits := FProcessedBytesCount * 8; 64 | if (FBuffer.Position < 56) then 65 | begin 66 | LPadIndex := 56 - FBuffer.Position 67 | end 68 | else 69 | begin 70 | LPadIndex := 120 - FBuffer.Position; 71 | end; 72 | System.SetLength(LPad, LPadIndex + 8); 73 | 74 | LPad[0] := $80; 75 | 76 | LBits := TConverters.le2me_64(LBits); 77 | 78 | TConverters.ReadUInt64AsBytesLE(LBits, LPad, LPadIndex); 79 | 80 | LPadIndex := LPadIndex + 8; 81 | 82 | TransformBytes(LPad, 0, LPadIndex); 83 | end; 84 | 85 | function TMDBase.GetResult: THashLibByteArray; 86 | begin 87 | System.SetLength(result, System.Length(FState) * System.SizeOf(UInt32)); 88 | TConverters.le32_copy(PCardinal(FState), 0, PByte(result), 0, 89 | System.Length(result)); 90 | end; 91 | 92 | procedure TMDBase.Initialize; 93 | begin 94 | FState[0] := $67452301; 95 | FState[1] := $EFCDAB89; 96 | FState[2] := $98BADCFE; 97 | FState[3] := $10325476; 98 | inherited Initialize(); 99 | end; 100 | 101 | end. 102 | -------------------------------------------------------------------------------- /HashLib/src/Base/HlpMultipleTransformNonBlock.pas: -------------------------------------------------------------------------------- 1 | unit HlpMultipleTransformNonBlock; 2 | 3 | {$I ..\Include\HashLib.inc} 4 | 5 | interface 6 | 7 | uses 8 | Classes, 9 | HlpHashLibTypes, 10 | HlpHash, 11 | HlpIHashInfo, 12 | HlpIHashResult; 13 | 14 | type 15 | 16 | TMultipleTransformNonBlock = class abstract(THash, INonBlockHash) 17 | 18 | strict private 19 | 20 | function Aggregate(): THashLibByteArray; 21 | 22 | strict protected 23 | var 24 | FBuffer: TMemoryStream; 25 | 26 | function ComputeAggregatedBytes(const AData: THashLibByteArray) 27 | : IHashResult; virtual; abstract; 28 | 29 | public 30 | constructor Create(AHashSize, ABlockSize: Int32); 31 | destructor Destroy; override; 32 | procedure Initialize(); override; 33 | procedure TransformBytes(const AData: THashLibByteArray; 34 | AIndex, ALength: Int32); override; 35 | function TransformFinal(): IHashResult; override; 36 | function ComputeBytes(const AData: THashLibByteArray): IHashResult; 37 | override; 38 | 39 | end; 40 | 41 | implementation 42 | 43 | { TMultipleTransformNonBlock } 44 | 45 | function TMultipleTransformNonBlock.Aggregate: THashLibByteArray; 46 | begin 47 | Result := Nil; 48 | if FBuffer.Size > 0 then 49 | begin 50 | FBuffer.Position := 0; 51 | System.SetLength(Result, FBuffer.Size); 52 | FBuffer.Read(Result[0], FBuffer.Size); 53 | end; 54 | end; 55 | 56 | constructor TMultipleTransformNonBlock.Create(AHashSize, ABlockSize: Int32); 57 | begin 58 | Inherited Create(AHashSize, ABlockSize); 59 | FBuffer := TMemoryStream.Create(); 60 | end; 61 | 62 | destructor TMultipleTransformNonBlock.Destroy; 63 | begin 64 | FBuffer.Free; 65 | inherited Destroy; 66 | end; 67 | 68 | procedure TMultipleTransformNonBlock.Initialize; 69 | begin 70 | FBuffer.Clear; 71 | FBuffer.SetSize(Int64(0)); 72 | end; 73 | 74 | procedure TMultipleTransformNonBlock.TransformBytes 75 | (const AData: THashLibByteArray; AIndex, ALength: Int32); 76 | begin 77 | {$IFDEF DEBUG} 78 | System.Assert(AIndex >= 0); 79 | System.Assert(ALength >= 0); 80 | System.Assert(AIndex + ALength <= System.Length(AData)); 81 | {$ENDIF DEBUG} 82 | FBuffer.Write(AData[AIndex], ALength); 83 | end; 84 | 85 | function TMultipleTransformNonBlock.TransformFinal: IHashResult; 86 | begin 87 | Result := ComputeAggregatedBytes(Aggregate()); 88 | Initialize(); 89 | end; 90 | 91 | function TMultipleTransformNonBlock.ComputeBytes(const AData: THashLibByteArray) 92 | : IHashResult; 93 | begin 94 | Initialize(); 95 | Result := ComputeAggregatedBytes(AData); 96 | end; 97 | 98 | end. 99 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | # Created by https://www.gitignore.io/api/delphi,lazarus,freepascal 3 | 4 | ### Delphi ### 5 | # Uncomment these types if you want even more clean repository. But be careful. 6 | # It can make harm to an existing project source. Read explanations below. 7 | # 8 | # Resource files are binaries containing manifest, project icon and version info. 9 | # They can not be viewed as text or compared by diff-tools. Consider replacing them with .rc files. 10 | #*.res 11 | # 12 | # Type library file (binary). In old Delphi versions it should be stored. 13 | # Since Delphi 2009 it is produced from .ridl file and can safely be ignored. 14 | #*.tlb 15 | # 16 | # Diagram Portfolio file. Used by the diagram editor up to Delphi 7. 17 | # Uncomment this if you are not using diagrams or use newer Delphi version. 18 | #*.ddp 19 | # 20 | # Visual LiveBindings file. Added in Delphi XE2. 21 | # Uncomment this if you are not using LiveBindings Designer. 22 | #*.vlb 23 | # 24 | # Deployment Manager configuration file for your project. Added in Delphi XE2. 25 | # Uncomment this if it is not mobile development and you do not use remote debug feature. 26 | #*.deployproj 27 | # 28 | # C++ object files produced when C/C++ Output file generation is configured. 29 | # Uncomment this if you are not using external objects (zlib library for example). 30 | #*.obj 31 | # 32 | 33 | # Delphi compiler-generated binaries (safe to delete) 34 | *.exe 35 | *.dll 36 | *.bpl 37 | *.bpi 38 | *.dcp 39 | *.so 40 | *.apk 41 | *.drc 42 | *.map 43 | *.dres 44 | *.rsm 45 | *.tds 46 | *.dcu 47 | *.lib 48 | *.a 49 | *.o 50 | *.ocx 51 | 52 | # Delphi autogenerated files (duplicated info) 53 | *.cfg 54 | *.hpp 55 | *Resource.rc 56 | 57 | # Delphi local files (user-specific info) 58 | *.local 59 | *.identcache 60 | *.projdata 61 | *.tvsconfig 62 | *.dsk 63 | 64 | # Delphi history and backups 65 | __history/ 66 | __recovery/ 67 | *.~* 68 | 69 | # Castalia statistics file (since XE7 Castalia is distributed with Delphi) 70 | *.stat 71 | 72 | ### FreePascal ### 73 | *.lps 74 | *.compiled 75 | *.[oa] 76 | *.ppu 77 | *.rst 78 | *.cgi 79 | *.log 80 | *.bak* 81 | fp.ini 82 | fp.cfg 83 | fp.dsk 84 | ### Lazarus ### 85 | # Lazarus compiler-generated binaries (safe to delete) 86 | *.dylib 87 | *.lrs 88 | *.res 89 | *.dbg 90 | *.or 91 | 92 | # Lazarus autogenerated files (duplicated info) 93 | *.rsj 94 | *.lrt 95 | 96 | # Lazarus local files (user-specific info) 97 | 98 | # Lazarus backups and unit output folders. 99 | # These can be changed by user in Lazarus/project options. 100 | backup/ 101 | *.bak 102 | lib/ 103 | 104 | # Application bundle for Mac OS 105 | *.app/ 106 | 107 | 108 | # End of https://www.gitignore.io/api/delphi,lazarus,freepascal 109 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | # Part of `travis-lazarus` (https://github.com/nielsAD/travis-lazarus) 2 | # License: MIT 3 | 4 | language: shell 5 | sudo: required 6 | dist: bionic 7 | 8 | os: 9 | - linux 10 | - osx 11 | - windows 12 | 13 | env: 14 | global: 15 | - WINEPREFIX=~/.winelaz 16 | - DISPLAY=:99.0 17 | matrix: 18 | - LAZ_PKG=true # Use the latest version from the default package manager. (apt, brew or choco) 19 | - LAZ_VER=1.6 # Use specific (binary) release 20 | # - LAZ_VER=2.0.4 21 | 22 | matrix: 23 | include: 24 | - os: linux 25 | env: LAZ_VER=1.6 LAZ_ENV=qemu-arm LAZ_OPT="--os=linux --cpu=arm" 26 | - os: linux 27 | env: LAZ_VER=2.0.4 LAZ_ENV=qemu-arm LAZ_OPT="--os=linux --cpu=arm" 28 | - os: linux 29 | arch: arm64 30 | env: LAZ_PKG=true 31 | # - os: windows 32 | # env: LAZ_VER=1.6 LAZ_REL=64 LAZ_OPT="--os=win64 --cpu=x86_64" 33 | # - os: windows 34 | # env: LAZ_VER=2.0.4 LAZ_REL=64 LAZ_OPT="--os=win64 --cpu=x86_64" 35 | 36 | # before_install: 37 | # # Start virtual display server 38 | # - Xvfb $DISPLAY & 39 | # # - if [ "$TRAVIS_OS_NAME" == "linux" ]; then 40 | # # sudo apt-get update; 41 | # # sudo apt-get install binutils-2.26; 42 | # # sudo ln -sf /usr/lib/binutils-2.26/bin/* /usr/bin/; 43 | # # sudo ln -sf /usr/lib/binutils-2.26/ldscripts/* /usr/lib/ldscripts/; 44 | # # fi 45 | # - chmod +x .travis.install.py 46 | 47 | before_install: 48 | - if [[ "$TRAVIS_OS_NAME" == "windows" ]]; then choco install python3 --params "/InstallDir:C:\python3"; fi 49 | - if [[ "$TRAVIS_OS_NAME" == "windows" ]]; then export PATH=/c/Python3:/c/Python3/Scripts:/c/lazarus:$PATH; fi 50 | - if [[ "$TRAVIS_OS_NAME" != "windows" ]]; then 51 | Xvfb $DISPLAY & 52 | fi 53 | - chmod +x .travis.install.py 54 | 55 | # install: 56 | # # Install prerequisites (fpc/lazarus/wine/qemu) 57 | # - ./.travis.install.py 58 | 59 | install: 60 | # Install prerequisites (fpc/lazarus/wine/qemu) 61 | - if [[ "$TRAVIS_OS_NAME" == "windows" ]]; then python ./.travis.install.py; fi 62 | - if [[ "$TRAVIS_OS_NAME" != "windows" ]]; then ./.travis.install.py; fi 63 | 64 | script: 65 | - lazbuild --add-package-link ./HashLib/src/Packages/FPC/HashLib4PascalPackage.lpk # Add HashLib4Pascal Package 66 | - lazbuild $LAZ_OPT ./HashLib/src/Packages/FPC/HashLib4PascalPackage.lpk # Build HashLib4Pascal Package 67 | - lazbuild $LAZ_OPT ./HashLib.Tests/FreePascal.Tests/HashLibConsole.Tests.lpi # Build HashLib4Pascal Test Project 68 | - travis_wait 120 $LAZ_ENV ./HashLib.Tests/FreePascal.Tests/bin/HashLib --all --format=plain --progress # Run HashLib4Pascal TestSuite with timeout of 120 mins 69 | 70 | notifications: 71 | email: 72 | on_success: false 73 | on_failure: change 74 | -------------------------------------------------------------------------------- /HashLib/src/NullDigest/HlpNullDigest.pas: -------------------------------------------------------------------------------- 1 | unit HlpNullDigest; 2 | 3 | {$I ..\Include\HashLib.inc} 4 | 5 | interface 6 | 7 | uses 8 | Classes, 9 | SysUtils, 10 | HlpHashLibTypes, 11 | HlpHash, 12 | HlpIHash, 13 | HlpIHashInfo, 14 | HlpHashResult, 15 | HlpIHashResult; 16 | 17 | resourcestring 18 | SHashSizeNotImplemented = 'HashSize Not Implemented For "%s"'; 19 | SBlockSizeNotImplemented = 'BlockSize Not Implemented For "%s"'; 20 | 21 | type 22 | TNullDigest = class sealed(THash, ITransformBlock) 23 | strict private 24 | var 25 | FOut: TMemoryStream; 26 | 27 | strict protected 28 | function GetBlockSize: Int32; override; 29 | function GetHashSize: Int32; override; 30 | 31 | public 32 | constructor Create(); 33 | destructor Destroy(); override; 34 | procedure Initialize(); override; 35 | procedure TransformBytes(const AData: THashLibByteArray; 36 | AIndex, ALength: Int32); override; 37 | function TransformFinal(): IHashResult; override; 38 | function Clone(): IHash; override; 39 | end; 40 | 41 | implementation 42 | 43 | { TNullDigest } 44 | 45 | function TNullDigest.GetBlockSize: Int32; 46 | begin 47 | raise ENotImplementedHashLibException.CreateResFmt 48 | (@SBlockSizeNotImplemented, [Name]); 49 | end; 50 | 51 | function TNullDigest.GetHashSize: Int32; 52 | begin 53 | raise ENotImplementedHashLibException.CreateResFmt 54 | (@SHashSizeNotImplemented, [Name]); 55 | end; 56 | 57 | function TNullDigest.Clone(): IHash; 58 | var 59 | LHashInstance: TNullDigest; 60 | begin 61 | LHashInstance := TNullDigest.Create(); 62 | FOut.Position := 0; 63 | LHashInstance.FOut.CopyFrom(FOut, FOut.Size); 64 | result := LHashInstance as IHash; 65 | result.BufferSize := BufferSize; 66 | end; 67 | 68 | constructor TNullDigest.Create; 69 | begin 70 | Inherited Create(-1, -1); // Dummy State 71 | FOut := TMemoryStream.Create(); 72 | end; 73 | 74 | destructor TNullDigest.Destroy; 75 | begin 76 | FOut.Free; 77 | inherited Destroy; 78 | end; 79 | 80 | procedure TNullDigest.Initialize; 81 | begin 82 | FOut.Clear; 83 | end; 84 | 85 | procedure TNullDigest.TransformBytes(const AData: THashLibByteArray; 86 | AIndex, ALength: Int32); 87 | begin 88 | if AData <> Nil then 89 | begin 90 | FOut.Write(AData[AIndex], ALength); 91 | end; 92 | end; 93 | 94 | function TNullDigest.TransformFinal: IHashResult; 95 | var 96 | LResult: THashLibByteArray; 97 | begin 98 | try 99 | if FOut.Size > 0 then 100 | begin 101 | FOut.Position := 0; 102 | System.SetLength(LResult, FOut.Size); 103 | FOut.Read(LResult[0], FOut.Size); 104 | end; 105 | result := THashResult.Create(LResult); 106 | finally 107 | Initialize(); 108 | end; 109 | end; 110 | 111 | end. 112 | -------------------------------------------------------------------------------- /HashLib/src/Checksum/HlpCRC32.pas: -------------------------------------------------------------------------------- 1 | unit HlpCRC32; 2 | 3 | {$I ..\Include\HashLib.inc} 4 | 5 | interface 6 | 7 | uses 8 | 9 | HlpHashLibTypes, 10 | HlpHash, 11 | HlpIHash, 12 | HlpICRC, 13 | HlpIHashResult, 14 | HlpIHashInfo, 15 | HlpCRC; 16 | 17 | type 18 | 19 | TCRC32Polynomials = class sealed(TObject) 20 | 21 | private 22 | 23 | const 24 | 25 | PKZIP = UInt32($04C11DB7); 26 | Castagnoli = UInt32($1EDC6F41); 27 | 28 | end; 29 | 30 | TCRC32 = class(THash, IChecksum, IHash32, ITransformBlock) 31 | 32 | strict private 33 | var 34 | FCRCAlgorithm: ICRC; 35 | 36 | public 37 | 38 | constructor Create(APolynomial, AInitial: UInt64; 39 | AIsInputReflected, AIsOutputReflected: Boolean; 40 | AOutputXor, ACheckValue: UInt64; const ANames: THashLibStringArray); 41 | 42 | procedure Initialize(); override; 43 | procedure TransformBytes(const AData: THashLibByteArray; 44 | AIndex, ALength: Int32); override; 45 | function TransformFinal(): IHashResult; override; 46 | function Clone(): IHash; override; 47 | 48 | end; 49 | 50 | TCRC32_PKZIP = class sealed(TCRC32) 51 | 52 | public 53 | constructor Create(); 54 | 55 | end; 56 | 57 | TCRC32_CASTAGNOLI = class sealed(TCRC32) 58 | 59 | public 60 | constructor Create(); 61 | 62 | end; 63 | 64 | implementation 65 | 66 | { TCRC32 } 67 | 68 | function TCRC32.Clone(): IHash; 69 | begin 70 | Result := FCRCAlgorithm.Clone(); 71 | end; 72 | 73 | constructor TCRC32.Create(APolynomial, AInitial: UInt64; 74 | AIsInputReflected, AIsOutputReflected: Boolean; 75 | AOutputXor, ACheckValue: UInt64; const ANames: THashLibStringArray); 76 | begin 77 | Inherited Create(4, 1); 78 | FCRCAlgorithm := TCRC.Create(32, APolynomial, AInitial, AIsInputReflected, 79 | AIsOutputReflected, AOutputXor, ACheckValue, ANames); 80 | end; 81 | 82 | procedure TCRC32.Initialize; 83 | begin 84 | FCRCAlgorithm.Initialize; 85 | end; 86 | 87 | procedure TCRC32.TransformBytes(const AData: THashLibByteArray; 88 | AIndex, ALength: Int32); 89 | begin 90 | FCRCAlgorithm.TransformBytes(AData, AIndex, ALength); 91 | end; 92 | 93 | function TCRC32.TransformFinal: IHashResult; 94 | begin 95 | Result := FCRCAlgorithm.TransformFinal(); 96 | end; 97 | 98 | { TCRC32_PKZIP } 99 | 100 | constructor TCRC32_PKZIP.Create; 101 | begin 102 | Inherited Create(TCRC32Polynomials.PKZIP, $FFFFFFFF, true, true, $FFFFFFFF, 103 | $CBF43926, THashLibStringArray.Create('CRC-32', 'CRC-32/ADCCP', 104 | 'CRC-32/V-42', 'CRC-32/XZ', 'PKZIP', 'CRC-32/ISO-HDLC')); 105 | 106 | end; 107 | 108 | { TCRC32_CASTAGNOLI } 109 | 110 | constructor TCRC32_CASTAGNOLI.Create; 111 | begin 112 | Inherited Create(TCRC32Polynomials.Castagnoli, $FFFFFFFF, true, true, 113 | $FFFFFFFF, $E3069283, THashLibStringArray.Create('CRC-32C', 114 | 'CRC-32/BASE91-C', 'CRC-32/CASTAGNOLI', 'CRC-32/INTERLAKEN', 115 | 'CRC-32/ISCSI')); 116 | 117 | end; 118 | 119 | end. 120 | -------------------------------------------------------------------------------- /HashLib/src/Checksum/HlpAdler32.pas: -------------------------------------------------------------------------------- 1 | unit HlpAdler32; 2 | 3 | {$I ..\Include\HashLib.inc} 4 | 5 | interface 6 | 7 | uses 8 | HlpHashLibTypes, 9 | HlpIHashInfo, 10 | HlpHash, 11 | HlpIHash, 12 | HlpHashResult, 13 | HlpIHashResult, 14 | HlpConverters; 15 | 16 | type 17 | TAdler32 = class sealed(THash, IChecksum, IHash32, ITransformBlock) 18 | 19 | strict private 20 | var 21 | FA, FB: UInt32; 22 | 23 | const 24 | MOD_ADLER = UInt32(65521); 25 | 26 | public 27 | constructor Create(); 28 | procedure Initialize(); override; 29 | procedure TransformBytes(const AData: THashLibByteArray; 30 | AIndex, ALength: Int32); override; 31 | function TransformFinal: IHashResult; override; 32 | function Clone(): IHash; override; 33 | 34 | end; 35 | 36 | implementation 37 | 38 | { TAdler32 } 39 | 40 | function TAdler32.Clone(): IHash; 41 | var 42 | LHashInstance: TAdler32; 43 | begin 44 | LHashInstance := TAdler32.Create(); 45 | LHashInstance.FA := FA; 46 | LHashInstance.FB := FB; 47 | result := LHashInstance as IHash; 48 | result.BufferSize := BufferSize; 49 | end; 50 | 51 | constructor TAdler32.Create; 52 | begin 53 | Inherited Create(4, 1); 54 | end; 55 | 56 | procedure TAdler32.Initialize; 57 | begin 58 | FA := 1; 59 | FB := 0; 60 | end; 61 | 62 | procedure TAdler32.TransformBytes(const AData: THashLibByteArray; 63 | AIndex, ALength: Int32); 64 | var 65 | LN: Int32; 66 | LPtrData: PByte; 67 | LA, LB: UInt32; 68 | begin 69 | {$IFDEF DEBUG} 70 | System.Assert(AIndex >= 0); 71 | System.Assert(ALength >= 0); 72 | System.Assert(AIndex + ALength <= System.Length(AData)); 73 | {$ENDIF DEBUG} 74 | LPtrData := PByte(AData) + AIndex; 75 | 76 | { 77 | LA := FA; 78 | LB := FB; 79 | while ALength > 0 do 80 | begin 81 | LA := (LA + LPtrData^) mod MOD_ADLER; 82 | LB := (LB + LA) mod MOD_ADLER; 83 | System.Inc(LPtrData); 84 | System.Dec(ALength); 85 | end; 86 | FA := LA; 87 | FB := LB; 88 | } 89 | 90 | // lifted from PngEncoder Adler32.cs 91 | 92 | while ALength > 0 do 93 | begin 94 | // We can defer the modulo operation: 95 | // FA maximally grows from 65521 to 65521 + 255 * 3800 96 | // FB maximally grows by 3800 * median(FA) = 2090079800 < 2^31 97 | LN := 3800; 98 | if (LN > ALength) then 99 | begin 100 | LN := ALength; 101 | end; 102 | ALength := ALength - LN; 103 | 104 | LA := FA; 105 | LB := FB; 106 | while (LN - 1) >= 0 do 107 | begin 108 | LA := (LA + LPtrData^); 109 | LB := (LB + LA); 110 | System.Inc(LPtrData); 111 | System.Dec(LN); 112 | end; 113 | LA := LA mod MOD_ADLER; 114 | LB := LB mod MOD_ADLER; 115 | 116 | FA := LA; 117 | FB := LB; 118 | end; 119 | end; 120 | 121 | function TAdler32.TransformFinal: IHashResult; 122 | var 123 | LBufferBytes: THashLibByteArray; 124 | begin 125 | System.SetLength(LBufferBytes, HashSize); 126 | TConverters.ReadUInt32AsBytesBE(UInt32((FB shl 16) or FA), LBufferBytes, 0); 127 | 128 | result := THashResult.Create(LBufferBytes); 129 | Initialize(); 130 | end; 131 | 132 | end. 133 | -------------------------------------------------------------------------------- /HashLib/src/Base/HlpHashCryptoNotBuildIn.pas: -------------------------------------------------------------------------------- 1 | unit HlpHashCryptoNotBuildIn; 2 | 3 | {$I ..\Include\HashLib.inc} 4 | 5 | interface 6 | 7 | uses 8 | HlpHashLibTypes, 9 | HlpHash, 10 | HlpHashBuffer, 11 | HlpIHashInfo, 12 | HlpHashResult, 13 | HlpIHashResult; 14 | 15 | type 16 | TBlockHash = class abstract(THash, IBlockHash) 17 | strict protected 18 | var 19 | FBuffer: THashBuffer; 20 | FProcessedBytesCount: UInt64; 21 | 22 | procedure TransformBuffer(); inline; 23 | procedure Finish(); virtual; abstract; 24 | procedure TransformBlock(AData: PByte; ADataLength: Int32; AIndex: Int32); 25 | virtual; abstract; 26 | function GetResult(): THashLibByteArray; virtual; abstract; 27 | 28 | public 29 | constructor Create(AHashSize, ABlockSize: Int32; ABufferSize: Int32 = -1); 30 | procedure TransformBytes(const AData: THashLibByteArray; 31 | AIndex, ALength: Int32); override; 32 | procedure Initialize(); override; 33 | function TransformFinal(): IHashResult; override; 34 | end; 35 | 36 | implementation 37 | 38 | { TBlockHash } 39 | 40 | constructor TBlockHash.Create(AHashSize, ABlockSize, ABufferSize: Int32); 41 | begin 42 | Inherited Create(AHashSize, ABlockSize); 43 | if (ABufferSize = -1) then 44 | begin 45 | ABufferSize := ABlockSize; 46 | end; 47 | FBuffer := THashBuffer.Create(ABufferSize); 48 | end; 49 | 50 | procedure TBlockHash.Initialize; 51 | begin 52 | FBuffer.Initialize(); 53 | FProcessedBytesCount := 0; 54 | end; 55 | 56 | procedure TBlockHash.TransformBuffer; 57 | begin 58 | {$IFDEF DEBUG} 59 | System.Assert(FBuffer.IsFull); 60 | {$ENDIF DEBUG} 61 | TransformBlock(PByte(FBuffer.GetBytes()), FBuffer.Length, 0); 62 | end; 63 | 64 | procedure TBlockHash.TransformBytes(const AData: THashLibByteArray; 65 | AIndex, ALength: Int32); 66 | var 67 | LPtrData: PByte; 68 | begin 69 | {$IFDEF DEBUG} 70 | System.Assert(AIndex >= 0); 71 | System.Assert(ALength >= 0); 72 | System.Assert(AIndex + ALength <= System.Length(AData)); 73 | {$ENDIF DEBUG} 74 | LPtrData := PByte(AData); 75 | 76 | if (not FBuffer.IsEmpty) then 77 | begin 78 | if (FBuffer.Feed(LPtrData, System.Length(AData), AIndex, ALength, 79 | FProcessedBytesCount)) then 80 | begin 81 | TransformBuffer(); 82 | end; 83 | end; 84 | 85 | while (ALength >= (FBuffer.Length)) do 86 | begin 87 | FProcessedBytesCount := FProcessedBytesCount + UInt64(FBuffer.Length); 88 | TransformBlock(LPtrData, FBuffer.Length, AIndex); 89 | AIndex := AIndex + (FBuffer.Length); 90 | ALength := ALength - (FBuffer.Length); 91 | end; 92 | 93 | if (ALength > 0) then 94 | begin 95 | FBuffer.Feed(LPtrData, System.Length(AData), AIndex, ALength, 96 | FProcessedBytesCount); 97 | end; 98 | end; 99 | 100 | function TBlockHash.TransformFinal: IHashResult; 101 | var 102 | LTempResult: THashLibByteArray; 103 | begin 104 | Finish(); 105 | 106 | {$IFDEF DEBUG} 107 | System.Assert(FBuffer.IsEmpty); 108 | {$ENDIF DEBUG} 109 | LTempResult := GetResult(); 110 | {$IFDEF DEBUG} 111 | System.Assert(System.Length(LTempResult) = HashSize); 112 | {$ENDIF DEBUG} 113 | Initialize(); 114 | 115 | result := THashResult.Create(LTempResult); 116 | end; 117 | 118 | end. 119 | -------------------------------------------------------------------------------- /HashLib.Tests/src/Hash128Tests.pas: -------------------------------------------------------------------------------- 1 | unit Hash128Tests; 2 | 3 | interface 4 | 5 | uses 6 | {$IFDEF FPC} 7 | fpcunit, 8 | testregistry, 9 | {$ELSE} 10 | TestFramework, 11 | {$ENDIF FPC} 12 | HashLibTestBase, 13 | HlpHashFactory; 14 | 15 | // Hash128 16 | 17 | type 18 | TTestMurmurHash3_x86_128 = class(THashWithUInt32AsKeyAlgorithmTestCase) 19 | 20 | protected 21 | procedure SetUp; override; 22 | procedure TearDown; override; 23 | 24 | end; 25 | 26 | type 27 | TTestMurmurHash3_x64_128 = class(THashWithUInt32AsKeyAlgorithmTestCase) 28 | 29 | protected 30 | procedure SetUp; override; 31 | procedure TearDown; override; 32 | 33 | end; 34 | 35 | type 36 | TTestSipHash128_2_4 = class(THashWithExternalKeyAlgorithmTestCase) 37 | 38 | protected 39 | procedure SetUp; override; 40 | procedure TearDown; override; 41 | 42 | end; 43 | 44 | implementation 45 | 46 | // Hash128 47 | 48 | { TTestMurmurHash3_x86_128 } 49 | 50 | procedure TTestMurmurHash3_x86_128.SetUp; 51 | begin 52 | inherited; 53 | HashInstance := THashFactory.THash128.CreateMurmurHash3_x86_128(); 54 | HashOfEmptyData := '00000000000000000000000000000000'; 55 | HashOfDefaultData := 'B35E1058738E067BF637B17075F14B8B'; 56 | HashOfOnetoNine := 'C65876BB119A1552C5E3E5D7A9168CA4'; 57 | HashOfABCDE := 'C5402EFB5D24C5BC5A7201775A720177'; 58 | HashOfDefaultDataWithMaxUInt32AsKey := '55315FA9E8129C7390C080B8FDB1C972'; 59 | end; 60 | 61 | procedure TTestMurmurHash3_x86_128.TearDown; 62 | begin 63 | HashInstance := Nil; 64 | inherited; 65 | end; 66 | 67 | { TTestMurmurHash3_x64_128 } 68 | 69 | procedure TTestMurmurHash3_x64_128.SetUp; 70 | begin 71 | inherited; 72 | HashInstance := THashFactory.THash128.CreateMurmurHash3_x64_128(); 73 | HashOfEmptyData := '00000000000000000000000000000000'; 74 | HashOfDefaultData := '705BD3C954B94BE056F06B68662E6364'; 75 | HashOfOnetoNine := '3C84645EDB66CCA499F8FAC73A1EA105'; 76 | HashOfABCDE := '2036D091F496BBB8C5C7EEA04BCFEC8C'; 77 | HashOfDefaultDataWithMaxUInt32AsKey := 'ADFD14988FB1F8582A1B67C1BBACC218'; 78 | end; 79 | 80 | procedure TTestMurmurHash3_x64_128.TearDown; 81 | begin 82 | HashInstance := Nil; 83 | inherited; 84 | end; 85 | 86 | { TTestSipHash128_2_4 } 87 | 88 | procedure TTestSipHash128_2_4.SetUp; 89 | begin 90 | inherited; 91 | HashInstance := THashFactory.THash128.CreateSipHash128_2_4(); 92 | HashOfEmptyData := 'A3817F04BA25A8E66DF67214C7550293'; 93 | HashOfDefaultData := '312C82F65D5A567B333CD772F045E36C'; 94 | HashOfOnetoNine := 'CE94828373303D1AB5FC781744AD71CE'; 95 | HashOfABCDE := 'EB8662A95F0D718811E7CEDBDF03541C'; 96 | HashOfDefaultDataWithExternalKey := '312C82F65D5A567B333CD772F045E36C'; 97 | end; 98 | 99 | procedure TTestSipHash128_2_4.TearDown; 100 | begin 101 | HashInstance := Nil; 102 | inherited; 103 | end; 104 | 105 | initialization 106 | 107 | // Register any test cases with the test runner 108 | 109 | {$IFDEF FPC} 110 | // Hash128 111 | RegisterTest(TTestMurmurHash3_x86_128); 112 | RegisterTest(TTestMurmurHash3_x64_128); 113 | RegisterTest(TTestSipHash128_2_4); 114 | {$ELSE} 115 | // Hash128 116 | RegisterTest(TTestMurmurHash3_x86_128.Suite); 117 | RegisterTest(TTestMurmurHash3_x64_128.Suite); 118 | RegisterTest(TTestSipHash128_2_4.Suite); 119 | {$ENDIF FPC} 120 | 121 | end. 122 | -------------------------------------------------------------------------------- /HashLib/src/Hash32/HlpSuperFast.pas: -------------------------------------------------------------------------------- 1 | unit HlpSuperFast; 2 | 3 | {$I ..\Include\HashLib.inc} 4 | 5 | interface 6 | 7 | uses 8 | HlpHashLibTypes, 9 | {$IFDEF DELPHI} 10 | HlpHash, 11 | {$ENDIF DELPHI} 12 | HlpIHash, 13 | HlpIHashInfo, 14 | HlpHashResult, 15 | HlpIHashResult, 16 | HlpMultipleTransformNonBlock; 17 | 18 | type 19 | 20 | TSuperFast = class sealed(TMultipleTransformNonBlock, IHash32, 21 | ITransformBlock) 22 | 23 | strict protected 24 | function ComputeAggregatedBytes(const AData: THashLibByteArray) 25 | : IHashResult; override; 26 | public 27 | constructor Create(); 28 | function Clone(): IHash; override; 29 | 30 | end; 31 | 32 | implementation 33 | 34 | { TSuperFast } 35 | 36 | constructor TSuperFast.Create; 37 | begin 38 | Inherited Create(4, 4); 39 | end; 40 | 41 | function TSuperFast.Clone(): IHash; 42 | var 43 | LHashInstance: TSuperFast; 44 | begin 45 | LHashInstance := TSuperFast.Create(); 46 | FBuffer.Position := 0; 47 | LHashInstance.FBuffer.CopyFrom(FBuffer, FBuffer.Size); 48 | result := LHashInstance as IHash; 49 | result.BufferSize := BufferSize; 50 | end; 51 | 52 | function TSuperFast.ComputeAggregatedBytes(const AData: THashLibByteArray) 53 | : IHashResult; 54 | var 55 | LHash, LTemp, U1: UInt32; 56 | LLength, LCurrentIndex, I1, I2: Int32; 57 | begin 58 | LLength := System.length(AData); 59 | 60 | if (LLength = 0) then 61 | begin 62 | result := THashResult.Create(Int32(0)); 63 | Exit; 64 | end; 65 | 66 | LHash := UInt32(LLength); 67 | 68 | LCurrentIndex := 0; 69 | 70 | while (LLength >= 4) do 71 | begin 72 | I1 := AData[LCurrentIndex]; 73 | System.Inc(LCurrentIndex); 74 | I2 := AData[LCurrentIndex] shl 8; 75 | System.Inc(LCurrentIndex); 76 | LHash := UInt16(LHash + UInt32(I1 or I2)); 77 | U1 := UInt32(AData[LCurrentIndex]); 78 | System.Inc(LCurrentIndex); 79 | LTemp := UInt32((Byte(U1) or AData[LCurrentIndex] shl 8) shl 11) xor LHash; 80 | System.Inc(LCurrentIndex); 81 | LHash := (LHash shl 16) xor LTemp; 82 | LHash := LHash + (LHash shr 11); 83 | 84 | System.Dec(LLength, 4); 85 | end; 86 | 87 | case LLength of 88 | 3: 89 | begin 90 | I1 := AData[LCurrentIndex]; 91 | System.Inc(LCurrentIndex); 92 | I2 := AData[LCurrentIndex]; 93 | System.Inc(LCurrentIndex); 94 | LHash := LHash + UInt16(I1 or I2 shl 8); 95 | LHash := LHash xor (LHash shl 16); 96 | LHash := LHash xor (UInt32(AData[LCurrentIndex]) shl 18); 97 | LHash := LHash + (LHash shr 11); 98 | end; 99 | 100 | 2: 101 | begin 102 | I1 := AData[LCurrentIndex]; 103 | System.Inc(LCurrentIndex); 104 | I2 := AData[LCurrentIndex]; 105 | LHash := LHash + UInt16(I1 or I2 shl 8); 106 | LHash := LHash xor (LHash shl 11); 107 | LHash := LHash + (LHash shr 17); 108 | end; 109 | 110 | 1: 111 | begin 112 | I1 := AData[LCurrentIndex]; 113 | 114 | LHash := LHash + UInt32(I1); 115 | LHash := LHash xor (LHash shl 10); 116 | LHash := LHash + (LHash shr 1); 117 | end; 118 | 119 | end; 120 | 121 | LHash := LHash xor (LHash shl 3); 122 | LHash := LHash + (LHash shr 5); 123 | LHash := LHash xor (LHash shl 4); 124 | LHash := LHash + (LHash shr 17); 125 | LHash := LHash xor (LHash shl 25); 126 | LHash := LHash + (LHash shr 6); 127 | 128 | result := THashResult.Create(LHash); 129 | end; 130 | 131 | end. 132 | -------------------------------------------------------------------------------- /HashLib.Benchmark/project/Lazarus/PerformanceBenchmarkConsole.lpi: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | <UseAppBundle Value="False"/> 16 | <ResourceType Value="res"/> 17 | </General> 18 | <BuildModes Count="2"> 19 | <Item1 Name="Default" Default="True"/> 20 | <Item2 Name="Debug"> 21 | <CompilerOptions> 22 | <Version Value="11"/> 23 | <PathDelim Value="\"/> 24 | <Target> 25 | <Filename Value="PerformanceBenchmarkConsole"/> 26 | </Target> 27 | <SearchPaths> 28 | <IncludeFiles Value="$(ProjOutDir)"/> 29 | <OtherUnitFiles Value="..\..\src\Core"/> 30 | <UnitOutputDirectory Value="lib\$(TargetCPU)-$(TargetOS)"/> 31 | </SearchPaths> 32 | <Parsing> 33 | <SyntaxOptions> 34 | <IncludeAssertionCode Value="True"/> 35 | </SyntaxOptions> 36 | </Parsing> 37 | <CodeGeneration> 38 | <Checks> 39 | <IOChecks Value="True"/> 40 | <RangeChecks Value="True"/> 41 | <OverflowChecks Value="True"/> 42 | <StackChecks Value="True"/> 43 | </Checks> 44 | <VerifyObjMethodCallValidity Value="True"/> 45 | </CodeGeneration> 46 | <Linking> 47 | <Debugging> 48 | <DebugInfoType Value="dsDwarf2Set"/> 49 | <UseHeaptrc Value="True"/> 50 | <TrashVariables Value="True"/> 51 | <UseExternalDbgSyms Value="True"/> 52 | </Debugging> 53 | </Linking> 54 | </CompilerOptions> 55 | </Item2> 56 | </BuildModes> 57 | <PublishOptions> 58 | <Version Value="2"/> 59 | </PublishOptions> 60 | <RunParams> 61 | <local> 62 | <FormatVersion Value="1"/> 63 | </local> 64 | </RunParams> 65 | <RequiredPackages Count="1"> 66 | <Item1> 67 | <PackageName Value="HashLib4PascalPackage"/> 68 | </Item1> 69 | </RequiredPackages> 70 | <Units Count="2"> 71 | <Unit0> 72 | <Filename Value="PerformanceBenchmarkConsole.lpr"/> 73 | <IsPartOfProject Value="True"/> 74 | </Unit0> 75 | <Unit1> 76 | <Filename Value="..\..\src\Core\uPerformanceBenchmark.pas"/> 77 | <IsPartOfProject Value="True"/> 78 | </Unit1> 79 | </Units> 80 | </ProjectOptions> 81 | <CompilerOptions> 82 | <Version Value="11"/> 83 | <PathDelim Value="\"/> 84 | <Target> 85 | <Filename Value="PerformanceBenchmarkConsole"/> 86 | </Target> 87 | <SearchPaths> 88 | <IncludeFiles Value="$(ProjOutDir)"/> 89 | <OtherUnitFiles Value="..\..\src\Core"/> 90 | <UnitOutputDirectory Value="lib\$(TargetCPU)-$(TargetOS)"/> 91 | </SearchPaths> 92 | </CompilerOptions> 93 | <Debugging> 94 | <Exceptions Count="3"> 95 | <Item1> 96 | <Name Value="EAbort"/> 97 | </Item1> 98 | <Item2> 99 | <Name Value="ECodetoolError"/> 100 | </Item2> 101 | <Item3> 102 | <Name Value="EFOpenError"/> 103 | </Item3> 104 | </Exceptions> 105 | </Debugging> 106 | </CONFIG> 107 | -------------------------------------------------------------------------------- /HashLib/src/Utils/HlpHashLibTypes.pas: -------------------------------------------------------------------------------- 1 | unit HlpHashLibTypes; 2 | 3 | {$I ..\Include\HashLib.inc} 4 | 5 | interface 6 | 7 | uses 8 | SysUtils; 9 | 10 | type 11 | 12 | {$IFDEF FPC} 13 | PUInt64 = ^UInt64; 14 | {$ENDIF FPC} 15 | EHashLibException = class(Exception); 16 | EInvalidOperationHashLibException = class(EHashLibException); 17 | EIndexOutOfRangeHashLibException = class(EHashLibException); 18 | EArgumentHashLibException = class(EHashLibException); 19 | EArgumentInvalidHashLibException = class(EHashLibException); 20 | EArgumentNilHashLibException = class(EHashLibException); 21 | EArgumentOutOfRangeHashLibException = class(EHashLibException); 22 | ENullReferenceHashLibException = class(EHashLibException); 23 | ENotImplementedHashLibException = class(EHashLibException); 24 | EUnsupportedTypeHashLibException = class(EHashLibException); 25 | 26 | /// <summary> 27 | /// Represents a dynamic array of Byte. 28 | /// </summary> 29 | THashLibByteArray = TBytes; 30 | 31 | /// <summary> 32 | /// Represents a dynamic generic array of Type T. 33 | /// </summary> 34 | THashLibGenericArray<T> = array of T; 35 | 36 | /// <summary> 37 | /// Represents a dynamic generic array of array of Type T. 38 | /// </summary> 39 | THashLibMatrixGenericArray<T> = array of THashLibGenericArray<T>; 40 | 41 | {$IFDEF DELPHIXE_UP} 42 | /// <summary> 43 | /// Represents a dynamic array of UInt32. 44 | /// </summary> 45 | THashLibUInt32Array = TArray<UInt32>; 46 | 47 | /// <summary> 48 | /// Represents a dynamic array of UInt64. 49 | /// </summary> 50 | THashLibUInt64Array = TArray<UInt64>; 51 | 52 | /// <summary> 53 | /// Represents a dynamic array of String. 54 | /// </summary> 55 | THashLibStringArray = TArray<String>; 56 | 57 | /// <summary> 58 | /// Represents a dynamic array of Char. 59 | /// </summary> 60 | THashLibCharArray = TArray<Char>; 61 | 62 | /// <summary> 63 | /// Represents a dynamic array of array of Byte. 64 | /// </summary> 65 | THashLibMatrixByteArray = TArray<THashLibByteArray>; 66 | 67 | /// <summary> 68 | /// Represents a dynamic array of array of UInt32. 69 | /// </summary> 70 | THashLibMatrixUInt32Array = TArray<THashLibUInt32Array>; 71 | 72 | /// <summary> 73 | /// Represents a dynamic array of array of UInt64. 74 | /// </summary> 75 | THashLibMatrixUInt64Array = TArray<THashLibUInt64Array>; 76 | 77 | {$ELSE} 78 | /// <summary> 79 | /// Represents a dynamic array of UInt32. 80 | /// </summary> 81 | THashLibUInt32Array = array of UInt32; 82 | 83 | /// <summary> 84 | /// Represents a dynamic array of UInt64. 85 | /// </summary> 86 | THashLibUInt64Array = array of UInt64; 87 | 88 | /// <summary> 89 | /// Represents a dynamic array of String. 90 | /// </summary> 91 | THashLibStringArray = array of String; 92 | 93 | /// <summary> 94 | /// Represents a dynamic array of Char. 95 | /// </summary> 96 | THashLibCharArray = array of Char; 97 | 98 | /// <summary> 99 | /// Represents a dynamic array of array of Byte. 100 | /// </summary> 101 | THashLibMatrixByteArray = array of THashLibByteArray; 102 | 103 | /// <summary> 104 | /// Represents a dynamic array of array of UInt32. 105 | /// </summary> 106 | THashLibMatrixUInt32Array = array of THashLibUInt32Array; 107 | 108 | /// <summary> 109 | /// Represents a dynamic array of array of UInt64. 110 | /// </summary> 111 | THashLibMatrixUInt64Array = array of THashLibUInt64Array; 112 | {$ENDIF DELPHIXE_UP} 113 | 114 | implementation 115 | 116 | {$IFDEF FPC} 117 | 118 | initialization 119 | 120 | // Set UTF-8 in AnsiStrings, just like Lazarus 121 | SetMultiByteConversionCodePage(CP_UTF8); 122 | // SetMultiByteFileSystemCodePage(CP_UTF8); not needed, this is the default under Windows 123 | SetMultiByteRTLFileSystemCodePage(CP_UTF8); 124 | {$ENDIF FPC} 125 | 126 | end. 127 | -------------------------------------------------------------------------------- /HashLib.Tests/FreePascal.Tests/HashLib.Tests.lpi: -------------------------------------------------------------------------------- 1 | <?xml version="1.0" encoding="UTF-8"?> 2 | <CONFIG> 3 | <ProjectOptions> 4 | <Version Value="11"/> 5 | <PathDelim Value="\"/> 6 | <General> 7 | <SessionStorage Value="InProjectDir"/> 8 | <MainUnit Value="0"/> 9 | <Title Value="HashLib.Tests"/> 10 | <ResourceType Value="res"/> 11 | <UseXPManifest Value="True"/> 12 | </General> 13 | <i18n> 14 | <EnableI18N LFM="False"/> 15 | </i18n> 16 | <BuildModes Count="1"> 17 | <Item1 Name="Default" Default="True"/> 18 | </BuildModes> 19 | <PublishOptions> 20 | <Version Value="2"/> 21 | </PublishOptions> 22 | <RunParams> 23 | <local> 24 | <FormatVersion Value="1"/> 25 | </local> 26 | </RunParams> 27 | <RequiredPackages Count="4"> 28 | <Item1> 29 | <PackageName Value="HashLib4PascalPackage"/> 30 | </Item1> 31 | <Item2> 32 | <PackageName Value="fpcunittestrunner"/> 33 | </Item2> 34 | <Item3> 35 | <PackageName Value="LCL"/> 36 | </Item3> 37 | <Item4> 38 | <PackageName Value="FCL"/> 39 | </Item4> 40 | </RequiredPackages> 41 | <Units Count="14"> 42 | <Unit0> 43 | <Filename Value="HashLib.lpr"/> 44 | <IsPartOfProject Value="True"/> 45 | <UnitName Value="HashLib.Tests"/> 46 | </Unit0> 47 | <Unit1> 48 | <Filename Value="..\src\BitConverterTests.pas"/> 49 | <IsPartOfProject Value="True"/> 50 | </Unit1> 51 | <Unit2> 52 | <Filename Value="..\src\PBKDF2_HMACTests.pas"/> 53 | <IsPartOfProject Value="True"/> 54 | </Unit2> 55 | <Unit3> 56 | <Filename Value="..\src\CryptoTests.pas"/> 57 | <IsPartOfProject Value="True"/> 58 | </Unit3> 59 | <Unit4> 60 | <Filename Value="..\src\Hash128Tests.pas"/> 61 | <IsPartOfProject Value="True"/> 62 | </Unit4> 63 | <Unit5> 64 | <Filename Value="..\src\Hash64Tests.pas"/> 65 | <IsPartOfProject Value="True"/> 66 | </Unit5> 67 | <Unit6> 68 | <Filename Value="..\src\Hash32Tests.pas"/> 69 | <IsPartOfProject Value="True"/> 70 | </Unit6> 71 | <Unit7> 72 | <Filename Value="..\src\NullDigestTests.pas"/> 73 | <IsPartOfProject Value="True"/> 74 | </Unit7> 75 | <Unit8> 76 | <Filename Value="..\src\ChecksumTests.pas"/> 77 | <IsPartOfProject Value="True"/> 78 | </Unit8> 79 | <Unit9> 80 | <Filename Value="..\src\HashLibTestBase.pas"/> 81 | <IsPartOfProject Value="True"/> 82 | </Unit9> 83 | <Unit10> 84 | <Filename Value="..\src\PBKDF_Argon2Tests.pas"/> 85 | <IsPartOfProject Value="True"/> 86 | </Unit10> 87 | <Unit11> 88 | <Filename Value="..\src\PBKDF_ScryptTests.pas"/> 89 | <IsPartOfProject Value="True"/> 90 | </Unit11> 91 | <Unit12> 92 | <Filename Value="..\src\CRCTests.pas"/> 93 | <IsPartOfProject Value="True"/> 94 | </Unit12> 95 | <Unit13> 96 | <Filename Value="..\src\TestVectors.pas"/> 97 | <IsPartOfProject Value="True"/> 98 | </Unit13> 99 | </Units> 100 | </ProjectOptions> 101 | <CompilerOptions> 102 | <Version Value="11"/> 103 | <PathDelim Value="\"/> 104 | <Target> 105 | <Filename Value=".\bin\HashLib"/> 106 | </Target> 107 | <SearchPaths> 108 | <IncludeFiles Value="$(ProjOutDir)"/> 109 | <OtherUnitFiles Value="..\src"/> 110 | </SearchPaths> 111 | <Linking> 112 | <Debugging> 113 | <UseHeaptrc Value="True"/> 114 | </Debugging> 115 | </Linking> 116 | </CompilerOptions> 117 | <Debugging> 118 | <Exceptions Count="3"> 119 | <Item1> 120 | <Name Value="EAbort"/> 121 | </Item1> 122 | <Item2> 123 | <Name Value="ECodetoolError"/> 124 | </Item2> 125 | <Item3> 126 | <Name Value="EFOpenError"/> 127 | </Item3> 128 | </Exceptions> 129 | </Debugging> 130 | </CONFIG> 131 | -------------------------------------------------------------------------------- /HashLib.Tests/src/Hash64Tests.pas: -------------------------------------------------------------------------------- 1 | unit Hash64Tests; 2 | 3 | interface 4 | 5 | uses 6 | {$IFDEF FPC} 7 | fpcunit, 8 | testregistry, 9 | {$ELSE} 10 | TestFramework, 11 | {$ENDIF FPC} 12 | HashLibTestBase, 13 | HlpHashFactory; 14 | 15 | // Hash64 16 | 17 | type 18 | TTestFNV64 = class(THashAlgorithmTestCase) 19 | 20 | protected 21 | procedure SetUp; override; 22 | procedure TearDown; override; 23 | 24 | end; 25 | 26 | type 27 | 28 | TTestFNV1a64 = class(THashAlgorithmTestCase) 29 | 30 | protected 31 | procedure SetUp; override; 32 | procedure TearDown; override; 33 | 34 | end; 35 | 36 | type 37 | 38 | TTestMurmur2_64 = class(THashWithUInt64AsKeyAlgorithmTestCase) 39 | 40 | protected 41 | procedure SetUp; override; 42 | procedure TearDown; override; 43 | 44 | end; 45 | 46 | type 47 | TTestSipHash2_4 = class(THashWithExternalKeyAlgorithmTestCase) 48 | 49 | protected 50 | procedure SetUp; override; 51 | procedure TearDown; override; 52 | 53 | end; 54 | 55 | type 56 | 57 | TTestXXHash64 = class(THashWithUInt64AsKeyAlgorithmTestCase) 58 | 59 | protected 60 | procedure SetUp; override; 61 | procedure TearDown; override; 62 | 63 | end; 64 | 65 | implementation 66 | 67 | // Hash64 68 | 69 | { TTestFNV64 } 70 | 71 | procedure TTestFNV64.SetUp; 72 | begin 73 | inherited; 74 | HashInstance := THashFactory.THash64.CreateFNV(); 75 | HashOfEmptyData := '0000000000000000'; 76 | HashOfDefaultData := '061A6856F5925B83'; 77 | HashOfOnetoNine := 'B8FB573C21FE68F1'; 78 | HashOfABCDE := '77018B280326F529'; 79 | end; 80 | 81 | procedure TTestFNV64.TearDown; 82 | begin 83 | HashInstance := Nil; 84 | inherited; 85 | end; 86 | 87 | { TTestFNV1a64 } 88 | 89 | procedure TTestFNV1a64.SetUp; 90 | begin 91 | inherited; 92 | HashInstance := THashFactory.THash64.CreateFNV1a(); 93 | HashOfEmptyData := 'CBF29CE484222325'; 94 | HashOfDefaultData := '5997E22BF92B0598'; 95 | HashOfOnetoNine := '06D5573923C6CDFC'; 96 | HashOfABCDE := '6348C52D762364A8'; 97 | end; 98 | 99 | procedure TTestFNV1a64.TearDown; 100 | begin 101 | HashInstance := Nil; 102 | inherited; 103 | end; 104 | 105 | { TTestMurmur2_64 } 106 | 107 | procedure TTestMurmur2_64.SetUp; 108 | begin 109 | inherited; 110 | HashInstance := THashFactory.THash64.CreateMurmur2(); 111 | HashOfEmptyData := '0000000000000000'; 112 | HashOfDefaultData := '831EFD69DC9E99F9'; 113 | HashOfOnetoNine := '4977490251674330'; 114 | HashOfABCDE := '1182974836D6DBB7'; 115 | HashOfDefaultDataWithMaxUInt64AsKey := 'FF0A342F0AF9ADC6'; 116 | end; 117 | 118 | procedure TTestMurmur2_64.TearDown; 119 | begin 120 | HashInstance := Nil; 121 | inherited; 122 | end; 123 | 124 | { TTestSipHash2_4 } 125 | 126 | procedure TTestSipHash2_4.SetUp; 127 | begin 128 | inherited; 129 | HashInstance := THashFactory.THash64.CreateSipHash2_4(); 130 | HashOfEmptyData := '310E0EDD47DB6F72'; 131 | HashOfDefaultData := '4ED2198628C443AA'; 132 | HashOfOnetoNine := 'FDFE0E0296FC60CA'; 133 | HashOfABCDE := '73B879EAE16345A7'; 134 | HashOfDefaultDataWithExternalKey := '4ED2198628C443AA'; 135 | end; 136 | 137 | procedure TTestSipHash2_4.TearDown; 138 | begin 139 | HashInstance := Nil; 140 | inherited; 141 | end; 142 | 143 | { TTestXXHash64 } 144 | 145 | procedure TTestXXHash64.SetUp; 146 | begin 147 | inherited; 148 | HashInstance := THashFactory.THash64.CreateXXHash64(); 149 | HashOfEmptyData := 'EF46DB3751D8E999'; 150 | HashOfDefaultData := '0F1FADEDD0B77861'; 151 | HashOfOnetoNine := '8CB841DB40E6AE83'; 152 | HashOfABCDE := '07E3670C0C8DC7EB'; 153 | HashOfDefaultDataWithMaxUInt64AsKey := '68DCC1056096A94F'; 154 | end; 155 | 156 | procedure TTestXXHash64.TearDown; 157 | begin 158 | HashInstance := Nil; 159 | inherited; 160 | end; 161 | 162 | initialization 163 | 164 | // Register any test cases with the test runner 165 | 166 | {$IFDEF FPC} 167 | // Hash64 168 | RegisterTest(TTestFNV64); 169 | RegisterTest(TTestFNV1a64); 170 | RegisterTest(TTestMurmur2_64); 171 | RegisterTest(TTestSipHash2_4); 172 | RegisterTest(TTestXXHash64); 173 | {$ELSE} 174 | // Hash64 175 | RegisterTest(TTestFNV64.Suite); 176 | RegisterTest(TTestFNV1a64.Suite); 177 | RegisterTest(TTestMurmur2_64.Suite); 178 | RegisterTest(TTestSipHash2_4.Suite); 179 | RegisterTest(TTestXXHash64.Suite); 180 | {$ENDIF FPC} 181 | 182 | end. 183 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | HashLib4Pascal: Hashing for Modern Object Pascal [![License](http://img.shields.io/badge/license-MIT-green.svg)](https://github.com/Xor-el/HashLib4Pascal/blob/master/LICENSE) 2 | ======================================== 3 | 4 | ``HashLib4Pascal`` is an Object Pascal hashing library released under the permissive [MIT License](https://github.com/Xor-el/HashLib4Pascal/blob/master/LICENSE) which provides an easy to use interface for computing hashes and checksums of data. It also supports state based (incremental) hashing. 5 | 6 | ``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. 7 | 8 | Development is coordinated on [GitHub](https://github.com/Xor-el/HashLib4Pascal) and contributions are welcome. If you need help, please open an issue [here](https://github.com/Xor-el/HashLib4Pascal/issues). 9 | 10 | 11 | **Build Status** 12 | [![Build Status](https://github.com/Xor-el/HashLib4Pascal/actions/workflows/make.yml/badge.svg)](https://github.com/Xor-el/HashLib4Pascal/actions/workflows/make.yml) 13 | 14 | Available Algorithms 15 | ---------------------------------------- 16 | 17 | ### Hashes 18 | ---------------------------------------- 19 | ##### Cyclic Redundancy Checks 20 | 21 | * `All CRC Variants from CRC3 to CRC64` 22 | 23 | ##### Checksums 24 | 25 | * `Adler32` 26 | 27 | ##### Non-Cryptographic Hash Functions 28 | ---------------------------------------- 29 | 30 | ###### 32 bit hashes 31 | 32 | * `AP` `BKDR` `Bernstein` `Bernstein1` `DEK` `DJB` `ELF` `FNV` 33 | 34 | * `FNV1a` `JS` `Jenkins3` `Murmur2` `MurmurHash3_x86_32` `OneAtTime` 35 | 36 | * `PJW` `RS` `Rotating` `SDBM` `ShiftAndXor` `SuperFast` `XXHash32` 37 | 38 | ###### 64 bit hashes 39 | 40 | * `FNV64` `FNV1a64` `Murmur2_64` `SipHash2_4` `XXHash64` 41 | 42 | ###### 128 bit hashes 43 | 44 | * `MurmurHash3_x86_128` `MurmurHash3_x64_128` 45 | 46 | ##### Cryptographic Hash Functions 47 | ---------------------------------------- 48 | 49 | * `MD2` 50 | 51 | * `MD4` 52 | 53 | * `MD5` 54 | 55 | * `SHA-0` 56 | 57 | * `SHA-1` 58 | 59 | * `SHA-2 (224, 256, 384, 512, 512-224, 512-256)` 60 | 61 | * `GOST 34.11-94` 62 | 63 | * `GOST R 34.11-2012 (AKA Streebog) (256, 512)` 64 | 65 | * `Grindahl (256, 512)` 66 | 67 | * `Has160` 68 | 69 | * `RIPEMD (128, 160, 256, 256, 320)` 70 | 71 | * `Tiger (128, 160, 192 (Rounds 3, 4, 5))` 72 | 73 | * `Tiger2 (128, 160, 192 (Rounds 3, 4, 5))` 74 | 75 | * `Snefru (128, 256)` 76 | 77 | * `Haval (128, 160, 192, 224, 256 (Rounds 3, 4, 5))` 78 | 79 | * `Panama` 80 | 81 | * `RadioGatun (RadioGatun32, RadioGatun64)` 82 | 83 | * `WhirlPool` 84 | 85 | * `Blake2B (160, 256, 384, 512)` 86 | 87 | * `Blake2S (128, 160, 224, 256)` 88 | 89 | * `SHA-3 (224, 256, 384, 512)` 90 | 91 | * `Keccak (224, 256, 288, 384, 512)` 92 | 93 | * `Blake2BP` 94 | 95 | * `Blake2SP` 96 | 97 | * `Blake3` 98 | 99 | ### Key Derivation Functions 100 | ---------------------------------------- 101 | 102 | ###### Password Hashing Schemes (Password Based Key Derivation Functions) 103 | 104 | ---------------------------------------- 105 | 106 | * `PBKDF2` 107 | 108 | * `Argon2 (2i, 2d and 2id variants)` 109 | 110 | * `Scrypt` 111 | 112 | ### MAC 113 | ---------------------------------------- 114 | 115 | * `HMAC (all supported hashes)` 116 | 117 | * `KMAC (KMAC128, KMAC256)` 118 | 119 | * `Blake2MAC (Blake2BMAC, Blake2SMAC)` 120 | 121 | ### XOF (Extendable Output Function) 122 | ---------------------------------------- 123 | 124 | * `Shake (Shake-128, Shake-256)` 125 | 126 | * `CShake (CShake-128, CShake-256)` 127 | 128 | * `Blake2X (Blake2XS, Blake2XB)` 129 | 130 | * `KMACXOF (KMAC128XOF, KMAC256XOF)` 131 | 132 | * `Blake3XOF` 133 | 134 | ### Supported Compilers 135 | ---------------------------------------- 136 | 137 | * `FreePascal 3.0.0+` 138 | 139 | * `Delphi 2010+` 140 | 141 | ### Other Implementations 142 | ---------------------------------------- 143 | 144 | If you want implementations in other languages, you can check out these 145 | 146 | * [HashLib4CPP](https://github.com/ron4fun/HashLib4CPP) by Mbadiwe Nnaemeka Ronald 147 | 148 | ### Tip Jar 149 | ---------------------------------------- 150 | 151 | * :dollar: **Bitcoin**: `1MhFfW7tDuEHQSgie65uJcAfJgCNchGeKf` 152 | * :euro: **Ethereum**: `0x6c1DC21aeC49A822A4f1E3bf07c623C2C1978a98` 153 | * :pound: **Pascalcoin**: `345367-40` 154 | -------------------------------------------------------------------------------- /HashLib/src/Include/HashLib.inc: -------------------------------------------------------------------------------- 1 | { *********************************************************************************** } 2 | { * HashLib Library * } 3 | { * Copyright (c) 2016 - 2020 Ugochukwu Mmaduekwe * } 4 | { * Github Repository <https://github.com/Xor-el> * } 5 | 6 | { * Distributed under the MIT software license, see the accompanying file LICENSE * } 7 | { * or visit http://www.opensource.org/licenses/mit-license.php. * } 8 | 9 | { * ******************************************************************************* * } 10 | 11 | (* &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& *) 12 | 13 | 14 | {$DEFINE DELPHI} 15 | 16 | (* &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& *) 17 | {$IFDEF FPC} 18 | {$I HashLibHelper.inc} // Had to Include this Since Delphi Does not allow "FPC_FULLVERSION" to Compile. 19 | {$UNDEF DELPHI} 20 | {$MODE delphi} 21 | 22 | {$IFDEF FPC_LITTLE_ENDIAN} 23 | {$DEFINE HASHLIB_LITTLE_ENDIAN} 24 | {$ENDIF} 25 | 26 | {$IFDEF FPC_REQUIRES_PROPER_ALIGNMENT} 27 | {$DEFINE HASHLIB_REQUIRES_PROPER_ALIGNMENT} 28 | {$ENDIF} 29 | 30 | {$DEFINE USE_UNROLLED_VARIANT} 31 | 32 | // Disable Overflow and RangeChecks. 33 | {$OVERFLOWCHECKS OFF} 34 | {$RANGECHECKS OFF} 35 | 36 | // Enable Pointer Math 37 | {$POINTERMATH ON} 38 | 39 | // Disable Warnings and Hints. 40 | {$WARNINGS OFF} 41 | {$HINTS OFF} 42 | {$NOTES OFF} 43 | 44 | // Optimizations 45 | {$OPTIMIZATION LEVEL3} 46 | // disable "USELOADMODIFYSTORE" because it produces incorrect result 47 | // when used in combination with -CpCOREAVX2 and -OpCOREAVX2 in FPC 3.2.0 beta 48 | {$IFDEF FPC_GREATER_THAN_3.0.4} 49 | {$OPTIMIZATION NOUSELOADMODIFYSTORE} 50 | {$ENDIF} 51 | // level 4 optimizations 52 | {$OPTIMIZATION ORDERFIELDS} 53 | {$OPTIMIZATION DEADVALUES} 54 | 55 | {$IFDEF CPUI386} 56 | {$OPTIMIZATION USEEBP} 57 | {$ENDIF} 58 | 59 | {$IFDEF CPUX86_64} 60 | {$OPTIMIZATION USERBP} 61 | {$ENDIF} 62 | 63 | {.$DEFINE USE_MTPROCS} 64 | {.$DEFINE USE_PASMP} 65 | 66 | {$IF DEFINED(USE_MTPROCS) AND DEFINED(USE_PASMP)} 67 | {$MESSAGE ERROR 'Only One Threading Library can be used at a time.'} 68 | {$IFEND} 69 | 70 | {$ENDIF FPC} 71 | 72 | (* &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& *) 73 | 74 | {$IFDEF DELPHI} 75 | 76 | {$DEFINE HASHLIB_LITTLE_ENDIAN} 77 | 78 | {$IFDEF CPUARM} 79 | {$DEFINE HASHLIB_REQUIRES_PROPER_ALIGNMENT} 80 | {$ENDIF} 81 | 82 | // XE3 and Above 83 | {$IF CompilerVersion >= 24.0} 84 | {$DEFINE DELPHIXE3_UP} 85 | {$LEGACYIFEND ON} 86 | {$ZEROBASEDSTRINGS OFF} 87 | {$IFEND} 88 | 89 | // 2010 only 90 | {$IF CompilerVersion = 21.0} 91 | {$DEFINE DELPHI2010} 92 | {$IFEND} 93 | 94 | // 2010 and Above 95 | {$IF CompilerVersion >= 21.0} 96 | {$DEFINE DELPHI2010_UP} 97 | {$IFEND} 98 | 99 | // XE and Above 100 | {$IF CompilerVersion >= 22.0} 101 | {$DEFINE DELPHIXE_UP} 102 | {$IFEND} 103 | 104 | // XE2 and Above 105 | {$IF CompilerVersion >= 23.0} 106 | {$DEFINE DELPHIXE2_UP} 107 | {$IFEND} 108 | 109 | // XE4 and Above 110 | {$IF CompilerVersion >= 25.0} 111 | {$DEFINE DELPHIXE4_UP} 112 | {$DEFINE SHIFT_OVERFLOW_BUG_FIXED} 113 | {$IFEND} 114 | 115 | // XE7 and Above 116 | {$IF CompilerVersion >= 28.0} 117 | {$DEFINE DELPHIXE7_UP} 118 | {.$DEFINE USE_DELPHI_PPL} // Use Delphi Parallel Programming Library 119 | {$DEFINE HAS_DELPHI_NET_ENCODING} 120 | {$IFEND} 121 | 122 | // 2010 and Above 123 | {$IFNDEF DELPHI2010_UP} 124 | {$MESSAGE ERROR 'This Library requires Delphi 2010 or higher.'} 125 | {$ENDIF} 126 | 127 | {$DEFINE USE_UNROLLED_VARIANT} 128 | 129 | {.$DEFINE USE_PASMP} 130 | 131 | {$IF DEFINED(USE_DELPHI_PPL) AND DEFINED(USE_PASMP)} 132 | {$MESSAGE ERROR 'Only One Threading Library can be used at a time.'} 133 | {$IFEND} 134 | 135 | // This option is needed to enable code browsing (aka Ctrl+Click) 136 | // It does not affect the binary size or generated code 137 | {$DEFINITIONINFO ON} 138 | 139 | // Disable Overflow and RangeChecks. 140 | {$OVERFLOWCHECKS OFF} 141 | {$RANGECHECKS OFF} 142 | 143 | // Enable Pointer Math 144 | {$POINTERMATH ON} 145 | 146 | // Disable String Checks 147 | {$STRINGCHECKS OFF} 148 | 149 | // Disable Duplicate Constructor Warnings 150 | {$WARN DUPLICATE_CTOR_DTOR OFF} 151 | 152 | {$ENDIF DELPHI} 153 | 154 | (* &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& *) 155 | -------------------------------------------------------------------------------- /HashLib/src/Hash32/HlpMurmur2.pas: -------------------------------------------------------------------------------- 1 | unit HlpMurmur2; 2 | 3 | {$I ..\Include\HashLib.inc} 4 | 5 | interface 6 | 7 | uses 8 | {$IFDEF DELPHI2010} 9 | SysUtils, // to get rid of compiler hint "not inlined" on Delphi 2010. 10 | {$ENDIF DELPHI2010} 11 | HlpHashLibTypes, 12 | {$IFDEF DELPHI} 13 | HlpHash, 14 | {$ENDIF DELPHI} 15 | HlpIHash, 16 | HlpConverters, 17 | HlpIHashInfo, 18 | HlpHashResult, 19 | HlpIHashResult, 20 | HlpMultipleTransformNonBlock, 21 | HlpNullable; 22 | 23 | resourcestring 24 | SInvalidKeyLength = 'KeyLength Must Be Equal to %d'; 25 | 26 | type 27 | // The original MurmurHash2 32-bit algorithm by Austin Appleby. 28 | TMurmur2 = class sealed(TMultipleTransformNonBlock, IHash32, IHashWithKey, 29 | ITransformBlock) 30 | 31 | strict private 32 | var 33 | FKey, FWorkingKey: UInt32; 34 | 35 | const 36 | CKEY = UInt32($0); 37 | M = UInt32($5BD1E995); 38 | R = Int32(24); 39 | 40 | function GetKeyLength(): TNullableInteger; 41 | function GetKey: THashLibByteArray; inline; 42 | procedure SetKey(const AValue: THashLibByteArray); inline; 43 | 44 | strict protected 45 | function ComputeAggregatedBytes(const AData: THashLibByteArray) 46 | : IHashResult; override; 47 | 48 | public 49 | constructor Create(); 50 | procedure Initialize(); override; 51 | function Clone(): IHash; override; 52 | property KeyLength: TNullableInteger read GetKeyLength; 53 | property Key: THashLibByteArray read GetKey write SetKey; 54 | 55 | end; 56 | 57 | implementation 58 | 59 | { TMurmur2 } 60 | 61 | constructor TMurmur2.Create; 62 | begin 63 | Inherited Create(4, 4); 64 | FKey := CKEY; 65 | end; 66 | 67 | function TMurmur2.GetKey: THashLibByteArray; 68 | begin 69 | result := TConverters.ReadUInt32AsBytesLE(FKey); 70 | end; 71 | 72 | procedure TMurmur2.SetKey(const AValue: THashLibByteArray); 73 | begin 74 | if (AValue = Nil) then 75 | begin 76 | FKey := CKEY; 77 | end 78 | else 79 | begin 80 | if System.Length(AValue) <> KeyLength.value then 81 | begin 82 | raise EArgumentHashLibException.CreateResFmt(@SInvalidKeyLength, 83 | [KeyLength.value]); 84 | end; 85 | FKey := TConverters.ReadBytesAsUInt32LE(PByte(AValue), 0); 86 | end; 87 | end; 88 | 89 | function TMurmur2.GetKeyLength: TNullableInteger; 90 | begin 91 | result := 4; 92 | end; 93 | 94 | procedure TMurmur2.Initialize; 95 | begin 96 | FWorkingKey := FKey; 97 | inherited Initialize(); 98 | end; 99 | 100 | function TMurmur2.Clone(): IHash; 101 | var 102 | LHashInstance: TMurmur2; 103 | begin 104 | LHashInstance := TMurmur2.Create(); 105 | LHashInstance.FKey := FKey; 106 | LHashInstance.FWorkingKey := FWorkingKey; 107 | FBuffer.Position := 0; 108 | LHashInstance.FBuffer.CopyFrom(FBuffer, FBuffer.Size); 109 | result := LHashInstance as IHash; 110 | result.BufferSize := BufferSize; 111 | end; 112 | 113 | function TMurmur2.ComputeAggregatedBytes(const AData: THashLibByteArray) 114 | : IHashResult; 115 | var 116 | LLength, LCurrentIndex, LNBlocks, LIdx: Int32; 117 | LBlock, LH: UInt32; 118 | LPtrData: PByte; 119 | LPtrDataCardinal: PCardinal; 120 | begin 121 | LLength := System.Length(AData); 122 | LPtrData := PByte(AData); 123 | 124 | if (LLength = 0) then 125 | begin 126 | result := THashResult.Create(Int32(0)); 127 | Exit; 128 | end; 129 | 130 | LH := FWorkingKey xor UInt32(LLength); 131 | 132 | LCurrentIndex := 0; 133 | LIdx := 0; 134 | LPtrDataCardinal := PCardinal(LPtrData); 135 | LNBlocks := LLength shr 2; 136 | 137 | while LIdx < LNBlocks do 138 | begin 139 | LBlock := TConverters.ReadPCardinalAsUInt32LE(LPtrDataCardinal + LIdx); 140 | 141 | LBlock := LBlock * M; 142 | LBlock := LBlock xor (LBlock shr R); 143 | LBlock := LBlock * M; 144 | 145 | LH := LH * M; 146 | LH := LH xor LBlock; 147 | 148 | System.Inc(LIdx); 149 | System.Inc(LCurrentIndex, 4); 150 | System.Dec(LLength, 4); 151 | end; 152 | 153 | case LLength of 154 | 3: 155 | begin 156 | LH := LH xor (AData[LCurrentIndex + 2] shl 16); 157 | 158 | LH := LH xor (AData[LCurrentIndex + 1] shl 8); 159 | 160 | LH := LH xor (AData[LCurrentIndex]); 161 | 162 | LH := LH * M; 163 | end; 164 | 165 | 2: 166 | begin 167 | LH := LH xor (AData[LCurrentIndex + 1] shl 8); 168 | 169 | LH := LH xor (AData[LCurrentIndex]); 170 | 171 | LH := LH * M; 172 | end; 173 | 174 | 1: 175 | begin 176 | LH := LH xor (AData[LCurrentIndex]); 177 | 178 | LH := LH * M; 179 | end; 180 | end; 181 | 182 | LH := LH xor (LH shr 13); 183 | 184 | LH := LH * M; 185 | LH := LH xor (LH shr 15); 186 | 187 | result := THashResult.Create(Int32(LH)); 188 | end; 189 | 190 | end. 191 | -------------------------------------------------------------------------------- /HashLib.Tests/FreePascal.Tests/HashLibConsole.lpi: -------------------------------------------------------------------------------- 1 | <?xml version="1.0" encoding="UTF-8"?> 2 | <CONFIG> 3 | <ProjectOptions> 4 | <Version Value="11"/> 5 | <PathDelim Value="\"/> 6 | <General> 7 | <SessionStorage Value="InProjectDir"/> 8 | <MainUnit Value="0"/> 9 | <Title Value="HashLib.Tests"/> 10 | <ResourceType Value="res"/> 11 | </General> 12 | <i18n> 13 | <EnableI18N LFM="False"/> 14 | </i18n> 15 | <BuildModes Count="2"> 16 | <Item1 Name="Default" Default="True"/> 17 | <Item2 Name="Debug"> 18 | <CompilerOptions> 19 | <Version Value="11"/> 20 | <PathDelim Value="\"/> 21 | <SearchPaths> 22 | <IncludeFiles Value="$(ProjOutDir)"/> 23 | <OtherUnitFiles Value="..\src"/> 24 | </SearchPaths> 25 | <Parsing> 26 | <SyntaxOptions> 27 | <IncludeAssertionCode Value="True"/> 28 | </SyntaxOptions> 29 | </Parsing> 30 | <CodeGeneration> 31 | <Checks> 32 | <IOChecks Value="True"/> 33 | <RangeChecks Value="True"/> 34 | <OverflowChecks Value="True"/> 35 | <StackChecks Value="True"/> 36 | </Checks> 37 | </CodeGeneration> 38 | <Linking> 39 | <Debugging> 40 | <DebugInfoType Value="dsDwarf2Set"/> 41 | <UseHeaptrc Value="True"/> 42 | <UseExternalDbgSyms Value="True"/> 43 | </Debugging> 44 | </Linking> 45 | </CompilerOptions> 46 | </Item2> 47 | </BuildModes> 48 | <PublishOptions> 49 | <Version Value="2"/> 50 | </PublishOptions> 51 | <RunParams> 52 | <local> 53 | <FormatVersion Value="1"/> 54 | <CommandLineParams Value="--format=plain --all --progress"/> 55 | </local> 56 | </RunParams> 57 | <RequiredPackages Count="2"> 58 | <Item1> 59 | <PackageName Value="HashLib4PascalPackage"/> 60 | </Item1> 61 | <Item2> 62 | <PackageName Value="FCL"/> 63 | </Item2> 64 | </RequiredPackages> 65 | <Units Count="14"> 66 | <Unit0> 67 | <Filename Value="HashLibConsole.lpr"/> 68 | <IsPartOfProject Value="True"/> 69 | <UnitName Value="HashLib.Tests"/> 70 | </Unit0> 71 | <Unit1> 72 | <Filename Value="..\src\BitConverterTests.pas"/> 73 | <IsPartOfProject Value="True"/> 74 | </Unit1> 75 | <Unit2> 76 | <Filename Value="..\src\PBKDF2_HMACTests.pas"/> 77 | <IsPartOfProject Value="True"/> 78 | </Unit2> 79 | <Unit3> 80 | <Filename Value="..\src\CryptoTests.pas"/> 81 | <IsPartOfProject Value="True"/> 82 | </Unit3> 83 | <Unit4> 84 | <Filename Value="..\src\Hash128Tests.pas"/> 85 | <IsPartOfProject Value="True"/> 86 | </Unit4> 87 | <Unit5> 88 | <Filename Value="..\src\Hash64Tests.pas"/> 89 | <IsPartOfProject Value="True"/> 90 | </Unit5> 91 | <Unit6> 92 | <Filename Value="..\src\Hash32Tests.pas"/> 93 | <IsPartOfProject Value="True"/> 94 | </Unit6> 95 | <Unit7> 96 | <Filename Value="..\src\NullDigestTests.pas"/> 97 | <IsPartOfProject Value="True"/> 98 | </Unit7> 99 | <Unit8> 100 | <Filename Value="..\src\ChecksumTests.pas"/> 101 | <IsPartOfProject Value="True"/> 102 | </Unit8> 103 | <Unit9> 104 | <Filename Value="..\src\HashLibTestBase.pas"/> 105 | <IsPartOfProject Value="True"/> 106 | </Unit9> 107 | <Unit10> 108 | <Filename Value="..\src\PBKDF_Argon2Tests.pas"/> 109 | <IsPartOfProject Value="True"/> 110 | </Unit10> 111 | <Unit11> 112 | <Filename Value="..\src\PBKDF_ScryptTests.pas"/> 113 | <IsPartOfProject Value="True"/> 114 | </Unit11> 115 | <Unit12> 116 | <Filename Value="..\src\CRCTests.pas"/> 117 | <IsPartOfProject Value="True"/> 118 | </Unit12> 119 | <Unit13> 120 | <Filename Value="..\src\TestVectors.pas"/> 121 | <IsPartOfProject Value="True"/> 122 | </Unit13> 123 | </Units> 124 | </ProjectOptions> 125 | <CompilerOptions> 126 | <Version Value="11"/> 127 | <PathDelim Value="\"/> 128 | <Target> 129 | <Filename Value=".\bin\HashLib"/> 130 | </Target> 131 | <SearchPaths> 132 | <IncludeFiles Value="$(ProjOutDir)"/> 133 | <OtherUnitFiles Value="..\src"/> 134 | </SearchPaths> 135 | </CompilerOptions> 136 | <Debugging> 137 | <Exceptions Count="3"> 138 | <Item1> 139 | <Name Value="EAbort"/> 140 | </Item1> 141 | <Item2> 142 | <Name Value="ECodetoolError"/> 143 | </Item2> 144 | <Item3> 145 | <Name Value="EFOpenError"/> 146 | </Item3> 147 | </Exceptions> 148 | </Debugging> 149 | </CONFIG> 150 | -------------------------------------------------------------------------------- /HashLib/src/Base/HlpHMACNotBuildInAdapter.pas: -------------------------------------------------------------------------------- 1 | unit HlpHMACNotBuildInAdapter; 2 | 3 | {$I ..\Include\HashLib.inc} 4 | 5 | interface 6 | 7 | uses 8 | SysUtils, 9 | HlpHashLibTypes, 10 | HlpHash, 11 | HlpIHash, 12 | HlpIHashInfo, 13 | HlpIHashResult, 14 | HlpArrayUtils; 15 | 16 | type 17 | 18 | THMACNotBuildInAdapter = class sealed(THash, IHMAC, IHMACNotBuildIn, ICrypto, 19 | ICryptoNotBuildIn) 20 | 21 | strict private 22 | var 23 | FHash: IHash; 24 | FOpad, FIpad, FKey: THashLibByteArray; 25 | 26 | constructor Create(const AUnderlyingHash: IHash; 27 | const AHMACKey: THashLibByteArray); 28 | 29 | strict protected 30 | 31 | function GetName: String; override; 32 | 33 | function GetKey(): THashLibByteArray; 34 | procedure SetKey(const AValue: THashLibByteArray); 35 | procedure UpdatePads(); 36 | 37 | public 38 | 39 | destructor Destroy; override; 40 | 41 | procedure Clear(); 42 | 43 | procedure Initialize(); override; 44 | function TransformFinal(): IHashResult; override; 45 | procedure TransformBytes(const AData: THashLibByteArray; 46 | AIndex, ALength: Int32); override; 47 | function Clone(): IHash; override; 48 | property Key: THashLibByteArray read GetKey write SetKey; 49 | property Name: String read GetName; 50 | 51 | class function CreateHMAC(const AHash: IHash; 52 | const AHMACKey: THashLibByteArray): IHMAC; static; 53 | 54 | end; 55 | 56 | implementation 57 | 58 | { THMACNotBuildInAdapter } 59 | 60 | procedure THMACNotBuildInAdapter.Clear(); 61 | begin 62 | TArrayUtils.ZeroFill(FKey); 63 | end; 64 | 65 | function THMACNotBuildInAdapter.Clone(): IHash; 66 | var 67 | HmacInstance: THMACNotBuildInAdapter; 68 | begin 69 | HmacInstance := THMACNotBuildInAdapter.Create(FHash.Clone(), FKey); 70 | HmacInstance.FOpad := System.Copy(FOpad); 71 | HmacInstance.FIpad := System.Copy(FIpad); 72 | result := HmacInstance as IHash; 73 | result.BufferSize := BufferSize; 74 | end; 75 | 76 | constructor THMACNotBuildInAdapter.Create(const AUnderlyingHash: IHash; 77 | const AHMACKey: THashLibByteArray); 78 | begin 79 | Inherited Create(AUnderlyingHash.HashSize, AUnderlyingHash.BlockSize); 80 | FHash := AUnderlyingHash; 81 | SetKey(AHMACKey); 82 | System.SetLength(FIpad, FHash.BlockSize); 83 | System.SetLength(FOpad, FHash.BlockSize); 84 | end; 85 | 86 | destructor THMACNotBuildInAdapter.Destroy; 87 | begin 88 | Clear(); 89 | inherited Destroy; 90 | end; 91 | 92 | function THMACNotBuildInAdapter.GetKey: THashLibByteArray; 93 | begin 94 | result := System.Copy(FKey); 95 | end; 96 | 97 | procedure THMACNotBuildInAdapter.SetKey(const AValue: THashLibByteArray); 98 | begin 99 | if (AValue = Nil) then 100 | begin 101 | FKey := Nil; 102 | end 103 | else 104 | begin 105 | FKey := System.Copy(AValue); 106 | end; 107 | end; 108 | 109 | procedure THMACNotBuildInAdapter.UpdatePads; 110 | var 111 | LKey: THashLibByteArray; 112 | LIdx, LBlockSize: Int32; 113 | begin 114 | LBlockSize := FHash.BlockSize; 115 | if (System.Length(Key) > LBlockSize) then 116 | begin 117 | LKey := FHash.ComputeBytes(Key).GetBytes(); 118 | end 119 | else 120 | begin 121 | LKey := Key; 122 | end; 123 | 124 | TArrayUtils.Fill(FIpad, 0, LBlockSize, Byte($36)); 125 | TArrayUtils.Fill(FOpad, 0, LBlockSize, Byte($5C)); 126 | 127 | LIdx := 0; 128 | while (LIdx < System.Length(LKey)) and (LIdx < LBlockSize) do 129 | begin 130 | FIpad[LIdx] := FIpad[LIdx] xor LKey[LIdx]; 131 | FOpad[LIdx] := FOpad[LIdx] xor LKey[LIdx]; 132 | System.Inc(LIdx); 133 | end; 134 | 135 | end; 136 | 137 | procedure THMACNotBuildInAdapter.Initialize; 138 | begin 139 | FHash.Initialize(); 140 | UpdatePads(); 141 | FHash.TransformBytes(FIpad); 142 | end; 143 | 144 | function THMACNotBuildInAdapter.TransformFinal: IHashResult; 145 | begin 146 | result := FHash.TransformFinal(); 147 | FHash.TransformBytes(FOpad); 148 | FHash.TransformBytes(result.GetBytes()); 149 | result := FHash.TransformFinal(); 150 | Initialize(); 151 | end; 152 | 153 | procedure THMACNotBuildInAdapter.TransformBytes(const AData: THashLibByteArray; 154 | AIndex, ALength: Int32); 155 | begin 156 | {$IFDEF DEBUG} 157 | System.Assert(AIndex >= 0); 158 | System.Assert(ALength >= 0); 159 | System.Assert(AIndex + ALength <= System.Length(AData)); 160 | {$ENDIF} 161 | FHash.TransformBytes(AData, AIndex, ALength); 162 | end; 163 | 164 | function THMACNotBuildInAdapter.GetName: String; 165 | begin 166 | result := Format('%s(%s)', ['THMAC', FHash.Name]); 167 | end; 168 | 169 | class function THMACNotBuildInAdapter.CreateHMAC(const AHash: IHash; 170 | const AHMACKey: THashLibByteArray): IHMAC; 171 | begin 172 | if Supports(AHash, IHMAC) then 173 | begin 174 | result := AHash as IHMAC; 175 | end 176 | else 177 | begin 178 | result := THMACNotBuildInAdapter.Create(AHash, AHMACKey); 179 | end; 180 | end; 181 | 182 | end. 183 | -------------------------------------------------------------------------------- /HashLib/src/Base/HlpHashBuffer.pas: -------------------------------------------------------------------------------- 1 | unit HlpHashBuffer; 2 | 3 | {$I ..\Include\HashLib.inc} 4 | 5 | interface 6 | 7 | uses 8 | SysUtils, 9 | HlpArrayUtils, 10 | HlpHashLibTypes; 11 | 12 | resourcestring 13 | SHashBufferMessage = 'HashBuffer, Length: %d, Position: %d, IsEmpty: %s'; 14 | 15 | type 16 | THashBuffer = record 17 | 18 | private 19 | var 20 | FData: THashLibByteArray; 21 | FPosition: Int32; 22 | 23 | function GetIsEmpty: Boolean; inline; 24 | function GetIsFull: Boolean; inline; 25 | function GetPosition: Int32; inline; 26 | function GetLength: Int32; inline; 27 | 28 | public 29 | constructor Create(ALength: Int32); 30 | 31 | procedure Initialize(); 32 | function GetBytes(): THashLibByteArray; inline; 33 | function GetBytesZeroPadded(): THashLibByteArray; inline; 34 | 35 | function Feed(AData: PByte; ADataLength: Int32; var AStartIndex: Int32; 36 | var ALength: Int32; var AProcessedBytesCount: UInt64): Boolean; overload; 37 | 38 | function Feed(AData: PByte; ADataLength: Int32; ALength: Int32) 39 | : Boolean; overload; 40 | 41 | function ToString(): String; 42 | function Clone(): THashBuffer; inline; 43 | 44 | property IsEmpty: Boolean read GetIsEmpty; 45 | property IsFull: Boolean read GetIsFull; 46 | property Position: Int32 read GetPosition; 47 | property Length: Int32 read GetLength; 48 | end; 49 | 50 | implementation 51 | 52 | { THashBuffer } 53 | 54 | function THashBuffer.Clone(): THashBuffer; 55 | begin 56 | result := Default (THashBuffer); 57 | result.FData := System.Copy(FData); 58 | result.FPosition := FPosition; 59 | end; 60 | 61 | constructor THashBuffer.Create(ALength: Int32); 62 | begin 63 | {$IFDEF DEBUG} 64 | System.Assert(ALength > 0); 65 | {$ENDIF DEBUG} 66 | System.SetLength(FData, ALength); 67 | Initialize(); 68 | end; 69 | 70 | function THashBuffer.GetIsFull: Boolean; 71 | begin 72 | result := FPosition = System.Length(FData); 73 | end; 74 | 75 | function THashBuffer.Feed(AData: PByte; ADataLength: Int32; 76 | ALength: Int32): Boolean; 77 | var 78 | LLength: Int32; 79 | begin 80 | {$IFDEF DEBUG} 81 | System.Assert(ALength >= 0); 82 | System.Assert(ALength <= ADataLength); 83 | System.Assert(not IsFull); 84 | {$ENDIF DEBUG} 85 | if (ADataLength = 0) then 86 | begin 87 | result := false; 88 | Exit; 89 | end; 90 | 91 | if (ALength = 0) then 92 | begin 93 | result := false; 94 | Exit; 95 | end; 96 | LLength := System.Length(FData) - FPosition; 97 | if (LLength > ALength) then 98 | begin 99 | LLength := ALength; 100 | end; 101 | 102 | System.Move(AData[0], FData[FPosition], LLength * System.SizeOf(Byte)); 103 | 104 | FPosition := FPosition + LLength; 105 | 106 | result := IsFull; 107 | end; 108 | 109 | function THashBuffer.Feed(AData: PByte; ADataLength: Int32; 110 | var AStartIndex, ALength: Int32; var AProcessedBytesCount: UInt64): Boolean; 111 | var 112 | LLength: Int32; 113 | begin 114 | {$IFDEF DEBUG} 115 | System.Assert(AStartIndex >= 0); 116 | System.Assert(ALength >= 0); 117 | System.Assert((AStartIndex + ALength) <= ADataLength); 118 | System.Assert(not IsFull); 119 | {$ENDIF DEBUG} 120 | if (ADataLength = 0) then 121 | begin 122 | result := false; 123 | Exit; 124 | end; 125 | 126 | if (ALength = 0) then 127 | begin 128 | result := false; 129 | Exit; 130 | end; 131 | 132 | LLength := System.Length(FData) - FPosition; 133 | if (LLength > ALength) then 134 | begin 135 | LLength := ALength; 136 | end; 137 | 138 | System.Move(AData[AStartIndex], FData[FPosition], 139 | LLength * System.SizeOf(Byte)); 140 | 141 | FPosition := FPosition + LLength; 142 | AStartIndex := AStartIndex + LLength; 143 | ALength := ALength - LLength; 144 | AProcessedBytesCount := AProcessedBytesCount + UInt64(LLength); 145 | 146 | result := IsFull; 147 | end; 148 | 149 | function THashBuffer.GetBytes: THashLibByteArray; 150 | begin 151 | {$IFDEF DEBUG} 152 | System.Assert(IsFull); 153 | {$ENDIF DEBUG} 154 | FPosition := 0; 155 | result := FData; 156 | end; 157 | 158 | function THashBuffer.GetBytesZeroPadded: THashLibByteArray; 159 | begin 160 | TArrayUtils.Fill(FData, FPosition, (System.Length(FData) - FPosition) + 161 | FPosition, Byte(0)); 162 | FPosition := 0; 163 | result := FData; 164 | end; 165 | 166 | function THashBuffer.GetIsEmpty: Boolean; 167 | begin 168 | result := FPosition = 0; 169 | end; 170 | 171 | function THashBuffer.GetLength: Int32; 172 | begin 173 | result := System.Length(FData); 174 | end; 175 | 176 | function THashBuffer.GetPosition: Int32; 177 | begin 178 | result := FPosition; 179 | end; 180 | 181 | procedure THashBuffer.Initialize; 182 | begin 183 | FPosition := 0; 184 | TArrayUtils.ZeroFill(FData); 185 | end; 186 | 187 | function THashBuffer.ToString: String; 188 | begin 189 | result := Format(SHashBufferMessage, [Self.Length, Self.Position, 190 | BoolToStr(Self.IsEmpty, True)]); 191 | end; 192 | 193 | end. 194 | -------------------------------------------------------------------------------- /HashLib.Benchmark/src/Core/uPerformanceBenchmark.pas: -------------------------------------------------------------------------------- 1 | unit uPerformanceBenchmark; 2 | 3 | {$IFDEF FPC} 4 | {$MODE DELPHI} 5 | {$WARNINGS OFF} 6 | {$ENDIF FPC} 7 | {$ZEROBASEDSTRINGS OFF} 8 | 9 | interface 10 | 11 | uses 12 | Classes, 13 | SysUtils, 14 | Math, 15 | HlpCRC, 16 | HlpIHash, 17 | HlpHashFactory; 18 | 19 | type 20 | TPerformanceBenchmark = class sealed(TObject) 21 | strict private 22 | 23 | class function Calculate(const AHashInstance: IHash; 24 | const ANamePrefix: String = ''; ASize: Int32 = 64 * 1024): String; 25 | 26 | class constructor PerformanceBenchmark(); 27 | 28 | public 29 | class procedure DoBenchmark(var AStringList: TStringList); 30 | 31 | end; 32 | 33 | implementation 34 | 35 | { TPerformanceBenchmark } 36 | 37 | class function TPerformanceBenchmark.Calculate(const AHashInstance: IHash; 38 | const ANamePrefix: String; ASize: Int32): String; 39 | const 40 | THREE_SECONDS_IN_MILLISECONDS = UInt32(3000); 41 | var 42 | MaxRate: Double; 43 | Data: TBytes; 44 | Idx: Int32; 45 | Total: Int64; 46 | A, B, TotalMilliSeconds: UInt32; 47 | NewName, BlockSizeAndUnit: String; 48 | begin 49 | 50 | System.SetLength(Data, ASize); 51 | 52 | for Idx := System.Low(Data) to System.High(Data) do 53 | begin 54 | Data[Idx] := Byte(Random(ASize)); 55 | end; 56 | 57 | MaxRate := 0.0; 58 | TotalMilliSeconds := 0; 59 | 60 | Idx := 3; 61 | while Idx > 0 do 62 | begin 63 | Total := 0; 64 | 65 | while (TotalMilliSeconds <= THREE_SECONDS_IN_MILLISECONDS) do 66 | begin 67 | A := TThread.GetTickCount; 68 | AHashInstance.ComputeBytes(Data); 69 | B := TThread.GetTickCount; 70 | Total := Total + System.Length(Data); 71 | TotalMilliSeconds := TotalMilliSeconds + (B - A); 72 | end; 73 | 74 | MaxRate := Math.Max(Total / (TotalMilliSeconds div 1000) / 1024 / 75 | 1024, MaxRate); 76 | 77 | System.Dec(Idx); 78 | end; 79 | 80 | if ANamePrefix <> '' then 81 | begin 82 | NewName := Format('%s_%s', [AHashInstance.Name, ANamePrefix]); 83 | end 84 | else 85 | begin 86 | NewName := AHashInstance.Name; 87 | end; 88 | 89 | if ASize >= 1024 * 1024 * 1024 then 90 | begin 91 | BlockSizeAndUnit := Format('%d GB', [(ASize div (1024 * 1024 * 1024))]); 92 | end 93 | else if ASize >= 1024 * 1024 then 94 | begin 95 | BlockSizeAndUnit := Format('%d MB', [(ASize div (1024 * 1024))]); 96 | end 97 | else 98 | begin 99 | BlockSizeAndUnit := Format('%d KB', [(ASize div 1024)]); 100 | end; 101 | 102 | Result := Format('%s Throughput: %.2f MB/s with Blocks of %s', 103 | [Copy(NewName, 2, System.Length(NewName) - 1), MaxRate, BlockSizeAndUnit]); 104 | end; 105 | 106 | class procedure TPerformanceBenchmark.DoBenchmark(var AStringList: TStringList); 107 | begin 108 | if not Assigned(AStringList) then 109 | begin 110 | raise Exception.Create('StringList Instance cannot be Nil'); 111 | end; 112 | 113 | AStringList.Clear; 114 | 115 | AStringList.Append(Calculate(THashFactory.TChecksum.CreateAdler32)); 116 | 117 | AStringList.Append(Calculate(THashFactory.TChecksum.TCRC.CreateCRC 118 | (TCRCStandard.CRC32), 'PKZIP_Generic')); 119 | 120 | AStringList.Append 121 | (Calculate(THashFactory.TChecksum.TCRC.CreateCRC32_PKZIP, 'Fast')); 122 | 123 | AStringList.Append(Calculate(THashFactory.THash32.CreateMurmurHash3_x86_32)); 124 | 125 | AStringList.Append(Calculate(THashFactory.THash32.CreateXXHash32)); 126 | 127 | AStringList.Append(Calculate(THashFactory.THash64.CreateSipHash2_4)); 128 | 129 | AStringList.Append(Calculate(THashFactory.THash64.CreateXXHash64)); 130 | 131 | AStringList.Append 132 | (Calculate(THashFactory.THash128.CreateMurmurHash3_x86_128)); 133 | 134 | AStringList.Append 135 | (Calculate(THashFactory.THash128.CreateMurmurHash3_x64_128)); 136 | 137 | AStringList.Append(Calculate(THashFactory.TCrypto.CreateMD5)); 138 | 139 | AStringList.Append(Calculate(THashFactory.TCrypto.CreateSHA1)); 140 | 141 | AStringList.Append(Calculate(THashFactory.TCrypto.CreateSHA2_256)); 142 | 143 | AStringList.Append(Calculate(THashFactory.TCrypto.CreateSHA2_512)); 144 | 145 | AStringList.Append(Calculate(THashFactory.TCrypto.CreateSHA3_256)); 146 | 147 | AStringList.Append(Calculate(THashFactory.TCrypto.CreateSHA3_512)); 148 | 149 | AStringList.Append(Calculate(THashFactory.TCrypto.CreateBlake2B_256)); 150 | 151 | AStringList.Append(Calculate(THashFactory.TCrypto.CreateBlake2B_512)); 152 | 153 | AStringList.Append(Calculate(THashFactory.TCrypto.CreateBlake2S_128)); 154 | 155 | AStringList.Append(Calculate(THashFactory.TCrypto.CreateBlake2S_256)); 156 | 157 | AStringList.Append(Calculate(THashFactory.TCrypto.CreateBlake2BP(64, Nil))); 158 | 159 | AStringList.Append(Calculate(THashFactory.TCrypto.CreateBlake2SP(32, Nil))); 160 | 161 | AStringList.Append(Calculate(THashFactory.TCrypto.CreateBlake3_256(Nil))); 162 | 163 | end; 164 | 165 | class constructor TPerformanceBenchmark.PerformanceBenchmark; 166 | begin 167 | Randomize(); 168 | end; 169 | 170 | end. 171 | -------------------------------------------------------------------------------- /HashLib/src/Crypto/HlpMD2.pas: -------------------------------------------------------------------------------- 1 | unit HlpMD2; 2 | 3 | {$I ..\Include\HashLib.inc} 4 | 5 | interface 6 | 7 | uses 8 | HlpHashLibTypes, 9 | {$IFDEF DELPHI} 10 | HlpHashBuffer, 11 | HlpHash, 12 | {$ENDIF DELPHI} 13 | HlpIHash, 14 | HlpIHashInfo, 15 | HlpArrayUtils, 16 | HlpHashCryptoNotBuildIn; 17 | 18 | type 19 | TMD2 = class sealed(TBlockHash, ICryptoNotBuildIn, ITransformBlock) 20 | 21 | strict private 22 | var 23 | FState, FChecksum: THashLibByteArray; 24 | 25 | {$REGION 'Consts'} 26 | 27 | const 28 | SPi: array [0 .. 255] of Byte = (41, 46, 67, 201, 162, 216, 124, 1, 61, 54, 29 | 84, 161, 236, 240, 6, 19, 30 | 31 | 98, 167, 5, 243, 192, 199, 115, 140, 152, 147, 43, 217, 188, 76, 130, 202, 32 | 33 | 30, 155, 87, 60, 253, 212, 224, 22, 103, 66, 111, 24, 138, 23, 229, 18, 34 | 35 | 190, 78, 196, 214, 218, 158, 222, 73, 160, 251, 245, 142, 187, 36 | 47, 238, 122, 37 | 38 | 169, 104, 121, 145, 21, 178, 7, 63, 148, 194, 16, 137, 11, 34, 95, 33, 39 | 40 | 128, 127, 93, 154, 90, 144, 50, 39, 53, 62, 204, 231, 191, 247, 151, 3, 41 | 42 | 255, 25, 48, 179, 72, 165, 181, 209, 215, 94, 146, 42, 172, 86, 170, 198, 43 | 44 | 79, 184, 56, 210, 150, 164, 125, 182, 118, 252, 107, 226, 156, 45 | 116, 4, 241, 46 | 47 | 69, 157, 112, 89, 100, 113, 135, 32, 134, 91, 207, 101, 230, 45, 168, 2, 48 | 49 | 27, 96, 37, 173, 174, 176, 185, 246, 28, 70, 97, 105, 52, 64, 126, 15, 50 | 51 | 85, 71, 163, 35, 221, 81, 175, 58, 195, 92, 249, 206, 186, 197, 234, 38, 52 | 53 | 44, 83, 13, 110, 133, 40, 132, 9, 211, 223, 205, 244, 65, 129, 77, 82, 54 | 55 | 106, 220, 55, 200, 108, 193, 171, 250, 36, 225, 123, 8, 12, 189, 177, 74, 56 | 57 | 120, 136, 149, 139, 227, 99, 232, 109, 233, 203, 213, 254, 59, 0, 29, 57, 58 | 59 | 242, 239, 183, 14, 102, 88, 208, 228, 166, 119, 114, 248, 235, 60 | 117, 75, 10, 61 | 62 | 49, 68, 80, 180, 143, 237, 31, 26, 219, 153, 141, 51, 159, 17, 131, 20); 63 | 64 | {$ENDREGION} 65 | strict protected 66 | procedure Finish(); override; 67 | function GetResult(): THashLibByteArray; override; 68 | procedure TransformBlock(AData: PByte; ADataLength: Int32; 69 | AIndex: Int32); override; 70 | 71 | public 72 | constructor Create(); 73 | procedure Initialize(); override; 74 | function Clone(): IHash; override; 75 | 76 | end; 77 | 78 | implementation 79 | 80 | { TMD2 } 81 | 82 | function TMD2.Clone(): IHash; 83 | var 84 | LHashInstance: TMD2; 85 | begin 86 | LHashInstance := TMD2.Create(); 87 | LHashInstance.FState := System.Copy(FState); 88 | LHashInstance.FChecksum := System.Copy(FChecksum); 89 | LHashInstance.FBuffer := FBuffer.Clone(); 90 | LHashInstance.FProcessedBytesCount := FProcessedBytesCount; 91 | result := LHashInstance as IHash; 92 | result.BufferSize := BufferSize; 93 | end; 94 | 95 | constructor TMD2.Create; 96 | begin 97 | Inherited Create(16, 16); 98 | System.SetLength(FState, 16); 99 | System.SetLength(FChecksum, 16); 100 | end; 101 | 102 | procedure TMD2.Finish; 103 | var 104 | LPadLength, LIdx: Int32; 105 | LPad: THashLibByteArray; 106 | begin 107 | LPadLength := 16 - FBuffer.Position; 108 | System.SetLength(LPad, LPadLength); 109 | 110 | LIdx := 0; 111 | while LIdx < LPadLength do 112 | begin 113 | LPad[LIdx] := Byte(LPadLength); 114 | System.Inc(LIdx); 115 | end; 116 | 117 | TransformBytes(LPad, 0, LPadLength); 118 | TransformBytes(FChecksum, 0, 16); 119 | end; 120 | 121 | function TMD2.GetResult: THashLibByteArray; 122 | begin 123 | result := System.Copy(FState); 124 | end; 125 | 126 | procedure TMD2.Initialize; 127 | begin 128 | TArrayUtils.ZeroFill(FState); 129 | TArrayUtils.ZeroFill(FChecksum); 130 | Inherited Initialize(); 131 | end; 132 | 133 | procedure TMD2.TransformBlock(AData: PByte; ADataLength: Int32; AIndex: Int32); 134 | var 135 | LIdx, LJdx: Int32; 136 | LT: UInt32; 137 | LTemp: array [0 .. 47] of Byte; 138 | begin 139 | System.Move(FState[0], LTemp[0], ADataLength); 140 | 141 | System.Move(AData[AIndex], LTemp[ADataLength], ADataLength); 142 | 143 | for LIdx := 0 to 15 do 144 | begin 145 | LTemp[LIdx + 32] := Byte(FState[LIdx] xor AData[LIdx + AIndex]); 146 | end; 147 | 148 | LT := 0; 149 | 150 | for LIdx := 0 to 17 do 151 | begin 152 | 153 | for LJdx := 0 to 47 do 154 | begin 155 | LTemp[LJdx] := Byte(LTemp[LJdx] xor SPi[LT]); 156 | LT := LTemp[LJdx]; 157 | end; 158 | 159 | LT := Byte(LT + UInt32(LIdx)); 160 | end; 161 | 162 | System.Move(LTemp[0], FState[0], 16); 163 | 164 | LT := FChecksum[15]; 165 | 166 | for LIdx := 0 to 15 do 167 | begin 168 | FChecksum[LIdx] := FChecksum[LIdx] xor (SPi[AData[LIdx + AIndex] xor LT]); 169 | LT := FChecksum[LIdx]; 170 | end; 171 | 172 | System.FillChar(LTemp, System.SizeOf(LTemp), Byte(0)); 173 | end; 174 | 175 | end. 176 | -------------------------------------------------------------------------------- /HashLib/src/Crypto/HlpRadioGatun64.pas: -------------------------------------------------------------------------------- 1 | unit HlpRadioGatun64; 2 | 3 | {$I ..\Include\HashLib.inc} 4 | 5 | interface 6 | 7 | uses 8 | HlpHashLibTypes, 9 | {$IFDEF DELPHI} 10 | HlpHashBuffer, 11 | HlpHash, 12 | {$ENDIF DELPHI} 13 | HlpBits, 14 | HlpConverters, 15 | HlpIHash, 16 | HlpIHashInfo, 17 | HlpArrayUtils, 18 | HlpHashCryptoNotBuildIn; 19 | 20 | type 21 | TRadioGatun64 = class sealed(TBlockHash, ICryptoNotBuildIn, ITransformBlock) 22 | 23 | strict private 24 | var 25 | FMill: THashLibUInt64Array; 26 | FBelt: THashLibMatrixUInt64Array; 27 | 28 | procedure RoundFunction(); 29 | 30 | strict protected 31 | procedure Finish(); override; 32 | function GetResult(): THashLibByteArray; override; 33 | procedure TransformBlock(AData: PByte; ADataLength: Int32; 34 | AIndex: Int32); override; 35 | 36 | public 37 | constructor Create(); 38 | procedure Initialize(); override; 39 | function Clone(): IHash; override; 40 | 41 | end; 42 | 43 | implementation 44 | 45 | { TRadioGatun64 } 46 | 47 | function TRadioGatun64.Clone(): IHash; 48 | var 49 | LHashInstance: TRadioGatun64; 50 | begin 51 | LHashInstance := TRadioGatun64.Create(); 52 | LHashInstance.FMill := System.Copy(FMill); 53 | LHashInstance.FBelt := TArrayUtils.Clone(FBelt); 54 | LHashInstance.FBuffer := FBuffer.Clone(); 55 | LHashInstance.FProcessedBytesCount := FProcessedBytesCount; 56 | result := LHashInstance as IHash; 57 | result.BufferSize := BufferSize; 58 | end; 59 | 60 | constructor TRadioGatun64.Create; 61 | var 62 | LIdx: Int32; 63 | begin 64 | Inherited Create(32, 24); 65 | System.SetLength(FMill, 19); 66 | 67 | System.SetLength(FBelt, 13); 68 | LIdx := 0; 69 | while LIdx < 13 do 70 | begin 71 | System.SetLength(FBelt[LIdx], 3); 72 | System.Inc(LIdx); 73 | end; 74 | end; 75 | 76 | procedure TRadioGatun64.Finish; 77 | var 78 | LPaddingSize, LIdx: Int32; 79 | LPad: THashLibByteArray; 80 | begin 81 | LPaddingSize := 24 - ((Int32(FProcessedBytesCount)) mod 24); 82 | 83 | System.SetLength(LPad, LPaddingSize); 84 | LPad[0] := $01; 85 | TransformBytes(LPad, 0, LPaddingSize); 86 | LIdx := 0; 87 | while LIdx < 16 do 88 | begin 89 | RoundFunction(); 90 | System.Inc(LIdx); 91 | end; 92 | end; 93 | 94 | function TRadioGatun64.GetResult: THashLibByteArray; 95 | var 96 | LBuffer: THashLibUInt64Array; 97 | LIdx: Int32; 98 | begin 99 | System.SetLength(LBuffer, 4); 100 | 101 | System.SetLength(result, System.Length(LBuffer) * System.SizeOf(UInt64)); 102 | LIdx := 0; 103 | 104 | while LIdx < 2 do 105 | begin 106 | RoundFunction(); 107 | 108 | System.Move(FMill[1], LBuffer[LIdx * 2], 2 * System.SizeOf(UInt64)); 109 | System.Inc(LIdx); 110 | end; 111 | 112 | TConverters.le64_copy(PCardinal(LBuffer), 0, PByte(result), 0, 113 | System.Length(result)); 114 | end; 115 | 116 | procedure TRadioGatun64.Initialize; 117 | begin 118 | TArrayUtils.ZeroFill(FMill); 119 | TArrayUtils.ZeroFill(FBelt); 120 | Inherited Initialize(); 121 | end; 122 | 123 | procedure TRadioGatun64.RoundFunction; 124 | var 125 | LQ: THashLibUInt64Array; 126 | LA: array [0 .. 18] of UInt64; 127 | LIdx: Int32; 128 | begin 129 | LQ := FBelt[12]; 130 | LIdx := 12; 131 | while LIdx > 0 do 132 | begin 133 | FBelt[LIdx] := FBelt[LIdx - 1]; 134 | System.Dec(LIdx); 135 | end; 136 | 137 | FBelt[0] := LQ; 138 | 139 | LIdx := 0; 140 | while LIdx < 12 do 141 | begin 142 | FBelt[LIdx + 1][LIdx mod 3] := FBelt[LIdx + 1][LIdx mod 3] xor FMill 143 | [LIdx + 1]; 144 | System.Inc(LIdx); 145 | end; 146 | 147 | LIdx := 0; 148 | while LIdx < 19 do 149 | begin 150 | LA[LIdx] := FMill[LIdx] xor (FMill[(LIdx + 1) mod 19] or 151 | not FMill[(LIdx + 2) mod 19]); 152 | System.Inc(LIdx); 153 | end; 154 | 155 | LIdx := 0; 156 | while LIdx < 19 do 157 | begin 158 | FMill[LIdx] := TBits.RotateRight64(LA[(7 * LIdx) mod 19], 159 | (LIdx * (LIdx + 1)) shr 1); 160 | System.Inc(LIdx); 161 | end; 162 | 163 | LIdx := 0; 164 | while LIdx < 19 do 165 | begin 166 | LA[LIdx] := FMill[LIdx] xor FMill[(LIdx + 1) mod 19] xor FMill 167 | [(LIdx + 4) mod 19]; 168 | System.Inc(LIdx); 169 | end; 170 | 171 | LA[0] := LA[0] xor 1; 172 | 173 | LIdx := 0; 174 | while LIdx < 19 do 175 | begin 176 | FMill[LIdx] := LA[LIdx]; 177 | System.Inc(LIdx); 178 | end; 179 | 180 | LIdx := 0; 181 | while LIdx < 3 do 182 | begin 183 | FMill[LIdx + 13] := FMill[LIdx + 13] xor LQ[LIdx]; 184 | System.Inc(LIdx); 185 | end; 186 | end; 187 | 188 | procedure TRadioGatun64.TransformBlock(AData: PByte; ADataLength: Int32; 189 | AIndex: Int32); 190 | var 191 | LData: array [0 .. 2] of UInt64; 192 | LIdx: Int32; 193 | begin 194 | TConverters.le64_copy(AData, AIndex, @(LData[0]), 0, ADataLength); 195 | LIdx := 0; 196 | while LIdx < 3 do 197 | begin 198 | FMill[LIdx + 16] := FMill[LIdx + 16] xor LData[LIdx]; 199 | FBelt[0][LIdx] := FBelt[0][LIdx] xor LData[LIdx]; 200 | System.Inc(LIdx); 201 | end; 202 | 203 | RoundFunction(); 204 | 205 | System.FillChar(LData, System.SizeOf(LData), UInt64(0)); 206 | end; 207 | 208 | end. 209 | -------------------------------------------------------------------------------- /HashLib/src/Crypto/HlpRadioGatun32.pas: -------------------------------------------------------------------------------- 1 | unit HlpRadioGatun32; 2 | 3 | {$I ..\Include\HashLib.inc} 4 | 5 | interface 6 | 7 | uses 8 | HlpHashLibTypes, 9 | {$IFDEF DELPHI} 10 | HlpHashBuffer, 11 | HlpHash, 12 | {$ENDIF DELPHI} 13 | HlpBits, 14 | HlpConverters, 15 | HlpIHash, 16 | HlpIHashInfo, 17 | HlpArrayUtils, 18 | HlpHashCryptoNotBuildIn; 19 | 20 | type 21 | TRadioGatun32 = class sealed(TBlockHash, ICryptoNotBuildIn, ITransformBlock) 22 | 23 | strict private 24 | var 25 | FMill: THashLibUInt32Array; 26 | FBelt: THashLibMatrixUInt32Array; 27 | 28 | procedure RoundFunction(); 29 | 30 | strict protected 31 | procedure Finish(); override; 32 | function GetResult(): THashLibByteArray; override; 33 | procedure TransformBlock(AData: PByte; ADataLength: Int32; 34 | AIndex: Int32); override; 35 | 36 | public 37 | constructor Create(); 38 | procedure Initialize(); override; 39 | function Clone(): IHash; override; 40 | 41 | end; 42 | 43 | implementation 44 | 45 | { TRadioGatun32 } 46 | 47 | function TRadioGatun32.Clone(): IHash; 48 | var 49 | LHashInstance: TRadioGatun32; 50 | begin 51 | LHashInstance := TRadioGatun32.Create(); 52 | LHashInstance.FMill := System.Copy(FMill); 53 | LHashInstance.FBelt := TArrayUtils.Clone(FBelt); 54 | LHashInstance.FBuffer := FBuffer.Clone(); 55 | LHashInstance.FProcessedBytesCount := FProcessedBytesCount; 56 | result := LHashInstance as IHash; 57 | result.BufferSize := BufferSize; 58 | end; 59 | 60 | constructor TRadioGatun32.Create; 61 | var 62 | LIdx: Int32; 63 | begin 64 | Inherited Create(32, 12); 65 | System.SetLength(FMill, 19); 66 | 67 | System.SetLength(FBelt, 13); 68 | LIdx := 0; 69 | while LIdx < 13 do 70 | begin 71 | System.SetLength(FBelt[LIdx], 3); 72 | 73 | System.Inc(LIdx); 74 | end; 75 | end; 76 | 77 | procedure TRadioGatun32.Finish; 78 | var 79 | LPaddingSize, LIdx: Int32; 80 | LPad: THashLibByteArray; 81 | begin 82 | LPaddingSize := 12 - ((Int32(FProcessedBytesCount)) mod 12); 83 | 84 | System.SetLength(LPad, LPaddingSize); 85 | LPad[0] := $01; 86 | TransformBytes(LPad, 0, LPaddingSize); 87 | LIdx := 0; 88 | while LIdx < 16 do 89 | begin 90 | RoundFunction(); 91 | System.Inc(LIdx); 92 | end; 93 | end; 94 | 95 | function TRadioGatun32.GetResult: THashLibByteArray; 96 | var 97 | LBuffer: THashLibUInt32Array; 98 | LIdx: Int32; 99 | begin 100 | System.SetLength(LBuffer, 8); 101 | 102 | System.SetLength(result, System.Length(LBuffer) * System.SizeOf(UInt32)); 103 | 104 | LIdx := 0; 105 | 106 | while LIdx < 4 do 107 | begin 108 | RoundFunction(); 109 | 110 | System.Move(FMill[1], LBuffer[LIdx * 2], 2 * System.SizeOf(UInt32)); 111 | System.Inc(LIdx); 112 | end; 113 | 114 | TConverters.le32_copy(PCardinal(LBuffer), 0, PByte(result), 0, 115 | System.Length(result)); 116 | end; 117 | 118 | procedure TRadioGatun32.Initialize; 119 | begin 120 | TArrayUtils.ZeroFill(FMill); 121 | TArrayUtils.ZeroFill(FBelt); 122 | Inherited Initialize(); 123 | end; 124 | 125 | procedure TRadioGatun32.RoundFunction; 126 | var 127 | LQ: THashLibUInt32Array; 128 | LA: array [0 .. 18] of UInt32; 129 | LIdx: Int32; 130 | begin 131 | LQ := FBelt[12]; 132 | LIdx := 12; 133 | while LIdx > 0 do 134 | begin 135 | FBelt[LIdx] := FBelt[LIdx - 1]; 136 | System.Dec(LIdx); 137 | end; 138 | 139 | FBelt[0] := LQ; 140 | 141 | LIdx := 0; 142 | while LIdx < 12 do 143 | begin 144 | FBelt[LIdx + 1][LIdx mod 3] := FBelt[LIdx + 1][LIdx mod 3] xor FMill 145 | [LIdx + 1]; 146 | System.Inc(LIdx); 147 | end; 148 | 149 | LIdx := 0; 150 | while LIdx < 19 do 151 | begin 152 | LA[LIdx] := FMill[LIdx] xor (FMill[(LIdx + 1) mod 19] or 153 | not FMill[(LIdx + 2) mod 19]); 154 | System.Inc(LIdx); 155 | end; 156 | 157 | LIdx := 0; 158 | while LIdx < 19 do 159 | begin 160 | FMill[LIdx] := TBits.RotateRight32(LA[(7 * LIdx) mod 19], 161 | (LIdx * (LIdx + 1)) shr 1); 162 | System.Inc(LIdx); 163 | end; 164 | 165 | LIdx := 0; 166 | while LIdx < 19 do 167 | begin 168 | LA[LIdx] := FMill[LIdx] xor FMill[(LIdx + 1) mod 19] xor FMill 169 | [(LIdx + 4) mod 19]; 170 | System.Inc(LIdx); 171 | end; 172 | 173 | LA[0] := LA[0] xor 1; 174 | 175 | LIdx := 0; 176 | while LIdx < 19 do 177 | begin 178 | FMill[LIdx] := LA[LIdx]; 179 | System.Inc(LIdx); 180 | end; 181 | 182 | LIdx := 0; 183 | while LIdx < 3 do 184 | begin 185 | FMill[LIdx + 13] := FMill[LIdx + 13] xor LQ[LIdx]; 186 | System.Inc(LIdx); 187 | end; 188 | 189 | end; 190 | 191 | procedure TRadioGatun32.TransformBlock(AData: PByte; ADataLength: Int32; 192 | AIndex: Int32); 193 | var 194 | LData: array [0 .. 2] of UInt32; 195 | LIdx: Int32; 196 | begin 197 | TConverters.le32_copy(AData, AIndex, @(LData[0]), 0, ADataLength); 198 | 199 | LIdx := 0; 200 | while LIdx < 3 do 201 | begin 202 | FMill[LIdx + 16] := FMill[LIdx + 16] xor LData[LIdx]; 203 | FBelt[0][LIdx] := FBelt[0][LIdx] xor LData[LIdx]; 204 | System.Inc(LIdx); 205 | end; 206 | 207 | RoundFunction(); 208 | 209 | System.FillChar(LData, System.SizeOf(LData), UInt32(0)); 210 | end; 211 | 212 | end. 213 | -------------------------------------------------------------------------------- /.github/workflows/make.pas: -------------------------------------------------------------------------------- 1 | program Make; 2 | {$mode objfpc}{$H+} 3 | 4 | uses 5 | Classes, 6 | SysUtils, 7 | StrUtils, 8 | FileUtil, 9 | Zipper, 10 | fphttpclient, 11 | RegExpr, 12 | openssl, 13 | opensslsockets, 14 | Process; 15 | 16 | const 17 | Target: string = '.'; 18 | Dependencies: array of string = (); 19 | 20 | type 21 | Output = record 22 | Code: boolean; 23 | Output: ansistring; 24 | end; 25 | 26 | function CheckModules: Output; 27 | begin 28 | if FileExists('.gitmodules') then 29 | if RunCommand('git', ['submodule', 'update', '--init', '--recursive', 30 | '--force', '--remote'], Result.Output) then 31 | Writeln(stderr, #27'[33m', Result.Output, #27'[0m'); 32 | end; 33 | 34 | function AddPackage(Path: string): Output; 35 | begin 36 | with TRegExpr.Create do 37 | begin 38 | Expression := 39 | {$IFDEF MSWINDOWS} 40 | '(cocoa|x11|_template)' 41 | {$ELSE} 42 | '(cocoa|gdi|_template)' 43 | {$ENDIF} 44 | ; 45 | if not Exec(Path) and RunCommand('lazbuild', ['--add-package-link', Path], 46 | Result.Output) then 47 | Writeln(stderr, #27'[33m', 'added ', Path, #27'[0m'); 48 | Free; 49 | end; 50 | end; 51 | 52 | function BuildProject(Path: string): Output; 53 | var 54 | Line: string; 55 | begin 56 | Write(stderr, #27'[33m', 'build from ', Path, #27'[0m'); 57 | try 58 | Result.Code := RunCommand('lazbuild', ['--build-all', '--recursive', 59 | '--no-write-project', Path], Result.Output); 60 | if Result.Code then 61 | for Line in SplitString(Result.Output, LineEnding) do 62 | begin 63 | if ContainsStr(Line, 'Linking') then 64 | begin 65 | Result.Output := SplitString(Line, ' ')[2]; 66 | Writeln(stderr, #27'[32m', ' to ', Result.Output, #27'[0m'); 67 | break; 68 | end; 69 | end 70 | else 71 | begin 72 | ExitCode += 1; 73 | for Line in SplitString(Result.Output, LineEnding) do 74 | with TRegExpr.Create do 75 | begin 76 | Expression := '(Fatal|Error):'; 77 | if Exec(Line) then 78 | begin 79 | WriteLn(stderr); 80 | Writeln(stderr, #27'[31m', Line, #27'[0m'); 81 | end; 82 | Free; 83 | end; 84 | end; 85 | except 86 | on E: Exception do 87 | WriteLn(stderr, 'Error: ' + E.ClassName + #13#10 + E.Message); 88 | end; 89 | end; 90 | 91 | function RunTest(Path: string): Output; 92 | var 93 | Temp: string; 94 | begin 95 | Result := BuildProject(Path); 96 | Temp:= Result.Output; 97 | if Result.Code then 98 | try 99 | if not RunCommand(Temp, ['--all', '--format=plain', '--progress'], Result.Output) then 100 | ExitCode += 1; 101 | WriteLn(stderr, Result.Output); 102 | except 103 | on E: Exception do 104 | WriteLn(stderr, 'Error: ' + E.ClassName + #13#10 + E.Message); 105 | end; 106 | end; 107 | 108 | function AddOPM(Each: string): string; 109 | var 110 | TempFile, Url: string; 111 | Zip: TStream; 112 | begin 113 | Result := 114 | {$IFDEF MSWINDOWS} 115 | GetEnvironmentVariable('APPDATA') + '\.lazarus\onlinepackagemanager\packages\' 116 | {$ELSE} 117 | GetEnvironmentVariable('HOME') + '/.lazarus/onlinepackagemanager/packages/' 118 | {$ENDIF} 119 | + Each; 120 | TempFile := GetTempFileName; 121 | Url := 'https://packages.lazarus-ide.org/' + Each + '.zip'; 122 | if not DirectoryExists(Result) then 123 | begin 124 | Zip := TFileStream.Create(TempFile, fmCreate or fmOpenWrite); 125 | with TFPHttpClient.Create(nil) do 126 | begin 127 | try 128 | AddHeader('User-Agent', 'Mozilla/5.0 (compatible; fpweb)'); 129 | AllowRedirect := True; 130 | Get(Url, Zip); 131 | WriteLn(stderr, 'Download from ', Url, ' to ', TempFile); 132 | finally 133 | Free; 134 | end; 135 | end; 136 | Zip.Free; 137 | CreateDir(Result); 138 | with TUnZipper.Create do 139 | begin 140 | try 141 | FileName := TempFile; 142 | OutputPath := Result; 143 | Examine; 144 | UnZipAllFiles; 145 | WriteLn(stderr, 'Unzip from ', TempFile, ' to ', Result); 146 | finally 147 | Free; 148 | end; 149 | end; 150 | DeleteFile(TempFile); 151 | end; 152 | end; 153 | 154 | function Main: Output; 155 | var 156 | Each, Item: string; 157 | List: TStringList; 158 | begin 159 | CheckModules; 160 | InitSSLInterface; 161 | for Each in Dependencies do 162 | begin 163 | List := FindAllFiles(AddOPM(Each), '*.lpk', True); 164 | try 165 | for Item in List do 166 | AddPackage(Item); 167 | finally 168 | List.Free; 169 | end; 170 | end; 171 | List := FindAllFiles(GetCurrentDir, '*.lpk', True); 172 | try 173 | for Each in List do 174 | AddPackage(Each); 175 | finally 176 | List.Free; 177 | end; 178 | List := FindAllFiles(Target, '*.lpi', True); 179 | try 180 | for Each in List do 181 | if ContainsStr(ReadFileToString(ReplaceStr(Each, '.lpi', '.lpr')), 182 | 'consoletestrunner') then 183 | RunTest(Each) 184 | else 185 | BuildProject(Each); 186 | finally 187 | List.Free; 188 | end; 189 | WriteLn(stderr); 190 | if ExitCode <> 0 then 191 | WriteLn(stderr, #27'[31m', 'Errors: ', ExitCode, #27'[0m') 192 | else 193 | WriteLn(stderr, #27'[32m', 'Errors: ', ExitCode, #27'[0m'); 194 | end; 195 | 196 | begin 197 | Main; 198 | end. 199 | -------------------------------------------------------------------------------- /HashLib.Tests/src/PBKDF_ScryptTests.pas: -------------------------------------------------------------------------------- 1 | unit PBKDF_ScryptTests; 2 | 3 | interface 4 | 5 | uses 6 | SysUtils, 7 | {$IFDEF FPC} 8 | fpcunit, 9 | testregistry, 10 | {$ELSE} 11 | TestFramework, 12 | {$ENDIF FPC} 13 | HlpIHashInfo, 14 | HlpHashFactory, 15 | HlpConverters, 16 | HlpHashLibTypes, 17 | HashLibTestBase; 18 | 19 | type 20 | 21 | TPBKDF_ScryptTestCase = class abstract(THashLibAlgorithmTestCase) 22 | 23 | end; 24 | 25 | type 26 | /// <summary> 27 | /// scrypt test vectors from "Stronger Key Derivation Via Sequential Memory-hard Functions" Appendix B. 28 | /// (http://www.tarsnap.com/scrypt/scrypt.pdf) 29 | /// </summary> 30 | TTestPBKDF_Scrypt = class(TPBKDF_ScryptTestCase) 31 | 32 | private 33 | 34 | function DoTestVector(const APassword, ASalt: String; 35 | ACost, ABlockSize, AParallelism, AOutputSize: Int32): String; 36 | 37 | procedure DoCheckOk(const AMsg: String; const APassword, ASalt: TBytes; 38 | ACost, ABlockSize, AParallelism, AOutputSize: Int32); 39 | 40 | procedure DoCheckIllegal(const AMsg: String; const APassword, ASalt: TBytes; 41 | ACost, ABlockSize, AParallelism, AOutputSize: Int32); 42 | 43 | published 44 | procedure TestVectors; 45 | procedure TestParameters; 46 | 47 | end; 48 | 49 | implementation 50 | 51 | { TTestPBKDF_Scrypt } 52 | 53 | function TTestPBKDF_Scrypt.DoTestVector(const APassword, ASalt: String; 54 | ACost, ABlockSize, AParallelism, AOutputSize: Int32): String; 55 | var 56 | PBKDF_Scrypt: IPBKDF_Scrypt; 57 | APasswordBytes, ASaltBytes, OutputBytes: TBytes; 58 | begin 59 | APasswordBytes := TConverters.ConvertStringToBytes(APassword, 60 | TEncoding.ASCII); 61 | ASaltBytes := TConverters.ConvertStringToBytes(ASalt, TEncoding.ASCII); 62 | PBKDF_Scrypt := TKDF.TPBKDF_Scrypt.CreatePBKDF_Scrypt(APasswordBytes, 63 | ASaltBytes, ACost, ABlockSize, AParallelism); 64 | OutputBytes := PBKDF_Scrypt.GetBytes(AOutputSize); 65 | PBKDF_Scrypt.Clear(); 66 | Result := TConverters.ConvertBytesToHexString(OutputBytes, False); 67 | end; 68 | 69 | procedure TTestPBKDF_Scrypt.DoCheckIllegal(const AMsg: String; 70 | const APassword, ASalt: TBytes; ACost, ABlockSize, AParallelism, 71 | AOutputSize: Int32); 72 | begin 73 | try 74 | TKDF.TPBKDF_Scrypt.CreatePBKDF_Scrypt(APassword, ASalt, ACost, ABlockSize, 75 | AParallelism).GetBytes(AOutputSize); 76 | Fail(AMsg); 77 | except 78 | on e: EArgumentHashLibException do 79 | begin 80 | // pass so we do nothing 81 | end; 82 | end; 83 | end; 84 | 85 | procedure TTestPBKDF_Scrypt.DoCheckOk(const AMsg: String; 86 | const APassword, ASalt: TBytes; ACost, ABlockSize, AParallelism, 87 | AOutputSize: Int32); 88 | var 89 | PBKDF_Scrypt: IPBKDF_Scrypt; 90 | begin 91 | try 92 | try 93 | PBKDF_Scrypt := TKDF.TPBKDF_Scrypt.CreatePBKDF_Scrypt(APassword, ASalt, 94 | ACost, ABlockSize, AParallelism); 95 | PBKDF_Scrypt.GetBytes(AOutputSize); 96 | except 97 | on e: EArgumentHashLibException do 98 | begin 99 | Fail(AMsg); 100 | end; 101 | end; 102 | finally 103 | PBKDF_Scrypt.Clear(); 104 | end; 105 | end; 106 | 107 | procedure TTestPBKDF_Scrypt.TestParameters; 108 | begin 109 | DoCheckOk('Minimal values', Nil, Nil, 2, 1, 1, 1); 110 | DoCheckIllegal('Cost parameter must be > 1', Nil, Nil, 1, 1, 1, 1); 111 | DoCheckOk('Cost parameter 32768 OK for r = 1', Nil, Nil, 32768, 1, 1, 1); 112 | DoCheckIllegal('Cost parameter must < 65536 for r = 1', Nil, Nil, 113 | 65536, 1, 1, 1); 114 | DoCheckIllegal('Block size must be >= 1', Nil, Nil, 2, 0, 2, 1); 115 | DoCheckIllegal('Parallelisation parameter must be >= 1', Nil, Nil, 2, 116 | 1, 0, 1); 117 | // disabled test because it's very expensive 118 | // DoCheckOk('Parallelisation parameter 65535 OK for r = 4', Nil, Nil, 2, 32, 119 | // 65535, 1); 120 | DoCheckIllegal('Parallelisation parameter must be < 65535 for r = 4', Nil, 121 | Nil, 2, 32, 65536, 1); 122 | 123 | DoCheckIllegal('Len parameter must be > 1', Nil, Nil, 2, 1, 1, 0); 124 | end; 125 | 126 | procedure TTestPBKDF_Scrypt.TestVectors; 127 | begin 128 | 129 | ActualString := DoTestVector('', '', 16, 1, 1, 64); 130 | ExpectedString := 131 | '77D6576238657B203B19CA42C18A0497F16B4844E3074AE8DFDFFA3FEDE21442FCD0069DED0948F8326A753A0FC81F17E8D3E0FB2E0D3628CF35E20C38D18906'; 132 | 133 | CheckEquals(ExpectedString, ActualString, Format('Expected %s but got %s.', 134 | [ExpectedString, ActualString])); 135 | 136 | ActualString := DoTestVector('password', 'NaCl', 1024, 8, 16, 64); 137 | ExpectedString := 138 | 'FDBABE1C9D3472007856E7190D01E9FE7C6AD7CBC8237830E77376634B3731622EAF30D92E22A3886FF109279D9830DAC727AFB94A83EE6D8360CBDFA2CC0640'; 139 | 140 | CheckEquals(ExpectedString, ActualString, Format('Expected %s but got %s.', 141 | [ExpectedString, ActualString])); 142 | 143 | ActualString := DoTestVector('pleaseletmein', 'SodiumChloride', 16384, 144 | 8, 1, 64); 145 | ExpectedString := 146 | '7023BDCB3AFD7348461C06CD81FD38EBFDA8FBBA904F8E3EA9B543F6545DA1F2D5432955613F0FCF62D49705242A9AF9E61E85DC0D651E40DFCF017B45575887'; 147 | 148 | CheckEquals(ExpectedString, ActualString, Format('Expected %s but got %s.', 149 | [ExpectedString, ActualString])); 150 | 151 | // disabled test because it's very expensive 152 | // ActualString := DoTestVector('pleaseletmein', 'SodiumChloride', 1048576, 153 | // 8, 1, 64); 154 | // ExpectedString := 155 | // '2101CB9B6A511AAEADDBBE09CF70F881EC568D574A2FFD4DABE5EE9820ADAA478E56FD8F4BA5D09FFA1C6D927C40F4C337304049E8A952FBCBF45C6FA77A41A4'; 156 | // 157 | // CheckEquals(ExpectedString, ActualString, Format('Expected %s but got %s.', 158 | // [ExpectedString, ActualString])); 159 | 160 | end; 161 | 162 | initialization 163 | 164 | // Register any test cases with the test runner 165 | 166 | {$IFDEF FPC} 167 | RegisterTest(TTestPBKDF_Scrypt); 168 | {$ELSE} 169 | RegisterTest(TTestPBKDF_Scrypt.Suite); 170 | {$ENDIF FPC} 171 | 172 | end. 173 | -------------------------------------------------------------------------------- /HashLib/src/Interfaces/HlpIHashInfo.pas: -------------------------------------------------------------------------------- 1 | unit HlpIHashInfo; 2 | 3 | {$I ..\Include\HashLib.inc} 4 | 5 | interface 6 | 7 | uses 8 | HlpHashLibTypes, 9 | HlpIKDF, 10 | HlpIHash, 11 | HlpArgon2TypeAndVersion, 12 | HlpNullable; 13 | 14 | type 15 | 16 | ITransformBlock = Interface(IInterface) 17 | ['{0C375CFF-B379-41B8-955F-A32E22991651}'] 18 | end; 19 | 20 | IBlockHash = Interface(IInterface) 21 | ['{3B9A2D29-AC4E-44E4-92B1-6AF9A64DFF0A}'] 22 | end; 23 | 24 | INonBlockHash = Interface(IInterface) 25 | ['{7C7E8B14-DBC7-44A3-BB7C-B24E0BFAA09C}'] 26 | end; 27 | 28 | IChecksum = Interface(IHash) 29 | ['{EF0885C5-D331-44D8-89CA-05409E20F76E}'] 30 | end; 31 | 32 | ICrypto = Interface(IHash) 33 | ['{5C669048-644C-4E96-B411-9FEA603D7086}'] 34 | end; 35 | 36 | ICryptoNotBuildIn = Interface(ICrypto) 37 | ['{391E62CE-219D-4D33-A753-C32D63353685}'] 38 | end; 39 | 40 | IWithKey = Interface(IHash) 41 | ['{DD5E0FE4-3573-4051-B7CF-F23BABE982D8}'] 42 | 43 | function GetKey(): THashLibByteArray; 44 | procedure SetKey(const AValue: THashLibByteArray); 45 | property Key: THashLibByteArray read GetKey write SetKey; 46 | function GetKeyLength(): TNullableInteger; 47 | property KeyLength: TNullableInteger read GetKeyLength; 48 | 49 | end; 50 | 51 | IMAC = Interface(IHash) 52 | ['{C75C99A1-B7D3-475F-AC39-03386EECC095}'] 53 | procedure Clear(); 54 | function GetKey(): THashLibByteArray; 55 | procedure SetKey(const AValue: THashLibByteArray); 56 | property Key: THashLibByteArray read GetKey write SetKey; 57 | end; 58 | 59 | IHMAC = Interface(IMAC) 60 | ['{A6D4DCC6-F6C3-4110-8CA2-FBE85227676E}'] 61 | end; 62 | 63 | IHMACNotBuildIn = Interface(IHMAC) 64 | ['{A44E01D3-164E-4E3F-9551-3EFFDE95A36C}'] 65 | end; 66 | 67 | IKMAC = Interface(IMAC) 68 | ['{49309B2F-20C3-4631-BFDD-06373D14CCE0}'] 69 | end; 70 | 71 | IKMACNotBuildIn = Interface(IKMAC) 72 | ['{FC7AF5A9-BD6A-4DBD-B1DD-B6E110B44A20}'] 73 | end; 74 | 75 | IBlake2BMAC = Interface(IMAC) 76 | ['{F6E0B1CA-1497-43C6-9CD9-2628F70E8451}'] 77 | end; 78 | 79 | IBlake2BMACNotBuildIn = Interface(IBlake2BMAC) 80 | ['{20B33EE5-48B4-4F7E-B1B8-1FD7B45E256E}'] 81 | end; 82 | 83 | IBlake2SMAC = Interface(IMAC) 84 | ['{7354FC5C-775C-42E9-9A25-274F62BF2CCE}'] 85 | end; 86 | 87 | IBlake2SMACNotBuildIn = Interface(IBlake2SMAC) 88 | ['{FFB17B7A-86A1-40D7-A5E7-60366FF8513C}'] 89 | end; 90 | 91 | IPBKDF2_HMAC = Interface(IKDF) 92 | ['{0D409BA8-7F98-4417-858F-3C1EBA11B7E1}'] 93 | end; 94 | 95 | IPBKDF2_HMACNotBuildIn = Interface(IPBKDF2_HMAC) 96 | ['{D7E23DFB-036D-44AD-AA0C-FB83C9970565}'] 97 | end; 98 | 99 | IPBKDF_Argon2 = Interface(IKDF) 100 | ['{A2BF19D2-8CEE-45B7-93A1-110A63A0A5A7}'] 101 | end; 102 | 103 | IPBKDF_Argon2NotBuildIn = Interface(IPBKDF_Argon2) 104 | ['{666D652C-E4E5-4C72-B09F-145495D1A95D}'] 105 | end; 106 | 107 | IPBKDF_Scrypt = Interface(IKDF) 108 | ['{D1AD2681-FBDB-41EF-B8F5-72E3F5872D27}'] 109 | end; 110 | 111 | IPBKDF_ScryptNotBuildIn = Interface(IPBKDF_Scrypt) 112 | ['{7DD70C4D-FBF6-4629-B587-C6A7CC047D35}'] 113 | end; 114 | 115 | IHash16 = Interface(IHash) 116 | ['{C15AF648-C9F7-460D-9F74-B68CA593C2F8}'] 117 | end; 118 | 119 | IHash32 = Interface(IHash) 120 | ['{004BBFDB-71B6-4C74-ABE8-88EC1777263D}'] 121 | end; 122 | 123 | IHash64 = Interface(IHash) 124 | ['{F0354E86-3BEC-4EBC-B17D-ABFC91C02997}'] 125 | end; 126 | 127 | IHash128 = Interface(IHash) 128 | ['{8DD14E37-DDD6-455C-A795-21A15C9E5376}'] 129 | end; 130 | 131 | IHashWithKey = Interface(IWithKey) 132 | ['{D38AE885-651F-4F15-BF90-5B64A0F24E49}'] 133 | end; 134 | 135 | IXOF = Interface(IHash) 136 | ['{944ED7F0-D033-4489-A5DD-9C83353F23F0}'] 137 | function GetXOFSizeInBits: UInt64; 138 | procedure SetXOFSizeInBits(AXofSizeInBits: UInt64); 139 | property XOFSizeInBits: UInt64 read GetXOFSizeInBits write SetXOFSizeInBits; 140 | procedure DoOutput(const ADestination: THashLibByteArray; 141 | ADestinationOffset, AOutputLength: UInt64); 142 | end; 143 | 144 | type 145 | IArgon2Parameters = interface(IInterface) 146 | ['{566D3381-57F1-4EE0-81EC-3DB21FF49FBC}'] 147 | procedure Clear(); 148 | 149 | function GetSalt(): THashLibByteArray; 150 | property Salt: THashLibByteArray read GetSalt; 151 | function GetSecret(): THashLibByteArray; 152 | property Secret: THashLibByteArray read GetSecret; 153 | function GetAdditional(): THashLibByteArray; 154 | property Additional: THashLibByteArray read GetAdditional; 155 | function GetIterations(): Int32; 156 | property Iterations: Int32 read GetIterations; 157 | function GetMemory(): Int32; 158 | property Memory: Int32 read GetMemory; 159 | function GetLanes(): Int32; 160 | property Lanes: Int32 read GetLanes; 161 | function GetType(): TArgon2Type; 162 | property &Type: TArgon2Type read GetType; 163 | function GetVersion(): TArgon2Version; 164 | property Version: TArgon2Version read GetVersion; 165 | end; 166 | 167 | type 168 | IArgon2ParametersBuilder = interface(IInterface) 169 | ['{DD0EF0C0-BAB8-4587-95FD-B9A266E67BC1}'] 170 | 171 | function WithParallelism(AParallelism: Int32): IArgon2ParametersBuilder; 172 | 173 | function WithSalt(const ASalt: THashLibByteArray): IArgon2ParametersBuilder; 174 | 175 | function WithSecret(const ASecret: THashLibByteArray) 176 | : IArgon2ParametersBuilder; 177 | 178 | function WithAdditional(const AAdditional: THashLibByteArray) 179 | : IArgon2ParametersBuilder; 180 | 181 | function WithIterations(AIterations: Int32): IArgon2ParametersBuilder; 182 | 183 | function WithMemoryAsKB(AMemory: Int32): IArgon2ParametersBuilder; 184 | 185 | function WithMemoryPowOfTwo(AMemory: Int32): IArgon2ParametersBuilder; 186 | 187 | function WithVersion(AVersion: TArgon2Version): IArgon2ParametersBuilder; 188 | 189 | procedure Clear(); 190 | 191 | function Build(): IArgon2Parameters; 192 | 193 | end; 194 | 195 | implementation 196 | 197 | end. 198 | -------------------------------------------------------------------------------- /HashLib/src/Packages/Delphi/HashLib4PascalPackage.dpk: -------------------------------------------------------------------------------- 1 | package HashLib4PascalPackage; 2 | 3 | {$R *.res} 4 | {$WARN DUPLICATE_CTOR_DTOR OFF} 5 | {$WARNINGS OFF} 6 | {$HINTS OFF} 7 | {$IFDEF IMPLICITBUILDING This IFDEF should not be used by users} 8 | {$ALIGN 8} 9 | {$ASSERTIONS ON} 10 | {$BOOLEVAL OFF} 11 | {$DEBUGINFO OFF} 12 | {$EXTENDEDSYNTAX ON} 13 | {$IMPORTEDDATA ON} 14 | {$IOCHECKS ON} 15 | {$LOCALSYMBOLS OFF} 16 | {$LONGSTRINGS ON} 17 | {$OPENSTRINGS ON} 18 | {$OPTIMIZATION ON} 19 | {$OVERFLOWCHECKS OFF} 20 | {$RANGECHECKS OFF} 21 | {$REFERENCEINFO OFF} 22 | {$SAFEDIVIDE OFF} 23 | {$STACKFRAMES OFF} 24 | {$TYPEDADDRESS OFF} 25 | {$VARSTRINGCHECKS ON} 26 | {$WRITEABLECONST OFF} 27 | {$MINENUMSIZE 1} 28 | {$IMAGEBASE $400000} 29 | {$DEFINE RELEASE} 30 | {$ENDIF IMPLICITBUILDING} 31 | {$RUNONLY} 32 | {$IMPLICITBUILD ON} 33 | 34 | requires 35 | rtl, 36 | soaprtl; 37 | 38 | contains 39 | HlpHash in '..\..\Base\HlpHash.pas', 40 | HlpHashBuffer in '..\..\Base\HlpHashBuffer.pas', 41 | HlpHashCryptoNotBuildIn in '..\..\Base\HlpHashCryptoNotBuildIn.pas', 42 | HlpHashFactory in '..\..\Base\HlpHashFactory.pas', 43 | HlpHashResult in '..\..\Base\HlpHashResult.pas', 44 | HlpHashRounds in '..\..\Base\HlpHashRounds.pas', 45 | HlpHashSize in '..\..\Base\HlpHashSize.pas', 46 | HlpHMACNotBuildInAdapter in '..\..\Base\HlpHMACNotBuildInAdapter.pas', 47 | HlpKDF in '..\..\Base\HlpKDF.pas', 48 | HlpMultipleTransformNonBlock in '..\..\Base\HlpMultipleTransformNonBlock.pas', 49 | HlpAdler32 in '..\..\Checksum\HlpAdler32.pas', 50 | HlpCRC in '..\..\Checksum\HlpCRC.pas', 51 | HlpCRC16 in '..\..\Checksum\HlpCRC16.pas', 52 | HlpCRC32 in '..\..\Checksum\HlpCRC32.pas', 53 | HlpCRC32Fast in '..\..\Checksum\HlpCRC32Fast.pas', 54 | HlpCRC64 in '..\..\Checksum\HlpCRC64.pas', 55 | HlpBlake2B in '..\..\Crypto\HlpBlake2B.pas', 56 | HlpBlake2S in '..\..\Crypto\HlpBlake2S.pas', 57 | HlpGost in '..\..\Crypto\HlpGost.pas', 58 | HlpGOST3411_2012 in '..\..\Crypto\HlpGOST3411_2012.pas', 59 | HlpGrindahl256 in '..\..\Crypto\HlpGrindahl256.pas', 60 | HlpGrindahl512 in '..\..\Crypto\HlpGrindahl512.pas', 61 | HlpHAS160 in '..\..\Crypto\HlpHAS160.pas', 62 | HlpHaval in '..\..\Crypto\HlpHaval.pas', 63 | HlpMD2 in '..\..\Crypto\HlpMD2.pas', 64 | HlpMD4 in '..\..\Crypto\HlpMD4.pas', 65 | HlpMD5 in '..\..\Crypto\HlpMD5.pas', 66 | HlpMDBase in '..\..\Crypto\HlpMDBase.pas', 67 | HlpPanama in '..\..\Crypto\HlpPanama.pas', 68 | HlpRadioGatun32 in '..\..\Crypto\HlpRadioGatun32.pas', 69 | HlpRadioGatun64 in '..\..\Crypto\HlpRadioGatun64.pas', 70 | HlpRIPEMD in '..\..\Crypto\HlpRIPEMD.pas', 71 | HlpRIPEMD128 in '..\..\Crypto\HlpRIPEMD128.pas', 72 | HlpRIPEMD160 in '..\..\Crypto\HlpRIPEMD160.pas', 73 | HlpRIPEMD256 in '..\..\Crypto\HlpRIPEMD256.pas', 74 | HlpRIPEMD320 in '..\..\Crypto\HlpRIPEMD320.pas', 75 | HlpSHA0 in '..\..\Crypto\HlpSHA0.pas', 76 | HlpSHA1 in '..\..\Crypto\HlpSHA1.pas', 77 | HlpSHA2_224 in '..\..\Crypto\HlpSHA2_224.pas', 78 | HlpSHA2_256 in '..\..\Crypto\HlpSHA2_256.pas', 79 | HlpSHA2_256Base in '..\..\Crypto\HlpSHA2_256Base.pas', 80 | HlpSHA2_384 in '..\..\Crypto\HlpSHA2_384.pas', 81 | HlpSHA2_512 in '..\..\Crypto\HlpSHA2_512.pas', 82 | HlpSHA2_512_224 in '..\..\Crypto\HlpSHA2_512_224.pas', 83 | HlpSHA2_512_256 in '..\..\Crypto\HlpSHA2_512_256.pas', 84 | HlpSHA2_512Base in '..\..\Crypto\HlpSHA2_512Base.pas', 85 | HlpSHA3 in '..\..\Crypto\HlpSHA3.pas', 86 | HlpSnefru in '..\..\Crypto\HlpSnefru.pas', 87 | HlpTiger in '..\..\Crypto\HlpTiger.pas', 88 | HlpTiger2 in '..\..\Crypto\HlpTiger2.pas', 89 | HlpWhirlPool in '..\..\Crypto\HlpWhirlPool.pas', 90 | HlpNullDigest in '..\..\NullDigest\HlpNullDigest.pas', 91 | HlpAP in '..\..\Hash32\HlpAP.pas', 92 | HlpBernstein in '..\..\Hash32\HlpBernstein.pas', 93 | HlpBernstein1 in '..\..\Hash32\HlpBernstein1.pas', 94 | HlpBKDR in '..\..\Hash32\HlpBKDR.pas', 95 | HlpDEK in '..\..\Hash32\HlpDEK.pas', 96 | HlpDJB in '..\..\Hash32\HlpDJB.pas', 97 | HlpELF in '..\..\Hash32\HlpELF.pas', 98 | HlpFNV in '..\..\Hash32\HlpFNV.pas', 99 | HlpFNV1a in '..\..\Hash32\HlpFNV1a.pas', 100 | HlpJenkins3 in '..\..\Hash32\HlpJenkins3.pas', 101 | HlpJS in '..\..\Hash32\HlpJS.pas', 102 | HlpMurmur2 in '..\..\Hash32\HlpMurmur2.pas', 103 | HlpMurmurHash3_x86_32 in '..\..\Hash32\HlpMurmurHash3_x86_32.pas', 104 | HlpOneAtTime in '..\..\Hash32\HlpOneAtTime.pas', 105 | HlpPJW in '..\..\Hash32\HlpPJW.pas', 106 | HlpRotating in '..\..\Hash32\HlpRotating.pas', 107 | HlpRS in '..\..\Hash32\HlpRS.pas', 108 | HlpSDBM in '..\..\Hash32\HlpSDBM.pas', 109 | HlpShiftAndXor in '..\..\Hash32\HlpShiftAndXor.pas', 110 | HlpSuperFast in '..\..\Hash32\HlpSuperFast.pas', 111 | HlpXXHash32 in '..\..\Hash32\HlpXXHash32.pas', 112 | HlpFNV1a64 in '..\..\Hash64\HlpFNV1a64.pas', 113 | HlpFNV64 in '..\..\Hash64\HlpFNV64.pas', 114 | HlpMurmur2_64 in '..\..\Hash64\HlpMurmur2_64.pas', 115 | HlpSipHash in '..\..\Hash64\HlpSipHash.pas', 116 | HlpXXHash64 in '..\..\Hash64\HlpXXHash64.pas', 117 | HlpMurmurHash3_x64_128 in '..\..\Hash128\HlpMurmurHash3_x64_128.pas', 118 | HlpMurmurHash3_x86_128 in '..\..\Hash128\HlpMurmurHash3_x86_128.pas', 119 | HlpSipHash128 in '..\..\Hash128\HlpSipHash128.pas', 120 | HlpICRC in '..\..\Interfaces\HlpICRC.pas', 121 | HlpIHash in '..\..\Interfaces\HlpIHash.pas', 122 | HlpIHashInfo in '..\..\Interfaces\HlpIHashInfo.pas', 123 | HlpIHashResult in '..\..\Interfaces\HlpIHashResult.pas', 124 | HlpIKDF in '..\..\Interfaces\HlpIKDF.pas', 125 | HlpBlake2BP in '..\..\Crypto\HlpBlake2BP.pas', 126 | HlpBlake2SP in '..\..\Crypto\HlpBlake2SP.pas', 127 | HlpPBKDF2_HMACNotBuildInAdapter in '..\..\KDF\HlpPBKDF2_HMACNotBuildInAdapter.pas', 128 | HlpPBKDF_Argon2NotBuildInAdapter in '..\..\KDF\HlpPBKDF_Argon2NotBuildInAdapter.pas', 129 | HlpArgon2TypeAndVersion in '..\..\KDF\HlpArgon2TypeAndVersion.pas', 130 | HlpPBKDF_ScryptNotBuildInAdapter in '..\..\KDF\HlpPBKDF_ScryptNotBuildInAdapter.pas', 131 | HlpNullable in '..\..\Nullable\HlpNullable.pas', 132 | HlpConverters in '..\..\Utils\HlpConverters.pas', 133 | HlpBitConverter in '..\..\Utils\HlpBitConverter.pas', 134 | HlpBits in '..\..\Utils\HlpBits.pas', 135 | HlpArrayUtils in '..\..\Utils\HlpArrayUtils.pas', 136 | HlpHashLibTypes in '..\..\Utils\HlpHashLibTypes.pas', 137 | HlpBlake2SParams in '..\..\Crypto\Blake2SParams\HlpBlake2SParams.pas', 138 | HlpBlake2BParams in '..\..\Crypto\Blake2BParams\HlpBlake2BParams.pas', 139 | HlpIBlake2SParams in '..\..\Interfaces\IBlake2SParams\HlpIBlake2SParams.pas', 140 | HlpIBlake2BParams in '..\..\Interfaces\IBlake2BParams\HlpIBlake2BParams.pas', 141 | HlpBlake3 in '..\..\Crypto\HlpBlake3.pas'; 142 | 143 | end. 144 | -------------------------------------------------------------------------------- /HashLib/src/Crypto/HlpMD4.pas: -------------------------------------------------------------------------------- 1 | unit HlpMD4; 2 | 3 | {$I ..\Include\HashLib.inc} 4 | 5 | interface 6 | 7 | uses 8 | {$IFDEF DELPHI2010} 9 | SysUtils, // to get rid of compiler hint "not inlined" on Delphi 2010. 10 | {$ENDIF DELPHI2010} 11 | HlpMDBase, 12 | {$IFDEF DELPHI} 13 | HlpHashBuffer, 14 | HlpHash, 15 | {$ENDIF DELPHI} 16 | HlpBits, 17 | HlpConverters, 18 | HlpIHash, 19 | HlpIHashInfo; 20 | 21 | type 22 | TMD4 = class sealed(TMDBase, ITransformBlock) 23 | 24 | strict protected 25 | procedure TransformBlock(AData: PByte; ADataLength: Int32; 26 | AIndex: Int32); override; 27 | 28 | public 29 | constructor Create(); 30 | function Clone(): IHash; override; 31 | 32 | end; 33 | 34 | implementation 35 | 36 | { TMD4 } 37 | 38 | function TMD4.Clone(): IHash; 39 | var 40 | LHashInstance: TMD4; 41 | begin 42 | LHashInstance := TMD4.Create(); 43 | LHashInstance.FState := System.Copy(FState); 44 | LHashInstance.FBuffer := FBuffer.Clone(); 45 | LHashInstance.FProcessedBytesCount := FProcessedBytesCount; 46 | result := LHashInstance as IHash; 47 | result.BufferSize := BufferSize; 48 | end; 49 | 50 | constructor TMD4.Create; 51 | begin 52 | Inherited Create(4, 16); 53 | end; 54 | 55 | procedure TMD4.TransformBlock(AData: PByte; ADataLength: Int32; AIndex: Int32); 56 | var 57 | a, b, c, d: UInt32; 58 | LData: array [0 .. 15] of UInt32; 59 | begin 60 | TConverters.le32_copy(AData, AIndex, @(LData[0]), 0, ADataLength); 61 | 62 | a := FState[0]; 63 | b := FState[1]; 64 | c := FState[2]; 65 | d := FState[3]; 66 | 67 | a := a + (LData[0] + ((b and c) or ((not b) and d))); 68 | a := TBits.RotateLeft32(a, 3); 69 | d := d + (LData[1] + ((a and b) or ((not a) and c))); 70 | d := TBits.RotateLeft32(d, 7); 71 | c := c + (LData[2] + ((d and a) or ((not d) and b))); 72 | c := TBits.RotateLeft32(c, 11); 73 | b := b + (LData[3] + ((c and d) or ((not c) and a))); 74 | b := TBits.RotateLeft32(b, 19); 75 | a := a + (LData[4] + ((b and c) or ((not b) and d))); 76 | a := TBits.RotateLeft32(a, 3); 77 | d := d + (LData[5] + ((a and b) or ((not a) and c))); 78 | d := TBits.RotateLeft32(d, 7); 79 | c := c + (LData[6] + ((d and a) or ((not d) and b))); 80 | c := TBits.RotateLeft32(c, 11); 81 | b := b + (LData[7] + ((c and d) or ((not c) and a))); 82 | b := TBits.RotateLeft32(b, 19); 83 | a := a + (LData[8] + ((b and c) or ((not b) and d))); 84 | a := TBits.RotateLeft32(a, 3); 85 | d := d + (LData[9] + ((a and b) or ((not a) and c))); 86 | d := TBits.RotateLeft32(d, 7); 87 | c := c + (LData[10] + ((d and a) or ((not d) and b))); 88 | c := TBits.RotateLeft32(c, 11); 89 | b := b + (LData[11] + ((c and d) or ((not c) and a))); 90 | b := TBits.RotateLeft32(b, 19); 91 | a := a + (LData[12] + ((b and c) or ((not b) and d))); 92 | a := TBits.RotateLeft32(a, 3); 93 | d := d + (LData[13] + ((a and b) or ((not a) and c))); 94 | d := TBits.RotateLeft32(d, 7); 95 | c := c + (LData[14] + ((d and a) or ((not d) and b))); 96 | c := TBits.RotateLeft32(c, 11); 97 | b := b + (LData[15] + ((c and d) or ((not c) and a))); 98 | b := TBits.RotateLeft32(b, 19); 99 | 100 | a := a + (LData[0] + C2 + ((b and (c or d)) or (c and d))); 101 | a := TBits.RotateLeft32(a, 3); 102 | d := d + (LData[4] + C2 + ((a and (b or c)) or (b and c))); 103 | d := TBits.RotateLeft32(d, 5); 104 | c := c + (LData[8] + C2 + ((d and (a or b)) or (a and b))); 105 | c := TBits.RotateLeft32(c, 9); 106 | b := b + (LData[12] + C2 + ((c and (d or a)) or (d and a))); 107 | b := TBits.RotateLeft32(b, 13); 108 | a := a + (LData[1] + C2 + ((b and (c or d)) or (c and d))); 109 | a := TBits.RotateLeft32(a, 3); 110 | d := d + (LData[5] + C2 + ((a and (b or c)) or (b and c))); 111 | d := TBits.RotateLeft32(d, 5); 112 | c := c + (LData[9] + C2 + ((d and (a or b)) or (a and b))); 113 | c := TBits.RotateLeft32(c, 9); 114 | b := b + (LData[13] + C2 + ((c and (d or a)) or (d and a))); 115 | b := TBits.RotateLeft32(b, 13); 116 | a := a + (LData[2] + C2 + ((b and (c or d)) or (c and d))); 117 | a := TBits.RotateLeft32(a, 3); 118 | d := d + (LData[6] + C2 + ((a and (b or c)) or (b and c))); 119 | d := TBits.RotateLeft32(d, 5); 120 | c := c + (LData[10] + C2 + ((d and (a or b)) or (a and b))); 121 | c := TBits.RotateLeft32(c, 9); 122 | b := b + (LData[14] + C2 + ((c and (d or a)) or (d and a))); 123 | b := TBits.RotateLeft32(b, 13); 124 | a := a + (LData[3] + C2 + ((b and (c or d)) or (c and d))); 125 | a := TBits.RotateLeft32(a, 3); 126 | d := d + (LData[7] + C2 + ((a and (b or c)) or (b and c))); 127 | d := TBits.RotateLeft32(d, 5); 128 | c := c + (LData[11] + C2 + ((d and (a or b)) or (a and b))); 129 | c := TBits.RotateLeft32(c, 9); 130 | b := b + (LData[15] + C2 + ((c and (d or a)) or (d and a))); 131 | b := TBits.RotateLeft32(b, 13); 132 | 133 | a := a + (LData[0] + C4 + (b xor c xor d)); 134 | a := TBits.RotateLeft32(a, 3); 135 | d := d + (LData[8] + C4 + (a xor b xor c)); 136 | d := TBits.RotateLeft32(d, 9); 137 | c := c + (LData[4] + C4 + (d xor a xor b)); 138 | c := TBits.RotateLeft32(c, 11); 139 | b := b + (LData[12] + C4 + (c xor d xor a)); 140 | b := TBits.RotateLeft32(b, 15); 141 | a := a + (LData[2] + C4 + (b xor c xor d)); 142 | a := TBits.RotateLeft32(a, 3); 143 | d := d + (LData[10] + C4 + (a xor b xor c)); 144 | d := TBits.RotateLeft32(d, 9); 145 | c := c + (LData[6] + C4 + (d xor a xor b)); 146 | c := TBits.RotateLeft32(c, 11); 147 | b := b + (LData[14] + C4 + (c xor d xor a)); 148 | b := TBits.RotateLeft32(b, 15); 149 | a := a + (LData[1] + C4 + (b xor c xor d)); 150 | a := TBits.RotateLeft32(a, 3); 151 | d := d + (LData[9] + C4 + (a xor b xor c)); 152 | d := TBits.RotateLeft32(d, 9); 153 | c := c + (LData[5] + C4 + (d xor a xor b)); 154 | c := TBits.RotateLeft32(c, 11); 155 | b := b + (LData[13] + C4 + (c xor d xor a)); 156 | b := TBits.RotateLeft32(b, 15); 157 | a := a + (LData[3] + C4 + (b xor c xor d)); 158 | a := TBits.RotateLeft32(a, 3); 159 | d := d + (LData[11] + C4 + (a xor b xor c)); 160 | d := TBits.RotateLeft32(d, 9); 161 | c := c + (LData[7] + C4 + (d xor a xor b)); 162 | c := TBits.RotateLeft32(c, 11); 163 | b := b + (LData[15] + C4 + (c xor d xor a)); 164 | b := TBits.RotateLeft32(b, 15); 165 | 166 | FState[0] := FState[0] + a; 167 | FState[1] := FState[1] + b; 168 | FState[2] := FState[2] + c; 169 | FState[3] := FState[3] + d; 170 | 171 | System.FillChar(LData, System.SizeOf(LData), UInt32(0)); 172 | end; 173 | 174 | end. 175 | -------------------------------------------------------------------------------- /HashLib/src/Crypto/HlpHAS160.pas: -------------------------------------------------------------------------------- 1 | unit HlpHAS160; 2 | 3 | {$I ..\Include\HashLib.inc} 4 | 5 | interface 6 | 7 | uses 8 | {$IFDEF DELPHI2010} 9 | SysUtils, // to get rid of compiler hint "not inlined" on Delphi 2010. 10 | {$ENDIF DELPHI2010} 11 | HlpHashLibTypes, 12 | {$IFDEF DELPHI} 13 | HlpHash, 14 | HlpHashBuffer, 15 | {$ENDIF DELPHI} 16 | HlpConverters, 17 | HlpIHash, 18 | HlpIHashInfo, 19 | HlpHashCryptoNotBuildIn; 20 | 21 | type 22 | THAS160 = class sealed(TBlockHash, ICryptoNotBuildIn, ITransformBlock) 23 | 24 | strict private 25 | var 26 | FHash: THashLibUInt32Array; 27 | 28 | {$REGION 'Consts'} 29 | 30 | const 31 | SRot: array [0 .. 19] of Int32 = (5, 11, 7, 15, 6, 13, 8, 14, 7, 12, 9, 11, 32 | 8, 15, 6, 12, 9, 14, 5, 13); 33 | 34 | Stor: array [0 .. 19] of Int32 = (27, 21, 25, 17, 26, 19, 24, 18, 25, 20, 35 | 23, 21, 24, 17, 26, 20, 23, 18, 27, 19); 36 | 37 | SIndex: array [0 .. 79] of Int32 = (18, 0, 1, 2, 3, 19, 4, 5, 6, 7, 16, 8, 38 | 9, 10, 11, 17, 12, 13, 14, 15, 18, 3, 6, 9, 12, 19, 15, 2, 5, 8, 16, 11, 39 | 14, 1, 4, 17, 7, 10, 13, 0, 18, 12, 5, 14, 7, 19, 0, 9, 2, 11, 16, 4, 13, 40 | 6, 15, 17, 8, 1, 10, 3, 18, 7, 2, 13, 8, 19, 3, 14, 9, 4, 16, 15, 10, 5, 41 | 0, 17, 11, 6, 1, 12); 42 | 43 | {$ENDREGION} 44 | strict protected 45 | procedure Finish(); override; 46 | function GetResult(): THashLibByteArray; override; 47 | procedure TransformBlock(AData: PByte; ADataLength: Int32; 48 | AIndex: Int32); override; 49 | 50 | public 51 | constructor Create(); 52 | procedure Initialize(); override; 53 | function Clone(): IHash; override; 54 | end; 55 | 56 | implementation 57 | 58 | { THAS160 } 59 | 60 | function THAS160.Clone(): IHash; 61 | var 62 | LHashInstance: THAS160; 63 | begin 64 | LHashInstance := THAS160.Create(); 65 | LHashInstance.FHash := System.Copy(FHash); 66 | LHashInstance.FBuffer := FBuffer.Clone(); 67 | LHashInstance.FProcessedBytesCount := FProcessedBytesCount; 68 | result := LHashInstance as IHash; 69 | result.BufferSize := BufferSize; 70 | end; 71 | 72 | constructor THAS160.Create; 73 | begin 74 | Inherited Create(20, 64); 75 | System.SetLength(FHash, 5); 76 | end; 77 | 78 | procedure THAS160.Finish; 79 | var 80 | LPadIndex: Int32; 81 | LBits: UInt64; 82 | LPad: THashLibByteArray; 83 | begin 84 | LBits := FProcessedBytesCount * 8; 85 | if (FBuffer.Position < 56) then 86 | begin 87 | LPadIndex := (56 - FBuffer.Position) 88 | end 89 | else 90 | begin 91 | LPadIndex := (120 - FBuffer.Position); 92 | end; 93 | 94 | System.SetLength(LPad, LPadIndex + 8); 95 | 96 | LPad[0] := $80; 97 | 98 | LBits := TConverters.le2me_64(LBits); 99 | 100 | TConverters.ReadUInt64AsBytesLE(LBits, LPad, LPadIndex); 101 | 102 | LPadIndex := LPadIndex + 8; 103 | 104 | TransformBytes(LPad, 0, LPadIndex); 105 | end; 106 | 107 | function THAS160.GetResult: THashLibByteArray; 108 | begin 109 | System.SetLength(result, 5 * System.SizeOf(UInt32)); 110 | TConverters.le32_copy(PCardinal(FHash), 0, PByte(result), 0, 111 | System.Length(result)); 112 | end; 113 | 114 | procedure THAS160.Initialize; 115 | begin 116 | FHash[0] := $67452301; 117 | FHash[1] := $EFCDAB89; 118 | FHash[2] := $98BADCFE; 119 | FHash[3] := $10325476; 120 | FHash[4] := $C3D2E1F0; 121 | Inherited Initialize(); 122 | end; 123 | 124 | procedure THAS160.TransformBlock(AData: PByte; ADataLength: Int32; 125 | AIndex: Int32); 126 | var 127 | A, B, C, D, E, T: UInt32; 128 | R: Int32; 129 | LData: array [0 .. 19] of UInt32; 130 | begin 131 | A := FHash[0]; 132 | B := FHash[1]; 133 | C := FHash[2]; 134 | D := FHash[3]; 135 | E := FHash[4]; 136 | 137 | TConverters.le32_copy(AData, AIndex, @(LData[0]), 0, ADataLength); 138 | 139 | LData[16] := LData[0] xor LData[1] xor LData[2] xor LData[3]; 140 | LData[17] := LData[4] xor LData[5] xor LData[6] xor LData[7]; 141 | LData[18] := LData[8] xor LData[9] xor LData[10] xor LData[11]; 142 | LData[19] := LData[12] xor LData[13] xor LData[14] xor LData[15]; 143 | 144 | R := 0; 145 | while R < 20 do 146 | begin 147 | T := LData[SIndex[R]] + (A shl SRot[R] or A shr Stor[R]) + 148 | ((B and C) or (not B and D)) + E; 149 | E := D; 150 | D := C; 151 | C := B shl 10 or B shr 22; 152 | B := A; 153 | A := T; 154 | System.Inc(R); 155 | end; 156 | 157 | LData[16] := LData[3] xor LData[6] xor LData[9] xor LData[12]; 158 | LData[17] := LData[2] xor LData[5] xor LData[8] xor LData[15]; 159 | LData[18] := LData[1] xor LData[4] xor LData[11] xor LData[14]; 160 | LData[19] := LData[0] xor LData[7] xor LData[10] xor LData[13]; 161 | 162 | R := 20; 163 | while R < 40 do 164 | begin 165 | T := LData[SIndex[R]] + $5A827999 + 166 | (A shl SRot[R - 20] or A shr Stor[R - 20]) + (B xor C xor D) + E; 167 | E := D; 168 | D := C; 169 | C := B shl 17 or B shr 15; 170 | B := A; 171 | A := T; 172 | System.Inc(R); 173 | end; 174 | 175 | LData[16] := LData[5] xor LData[7] xor LData[12] xor LData[14]; 176 | LData[17] := LData[0] xor LData[2] xor LData[9] xor LData[11]; 177 | LData[18] := LData[4] xor LData[6] xor LData[13] xor LData[15]; 178 | LData[19] := LData[1] xor LData[3] xor LData[8] xor LData[10]; 179 | 180 | R := 40; 181 | while R < 60 do 182 | begin 183 | T := LData[SIndex[R]] + $6ED9EBA1 + 184 | (A shl SRot[R - 40] or A shr Stor[R - 40]) + (C xor (B or not D)) + E; 185 | E := D; 186 | D := C; 187 | C := B shl 25 or B shr 7; 188 | B := A; 189 | A := T; 190 | System.Inc(R); 191 | end; 192 | 193 | LData[16] := LData[2] xor LData[7] xor LData[8] xor LData[13]; 194 | LData[17] := LData[3] xor LData[4] xor LData[9] xor LData[14]; 195 | LData[18] := LData[0] xor LData[5] xor LData[10] xor LData[15]; 196 | LData[19] := LData[1] xor LData[6] xor LData[11] xor LData[12]; 197 | 198 | R := 60; 199 | while R < 80 do 200 | begin 201 | T := LData[SIndex[R]] + $8F1BBCDC + 202 | (A shl SRot[R - 60] or A shr Stor[R - 60]) + (B xor C xor D) + E; 203 | E := D; 204 | D := C; 205 | C := (B shl 30) or (B shr 2); 206 | B := A; 207 | A := T; 208 | System.Inc(R); 209 | end; 210 | 211 | FHash[0] := FHash[0] + A; 212 | FHash[1] := FHash[1] + B; 213 | FHash[2] := FHash[2] + C; 214 | FHash[3] := FHash[3] + D; 215 | FHash[4] := FHash[4] + E; 216 | 217 | System.FillChar(LData, System.SizeOf(LData), UInt32(0)); 218 | end; 219 | 220 | end. 221 | -------------------------------------------------------------------------------- /HashLib/src/Nullable/HlpNullable.pas: -------------------------------------------------------------------------------- 1 | unit HlpNullable; 2 | 3 | {$I ..\Include\HashLib.inc} 4 | { /* The "Nullable Types" found in this Unit were extracted from 5 | https://github.com/jpluimers/Conferences/blob/master/2009/DelphiLive.2009/Nullable-types-in-Delphi-Win32/Delphi-generic/src/NullableTypes.pas 6 | with some little modifications by me. */ } 7 | 8 | interface 9 | 10 | uses 11 | TypInfo, 12 | HlpHashLibTypes; 13 | 14 | resourcestring 15 | SCannotAssignPointerToNullable = 16 | 'Cannot assign non-null pointer to nullable type.'; 17 | SUnsupportedType = 18 | 'Unsupported Type: Only supports Integers, Int64, Floats and Strings.'; 19 | SGetNullValue = 'Attempted to get a null value.'; 20 | 21 | type 22 | 23 | Nullable<T> = record 24 | private 25 | var 26 | fValue: T; 27 | class function CastBack(const aValue): T; static; 28 | class function AddFloat(const aFloat, bFloat): T; static; 29 | class function AddString(const aString, bString): T; static; 30 | class function AddInt64(const aInt64, bInt64): T; static; 31 | class function NewAddInt(const aInt, bInt): T; static; 32 | 33 | var 34 | fInitValue: string; 35 | 36 | var 37 | fDefault: T; 38 | 39 | var 40 | fInitDefault: string; 41 | procedure SetValue(const aValue: T); inline; 42 | procedure CheckValue; inline; 43 | procedure CheckType; inline; 44 | function GetValue: T; inline; 45 | function GetIsNull: Boolean; inline; 46 | function GetHasValue: Boolean; inline; 47 | function GetHasDefault: Boolean; inline; 48 | public 49 | property Value: T read GetValue write SetValue; 50 | property IsNull: Boolean read GetIsNull; 51 | property HasValue: Boolean read GetHasValue; 52 | property HasDefault: Boolean read GetHasDefault; 53 | procedure ClearValue; 54 | procedure SetDefault(const aDefault: T); 55 | 56 | constructor Create(const aValue: T); overload; 57 | constructor Create(const aValue: T; const aDefault: T); overload; 58 | 59 | class operator Implicit(a: T): Nullable<T>; 60 | class operator Implicit(a: Nullable<T>): T; 61 | class operator Implicit(a: Pointer): Nullable<T>; 62 | class operator Explicit(aValue: Nullable<T>): T; 63 | 64 | class operator Add(a, b: Nullable<T>): Nullable<T>; 65 | end; 66 | 67 | /// <summary> 68 | /// Represents a Nullable Integer. 69 | /// </summary> 70 | TNullableInteger = Nullable<Int32>; 71 | 72 | implementation 73 | 74 | { Nullable<T> } 75 | 76 | function Nullable<T>.GetHasDefault: Boolean; 77 | begin 78 | Result := fInitDefault = 'I'; 79 | end; 80 | 81 | function Nullable<T>.GetHasValue: Boolean; 82 | begin 83 | Result := not IsNull; 84 | end; 85 | 86 | function Nullable<T>.GetIsNull: Boolean; 87 | begin 88 | Result := fInitValue <> 'I'; 89 | end; 90 | 91 | procedure Nullable<T>.CheckType; 92 | var 93 | info: PTypeInfo; 94 | begin 95 | info := TypeInfo(T); 96 | case info^.Kind of 97 | tkInteger: 98 | ; 99 | tkFloat: 100 | ; 101 | tkString: 102 | ; 103 | tkInt64: 104 | ; 105 | tkUString: 106 | ; 107 | else 108 | Raise EUnsupportedTypeHashLibException.CreateRes(@SUnsupportedType); 109 | end; 110 | end; 111 | 112 | procedure Nullable<T>.CheckValue; 113 | begin 114 | if IsNull then 115 | if HasDefault then 116 | fValue := fDefault 117 | else 118 | raise ENullReferenceHashLibException.CreateRes(@SGetNullValue); 119 | end; 120 | 121 | function Nullable<T>.GetValue: T; 122 | begin 123 | CheckType; 124 | CheckValue; 125 | Result := fValue; 126 | end; 127 | 128 | procedure Nullable<T>.SetDefault(const aDefault: T); 129 | begin 130 | fDefault := aDefault; 131 | fInitDefault := 'I'; 132 | if IsNull then 133 | fValue := aDefault; 134 | end; 135 | 136 | procedure Nullable<T>.SetValue(const aValue: T); 137 | begin 138 | fInitValue := 'I'; 139 | fValue := aValue; 140 | end; 141 | 142 | class operator Nullable<T>.Implicit(a: Nullable<T>): T; 143 | begin 144 | Result := a.Value; 145 | end; 146 | 147 | class operator Nullable<T>.Implicit(a: T): Nullable<T>; 148 | begin 149 | Result.Value := a; 150 | end; 151 | 152 | class operator Nullable<T>.Implicit(a: Pointer): Nullable<T>; 153 | begin 154 | if not System.Assigned(a) then 155 | Result.ClearValue 156 | else 157 | raise EInvalidOperationHashLibException.CreateRes 158 | (@SCannotAssignPointerToNullable); 159 | end; 160 | 161 | // got the idea from Andreas Hausladen 162 | class function Nullable<T>.CastBack(const aValue): T; 163 | begin 164 | Result := T(aValue); 165 | end; 166 | 167 | class function Nullable<T>.AddInt64(const aInt64, bInt64): T; 168 | var 169 | Value: Int64; 170 | begin 171 | Value := Int64(aInt64) + Int64(bInt64); 172 | Result := CastBack(Value); 173 | end; 174 | 175 | class function Nullable<T>.AddFloat(const aFloat, bFloat): T; 176 | var 177 | Value: Double; 178 | begin 179 | Value := Double(aFloat) + Double(bFloat); 180 | Result := CastBack(Value); 181 | end; 182 | 183 | class function Nullable<T>.AddString(const aString, bString): T; 184 | var 185 | Value: String; 186 | begin 187 | Value := String(aString) + String(bString); 188 | Result := CastBack(Value); 189 | end; 190 | 191 | class function Nullable<T>.NewAddInt(const aInt, bInt): T; 192 | var 193 | Value: Int32; 194 | begin 195 | Value := Int32(aInt) + Int32(bInt); 196 | Result := CastBack(Value); 197 | end; 198 | 199 | class operator Nullable<T>.Add(a, b: Nullable<T>): Nullable<T>; 200 | var 201 | info: PTypeInfo; 202 | begin 203 | if a.IsNull or b.IsNull then 204 | Result.ClearValue 205 | else 206 | begin 207 | info := TypeInfo(T); 208 | case info^.Kind of 209 | tkInteger: 210 | Result.Value := NewAddInt(a.fValue, b.fValue); 211 | tkFloat: 212 | Result.Value := AddFloat(a.fValue, b.fValue); 213 | tkString: 214 | Result.Value := AddString(a.fValue, b.fValue); 215 | tkInt64: 216 | Result.Value := AddInt64(a.fValue, b.fValue); 217 | end; 218 | end; 219 | end; 220 | 221 | procedure Nullable<T>.ClearValue; 222 | begin 223 | fInitValue := ''; 224 | end; 225 | 226 | constructor Nullable<T>.Create(const aValue: T); 227 | begin 228 | SetValue(aValue); 229 | end; 230 | 231 | constructor Nullable<T>.Create(const aValue, aDefault: T); 232 | begin 233 | SetValue(aValue); 234 | SetDefault(aDefault); 235 | end; 236 | 237 | class operator Nullable<T>.Explicit(aValue: Nullable<T>): T; 238 | begin 239 | Result := aValue.Value; 240 | end; 241 | 242 | end. 243 | --------------------------------------------------------------------------------