IdTestSHA1Hash.pas 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. unit IdTestSHA1Hash;
  2. {
  3. refer http://www.faqs.org/rfcs/rfc3174.html
  4. examples are given in section 7.1
  5. }
  6. interface
  7. uses
  8. IdTest;
  9. type
  10. TIdTestSHA1Hash = class(TIdTest)
  11. private
  12. procedure CheckHash(const aStr,aExpect:string);
  13. published
  14. procedure TestRFC;
  15. end;
  16. implementation
  17. uses
  18. IdHashSHA1,
  19. IdObjs,
  20. IdSys;
  21. procedure TIdTestSHA1Hash.TestRFC;
  22. {
  23. Note: For test 3 and 4, we were getting failures with the checksums listed in
  24. RFC 3174.
  25. I did some testing with the following:
  26. Microsoft's fciv
  27. (File Checksum Integrity Verifier version 2.05).
  28. and from:
  29. SlavaSoft Optimizing Checksum Utility - fsum 2.51
  30. Implemented using SlavaSoft QuickHash Library <www.slavasoft.com>
  31. Copyright (C) SlavaSoft Inc. 1999-2004. All rights reserved.
  32. and both returned the same checks8um and that didn't match the RFC.
  33. I suspect that the RFC was wrong and that FCIV, lavaSoft Optimizing Checksum Utility,
  34. and our code are correct.
  35. }
  36. const
  37. s1='abc';
  38. e1='A9993E364706816ABA3E25717850C26C9CD0D89D';
  39. s2='abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq';
  40. e2='84983E441C3BD26EBAAE4AA1F95129E5E54670F1';
  41. s3='a';
  42. // e3='34AA973CD4C4DAA4F61EEB2BDBAD27316534016F';
  43. e3='86F7E437FAA5A7FCE15D1DDCB9EAEAEA377667B8';
  44. s4='0123456701234567012345670123456701234567012345670123456701234567';
  45. // e4='DEA356A2CDDD90C7A7ECEDC5EBB563934F460452';
  46. e4='E0C094E867EF46C350EF54A7F59DD60BED92AE83';
  47. begin
  48. CheckHash(s1,e1);
  49. CheckHash(s2,e2);
  50. CheckHash(s3,e3);
  51. CheckHash(s4,e4);
  52. end;
  53. procedure TIdTestSHA1Hash.CheckHash(const aStr, aExpect: string);
  54. var
  55. LH : TIdHashSHA1;
  56. LStrm : TIdStream;
  57. s : String;
  58. begin
  59. LH := TIdHashSHA1.Create;
  60. try
  61. s := lh.HashStringAsHex(aStr);
  62. Assert(Sys.UpperCase(s)=aExpect,aStr);
  63. finally
  64. Sys.FreeAndNil(LH);
  65. end;
  66. end;
  67. initialization
  68. TIdTest.RegisterTest(TIdTestSHA1Hash);
  69. end.