CREATE FUNCTION dbo.GetMyLongHash(@data VARBINARY(MAX))
RETURNS VARBINARY(MAX)
WITH RETURNS NULL ON NULL INPUT
AS
BEGIN
DECLARE @res VARBINARY(MAX) = 0x
DECLARE @position INT = 1, @len INT = DATALENGTH(@data)
WHILE 1 = 1
BEGIN
SET @res = @res + HASHBYTES('MD5', SUBSTRING(@data, @position, 8000))
SET @position = @position+8000
IF @Position > @len
BREAK
END
RETURN @res
END
但是使用这个 CLR 函数和任何长度的散列数据到真正的 MD5 散列中可能会更好?
using System;
using System.Data.SqlTypes;
using System.IO;
namespace ClrHelpers
{
public partial class UserDefinedFunctions {
[Microsoft.SqlServer.Server.SqlFunction]
public static Guid HashMD5(SqlBytes data) {
System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
md5.Initialize();
int len = 0;
byte[] b = new byte[8192];
Stream s = data.Stream;
do {
len = s.Read(b, 0, 8192);
md5.TransformBlock(b, 0, len, b, 0);
} while(len > 0);
md5.TransformFinalBlock(b, 0, 0);
Guid g = new Guid(md5.Hash);
return g;
}
};
}
如果你坚持,那么
但是使用这个 CLR 函数和任何长度的散列数据到真正的 MD5 散列中可能会更好?