mageec  0.1.0
MAchine Guided Energy Efficient Compilation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
hasher.cpp
Go to the documentation of this file.
1 /* Generic Blob Hasher
2  Copyright (C) 2013, 2014 Embecosm Limited and University of Bristol
3 
4  This file is part of MAGEEC.
5 
6  This program is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 
20 #include "mageec/mageec.h"
21 #include "gcrypt.h"
22 
23 uint64_t mageec::hash_data(const void *data, unsigned long size)
24 {
25  gcry_md_hd_t handle = NULL;
26  gcry_error_t err = 0;
27  unsigned char *hash = NULL;
28  uint64_t finalhash;
29 
30  err = gcry_md_open (&handle, GCRY_MD_SHA256, 0);
31  if (err != 0)
32  return 0;
33 
34  gcry_md_write(handle, data, size);
35  hash = gcry_md_read(handle, GCRY_MD_SHA256);
36  if (hash == NULL)
37  return 0;
38 
39  /* XOR each 64-bit block to generate 64-bit hash */
40  finalhash = static_cast<uint64_t>(hash[0] ^ hash[8] ^ hash[16] ^ hash[24]) << 56;
41  finalhash |= static_cast<uint64_t>(hash[1] ^ hash[9] ^ hash[17] ^ hash[25]) << 48;
42  finalhash |= static_cast<uint64_t>(hash[2] ^ hash[10] ^ hash[18] ^ hash[26]) << 40;
43  finalhash |= static_cast<uint64_t>(hash[3] ^ hash[11] ^ hash[19] ^ hash[27]) << 32;
44  finalhash |= static_cast<uint64_t>(hash[4] ^ hash[12] ^ hash[20] ^ hash[28]) << 24;
45  finalhash |= static_cast<uint64_t>(hash[5] ^ hash[13] ^ hash[21] ^ hash[29]) << 16;
46  finalhash |= static_cast<uint64_t>(hash[6] ^ hash[14] ^ hash[22] ^ hash[30]) << 8;
47  finalhash |= static_cast<uint64_t>(hash[7] ^ hash[15] ^ hash[23] ^ hash[31]);
48 
49  return finalhash;
50 }