Advertisement
Guest User

Untitled

a guest
Mar 24th, 2013
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 2.22 KB | None | 0 0
  1. (*
  2.   HMAC plugin.
  3.   Copyright (C) 2012-2014 Silvio Clecio.
  4.  
  5.   Please see the LICENSE file.
  6. *)
  7.  
  8. unit HMAC;
  9.  
  10. {$mode objfpc}{$H+}
  11.  
  12. interface
  13.  
  14. uses
  15.   SysUtils, SHA1;
  16.  
  17. const
  18.   SHA1_BLOCK_SIZE = 64;
  19.   SHA1_BLOCK_COUNT = 20;
  20.   SHA1_HEX_TABLE: array[0..15] of Char = '0123456789abcdef';
  21.  
  22. function SHA1Raw(const ABuffer; const ABufferLength: PtrUInt): string;
  23. { TODO: function HMACMD5(const AKey, AMessage: string): string; }
  24. function HMACSHA1(const AKey, AMessage: string): string;
  25.  
  26. implementation
  27.  
  28. function SHA1Raw(const ABuffer; const ABufferLength: PtrUInt): string;
  29. var
  30.   I: Byte;
  31.   VBytes : TBytes;
  32.   VDigest: TSHA1Digest;
  33.   VContext: TSHA1Context;
  34. begin
  35.   Result := '';
  36.   SHA1Init(VContext);
  37.   SHA1Update(VContext, ABuffer, ABufferLength);
  38.   SHA1Final(VContext, VDigest);
  39.   SetLength(VBytes, SHA1_BLOCK_COUNT);
  40.   Move(VDigest, VBytes[0], SHA1_BLOCK_COUNT);
  41.   for I := 0 to SHA1_BLOCK_COUNT - 1 do
  42.     Result := Result + Char(VBytes[I]);
  43. end;
  44.  
  45. function HMACSHA1(const AKey, AMessage: string): string;
  46. var
  47.   I: Byte;
  48.   VDest: PChar;
  49.   VLenght: PtrUInt;
  50.   VDigest: TSHA1Digest;
  51.   VKey, VOPad, VIPad: string;
  52. begin
  53.   VLenght := Length(AKey);
  54.   if VLenght > SHA1_BLOCK_SIZE then
  55.   begin
  56.     SetLength(VKey, SHA1_BLOCK_SIZE);
  57.     FillChar(Pointer(VKey)^, SHA1_BLOCK_SIZE, #0);
  58.     VKey := SHA1Raw(Pointer(AKey)^, VLenght) + VKey;
  59.   end
  60.   else
  61.   begin
  62.     SetLength(VKey, SHA1_BLOCK_SIZE - VLenght);
  63.     FillChar(Pointer(VKey)^, SHA1_BLOCK_SIZE - VLenght, #0);
  64.     VKey := AKey + VKey;
  65.   end;
  66.   SetLength(VOPad, SHA1_BLOCK_SIZE);
  67.   FillChar(Pointer(VOPad)^, SHA1_BLOCK_SIZE, $5c);
  68.   SetLength(VIPad, SHA1_BLOCK_SIZE);
  69.   FillChar(Pointer(VIPad)^, SHA1_BLOCK_SIZE, $36);
  70.   for I := 1 to SHA1_BLOCK_SIZE do
  71.   begin
  72.     VOPad[I] := Char(Byte(VOPad[I]) xor Byte(VKey[I]));
  73.     VIPad[I] := Char(Byte(VIPad[I]) xor Byte(VKey[I]));
  74.   end;
  75.   VIPad := VIPad + AMessage;
  76.   VDigest := SHA1String(VOPad + SHA1Raw(Pointer(VIPad)^, Length(VIPad)));
  77.   SetLength(Result, SHA1_BLOCK_COUNT * 2);
  78.   VDest := Pointer(Result);
  79.   for I := 0 to SHA1_BLOCK_COUNT - 1 do
  80.   begin
  81.     VDest[0] := SHA1_HEX_TABLE[(VDigest[I] shr 4) and 15];
  82.     VDest[1] := SHA1_HEX_TABLE[VDigest[I] and 15];
  83.     Inc(VDest, 2);
  84.   end;
  85. end;
  86.  
  87. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement