Вариант 1 (встроенная формула):
Приведенный ниже код можно использовать для компиляции SHA256 или MD5 для любой строки, и он работает без каких-либо особых зависимостей и без необходимостиза файлом.
CREATE TABLE master.dbo.Test_MyEmail_Hashes AS
SELECT
cust.CustEmailAddr AS Email
, String.Concat(System.Security.Cryptography.SHA256.Create()
.ComputeHash(Encoding.UTF8.GetBytes(
cust.CustEmailAddr))
.Select(item => item.ToString("x2")))
AS Email_SHA2
, String.Concat(System.Security.Cryptography.MD5.Create()
.ComputeHash(Encoding.UTF8.GetBytes(
cust.CustEmailAddr))
.Select(item => item.ToString("x2")))
AS Email_MD5
FROM master.dbo.Customers AS cust
;
Вариант 2 (с использованием лямбда-функций): (ОБНОВЛЕНО)
Спасибо @MichaelRys за указатель на то, что USQL теперь поддерживает лямбда-функции и можетбыть очищенным, как показано ниже:
// Generic get_hash() function
DECLARE @get_hash Func<string,System.Security.Cryptography.HashAlgorithm,string> =
(raw_value, hasher) => String.Concat(hasher.ComputeHash(Encoding.UTF8.GetBytes(raw_value)));
// Short-hand functions for MD5 and SHA256:
DECLARE @md5 = System.Security.Cryptography.MD5.Create();
DECLARE @get_md5 Func<string,string> =
(raw_value) => @get_hash(raw_value, @md5);
DECLARE @sha256 = System.Security.Cryptography.SHA256.Create();
DECLARE @get_sha256 Func<string,string> =
(raw_value) => @get_hash(raw_value, @sha256);
// Core query:
CREATE TABLE master.dbo.Test_MyEmail_Hashes AS
SELECT
cust.CustEmailAddr AS Email
, @get_sha256(cust.CustEmailAddr) AS Email_SHA2
, @get_md5(cust.CustEmailAddr) AS Email_MD5
FROM master.dbo.Customers AS cust