• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef __HASHFUNCTIONS_H__
2 #define __HASHFUNCTIONS_H__
3 
4 #include <stdio.h>
5 #include <string.h>
6 #include <gtest/gtest.h>
7 #include "../sha1.h"
8 
ToHashStr(char * dst,const unsigned char * src,size_t src_len)9 static void ToHashStr (char* dst, const unsigned char* src, size_t src_len) {
10   for (size_t i = 0; i < src_len; ++i) {
11     sprintf (&dst[i * 2], "%.2x", src[i]);
12   }
13   dst[src_len * 2] = '\0';
14 }
15 
CompareHash(const unsigned char * digest,const char * hashStr)16 inline void CompareHash (const unsigned char* digest, const char* hashStr) {
17   char hashStrCmp[SHA_DIGEST_LENGTH * 2 + 1];
18   ToHashStr (hashStrCmp, digest, SHA_DIGEST_LENGTH);
19   EXPECT_STREQ (hashStr, hashStrCmp);
20 }
21 
CompareHashAnyOf(const unsigned char * digest,const char * const hashStr[],size_t nHashStr)22 inline void CompareHashAnyOf (const unsigned char* digest, const char* const hashStr[], size_t nHashStr) {
23   char hashStrCmp[SHA_DIGEST_LENGTH * 2 + 1];
24   ToHashStr (hashStrCmp, digest, SHA_DIGEST_LENGTH);
25   for (size_t i = 0; i < nHashStr && hashStr[i]; ++i) {
26     if (0 == strcmp (hashStr[i], hashStrCmp))
27       return;
28   }
29   // No match found. Compare to first hash so as to produce a grepable failure.
30   EXPECT_STREQ (hashStr[0], hashStrCmp);
31 }
32 
33 #endif //__HASHFUNCTIONS_H__
34