3 回答
TA贡献1802条经验 获得超5个赞
你在正确的轨道上使用HASHBYTES,只需要得到Base64散列BinaryHash:
DECLARE @HASHBYTES VARBINARY(128) = hashbytes('sha2_256', 'secret')
SELECT cast(N'' as xml).value('xs:base64Binary(sql:variable("@HASHBYTES"))', 'varchar(128)');
TA贡献1795条经验 获得超7个赞
希望这会有所帮助。
CREATE FUNCTION [dbo].[ConvertToSha256ForIdentityServer](@secret varchar(128))
RETURNS varchar(128) AS
BEGIN
DECLARE @HASHBYTES VARBINARY(128) = hashbytes('sha2_256', @secret)
RETURN (SELECT top 1 cast(N'' as xml).value('xs:base64Binary(sql:variable("@HASHBYTES"))', 'varchar(128)'));
END
插入
MERGE INTO dbo.[ClientSecrets] AS TARGET
USING (
VALUES
(dbo.ConvertToSha256ForIdentityServer('mysecret'),'SharedSecret',getdate(),1)
)
AS Source ([Value],[Type],[Created],[ClientId])
ON Target.[ClientId] = source.[ClientId]
WHEN matched THEN
UPDATE SET
[Value] = Source.[Value],
[Type] = Source.[Type]
WHEN NOT matched BY target THEN
INSERT ([Value],[Type],[Created],[ClientId])
VALUES ([Value],[Type],[Created],[ClientId]);
TA贡献1801条经验 获得超16个赞
您可以使用下面的算法来创建秘密,然后您可以将其存储在 SQL 中:
using System.Security.Cryptography;
static class Extentions
{
public static string Sha256(this string input)
{
using (SHA256 shA256 = SHA256.Create())
{
byte[] bytes = Encoding.UTF8.GetBytes(input);
return Convert.ToBase64String(((HashAlgorithm)shA256).ComputeHash(bytes));
}
}
}
void Main()
{
Console.WriteLine( "secret-as-guid".Sha256());
}
你也可以使用passwordgenerator.net
- 3 回答
- 0 关注
- 129 浏览
添加回答
举报