<div dir="ltr"><div class="gmail_extra">A small improvement:</div><div class="gmail_extra"><br></div><div class="gmail_extra"><div class="gmail_extra">uses</div><div class="gmail_extra"> SHA1;</div><div class="gmail_extra">
<br></div><div class="gmail_extra">function SHA1Raw(const ABuffer; const ABufferLength: PtrUInt): string;</div><div class="gmail_extra">var</div><div class="gmail_extra"> I: Byte;</div><div class="gmail_extra"> VBytes : TBytes;</div>
<div class="gmail_extra"> VDigest: TSHA1Digest;</div><div class="gmail_extra"> VContext: TSHA1Context;</div><div class="gmail_extra">begin</div><div class="gmail_extra"> Result := '';</div><div class="gmail_extra">
SHA1Init(VContext);</div><div class="gmail_extra"> SHA1Update(VContext, ABuffer, ABufferLength);</div><div class="gmail_extra"> SHA1Final(VContext, VDigest);</div><div class="gmail_extra"> SetLength(VBytes, 20);</div>
<div class="gmail_extra"> Move(VDigest, VBytes[0], 20);</div><div class="gmail_extra"> for I := 0 to 19 do</div><div class="gmail_extra"> Result := Result + Char(VBytes[I]);</div><div class="gmail_extra">end;</div><div class="gmail_extra">
<br></div><div class="gmail_extra">function HMACSHA1(const AKey, AMessage: string): string;</div><div class="gmail_extra">const</div><div class="gmail_extra"> BLOCK_SIZE = 64;</div><div class="gmail_extra">var</div><div class="gmail_extra">
I: Byte;</div><div class="gmail_extra"> VKey: string;</div><div class="gmail_extra"> VLenght: PtrUInt;</div><div class="gmail_extra"> VOPad, VIPad: string;</div><div class="gmail_extra">begin</div><div class="gmail_extra">
VLenght := Length(AKey);</div><div class="gmail_extra"> if VLenght > BLOCK_SIZE then</div><div class="gmail_extra"> begin</div><div class="gmail_extra"> SetLength(VKey, BLOCK_SIZE);</div><div class="gmail_extra">
FillChar(Pointer(VKey)^, BLOCK_SIZE, #0);</div><div class="gmail_extra"> VKey := SHA1Raw(Pointer(AKey)^, VLenght) + VKey;</div><div class="gmail_extra"> end</div><div class="gmail_extra"> else</div><div class="gmail_extra">
begin</div><div class="gmail_extra"> SetLength(VKey, BLOCK_SIZE - VLenght);</div><div class="gmail_extra"> FillChar(Pointer(VKey)^, BLOCK_SIZE - VLenght, #0);</div><div class="gmail_extra"> VKey := AKey + VKey;</div>
<div class="gmail_extra"> end;</div><div class="gmail_extra"> SetLength(VOPad, BLOCK_SIZE);</div><div class="gmail_extra"> FillChar(Pointer(VOPad)^, BLOCK_SIZE, $5c);</div><div class="gmail_extra"> SetLength(VIPad, BLOCK_SIZE);</div>
<div class="gmail_extra"> FillChar(Pointer(VIPad)^, BLOCK_SIZE, $36);</div><div class="gmail_extra"> for I := 1 to BLOCK_SIZE do</div><div class="gmail_extra"> begin</div><div class="gmail_extra"> VOPad[I] := Char(Byte(VOPad[I]) xor Byte(VKey[I]));</div>
<div class="gmail_extra"> VIPad[I] := Char(Byte(VIPad[I]) xor Byte(VKey[I]));</div><div class="gmail_extra"> end;</div><div class="gmail_extra"> VIPad := VIPad + AMessage;</div><div class="gmail_extra"> Result := SHA1Print(SHA1String(VOPad + SHA1Raw(Pointer(VIPad)^, Length(VIPad))));</div>
<div class="gmail_extra">end;</div><div><br></div>-- <br>Silvio Clécio<br>My public projects - <a href="http://github.com/silvioprog" target="_blank">github.com/silvioprog</a>
</div></div>