我正在尝试在USQL中为字符串列运行单向散列。有没有办法在线进行此操作?在线发现的大多数C#样本都需要多行代码 - 这在USQL中很棘手,没有代码隐藏或编译的C#程序集。
答案 0 :(得分:2)
选项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(使用Lambda函数):(更新)
感谢@MichaelRys指针,USQL现在支持Lambda函数,可以清理如下:
// 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
答案 1 :(得分:1)
实际上我建议你使用最近添加的&#34;名为lambdas&#34; (Func&lt;&gt;类型变量)使用多行C#样本。这里有一个例子:https://github.com/Azure/AzureDataLake/blob/master/docs/Release_Notes/2018/2018_Spring/USQL_Release_Notes_2018_Spring.md#u-sql-adds-c-func-typed-variables-in-declare-statements-named-lambdas