Merge pull request #68639 from m7kss1/ripedmd-160

Add RIPEMD160 function
This commit is contained in:
Konstantin Bogdanov 2024-08-26 12:06:18 +00:00 committed by GitHub
commit 1cdccd527f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 147 additions and 7 deletions

View File

@ -688,6 +688,40 @@ SELECT kostikConsistentHash(16045690984833335023, 2);
└───────────────────────────────────────────────┘
```
## ripeMD160
Produces [RIPEMD-160](https://en.wikipedia.org/wiki/RIPEMD) hash value.
**Syntax**
```sql
ripeMD160(input)
```
**Parameters**
- `input`: Input string. [String](../data-types/string.md)
**Returned value**
- A [UInt256](../data-types/int-uint.md) hash value where the 160-bit RIPEMD-160 hash is stored in the first 20 bytes. The remaining 12 bytes are zero-padded.
**Example**
Use the [hex](../functions/encoding-functions.md/#hex) function to represent the result as a hex-encoded string.
Query:
```sql
SELECT hex(ripeMD160('The quick brown fox jumps over the lazy dog'));
```
```response
┌─hex(ripeMD160('The quick brown fox jumps over the lazy dog'))─┐
│ 37F332F68DB77BD9D7EDD4969571AD671CF9DD3B │
└───────────────────────────────────────────────────────────────┘
```
## murmurHash2_32, murmurHash2_64
Produces a [MurmurHash2](https://github.com/aappleby/smhasher) hash value.

View File

@ -124,6 +124,40 @@ SELECT hex(sipHash128('foo', '\x01', 3));
└──────────────────────────────────┘
```
## ripeMD160
Генерирует [RIPEMD-160](https://en.wikipedia.org/wiki/RIPEMD) хеш строки.
**Синтаксис**
```sql
ripeMD160(input)
```
**Аргументы**
- `input`: Строка [String](../data-types/string.md)
**Возвращаемое значение**
- [UInt256](../data-types/int-uint.md), где 160-битный хеш RIPEMD-160 хранится в первых 20 байтах. Оставшиеся 12 байт заполняются нулями.
**Пример**
Используйте функцию [hex](../functions/encoding-functions.md#hex) для представления результата в виде строки с шестнадцатеричной кодировкой
Запрос:
```sql
SELECT hex(ripeMD160('The quick brown fox jumps over the lazy dog'));
```
Результат:
```response
┌─hex(ripeMD160('The quick brown fox jumps over the lazy dog'))─┐
│ 37F332F68DB77BD9D7EDD4969571AD671CF9DD3B │
└───────────────────────────────────────────────────────────────┘
```
## cityHash64 {#cityhash64}
Генерирует 64-х битное значение [CityHash](https://github.com/google/cityhash).

View File

@ -19,7 +19,9 @@
#include <Common/HashTable/Hash.h>
#if USE_SSL
# include <openssl/evp.h>
# include <openssl/md5.h>
# include <openssl/ripemd.h>
#endif
#include <bit>
@ -196,6 +198,34 @@ T combineHashesFunc(T t1, T t2)
return HashFunction::apply(reinterpret_cast<const char *>(hashes), sizeof(hashes));
}
#if USE_SSL
struct RipeMD160Impl
{
static constexpr auto name = "ripeMD160";
using ReturnType = UInt256;
static UInt256 apply(const char * begin, size_t size)
{
UInt8 digest[RIPEMD160_DIGEST_LENGTH];
RIPEMD160(reinterpret_cast<const unsigned char *>(begin), size, reinterpret_cast<unsigned char *>(digest));
std::reverse(digest, digest + RIPEMD160_DIGEST_LENGTH);
UInt256 res = 0;
std::memcpy(&res, digest, RIPEMD160_DIGEST_LENGTH);
return res;
}
static UInt256 combineHashes(UInt256 h1, UInt256 h2)
{
return combineHashesFunc<UInt256, RipeMD160Impl>(h1, h2);
}
static constexpr bool use_int_hash_for_pods = false;
};
#endif
struct SipHash64Impl
{
@ -1624,6 +1654,7 @@ using FunctionIntHash32 = FunctionIntHash<IntHash32Impl, NameIntHash32>;
using FunctionIntHash64 = FunctionIntHash<IntHash64Impl, NameIntHash64>;
#if USE_SSL
using FunctionHalfMD5 = FunctionAnyHash<HalfMD5Impl>;
using FunctionRipeMD160Hash = FunctionAnyHash<RipeMD160Impl>;
#endif
using FunctionSipHash128 = FunctionAnyHash<SipHash128Impl>;
using FunctionSipHash128Keyed = FunctionAnyHash<SipHash128KeyedImpl, true, SipHash128KeyedImpl::Key, SipHash128KeyedImpl::KeyColumns>;
@ -1652,6 +1683,7 @@ using FunctionXxHash64 = FunctionAnyHash<ImplXxHash64>;
using FunctionXXH3 = FunctionAnyHash<ImplXXH3>;
using FunctionWyHash64 = FunctionAnyHash<ImplWyHash64>;
}
#pragma clang diagnostic pop

View File

@ -0,0 +1,23 @@
#include "FunctionsHashing.h"
#include <Functions/FunctionFactory.h>
/// FunctionsHashing instantiations are separated into files FunctionsHashing*.cpp
/// to better parallelize the build procedure and avoid MSan build failure
/// due to excessive resource consumption.
namespace DB
{
#if USE_SSL
REGISTER_FUNCTION(HashingRipe)
{
factory.registerFunction<FunctionRipeMD160Hash>(FunctionDocumentation{
.description = "RIPEMD-160 hash function, primarily used in Bitcoin address generation.",
.examples{{"", "SELECT hex(ripeMD160('The quick brown fox jumps over the lazy dog'));", R"(
hex(ripeMD160('The quick brown fox jumps over the lazy dog'))
37F332F68DB77BD9D7EDD4969571AD671CF9DD3B
)"}},
.categories{"Hash"}});
}
#endif
}

View File

@ -0,0 +1,5 @@
37F332F68DB77BD9D7EDD4969571AD671CF9DD3B
132072DF690933835EB8B6AD0B77E7B6F14ACAD7
9C1185A5C5E9FC54612808977EE8F548B2258D31
13920F39C93D503A0AC02EAB9AA8F672BC523ADA
3FEDF0C212CCFA54C0EBA676C8A8A2A10BC218BE

View File

@ -0,0 +1,12 @@
-- Tags: no-fasttest
-- Ouput can be verified using: https://emn178.github.io/online-tools/ripemd-160/
SELECT hex(ripeMD160('The quick brown fox jumps over the lazy dog'));
SELECT hex(ripeMD160('The quick brown fox jumps over the lazy cog'));
SELECT hex(ripeMD160(''));
SELECT hex(ripeMD160('CheREpaha1512'));
SELECT hex(ripeMD160('A-very-long-string-that-should-be-hashed-using-ripeMD160'));

View File

@ -1,4 +1,4 @@
personal_ws-1.1 en 2942
personal_ws-1.1 en 2983
AArch
ACLs
ALTERs
@ -975,6 +975,7 @@ ThreadPoolRemoteFSReaderThreads
ThreadPoolRemoteFSReaderThreadsActive
ThreadsActive
ThreadsInOvercommitTracker
TimeSeries
Timeunit
TinyLog
Tkachenko
@ -1116,12 +1117,12 @@ addressToLineWithInlines
addressToSymbol
adviced
agg
aggThrow
aggregatefunction
aggregatingmergetree
aggregatio
aggretate
aggthrow
aggThrow
aiochclient
allocator
alphaTokens
@ -1895,8 +1896,8 @@ joinGet
joinGetOrNull
json
jsonMergePatch
jsonasstring
jsonasobject
jsonasstring
jsoncolumns
jsoncolumnsmonoblock
jsoncompact
@ -1937,8 +1938,8 @@ kurtSamp
kurtosis
kurtpop
kurtsamp
laion
lagInFrame
laion
lang
laravel
largestTriangleThreeBuckets
@ -2040,7 +2041,6 @@ maxMap
maxintersections
maxintersectionsposition
maxmap
minMappedArrays
maxmind
mdadm
meanZTest
@ -2237,8 +2237,8 @@ parseReadableSizeOrZero
parseTimeDelta
parseable
parsers
partitionId
partitionID
partitionId
pathFull
pclmulqdq
pcre
@ -2467,6 +2467,7 @@ rewritable
rightPad
rightPadUTF
rightUTF
ripeMD
risc
riscv
ro
@ -2718,7 +2719,6 @@ themself
threadpool
throwIf
timeDiff
TimeSeries
timeSeriesData
timeSeriesMetrics
timeSeriesTags