[fpc-pascal] HMAC_SHA1 and FPC
silvioprog
silvioprog at gmail.com
Sun Mar 24 14:54:09 CET 2013
A small improvement:
uses
SHA1;
function SHA1Raw(const ABuffer; const ABufferLength: PtrUInt): string;
var
I: Byte;
VBytes : TBytes;
VDigest: TSHA1Digest;
VContext: TSHA1Context;
begin
Result := '';
SHA1Init(VContext);
SHA1Update(VContext, ABuffer, ABufferLength);
SHA1Final(VContext, VDigest);
SetLength(VBytes, 20);
Move(VDigest, VBytes[0], 20);
for I := 0 to 19 do
Result := Result + Char(VBytes[I]);
end;
function HMACSHA1(const AKey, AMessage: string): string;
const
BLOCK_SIZE = 64;
var
I: Byte;
VKey: string;
VLenght: PtrUInt;
VOPad, VIPad: string;
begin
VLenght := Length(AKey);
if VLenght > BLOCK_SIZE then
begin
SetLength(VKey, BLOCK_SIZE);
FillChar(Pointer(VKey)^, BLOCK_SIZE, #0);
VKey := SHA1Raw(Pointer(AKey)^, VLenght) + VKey;
end
else
begin
SetLength(VKey, BLOCK_SIZE - VLenght);
FillChar(Pointer(VKey)^, BLOCK_SIZE - VLenght, #0);
VKey := AKey + VKey;
end;
SetLength(VOPad, BLOCK_SIZE);
FillChar(Pointer(VOPad)^, BLOCK_SIZE, $5c);
SetLength(VIPad, BLOCK_SIZE);
FillChar(Pointer(VIPad)^, BLOCK_SIZE, $36);
for I := 1 to BLOCK_SIZE do
begin
VOPad[I] := Char(Byte(VOPad[I]) xor Byte(VKey[I]));
VIPad[I] := Char(Byte(VIPad[I]) xor Byte(VKey[I]));
end;
VIPad := VIPad + AMessage;
Result := SHA1Print(SHA1String(VOPad + SHA1Raw(Pointer(VIPad)^,
Length(VIPad))));
end;
--
Silvio Clécio
My public projects - github.com/silvioprog
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20130324/d20e2c39/attachment.html>
More information about the fpc-pascal
mailing list