• 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 #include "crt_util_safe_x.h"
9 
ToHashStr(char * dst,const unsigned char * src,size_t src_len)10 static void ToHashStr (char* dst, const unsigned char* src, size_t src_len) {
11   for (size_t i = 0; i < src_len; ++i) {
12     WelsSnprintf (&dst[i * 2], 3, "%.2x", src[i]);
13   }
14   dst[src_len * 2] = '\0';
15 }
16 
CompareHash(const unsigned char * digest,const char * hashStr)17 inline void CompareHash (const unsigned char* digest, const char* hashStr) {
18   char hashStrCmp[SHA_DIGEST_LENGTH * 2 + 1];
19   ToHashStr (hashStrCmp, digest, SHA_DIGEST_LENGTH);
20   EXPECT_STREQ (hashStr, hashStrCmp);
21 }
22 
CompareHashAnyOf(const unsigned char * digest,const char * const hashStr[],size_t nHashStr)23 inline void CompareHashAnyOf (const unsigned char* digest, const char* const hashStr[], size_t nHashStr) {
24   char hashStrCmp[SHA_DIGEST_LENGTH * 2 + 1];
25   ToHashStr (hashStrCmp, digest, SHA_DIGEST_LENGTH);
26   for (size_t i = 0; i < nHashStr && hashStr[i]; ++i) {
27     if (0 == strcmp (hashStr[i], hashStrCmp))
28       return;
29   }
30   // No match found. Compare to first hash so as to produce a grepable failure.
31   EXPECT_STREQ (hashStr[0], hashStrCmp);
32 }
33 
34 #endif //__HASHFUNCTIONS_H__
35