-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathrinchi_hashing.cpp
More file actions
160 lines (130 loc) · 4.33 KB
/
rinchi_hashing.cpp
File metadata and controls
160 lines (130 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#ifdef MSVC
#pragma region InChI-Trust Licence
/*
* Reaction International Chemical Identifier (RInChI)
* Version 1
* Software version 1.00
* 2022-01-14
*
* The RInChI library and programs are free software developed under the
* auspices of the International Union of Pure and Applied Chemistry (IUPAC).
*
* IUPAC/InChI-Trust Licence No.1.0 for the
* Reaction International Chemical Identifier (RInChI) Software version 1.0
* Copyright (C) IUPAC and InChI Trust Limited
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the IUPAC/InChI Trust InChI Licence No.1.0,
* or any later version.
*
* Please note that this library is distributed WITHOUT ANY WARRANTIES
* whatsoever, whether expressed or implied. See the IUPAC/InChI Trust
* Licence for the International Chemical Identifier (InChI) Software
* ("IUPAC/InChI-Trust InChI Licence No. 1.0" in "LICENCE.TXT")
* for more details.
*
* You should have received a copy of the IUPAC/InChI Trust InChI
* Licence No. 1.0 with this library; if not, please write to:
*
* The InChI Trust
* 8 Cavendish Avenue
* Cambridge CB1 7US
* UK
*
* or email to: alan@inchi-trust.org.
*
*/
#pragma endregion
#endif
#include <sstream>
#include "rinchi_hashing.h"
#include "sha2.h"
#include "ikey_base26.h"
// #define RINCHI_DEBUG_OUTPUT_HASHING 1
#ifdef RINCHI_DEBUG_OUTPUT_HASHING
#include <iostream>
#endif
namespace rinchi {
std::string sha256hex(const std::string& input)
{
unsigned char chksum[32];
sha2_csum((unsigned char*) input.c_str(), (int) input.length(), chksum);
std::ostringstream result;
result << std::hex;
for (int i = 0; i < 32; i++)
result << ((int) chksum[i]);
return result.str();
}
std::string hash04char(const std::string& input)
{
unsigned char chksum[32];
sha2_csum((unsigned char*) input.c_str(), (int) input.length(), chksum);
std::string result;
result += base26_triplet_1(chksum);
result += base26_triplet_2(chksum);
result = result.substr(0, 4);
#ifdef RINCHI_DEBUG_OUTPUT_HASHING
std::cout << "[#04[" << input << "]#] => " << result << std::endl;
#endif
return result;
}
std::string hash10char(const std::string& input)
{
std::string result = hash12char(input).substr(0, 10);
#ifdef RINCHI_DEBUG_OUTPUT_HASHING
std::cout << "[#10[" << input << "]#] => " << result << std::endl;
#endif
return result;
}
std::string hash12char(const std::string& input)
{
unsigned char chksum[32];
sha2_csum((unsigned char*) input.c_str(), (int) input.length(), chksum);
std::string result;
result += base26_triplet_1(chksum);
result += base26_triplet_2(chksum);
result += base26_triplet_3(chksum);
result += base26_triplet_4(chksum);
#ifdef RINCHI_DEBUG_OUTPUT_HASHING
std::cout << "[#12[" << input << "]#] => " << result << std::endl;
#endif
return result;
}
std::string hash14char(const std::string& input)
{
unsigned char chksum[32];
sha2_csum((unsigned char*) input.c_str(), (int) input.length(), chksum);
std::string result;
result += base26_triplet_1(chksum);
result += base26_triplet_2(chksum);
result += base26_triplet_3(chksum);
result += base26_triplet_4(chksum);
result += base26_dublet_for_bits_56_to_64(chksum);
#ifdef RINCHI_DEBUG_OUTPUT_HASHING
std::cout << "[#14[" << input << "]#] => " << result << std::endl;
#endif
return result;
}
std::string hash17char(const std::string& input)
{
unsigned char chksum[32];
sha2_csum((unsigned char*) input.c_str(), (int) input.length(), chksum);
std::string result;
result += base26_triplet_1(chksum);
result += base26_triplet_2(chksum);
result += base26_triplet_3(chksum);
result += base26_triplet_4(chksum);
result += base26_dublet_for_bits_56_to_64(chksum);
// chksum[8] = bits 64.. (bit 64 is used twice)
result += base26_triplet_1(&chksum[8]);
#ifdef RINCHI_DEBUG_OUTPUT_HASHING
std::cout << "[#17[" << input << "]#] => " << result << std::endl;
#endif
return result;
}
const std::string HASH_04_EMPTY_STRING = "UHFF";
const std::string HASH_10_EMPTY_STRING = "UHFFFADPSC";
const std::string HASH_12_EMPTY_STRING = "UHFFFADPSCTJ";
const std::string HASH_14_EMPTY_STRING = "UHFFFADPSCTJAU";
const std::string HASH_17_EMPTY_STRING = "UHFFFADPSCTJAUYIS";
} // end of namespace