Grid 0.7.0
Sha.h
Go to the documentation of this file.
1 /*************************************************************************************
2
3 Grid physics library, www.github.com/paboyle/Grid
4
5 Source file: ./lib/util/Sha.h
6
7 Copyright (C) 2018
8
9 Author: Peter Boyle
10
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License along
22 with this program; if not, write to the Free Software Foundation, Inc.,
23 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24
25 See the full license in the file "LICENSE" in the top level distribution directory
26 *************************************************************************************/
27 /* END LEGAL */
28extern "C" {
29#include <openssl/sha.h>
30#include <openssl/evp.h>
31}
32#ifdef USE_IPP
33#include "ipp.h"
34#endif
35
36#pragma once
37
39{
40public:
41 static inline uint32_t crc32(const void *data, size_t bytes)
42 {
43 return ::crc32(0L,(unsigned char *)data,bytes);
44 }
45
46#ifdef USE_IPP
47 static inline uint32_t crc32c(const void* data, size_t bytes)
48 {
49 uint32_t crc32c = ~(uint32_t)0;
50 ippsCRC32C_8u(reinterpret_cast<const unsigned char *>(data), bytes, &crc32c);
51 ippsSwapBytes_32u_I(&crc32c, 1);
52
53 return ~crc32c;
54 }
55#endif
56
57 template <typename T>
58 static inline std::string sha256_string(const std::vector<T> &hash)
59 {
60 std::stringstream sha;
61 std::string s;
62
63 for(unsigned int i = 0; i < hash.size(); i++)
64 {
65 sha << std::hex << static_cast<unsigned int>(hash[i]);
66 }
67 s = sha.str();
68
69 return s;
70 }
71 static inline std::vector<unsigned char> sha256(const void *data,size_t bytes)
72 {
73 std::vector<unsigned char> hash(SHA256_DIGEST_LENGTH);
74 auto digest = EVP_get_digestbyname("SHA256");
75 EVP_Digest(data, bytes, &hash[0], NULL, digest, NULL);
76 return hash;
77 }
78 static inline std::vector<int> sha256_seeds(const std::string &s)
79 {
80 std::vector<int> seeds;
81 std::vector<unsigned char> uchars = sha256((void *)s.c_str(),s.size());
82 for(int i=0;i<uchars.size();i++) seeds.push_back(uchars[i]);
83 return seeds;
84 }
85};
86
87/*
88int main(int argc,char **argv)
89{
90 std::string s("The quick brown fox jumps over the lazy dog");
91 auto csum = GridChecksum::sha256_seeds(s);
92 std::cout << "SHA256 sum is 0x";
93 for(int i=0;i<csum.size;i++) {
94 std::cout << std::hex << csum[i];
95 }
96 std::cout << std::endl;
97}
98*/
static uint32_t crc32(const void *data, size_t bytes)
Definition Sha.h:41
static std::vector< int > sha256_seeds(const std::string &s)
Definition Sha.h:78
static std::string sha256_string(const std::vector< T > &hash)
Definition Sha.h:58
static std::vector< unsigned char > sha256(const void *data, size_t bytes)
Definition Sha.h:71