Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
2026-06-19 0cfc87bd32822e5ba22d8c2b1a26fa60f63ad7fa
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#include "coder.hpp"
 
namespace Coder {
    typedef uint8_t vect[4];
 
    thread_local uint8_t key[32] =
    {
        0xff, 0xee, 0xdd, 0xcc,
        0xbb, 0xaa, 0x99, 0x88,
        0x77, 0x66, 0x55, 0x44,
        0x33, 0x22, 0x11, 0x00,
        0xf0, 0xf1, 0xf2, 0xf3,
        0xf4, 0xf5, 0xf6, 0xf7,
        0xf8, 0xf9, 0xfa, 0xfb,
        0xfc, 0xfd, 0xfe, 0xff
    }; //Ключ шифрования
 
    thread_local unsigned char Pi[8][16]=
    {
        {1,7,14,13,0,5,8,3,4,15,10,6,9,12,11,2},
        {8,14,2,5,6,9,1,12,15,4,11,0,13,10,3,7},
        {5,13,15,6,9,2,12,10,11,7,8,1,4,3,14,0},
        {7,15,5,10,8,1,6,13,0,9,3,14,11,4,2,12},
        {12,8,2,1,13,4,15,6,7,0,10,5,3,14,9,11},
        {11,3,5,8,2,15,10,13,14,1,7,4,12,9,6,0},
        {6,8,2,3,9,10,5,12,1,14,4,7,11,13,0,15},
        {12,4,6,2,10,5,11,9,14,8,13,7,0,3,15,1}
    }; //Таблица подстановок
 
 
    void Transform_T(const uint8_t *in_data, uint8_t *out_data)
    {
        uint8_t first_part_byte, sec_part_byte;
        int i;
        for (i = 0; i < 4; i++)
        {
            // Извлекаем первую 4-битную часть байта
            first_part_byte = (in_data[i] & 0xf0) >> 4;
            // Извлекаем вторую 4-битную часть байта
            sec_part_byte = (in_data[i] & 0x0f);
            // Выполняем замену в соответствии с таблицей подстановок
            first_part_byte = Pi[i * 2][first_part_byte];
            sec_part_byte = Pi[i * 2 + 1][sec_part_byte];
            // «Склеиваем» обе 4-битные части обратно в байт
            out_data[i] = (first_part_byte << 4) | sec_part_byte;
        }
    }
 
    //Сложение двух двоичных векторов по модулю 2
    void Sum_ABS_2(const uint8_t *a, const uint8_t *b, uint8_t *c)
    {
        int i;
        for (i = 0; i < 4; i++)
            c[i] = a[i]^b[i];
    }
 
    //Сложение двух двоичных векторов по модулю 32
    void Sum_ABS_32(const uint8_t *a, const uint8_t *b, uint8_t *c)
    {
        int i;
        unsigned int internal = 0;
        for (i = 3; i >= 0; i--)
        {
            internal = a[i] + b[i] + (internal >> 8);
            c[i] = internal & 0xff;
        }
    }
 
    //Развертывание ключа
    void Expand_Key(vect* iter_key)
    {
        for(uint8_t i=0; i<8; i++) {
            memcpy(iter_key[i], key + 4*i, 4);
            memcpy(iter_key[i+8], key + 4*i, 4);
            memcpy(iter_key[i+16], key + 4*i, 4);
            memcpy(iter_key[i+24], key + 4*(7-i), 4);
        }
    }
 
    void Transform_g(const uint8_t *k, const uint8_t *a, uint8_t *out_data)
    {
        uint8_t internal[4];
        uint32_t out_data_32;
        // Складываем по модулю 32 правую половину блока с итерационным ключом
        Sum_ABS_32(a, k, internal);
        // Производим нелинейное биективное преобразование результата
        Transform_T(internal, internal);
        // Преобразовываем четырехбайтный вектор в одно 32-битное число
        out_data_32 = internal[0];
        out_data_32 = (out_data_32 << 8) + internal[1];
        out_data_32 = (out_data_32 << 8) + internal[2];
        out_data_32 = (out_data_32 << 8) + internal[3];
        // Циклически сдвигаем все влево на 11 разрядов
        out_data_32 = (out_data_32 << 11)|(out_data_32 >> 21);
        // Преобразовываем 32-битный результат сдвига обратно в 4-байтовый вектор
        out_data[3] = out_data_32;
        out_data[2] = out_data_32 >> 8;
        out_data[1] = out_data_32 >> 16;
        out_data[0] = out_data_32 >> 24;
    }
 
    void Transform_G(const uint8_t *k, const uint8_t *a, uint8_t *out_data)
    {
        uint8_t a_0[4]; // Правая половина блока
        uint8_t a_1[4]; // Левая половина блока
        uint8_t G[4];
 
        int i;
        // Делим 64-битный исходный блок на две части
        for(i = 0; i < 4; i++)
        {
            a_0[i] = a[4 + i];
            a_1[i] = a[i];
        }
 
        // Производим преобразование g
        Transform_g(k, a_0, G);
        // Ксорим результат преобразования g с левой половиной блока
        Sum_ABS_2(a_1, G, G);
 
        for(i = 0; i < 4; i++)
        {
            a_1[i] = a_0[i];
            a_0[i] = G[i];
        }
 
        // Сводим правую и левую части блока в одно целое
        for(i = 0; i < 4; i++)
        {
            out_data[i] = a_1[i];
            out_data[4 + i] = a_0[i];
        }
    }
 
