From 10eefe1ef431f3df713b8f883840134a86746fad Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Mon, 19 Oct 2020 20:08:45 -0400 Subject: [PATCH] builtins: add sha224 and sha384 functions Fixes #42450 Release note (sql change): Add sha224 and sha384 functions. --- docs/generated/sql/functions.md | 8 ++++++++ .../testdata/logic_test/builtin_function | 20 +++++++++++++++++++ pkg/sql/sem/builtins/builtins.go | 10 ++++++++++ 3 files changed, 38 insertions(+) diff --git a/docs/generated/sql/functions.md b/docs/generated/sql/functions.md index 0c318cae6c00..3910a16fbde0 100644 --- a/docs/generated/sql/functions.md +++ b/docs/generated/sql/functions.md @@ -2395,10 +2395,18 @@ The swap_ordinate_string parameter is a 2-character string naming the ordinates sha1(string...) → string

Calculates the SHA1 hash value of a set of values.

+sha224(bytes...) → string

Calculates the SHA224 hash value of a set of values.

+
+sha224(string...) → string

Calculates the SHA224 hash value of a set of values.

+
sha256(bytes...) → string

Calculates the SHA256 hash value of a set of values.

sha256(string...) → string

Calculates the SHA256 hash value of a set of values.

+sha384(bytes...) → string

Calculates the SHA384 hash value of a set of values.

+
+sha384(string...) → string

Calculates the SHA384 hash value of a set of values.

+
sha512(bytes...) → string

Calculates the SHA512 hash value of a set of values.

sha512(string...) → string

Calculates the SHA512 hash value of a set of values.

diff --git a/pkg/sql/logictest/testdata/logic_test/builtin_function b/pkg/sql/logictest/testdata/logic_test/builtin_function index 4be3fdb42411..7b3d7f2853c9 100644 --- a/pkg/sql/logictest/testdata/logic_test/builtin_function +++ b/pkg/sql/logictest/testdata/logic_test/builtin_function @@ -407,11 +407,21 @@ SELECT sha1('abc') ---- a9993e364706816aba3e25717850c26c9cd0d89d +query T +SELECT sha224('abc') +---- +23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7 + query T SELECT sha256('abc') ---- ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad +query T +SELECT sha384('abc') +---- +cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7 + query IIII SELECT fnv32('abc'), fnv32a('abc'), fnv64('abc'), fnv64a('abc') ---- @@ -443,11 +453,21 @@ SELECT sha1(NULL::STRING) ---- NULL +query T +SELECT sha224(NULL::STRING) +---- +NULL + query T SELECT sha256(NULL::STRING) ---- NULL +query T +SELECT sha384(NULL::STRING) +---- +NULL + query T SELECT sha512(NULL::STRING, NULL::STRING) ---- diff --git a/pkg/sql/sem/builtins/builtins.go b/pkg/sql/sem/builtins/builtins.go index c7b0aee283e9..81cce50ed11e 100644 --- a/pkg/sql/sem/builtins/builtins.go +++ b/pkg/sql/sem/builtins/builtins.go @@ -1003,11 +1003,21 @@ var builtins = map[string]builtinDefinition{ "Calculates the SHA1 hash value of a set of values.", ), + "sha224": hashBuiltin( + func() hash.Hash { return sha256.New224() }, + "Calculates the SHA224 hash value of a set of values.", + ), + "sha256": hashBuiltin( func() hash.Hash { return sha256.New() }, "Calculates the SHA256 hash value of a set of values.", ), + "sha384": hashBuiltin( + func() hash.Hash { return sha512.New384() }, + "Calculates the SHA384 hash value of a set of values.", + ), + "sha512": hashBuiltin( func() hash.Hash { return sha512.New() }, "Calculates the SHA512 hash value of a set of values.",