|
29 | 29 | #include "vec/functions/function_totype.h" |
30 | 30 | #include "vec/functions/simple_function_factory.h" |
31 | 31 |
|
| 32 | +#include "util/aes_util.h" |
32 | 33 | #include "util/url_coding.h" |
33 | 34 |
|
34 | 35 | namespace doris::vectorized { |
@@ -389,6 +390,88 @@ struct StringSpace { |
389 | 390 | } |
390 | 391 | }; |
391 | 392 |
|
| 393 | + |
| 394 | +struct AesEncryptImpl { |
| 395 | + static constexpr auto name = "aes_encrypt"; |
| 396 | + using Chars = ColumnString::Chars; |
| 397 | + using Offsets = ColumnString::Offsets; |
| 398 | + using ReturnType = DataTypeString; |
| 399 | + using ColumnType = ColumnString; |
| 400 | + static void vector_vector(const Chars& ldata, const Offsets& loffsets, const Chars& rdata, |
| 401 | + const Offsets& roffsets, Chars& res_data, Offsets& res_offsets, |
| 402 | + NullMap& null_map_data) { |
| 403 | + DCHECK_EQ(loffsets.size(), roffsets.size()); |
| 404 | + size_t input_rows_count = loffsets.size(); |
| 405 | + res_offsets.resize(input_rows_count); |
| 406 | + |
| 407 | + for (size_t i = 0; i < input_rows_count; ++i) { |
| 408 | + int l_size = loffsets[i] - loffsets[i - 1] - 1; |
| 409 | + const auto l_raw = reinterpret_cast<const char*>(&ldata[loffsets[i - 1]]); |
| 410 | + |
| 411 | + int r_size = roffsets[i] - roffsets[i - 1] - 1; |
| 412 | + const auto r_raw = reinterpret_cast<const char*>(&rdata[roffsets[i - 1]]); |
| 413 | + |
| 414 | + if (*l_raw == '\0' || l_size == 0) { |
| 415 | + StringOP::push_null_string(i, res_data, res_offsets, null_map_data); |
| 416 | + continue; |
| 417 | + } |
| 418 | + |
| 419 | + int cipher_len = l_size + 16; |
| 420 | + char p[cipher_len]; |
| 421 | + |
| 422 | + int outlen = |
| 423 | + AesUtil::encrypt(AES_128_ECB, (unsigned char*)l_raw, l_size, (unsigned char*)r_raw, |
| 424 | + r_size, NULL, true, (unsigned char*)p); |
| 425 | + if (outlen < 0) { |
| 426 | + StringOP::push_null_string(i, res_data, res_offsets, null_map_data); |
| 427 | + } else { |
| 428 | + StringOP::push_value_string(std::string_view(p, outlen), i, res_data, res_offsets); |
| 429 | + } |
| 430 | + } |
| 431 | + } |
| 432 | +}; |
| 433 | + |
| 434 | +struct AesDecryptImpl { |
| 435 | + static constexpr auto name = "aes_decrypt"; |
| 436 | + using Chars = ColumnString::Chars; |
| 437 | + using Offsets = ColumnString::Offsets; |
| 438 | + using ReturnType = DataTypeString; |
| 439 | + using ColumnType = ColumnString; |
| 440 | + static void vector_vector(const Chars& ldata, const Offsets& loffsets, const Chars& rdata, |
| 441 | + const Offsets& roffsets, Chars& res_data, Offsets& res_offsets, |
| 442 | + NullMap& null_map_data) { |
| 443 | + DCHECK_EQ(loffsets.size(), roffsets.size()); |
| 444 | + size_t input_rows_count = loffsets.size(); |
| 445 | + res_offsets.resize(input_rows_count); |
| 446 | + |
| 447 | + for (size_t i = 0; i < input_rows_count; ++i) { |
| 448 | + int l_size = loffsets[i] - loffsets[i - 1] - 1; |
| 449 | + const auto l_raw = reinterpret_cast<const char*>(&ldata[loffsets[i - 1]]); |
| 450 | + |
| 451 | + int r_size = roffsets[i] - roffsets[i - 1] - 1; |
| 452 | + const auto r_raw = reinterpret_cast<const char*>(&rdata[roffsets[i - 1]]); |
| 453 | + |
| 454 | + if (*l_raw == '\0' || l_size == 0) { |
| 455 | + StringOP::push_null_string(i, res_data, res_offsets, null_map_data); |
| 456 | + continue; |
| 457 | + } |
| 458 | + |
| 459 | + int cipher_len = l_size; |
| 460 | + char p[cipher_len]; |
| 461 | + |
| 462 | + int outlen = |
| 463 | + AesUtil::decrypt(AES_128_ECB, (unsigned char*)l_raw, l_size, (unsigned char*)r_raw, |
| 464 | + r_size, NULL, true, (unsigned char*)p); |
| 465 | + if (outlen < 0) { |
| 466 | + StringOP::push_null_string(i, res_data, res_offsets, null_map_data); |
| 467 | + } else { |
| 468 | + StringOP::push_value_string(std::string_view(p, outlen), i, res_data, res_offsets); |
| 469 | + } |
| 470 | + } |
| 471 | + } |
| 472 | +}; |
| 473 | + |
| 474 | + |
392 | 475 | struct ToBase64Impl { |
393 | 476 | static constexpr auto name = "to_base64"; |
394 | 477 | using ReturnType = DataTypeString; |
@@ -557,6 +640,10 @@ using FunctionRTrim = FunctionStringToString<TrimImpl<false, true>, NameRTrim>; |
557 | 640 |
|
558 | 641 | using FunctionTrim = FunctionStringToString<TrimImpl<true, true>, NameTrim>; |
559 | 642 |
|
| 643 | +using FunctionAesEncrypt = FunctionBinaryStringOperateToNullType<AesEncryptImpl>; |
| 644 | + |
| 645 | +using FunctionAesDecrypt = FunctionBinaryStringOperateToNullType<AesDecryptImpl>; |
| 646 | + |
560 | 647 | using FunctionToBase64 = FunctionStringOperateToNullType<ToBase64Impl>; |
561 | 648 |
|
562 | 649 | using FunctionFromBase64 = FunctionStringOperateToNullType<FromBase64Impl>; |
@@ -594,6 +681,8 @@ void register_function_string(SimpleFunctionFactory& factory) { |
594 | 681 | factory.register_function<FunctionStringRepeat>(); |
595 | 682 | factory.register_function<FunctionStringLPad>(); |
596 | 683 | factory.register_function<FunctionStringRPad>(); |
| 684 | + factory.register_function<FunctionAesEncrypt>(); |
| 685 | + factory.register_function<FunctionAesDecrypt>(); |
597 | 686 | factory.register_function<FunctionToBase64>(); |
598 | 687 | factory.register_function<FunctionFromBase64>(); |
599 | 688 | factory.register_function<FunctionSplitPart>(); |
|
0 commit comments