    void Transform_G_Fin(const uint8_t *k, const uint8_t *a, uint8_t *out_data)
    {
        uint8_t a_0[4]; // Правая половина блока
        uint8_t a_1[4]; // Левая половина блока
        uint8_t G[4];
 
        int i;
        // Делим 64-битный исходный блок на две части
        for(i = 0; i < 4; i++)
        {
            a_0[i] = a[4 + i];
            a_1[i] = a[i];
        }
 
        // Производим преобразование g
        Transform_g(k, a_0, G);
        // Ксорим результат преобразования g с левой половиной блока
        Sum_ABS_2(a_1, G, G);
 
        for(i = 0; i < 4; i++)
            a_1[i] = G[i];
 
        for(i = 0; i < 4; i++)
        {
            out_data[i] = a_1[i];
            out_data[4 + i] = a_0[i];
        }
    }
 
    void Encrypt(const uint8_t *blk, uint8_t *out_blk, vect* iter_key)
    {
        int i;
        Transform_G(iter_key[0], blk, out_blk);
        for(i = 1; i < 31; i++)
            Transform_G(iter_key[i], out_blk, out_blk);
        Transform_G_Fin(iter_key[31], out_blk, out_blk);
    }
 
    void Decrypt(const uint8_t *blk, uint8_t *out_blk, vect* iter_key)
    {
        int i;
        Transform_G(iter_key[31], blk, out_blk);
        for(i = 30; i > 0; i--)
            Transform_G(iter_key[i], out_blk, out_blk);
        Transform_G_Fin(iter_key[0], out_blk, out_blk);
    }
 
    std::string Encode(std::string str) {
        vect iter_key[32];
        Expand_Key(iter_key);
        size_t i = 0;
        while(i + 7 < str.length()) {
            uint8_t block[8];
            for(size_t j = 0; j < 8; j++) {
                block[j] = static_cast<uint8_t>(str[i+j]);
            }
            Encrypt(block, block, iter_key);
            for(size_t j = 0; j < 8; j++) {
                str[i+j] = static_cast<char>(block[j]);
            }
            i = i + 8;
        }
        if(i < str.length()) {
            uint8_t block[8];
            size_t j = 0;
            while(j + i < str.length()) {
                block[j] = static_cast<uint8_t>(str[i+j]);
                j++;
            }
            while(j < 8) {
                block[j] = static_cast<uint8_t>(' ');
                j++;
            }
            Encrypt(block, block, iter_key);
            str.resize(i+8);
            for(size_t j = 0; j < 8; j++) {
                str[i+j] = static_cast<char>(block[j]);
            }
        }
        return str;
    }
 
    uint8_t CharToUint8_t(char& ch) {
        if((ch>='0')&&(ch<='9'))  {
            return static_cast<uint8_t>(ch - '0');
        } else {
            return static_cast<uint8_t>(10 + ch - 'a');
        }
    }
 
    std::string HexToStr(std::string& hexstr) {
        std::string str(hexstr.length()/2, ' ');
        size_t j=0;
        for(size_t i=0; i<hexstr.length(); i=i+2){
            uint8_t ch1, ch2;
            ch1 = 16*CharToUint8_t(hexstr[i]);
            ch2 = CharToUint8_t(hexstr[i+1]);
            str[j++] = static_cast<char>(ch1 + ch2);
        }
        return str;
    }
 
    std::string Decode(std::string str) {
        vect iter_key[32];
        Expand_Key(iter_key);
        size_t i = 0;
        while(i + 7 < str.length()) {
            uint8_t block[8];
            for(size_t j = 0; j < 8; j++) {
                block[j] = static_cast<uint8_t>(str[i+j]);
            }
            Decrypt(block, block, iter_key);
            for(size_t j = 0; j < 8; j++) {
                str[i+j] = static_cast<char>(block[j]);
            }
            i = i + 8;
        }
        if(i > 0){
            while(str[--i] == ' ') {}
            str.resize(++i);
        }
        return str;
    }
 
    bool CheckHex(const std::string& str) {
        if(str.length()%16!=0) {
            return false;
        } else {
            for(char ch : str) {
                if(!(((ch>='0')&&(ch<='9'))||((ch>='a')&&(ch<='f'))))
                    return false;
            }
        }
        return true;
    }
 
    std::string str_to_hex(const unsigned char* data, size_t length) {
        std::ostringstream oss;
        for (size_t i = 0; i < length; ++i) {
            oss << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(data[i]);
        }
        return oss.str();
    }
 
    std::string hex_to_str(const std::string& hex) {
        std::vector<unsigned char> bytes;
        size_t length = hex.length();
 
        // Проверяем, что длина строки четная
        if (length % 2 != 0) {
            throw std::invalid_argument("Hex string must have an even length");
        }
 
        for (size_t i = 0; i < length; i += 2) {
            std::string byteString = hex.substr(i, 2);
            unsigned char byte = static_cast<unsigned char>(std::stoi(byteString, nullptr, 16));
            bytes.push_back(byte);
        }
 
        std::ostringstream result;
        for (const auto& b : bytes) {
            result << b;
        }
 
        return result.str();
    }
 
    std::string decode_pwd(std::string pwd)
    {
        if (pwd.rfind(HASH_KWD) != 0)
            return pwd;
 
        pwd = pwd.substr(strlen(HASH_KWD));
        pwd = Decode(hex_to_str(pwd)).substr(strlen(pPASSWORDSALT));
        return pwd;
    }
 
    std::string encode_pwd(std::string pwd)
    {
        if (pwd.rfind(HASH_KWD) == 0)
            return pwd;
 
        auto hased_pwd = Encode(pPASSWORDSALT + pwd);
        pwd = HASH_KWD + str_to_hex(reinterpret_cast<const unsigned char*>(hased_pwd.c_str()), hased_pwd.size());
        return pwd;
    }
}