edit
[c11concurrency-benchmarks.git] / mabain / examples / test_key.h
1 /**
2  * Copyright (C) 2017 Cisco Inc.
3  *
4  * This program is free software: you can redistribute it and/or  modify
5  * it under the terms of the GNU General Public License, version 2,
6  * as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15  */
16
17 // @author Changxue Deng <chadeng@cisco.com>
18
19 #include <openssl/sha.h>
20
21 #define MABAIN_TEST_KEY_TYPE_INT        0
22 #define MABAIN_TEST_KEY_TYPE_SHA_128    1
23 #define MABAIN_TEST_KEY_TYPE_SHA_256    2
24 #define MABAIN_TEST_KEY_BUFF_SIZE       1024
25
26 namespace {
27
28 class TestKey
29 {
30 public:
31     TestKey(int ktype) {
32         key_type = ktype;
33         key_buff[0] = '\0';
34     }
35     ~TestKey() {
36     }
37     const char* get_key(int key) {
38         switch(key_type) {
39             case MABAIN_TEST_KEY_TYPE_INT:
40                 snprintf(key_buff, MABAIN_TEST_KEY_BUFF_SIZE, "%d", key);
41                 break;
42             case MABAIN_TEST_KEY_TYPE_SHA_128:
43                 {
44                     unsigned char hash[SHA_DIGEST_LENGTH];
45                     SHA_CTX sha1;
46                     SHA1_Init(&sha1);
47                     SHA1_Update(&sha1, (unsigned char*)&key, 4);
48                     SHA1_Final(hash, &sha1);
49                     int i = 0;
50                     for(i = 0; i < SHA_DIGEST_LENGTH; i++)
51                     {
52                         sprintf(key_buff + (i * 2), "%02x", hash[i]);
53                     }
54                     key_buff[32] = 0;
55                 }
56                 break;
57             case MABAIN_TEST_KEY_TYPE_SHA_256:
58                 {
59                     unsigned char hash[SHA256_DIGEST_LENGTH];
60                     SHA256_CTX sha256;
61                     SHA256_Init(&sha256);
62                     SHA256_Update(&sha256, (unsigned char*)&key, 4);
63                     SHA256_Final(hash, &sha256);
64                     int i = 0;
65                     for(i = 0; i < SHA256_DIGEST_LENGTH; i++)
66                     {
67                         sprintf(key_buff + (i * 2), "%02x", hash[i]);
68                     }
69                     key_buff[64] = 0;
70                 }
71                 break;
72             default:
73                 abort();
74         }
75         return (const char *) key_buff;
76     }
77
78 private:
79     int key_type;
80     char key_buff[MABAIN_TEST_KEY_BUFF_SIZE];
81 };
82
83 }