Advertisement
programmer1997

SHA1 Test

Dec 16th, 2017
459
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.20 KB | None | 0 0
  1. /*
  2. SHA1 tests by Philip Woolford <woolford.philip@gmail.com>
  3. 100% Public Domain
  4.  */
  5.  
  6. #include "sha1.h"
  7. #include "CUnit/Basic.h"
  8. #include "stdio.h"
  9. #include "string.h"
  10.  
  11. #define SUCCESS 0
  12.  
  13. /* The suite initialization function.
  14.  * Returns zero on success, non-zero otherwise.
  15.  */
  16. int init_suite(
  17.     void
  18. )
  19. {
  20.   return 0;
  21. }
  22.  
  23. /* The suite cleanup function.
  24.  * Returns zero on success, non-zero otherwise.
  25.  */
  26. int clean_suite(
  27.     void
  28. )
  29. {
  30.   return 0;
  31. }
  32.  
  33. /* Test Vector 1 */
  34. void testvec1(
  35.     void
  36. )
  37. {
  38.   char const string[] = "abc";
  39.   char const expect[] = "a9993e364706816aba3e25717850c26c9cd0d89d";
  40.   char result[21];
  41.   char hexresult[41];
  42.   size_t offset;
  43.  
  44.   clock_t begin = clock();
  45.  
  46.   /* calculate hash */
  47.   SHA1( result, string, strlen(string) );
  48.  
  49.   clock_t end = clock();
  50.   printf(" Elapsed standard sha1: %f seconds\n", (double)(end - begin) / CLOCKS_PER_SEC);
  51.  
  52.   /* format the hash for comparison */
  53.   for( offset = 0; offset < 20; offset++) {
  54.     sprintf( ( hexresult + (2*offset)), "%02x", result[offset]&0xff);
  55.   }
  56.  
  57.   CU_ASSERT( strncmp(hexresult, expect, 40) == SUCCESS );
  58. }
  59.  
  60. /* Test Vector 2 */
  61. void testvec2(
  62.     void
  63. )
  64. {
  65.   char const string[] = "";
  66.   char const expect[] = "da39a3ee5e6b4b0d3255bfef95601890afd80709";
  67.   char result[21];
  68.   char hexresult[41];
  69.   size_t offset;
  70.  
  71.   clock_t begin = clock();
  72.  
  73.   /* calculate hash */
  74.   SHA1( result, string, strlen(string) );
  75.  
  76.   clock_t end = clock();
  77.   printf(" Elapsed standard sha1: %f seconds\n", (double)(end - begin) / CLOCKS_PER_SEC);
  78.  
  79.   /*format the hash for comparison */
  80.   for( offset = 0; offset < 20; offset++) {
  81.     sprintf( ( hexresult + (2*offset)), "%02x", result[offset]&0xff);
  82.   }
  83.  
  84.   CU_ASSERT( strncmp(hexresult, expect, 40) == SUCCESS );
  85. }
  86.  
  87. /* Test Vector 3 */
  88. void testvec3(
  89.     void
  90. )
  91. {
  92.   char const string[] = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  93.   char const expect[] = "84983e441c3bd26ebaae4aa1f95129e5e54670f1";
  94.   char result[21];
  95.   char hexresult[41];
  96.   size_t offset;
  97.  
  98.   clock_t begin = clock();
  99.  
  100.   /* calculate hash */
  101.   SHA1( result, string, strlen(string) );
  102.  
  103.   clock_t end = clock();
  104.   printf(" Elapsed standard sha1: %f seconds\n", (double)(end - begin) / CLOCKS_PER_SEC);
  105.  
  106.   /* format the hash for comparison */
  107.   for( offset = 0; offset < 20; offset++) {
  108.     sprintf( ( hexresult + (2*offset)), "%02x", result[offset]&0xff);
  109.   }
  110.  
  111.   CU_ASSERT( strncmp(hexresult, expect, 40) == SUCCESS );
  112. }
  113.  
  114. /* Test Vector 4 */
  115. void testvec4(
  116.     void
  117. )
  118. {
  119.   char const string1[] = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghij";
  120.   char const string2[] = "klmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  121.   char const expect[] = "a49b2446a02c645bf419f995b67091253a04a259";
  122.   unsigned char result[21];
  123.   char hexresult[41];
  124.   size_t offset;
  125.   SHA1_CTX ctx;
  126.  
  127.   clock_t begin = clock();  
  128.  
  129.   /* calculate hash */
  130.   SHA1Init(&ctx);
  131.   SHA1Update( &ctx, (unsigned char const *)string1, strlen(string1) );
  132.   SHA1Update( &ctx, (unsigned char const *)string2, strlen(string2) );
  133.   SHA1Final(result, &ctx);
  134.  
  135.   clock_t end = clock();
  136.   printf(" Elapsed double update sha1: %f seconds\n", (double)(end - begin) / CLOCKS_PER_SEC);
  137.  
  138.   /* format the hash for comparison */
  139.   for( offset = 0; offset < 20; offset++) {
  140.     sprintf( ( hexresult + (2*offset)), "%02x", result[offset]&0xff);
  141.   }
  142.  
  143.   CU_ASSERT( strncmp(hexresult, expect, 40) == SUCCESS );
  144. }
  145.  
  146. /* Test Vector 5 */
  147. void testvec5(
  148.     void
  149. )
  150. {
  151.   char string[1000001];
  152.   char const expect[] = "34aa973cd4c4daa4f61eeb2bdbad27316534016f";
  153.   char result[21];
  154.   char hexresult[41];
  155.   int iterator;
  156.   size_t offset;
  157.  
  158.   /* generate string */
  159.   for( iterator = 0; iterator < 1000000; iterator++) {
  160.     string[iterator] = 'a';
  161.   }
  162.   string[1000000] = '\0';
  163.  
  164.   clock_t begin = clock();  
  165.  
  166.   /* calculate hash */
  167.   SHA1( result, string, strlen(string) );
  168.  
  169.   clock_t end = clock();
  170.   printf(" Elapsed standard sha1: %f seconds\n", (double)(end - begin) / CLOCKS_PER_SEC);
  171.  
  172.   /* format the hash for comparison */
  173.   for( offset = 0; offset < 20; offset++) {
  174.     sprintf( ( hexresult + (2*offset)), "%02x", result[offset]&0xff);
  175.   }
  176.  
  177.   CU_ASSERT( strncmp(hexresult, expect, 40) == SUCCESS );
  178. }
  179.  
  180. /* Test Vector 6 */
  181. void testvec6(
  182.     void
  183. )
  184. {
  185.   char const string[] = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmno";
  186.   char const expect[] = "7789f0c9ef7bfc40d93311143dfbe69e2017f592";
  187.   unsigned char result[21];
  188.   char hexresult[41];
  189.   int iterator;
  190.   size_t offset;
  191.   SHA1_CTX ctx;
  192.  
  193.   clock_t begin = clock();
  194.  
  195.   /* calculate hash */
  196.   SHA1Init(&ctx);
  197.   for ( iterator = 0; iterator < 16777216; iterator++) {
  198.     SHA1Update( &ctx, (unsigned char const *)string, strlen(string) );
  199.   }
  200.   SHA1Final(result, &ctx);
  201.  
  202.   clock_t end = clock();
  203.   printf(" Elapsed complex sha1: %f seconds\n", (double)(end - begin) / CLOCKS_PER_SEC);
  204.    
  205.   /* format the hash for comparison */
  206.   for( offset = 0; offset < 20; offset++) {
  207.     sprintf( ( hexresult + (2*offset)), "%02x", result[offset]&0xff);
  208.   }
  209.  
  210.   CU_ASSERT( strncmp(hexresult, expect, 40) == SUCCESS );
  211. }
  212.  
  213. int main(
  214.     void
  215. )
  216. {
  217.   CU_pSuite pSuite = NULL;
  218.  
  219.   /* initialize the CUnit test registry */
  220.   if (CUE_SUCCESS != CU_initialize_registry())
  221.     return CU_get_error();
  222.  
  223.   /* add a suite to the registry */
  224.   pSuite = CU_add_suite("http://www.di-mgt.com.au/sha_testvectors.html", init_suite, clean_suite);
  225.   if (NULL == pSuite) {
  226.     CU_cleanup_registry();
  227.     return CU_get_error();
  228.   }
  229.  
  230.   /* add the tests to the suite */
  231.   if ((NULL == CU_add_test(pSuite, "Test of Test Vector 1", testvec1)) ||
  232.      (NULL == CU_add_test(pSuite, "Test of Test Vector 2", testvec2)) ||
  233.      (NULL == CU_add_test(pSuite, "Test of Test Vector 3", testvec3)) ||
  234.      (NULL == CU_add_test(pSuite, "Test of Test Vector 4", testvec4)) ||
  235.      (NULL == CU_add_test(pSuite, "Test of Test Vector 5", testvec5)) ||
  236.      (NULL == CU_add_test(pSuite, "Test of Test Vector 6", testvec6)))
  237.   {
  238.     CU_cleanup_registry();
  239.     return CU_get_error();
  240.   }
  241.  
  242.   /* Run all tests using the CUnit Basic interface */
  243.   CU_basic_set_mode(CU_BRM_VERBOSE);
  244.   CU_basic_run_tests();
  245.   CU_cleanup_registry();
  246.   return CU_get_error();
  247. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement