IdHash.pas 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. { $HDR$}
  2. {**********************************************************************}
  3. { Unit archived using Team Coherence }
  4. { Team Coherence is Copyright 2002 by Quality Software Components }
  5. { }
  6. { For further information / comments, visit our WEB site at }
  7. { http://www.TeamCoherence.com }
  8. {**********************************************************************}
  9. {}
  10. { $Log: 10177: IdHash.pas
  11. {
  12. { Rev 1.0 2002.11.12 10:40:00 PM czhower
  13. }
  14. unit IdHash;
  15. interface
  16. uses
  17. Classes;
  18. type
  19. TIdHash = class(TObject);
  20. TIdHash16 = class(TIdHash)
  21. public
  22. function HashValue(const ASrc: string): Word; overload;
  23. function HashValue(AStream: TStream): Word; overload; virtual; abstract;
  24. end;
  25. TIdHash32 = class(TIdHash)
  26. public
  27. function HashValue(const ASrc: string): LongWord; overload;
  28. function HashValue(AStream: TStream): LongWord; overload; virtual; abstract;
  29. end;
  30. T4x4LongWordRecord = array [0..3] of LongWord;
  31. TIdHash128 = class(TIdHash)
  32. public
  33. class function AsHex(const AValue: T4x4LongWordRecord): string;
  34. function HashValue(const ASrc: string): T4x4LongWordRecord; overload;
  35. function HashValue(AStream: TStream): T4x4LongWordRecord; overload; virtual; abstract;
  36. end;
  37. implementation
  38. uses
  39. IdGlobal,
  40. SysUtils;
  41. { TIdHash32 }
  42. function TIdHash32.HashValue(const ASrc: string): LongWord;
  43. var
  44. LStream: TIdReadMemoryStream;
  45. begin
  46. LStream := TIdReadMemoryStream.Create; try
  47. LStream.SetPointer(Pointer(ASrc),Length(ASrc));
  48. Result := HashValue(LStream);
  49. finally FreeAndNil(LStream); end;
  50. end;
  51. { TIdHash16 }
  52. function TIdHash16.HashValue(const ASrc: string): Word;
  53. var
  54. LStream: TIdReadMemoryStream;
  55. begin
  56. LStream := TIdReadMemoryStream.Create; try
  57. LStream.SetPointer(Pointer(ASrc),Length(ASrc));
  58. Result := HashValue(LStream);
  59. finally FreeAndNil(LStream); end;
  60. end;
  61. { TIdHash128 }
  62. function TIdHash128.HashValue(const ASrc: string): T4x4LongWordRecord;
  63. var
  64. LStream: TIdReadMemoryStream;
  65. begin
  66. LStream := TIdReadMemoryStream.Create; try
  67. LStream.SetPointer(Pointer(ASrc),Length(ASrc));
  68. Result := HashValue(LStream);
  69. finally FreeAndNil(LStream); end;
  70. end;
  71. class function TIdHash128.AsHex(const AValue: T4x4LongWordRecord): string;
  72. var
  73. P: PChar;
  74. i: Integer;
  75. Begin
  76. P:=PChar(@AValue);
  77. SetString(Result,NIL,4*4*2);//32
  78. for i:=0 to 15 do begin
  79. Result[i*2+1]:=IdHexDigits[ord(P[i]) shr 4];
  80. Result[i*2+2]:=IdHexDigits[ord(P[i]) and $F];
  81. end;//for
  82. end;
  83. end.