|
|
|
|
|
CryptoAPI CryptoAPI unit provides common interface for using the algorithms. HashContext:
THashContext = record
IntData: Pointer; {Reserved for internal use}
HashType: LongWord; {Hash type}
lParam: LongWord; {First Param}
wParam: LongWord; {Second Param}
end;
Availble hash types: Low-level functions:
uses
CryptoAPI;
<......>
procedure TestHash;
var
buf: array[0..2] of Char; //Source buffer
ctx: THashContext; //Hash context
ret: LongWord; //Error status
S: String; //String for hash
begin
buf[0] := 'a'; buf[1] := 'b'; buf[2] := 'c'; //store abc in the buffer
HashInit(@ctx, HASH_SHA1); //Initialize
HashUpdate(@ctx, @buf, 3); //Update the whole buffer
{
equivalent to:
HashUpdate(@ctx, @(buf[0]), 1); //Update first byte ('a')
HashUpdate(@ctx, @(buf[1]), 1); //Update second byte ('b')
HashUpdate(@ctx, @(buf[2]), 1); //Update third byte ('c')
equivalent to:
HashUpdate(@ctx, @(buf[0]), 1); //Update first byte ('a')
HashUpdate(@ctx, @(buf[1]), 2); //Update last two byts ('bc')
}
HashFinal(@ctx, S); //Finalize
Memo1.Lines.Add(S); //in this case S = a9993e364706816aba3e25717850c26c9cd0d89d, this is a hash of 'abc'
end;
High-level functions:
uses
CryptoAPI;
<......>
procedure TestHash2;
var
S: String;
ret: LongWord;
begin
ret := HashStr(HASH_SHA1, 'abc', S);
if ret <> HASH_NOERROR then
Memo1.Lines.Add(HashErrorToStr(ret))
else
Memo1.Lines.Add(S);
end;
Misc. functions and constants:
uses
CryptoAPI;
<......>
procedure DoSomething;
var
i, HAv: LongWord;
EnumArray: array[0..HASH_MAX_TYPES - 1] of LongWord;
begin
HAv := EnumHashTypes(@EnumArray, SizeOf(EnumArray));
for i := 0 to HAv - 1 do
begin
{
Do something with each HashType (EnumArray[i])
}
end;
end;
|
|
HashLib! testing I've made a unit(HashTest.pas) which helps with testing the whole library for proper working. function HashTestLibrary: LongWord; This function verifies every hash algorithm and returns HASH_NOERROR if there are no errors, otherwise you can understand happend error by calling HashErrorToStr function with the return value. function HashTestHash(HashType: LongWord): LongWord; This function verifies specified algorithm and returns error status as in HashTestLibrary. It's strongly recommended to test algorithms before using them. |
|
Size & perfomance issues You can remove {$DEFINE INCLUDE_...$} lines from CryptoAPI unit if you don't use this algorithm, it will strongly reduce filesize, also pay attention on comments in source files: some files contain {$DEFINE USE_ASM} line in the header, it's possible to remove theese lines but it will decrease the overal perfomance of some algorithms. |
|
|
|
|
HashLib! 1.03
(C) Alex Demchenko, 2002, Moldova, Chishinev.