Merge pull request #29602 from NSTikhomirov/master

Added MD4 and SHA384 functions.
This commit is contained in:
Maksim Kita 2021-10-01 12:08:30 +03:00 committed by GitHub
commit d72e10367f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 83 additions and 10 deletions

View File

@ -40,6 +40,10 @@ SELECT halfMD5(array('e','x','a'), 'mple', 10, toDateTime('2019-06-15 23:00:00')
└────────────────────┴────────┘
```
## MD4 {#hash_functions-md4}
Calculates the MD4 from a string and returns the resulting set of bytes as FixedString(16).
## MD5 {#hash_functions-md5}
Calculates the MD5 from a string and returns the resulting set of bytes as FixedString(16).
@ -143,9 +147,11 @@ It works faster than intHash32. Average quality.
## SHA256 {#sha256}
## SHA384 {#sha384}
## SHA512 {#sha512}
Calculates SHA-1, SHA-224, SHA-256 or SHA-512 from a string and returns the resulting set of bytes as FixedString(20), FixedString(28), FixedString(32), or FixedString(64).
Calculates SHA-1, SHA-224, SHA-256, SHA-384 or SHA-512 from a string and returns the resulting set of bytes as FixedString(20), FixedString(28), FixedString(32), FixedString(48) or FixedString(64).
The function works fairly slowly (SHA-1 processes about 5 million short strings per second per processor core, while SHA-224 and SHA-256 process about 2.2 million).
We recommend using this function only in cases when you need a specific hash function and you cant select it.
Even in these cases, we recommend applying the function offline and pre-calculating values when inserting them into the table, instead of applying it in SELECTS.

View File

@ -40,6 +40,10 @@ SELECT halfMD5(array('e','x','a'), 'mple', 10, toDateTime('2019-06-15 23:00:00')
└────────────────────┴────────┘
```
## MD4 {#hash_functions-md4}
文字列からMD4を計算し、結果のバイトセットをFixedString(16)として返します。
## MD5 {#hash_functions-md5}
文字列からMD5を計算し、結果のバイトセットをFixedString(16)として返します。
@ -143,7 +147,11 @@ SELECT groupBitXor(cityHash64(*)) FROM table
## SHA256 {#sha256}
文字列からSHA-1、SHA-224、またはSHA-256を計算し、結果のバイトセットをFixedString(20)、FixedString(28)、またはFixedString(32)として返します。
## SHA384 {#sha384}
## SHA512 {#sha512}
文字列からSHA-1、SHA-224、SHA-256、SHA-384、またはSHA-512を計算し、結果のバイトセットをFixedString(20)、FixedString(28)、FixedString(32)、FixedString(48)またはFixedString(64)として返します。
この機能はかなりゆっくりと動作しますSHA-1はプロセッサコア毎秒約5万の短い文字列を処理しますが、SHA-224とSHA-256は約2.2万の短い文字列を処理
この関数は、特定のハッシュ関数が必要で選択できない場合にのみ使用することをお勧めします。
このような場合でも、SELECTに適用するのではなく、関数をオフラインで適用し、テーブルに挿入するときに値を事前に計算することをお勧めします。

View File

@ -40,6 +40,10 @@ SELECT halfMD5(array('e','x','a'), 'mple', 10, toDateTime('2019-06-15 23:00:00')
└────────────────────┴────────┘
```
## MD4 {#hash_functions-md4}
Вычисляет MD4 от строки и возвращает полученный набор байт в виде FixedString(16).
## MD5 {#hash_functions-md5}
Вычисляет MD5 от строки и возвращает полученный набор байт в виде FixedString(16).
@ -143,7 +147,11 @@ SELECT groupBitXor(cityHash64(*)) FROM table
## SHA256 {#sha256}
Вычисляет SHA-1, SHA-224, SHA-256 от строки и возвращает полученный набор байт в виде FixedString(20), FixedString(28), FixedString(32).
## SHA384 {#sha384}
## SHA512 {#sha512}
Вычисляет SHA-1, SHA-224, SHA-256 от строки и возвращает полученный набор байт в виде FixedString(20), FixedString(28), FixedString(32), FixedLength(48) или FixedString(64).
Функция работает достаточно медленно (SHA-1 - примерно 5 миллионов коротких строк в секунду на одном процессорном ядре, SHA-224 и SHA-256 - примерно 2.2 миллионов).
Рекомендуется использовать эти функции лишь в тех случаях, когда вам нужна конкретная хэш-функция и вы не можете её выбрать.
Даже в этих случаях, рекомендуется применять функцию оффлайн - заранее вычисляя значения при вставке в таблицу, вместо того, чтобы применять её при SELECT-ах.

View File

@ -9,11 +9,13 @@ namespace DB
void registerFunctionsHashing(FunctionFactory & factory)
{
#if USE_SSL
factory.registerFunction<FunctionMD4>();
factory.registerFunction<FunctionHalfMD5>();
factory.registerFunction<FunctionMD5>();
factory.registerFunction<FunctionSHA1>();
factory.registerFunction<FunctionSHA224>();
factory.registerFunction<FunctionSHA256>();
factory.registerFunction<FunctionSHA384>();
factory.registerFunction<FunctionSHA512>();
#endif
factory.registerFunction<FunctionSipHash64>();

View File

@ -20,6 +20,7 @@
#endif
#if USE_SSL
# include <openssl/md4.h>
# include <openssl/md5.h>
# include <openssl/sha.h>
#endif
@ -138,10 +139,24 @@ struct HalfMD5Impl
static constexpr bool use_int_hash_for_pods = false;
};
struct MD4Impl
{
static constexpr auto name = "MD4";
enum { length = MD4_DIGEST_LENGTH };
static void apply(const char * begin, const size_t size, unsigned char * out_char_data)
{
MD4_CTX ctx;
MD4_Init(&ctx);
MD4_Update(&ctx, reinterpret_cast<const unsigned char *>(begin), size);
MD4_Final(out_char_data, &ctx);
}
};
struct MD5Impl
{
static constexpr auto name = "MD5";
enum { length = 16 };
enum { length = MD5_DIGEST_LENGTH };
static void apply(const char * begin, const size_t size, unsigned char * out_char_data)
{
@ -155,7 +170,7 @@ struct MD5Impl
struct SHA1Impl
{
static constexpr auto name = "SHA1";
enum { length = 20 };
enum { length = SHA_DIGEST_LENGTH };
static void apply(const char * begin, const size_t size, unsigned char * out_char_data)
{
@ -169,7 +184,7 @@ struct SHA1Impl
struct SHA224Impl
{
static constexpr auto name = "SHA224";
enum { length = 28 };
enum { length = SHA224_DIGEST_LENGTH };
static void apply(const char * begin, const size_t size, unsigned char * out_char_data)
{
@ -183,7 +198,7 @@ struct SHA224Impl
struct SHA256Impl
{
static constexpr auto name = "SHA256";
enum { length = 32 };
enum { length = SHA256_DIGEST_LENGTH };
static void apply(const char * begin, const size_t size, unsigned char * out_char_data)
{
@ -194,6 +209,20 @@ struct SHA256Impl
}
};
struct SHA384Impl
{
static constexpr auto name = "SHA384";
enum { length = SHA384_DIGEST_LENGTH };
static void apply(const char * begin, const size_t size, unsigned char * out_char_data)
{
SHA512_CTX ctx;
SHA384_Init(&ctx);
SHA384_Update(&ctx, reinterpret_cast<const unsigned char *>(begin), size);
SHA384_Final(out_char_data, &ctx);
}
};
struct SHA512Impl
{
static constexpr auto name = "SHA512";
@ -1321,17 +1350,17 @@ private:
struct NameIntHash32 { static constexpr auto name = "intHash32"; };
struct NameIntHash64 { static constexpr auto name = "intHash64"; };
#if USE_SSL
using FunctionHalfMD5 = FunctionAnyHash<HalfMD5Impl>;
#endif
using FunctionSipHash64 = FunctionAnyHash<SipHash64Impl>;
using FunctionIntHash32 = FunctionIntHash<IntHash32Impl, NameIntHash32>;
using FunctionIntHash64 = FunctionIntHash<IntHash64Impl, NameIntHash64>;
#if USE_SSL
using FunctionMD4 = FunctionStringHashFixedString<MD4Impl>;
using FunctionHalfMD5 = FunctionAnyHash<HalfMD5Impl>;
using FunctionMD5 = FunctionStringHashFixedString<MD5Impl>;
using FunctionSHA1 = FunctionStringHashFixedString<SHA1Impl>;
using FunctionSHA224 = FunctionStringHashFixedString<SHA224Impl>;
using FunctionSHA256 = FunctionStringHashFixedString<SHA256Impl>;
using FunctionSHA384 = FunctionStringHashFixedString<SHA384Impl>;
using FunctionSHA512 = FunctionStringHashFixedString<SHA512Impl>;
#endif
using FunctionSipHash128 = FunctionStringHashFixedString<SipHash128Impl>;

View File

@ -296,8 +296,10 @@ SELECT runningDifference(CAST(NULL AS Nullable(Float64)));
SELECT runningDifference(CAST(NULL AS Nullable(Date)));
SELECT runningDifference(CAST(NULL AS Nullable(DateTime)));
SELECT bitmaskToList(NULL);
SELECT MD4(NULL);
SELECT MD5(NULL);
SELECT SHA256(NULL);
SELECT SHA384(NULL);
SELECT asin(NULL);
SELECT SHA1(NULL);
SELECT sipHash128(NULL);

View File

@ -0,0 +1,8 @@
098F6BCD4621D373
DB346D691D7ACC4DC2625DB19F9E3F52
098F6BCD4621D373CADE4E832627B4F6
A94A8FE5CCB19BA61C4C0873D391E987982FBBD3
90A3ED9E32B2AAF4C61C410EB925426119E1A9DC53D4286ADE99A809
9F86D081884C7D659A2FEAA0C55AD015A3BF4F1B2B0B822CD15D6C15B0F00A08
768412320F7B0AA5812FCE428DC4706B3CAE50E02A64CAA16A782249BFE8EFC4B7EF1CCB126255D196047DFEDF17A0A9
EE26B0DD4AF7E749AA1A8EE3C10AE9923F618980772E473F8819A5D4940E0DB27AC185F8A0E1D5F84F88BC887FD67B143732C304CC5FA9AD8E6F57F50028A8FF

View File

@ -0,0 +1,10 @@
-- Tags: no-fasttest
SELECT hex(halfMD5('test'));
SELECT hex(MD4('test'));
SELECT hex(MD5('test'));
SELECT hex(SHA1('test'));
SELECT hex(SHA224('test'));
SELECT hex(SHA256('test'));
SELECT hex(SHA384('test'));
SELECT hex(SHA512('test'));