如何在USQL中SHA2散列字符串 [英] How to SHA2 hash a string in USQL

查看:267
本文介绍了如何在USQL中SHA2散列字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在USQL中为字符串列运行单向散列。有没有办法做到这一点?大多数在线发现的C#示例需要多行代码 - 这在USQL中很棘手,没有代码隐藏或编译的C#程序集。 解决方案

选项1(内联公式):



以下代码可用于编译任何字符串上的SHA256或MD5,并运行

  CREATE TABLE master.dbo.Test_MyEmail_Hashes AS 
SELECT
cust.CustEmailAddr AS电子邮件
,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(
cu st.CustEmailAddr))
.Select(item => item.ToString(x2)))
AS Email_MD5
FROM master.dbo.Customers AS cust
;

选项2(使用Lambda函数):(UPDATED)



感谢@MichaelRys提供的USQL现在支持Lambda函数的指针,可以像下面这样清理:

  // Generic get_hash()函数
DECLARE @get_hash Func< string,System.Security.Cryptography.HashAlgorithm,string> =
(raw_value,hasher)=> String.Concat(hasher.ComputeHash(Encoding.UTF8.GetBytes(RAW_VALUE)));

// MD5和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);

//核心查询:
CREATE TABLE master.dbo.Test_MyEmail_Hashes AS
SELECT
cust.CustEmailAddr AS电子邮件
,@ get_sha256(cust.CustEmailAddr )AS Email_SHA2
,@ get_md5(cust.CustEmailAddr)AS Email_MD5
FROM master.dbo.Customers AS cust


I am trying to run a one-way hash for a string column in USQL. Is there a way to do this inline? Most of the C# samples found online require multiple lines of code - which is tricky in USQL without a code-behind or compiled C# assembly.

解决方案

Option 1 (Inline formula):

The code below can be used to compile a SHA256 or MD5 on any string, and runs without any special dependencies and without needing a code-behind file.

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
;

Option 2 (using Lambda functions): (UPDATED)

Thanks to @MichaelRys for the pointer that USQL now supports Lambda functions and can be cleaned up as in the below:

// 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

这篇关于如何在USQL中SHA2散列字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