Technical Article

HashBytes Nvarchar(MAX)

,

CREATE ASSEMBLY [Hashbytes]
FROM D:\HashBytes.dll --Location of precompiled DLL
WITH PERMISSION_SET = UNSAFE;

CREATE FUNCTION Hashbyte(@Inputstring NVARCHAR(MAX))
RETURNS NVARCHAR(MAX) WITH EXECUTE AS CALLER
AS 
EXTERNAL NAME Hashbytes.hashbytes.hasBytes


C# Code
---------

using System;
using System.Collections;
using System.Data;
using System.Security.Cryptography;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;

public class hashbytes
{

public hashbytes()
{

}
   
    public static string hashBytes(SqlString Hashsource)
    {

        string str = Convert.ToString(Hashsource);

        SHA256 sha = new SHA256CryptoServiceProvider(); // used to create the SHA256 hash

        byte[] hashBytes = sha.ComputeHash(System.Text.UnicodeEncoding.Unicode.GetBytes(str));

        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        for (int i = 0; i < hashBytes.Length; i++)
        {
            sb.Append(hashBytes[i].ToString("X2"));
        }

        return "0X" +sb.ToString();
    }

}

Rate

4 (1)

You rated this post out of 5. Change rating

Share

Share

Rate

4 (1)

You rated this post out of 5. Change rating