<div dir="ltr">2013/3/23 Tony Whyman <span dir="ltr"><<a href="mailto:tony.whyman@mccallumwhyman.com" target="_blank">tony.whyman@mccallumwhyman.com</a>></span><br><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<div text="#000000" bgcolor="#FFFFFF">
Silvio,<br>
<br>
I had the same requirement for an HMAC and used the DCP library:<br>
<br>
<a href="http://wiki.freepascal.org/DCPcrypt" target="_blank">http://wiki.freepascal.org/DCPcrypt</a><br>
<br>
I then used the following code snippet to generate the HMAC<br>
<br>
Regards<br>
<br>
Tony Whyman<br>
MWA<br>
<br>
unit hmac;<br>
<br>
{$mode objfpc}{$H+}<br>
<br>
interface<br>
<br>
uses<br>
Classes, SysUtils;<br>
<br>
procedure MakeHMAC(text: string; var seed: LongInt; var hash:
string);<br>
function ValidateHMAC(text: string; seed: LongInt; hmac: string):
boolean;<br>
<br>
implementation<br>
<br>
uses DCPsha1;<br>
<br>
function GenerateHash(seed: longint; data: string):string;<br>
var b1, b2, b3, b4: byte;<br>
q: integer;<br>
sha1: TDCP_sha1;<br>
buffer: PChar;<br>
memsize: integer;<br>
len: integer;<br>
begin<br>
len := Length(data);<br>
b1 := seed mod 256;<br>
q := seed div 256;<br>
b2 := q mod 256;<br>
q := q div 256;<br>
b3 := q mod 256;<br>
b4 := q div 256;<br>
<br>
sha1 := TDCP_sha1.Create(nil);<br>
try<br>
sha1.Init;<br>
memsize := len + 4;<br>
buffer := SysGetMem(memsize);<br>
try<br>
Move(b1,buffer^,1);<br>
Move(b2,(buffer+1)^,1);<br>
Move(b3,(buffer+2)^,1);<br>
Move(b4,(buffer+3)^,1);<br>
Move(data[1],(buffer+4)^,len);<br>
SHA1.Update(buffer^,len+4);<br>
setlength(Result,20);<br>
SHA1.Final(Result[1]);<br>
finally<br>
SysFreeMem(buffer);<br>
end;<br>
finally<br>
sha1.free;<br>
end;<br>
end;<br>
<br>
procedure MakeHMAC(text: string; var seed: LongInt;<br>
var hash: string);<br>
begin<br>
Randomize;<br>
seed := Round(Random(MaxLongInt));<br>
hash := GenerateHash(seed,text);<br>
hash := GenerateHash(seed,hash);<br>
end;<br>
<br>
function ValidateHMAC(text: string; seed: LongInt; hmac: string):
boolean;<br>
var hash1, hash2: string;<br>
begin<br>
hash1 := GenerateHash(seed,text);<br>
hash2 := GenerateHash(seed,hash1);<br>
Result := CompareMem(@(hmac[1]),@(hash2[1]),20)<br>
end;<br>
<br>
end.</div></blockquote><div><br></div><div style>Very nice. I will analyze this routine and see if I can remove the dependence of DCP library. Thank you!</div><div><br></div><div>-- </div></div>Silvio Clécio<br>My public projects - <a href="http://github.com/silvioprog" target="_blank">github.com/silvioprog</a>
</div></div>