mdtest.pas 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. {
  2. This file is part of the Free Pascal packages.
  3. Copyright (c) 1999-2014 by the Free Pascal development team
  4. Tests MD2, MD4 and MD5 hashes.
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. program mdtest;
  12. {$mode objfpc}{$h+}
  13. uses
  14. {$ifdef unix}
  15. cwstring,
  16. {$endif}
  17. SysUtils, md5;
  18. const
  19. Suite: array[1..7] of string = (
  20. '',
  21. 'a',
  22. 'abc',
  23. 'message digest',
  24. 'abcdefghijklmnopqrstuvwxyz',
  25. 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',
  26. '12345678901234567890123456789012345678901234567890123456789012345678901234567890'
  27. );
  28. Results: array[TMDVersion, Low(Suite)..High(Suite)] of string = (
  29. // MD_VERSION_2
  30. ('8350e5a3e24c153df2275c9f80692773',
  31. '32ec01ec4a6dac72c0ab96fb34c0b5d1',
  32. 'da853b0d3f88d99b30283a69e6ded6bb',
  33. 'ab4f496bfb2a530b219ff33031fe06b0',
  34. '4e8ddff3650292ab5a4108c3aa47940b',
  35. 'da33def2a42df13975352846c30338cd',
  36. 'd5976f79d83d3a0dc9806c3c66f3efd8'),
  37. // MD_VERSION_4
  38. ('31d6cfe0d16ae931b73c59d7e0c089c0',
  39. 'bde52cb31de33e46245e05fbdbd6fb24',
  40. 'a448017aaf21d8525fc10ae87aa6729d',
  41. 'd9130a8164549fe818874806e1c7014b',
  42. 'd79e1c308aa5bbcdeea8ed63df412da9',
  43. '043f8582f241db351ce627e153e7f0e4',
  44. 'e33b4ddc9c38f2199c3e7b164fcc0536'),
  45. // MD_VERSION_5
  46. ('d41d8cd98f00b204e9800998ecf8427e',
  47. '0cc175b9c0f1b6a831c399e269772661',
  48. '900150983cd24fb0d6963f7d28e17f72',
  49. 'f96b697d7cb7938d525a2f31aaf161d0',
  50. 'c3fcd3d76192e4007dfb496cca67e13b',
  51. 'd174ab98d277d9f5a5611c2c9f419d9f',
  52. '57edf4a22be3c955ac49da2e2107b67a')
  53. );
  54. function performTest(const Ver: TMDVersion; const Verbose: boolean): boolean;
  55. // Runs test and returns success or failure
  56. var
  57. I: Integer;
  58. S: String;
  59. begin
  60. result := false;
  61. for I := Low(Suite) to High(Suite) do
  62. begin
  63. S := LowerCase(MDPrint(MDString(Suite[I], Ver)));
  64. if S = Results[Ver, I] then
  65. result := true;
  66. if Verbose then WriteLn(' "', Suite[I], '" = ', S);
  67. end;
  68. end;
  69. var
  70. i: integer;
  71. begin
  72. i:=0;
  73. Writeln('Executing RFC 1319 test suite ...');
  74. if performTest(MD_VERSION_2,true) then
  75. Write('RFC 1319 test suite passed ')
  76. else
  77. begin
  78. Write('RFC 1319 test suite failed ');
  79. i:=i or 1;
  80. end;
  81. Writeln;
  82. Writeln;
  83. Writeln('Executing RFC 1320 test suite ...');
  84. if performTest(MD_VERSION_4,true) then
  85. Write('RFC 1320 test suite passed ')
  86. else
  87. begin
  88. Write('RFC 1320 test suite failed ');
  89. i:=i or 2;
  90. end;
  91. Writeln;
  92. Writeln;
  93. Writeln('Executing RFC 1321 test suite ...');
  94. if performTest(MD_VERSION_5,true) then
  95. Write('RFC 1321 test suite passed ')
  96. else
  97. begin
  98. Write('RFC 1321 test suite failed ');
  99. i:=i or 4;
  100. end;
  101. Writeln;
  102. Writeln;
  103. halt(i); //halt with error code 0 if everything ok
  104. end.