SQL 2016 - Base64编码SHA256散列字符串

问题描述 投票:0回答:1

我有以下SQL select查询,它使用SHA256散列用户ID。我想进一步将此哈希编码为base64,以便其他应用程序(如Excel)可以轻松读取,这在解析哈希用户ID时会立即引发问题。我怎样才能做到这一点?

Select
cast(HASHBYTES('SHA2_256', cast(Users.UserID as nvarchar(10))) as varchar(50)) as UserID 
FROM UserTable
sql-server encryption hash sql-server-2016
1个回答
1
投票

以下对我有用,整个XML解析/黑客需要一个VARBINARY:

SELECT  CAST(N'' AS XML).value('xs:base64Binary(xs:hexBinary(sql:column("UserID")))', 'VARCHAR(MAX)') Base64Encoding
FROM    (
            SELECT HASHBYTES('SHA2_256', CAST(Users.UserID AS NVARCHAR(10))) AS UserID
            FROM UserTable
        ) a 

(公然改编自this回答)

© www.soinside.com 2019 - 2024. All rights reserved.