SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to implement SHA-2 in SQL Server 2005 or 2008 with a CLR assembly


How to implement SHA-2 in SQL Server 2005 or 2008 with a CLR assembly

Author
Message
Stan Kulp-439977
Stan Kulp-439977
SSCrazy
SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)

Group: General Forum Members
Points: 2441 Visits: 1143
Comments posted to this topic are about the item How to implement SHA-2 in SQL Server 2005 or 2008 with a CLR assembly
Knut Boehnert
Knut Boehnert
Ten Centuries
Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)

Group: General Forum Members
Points: 1351 Visits: 383
Absolute thorough walk through with no corners cut.
Michael Meierruth
Michael Meierruth
SSCrazy
SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)

Group: General Forum Members
Points: 2338 Visits: 2516
Nice way to get your feet wet with getting a CLR to work.

Just a few comments:
On my W7 system, the location of sn.exe is not in the path. Thus I had to navigate to C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin.
On SS2005 after sp_configure and reconfigure, the service also had to be re-started. On SS2008R2, reconfigure was sufficient.

Then I tried:

select dbo.sha256Hash(o1.name)
from sys.all_objects o1,sys.all_objects o2

which took 46 seconds and extracted 4 million rows, followed by

select dbo.sha256Hash(dbo.sha256Hash(o1.name))
from sys.all_objects o1,sys.all_objects o2

which took 93 seconds, as expected.

Will anyone beat this writing the function in SQL? w00t
Andy Warren
Andy Warren
One Orange Chip
One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)

Group: Moderators
Points: 25405 Visits: 2746
Nice write up Stan!

Andy
SQLAndy - My Blog!
Connect with me on LinkedIn
Follow me on Twitter
werner.broser-775735
werner.broser-775735
Valued Member
Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)

Group: General Forum Members
Points: 57 Visits: 36
yes, 15 seconds for

select hashbytes('sha2_256',o1.name)
from sys.all_objects o1,sys.all_objects o2

i agree in advance sha2_256 (and 512) needs sql 2012

kr/Werner
Steve Jones
Steve Jones
SSC Guru
SSC Guru (146K reputation)SSC Guru (146K reputation)SSC Guru (146K reputation)SSC Guru (146K reputation)SSC Guru (146K reputation)SSC Guru (146K reputation)SSC Guru (146K reputation)SSC Guru (146K reputation)

Group: Administrators
Points: 146414 Visits: 19430
Michael Meierruth (10/7/2013)


Then I tried:

select dbo.sha256Hash(o1.name)
from sys.all_objects o1,sys.all_objects o2

which took 46 seconds and extracted 4 million rows, followed by

select dbo.sha256Hash(dbo.sha256Hash(o1.name))
from sys.all_objects o1,sys.all_objects o2

which took 93 seconds, as expected.

Will anyone beat this writing the function in SQL? w00t


There's a few things to note here. First, we typically wouldn't hash millions of rows regularly. You'd use this in spots for a few values, so some of the inefficiencies here are fine.

Second, you can't implemen SHA-3 (or strong SHA-2 algorithms pre SQL2K12) any other way. SHA-11 has been attacked, so I'd recommend this moving forward for that data that you want to protect and use in a hashing situation.

Follow me on Twitter: @way0utwest
Forum Etiquette: How to post data/code on a forum to get the best help
My Blog: www.voiceofthedba.com
Stan Kulp-439977
Stan Kulp-439977
SSCrazy
SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)

Group: General Forum Members
Points: 2441 Visits: 1143
"Absolute thorough walk through with no corners cut."

I try to write for the accidental developer, such as the DBA who who is not interested in programming but needs to get something done, such as having an SHA-256 hash function available on a 2005/2008 SQL Server.

The detail is probably annoying to seasoned programmers, but seasoned programmers probably already know how to do what I am writing about anyway.
dei401
dei401
SSC Rookie
SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)

Group: General Forum Members
Points: 46 Visits: 12
Thank you Stan for the walk through. A couple of comments:
1. Adding VS Command Prompt to external tools
dei401
dei401
SSC Rookie
SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)

Group: General Forum Members
Points: 46 Visits: 12
Thank you Stan for the walk through.
A couple of comments...
1. Adding VS Command Prompt to external tools, use
Arguments: /k "\path\to\Microsoft Visual Studio 10.0\VC\vcvarsall.bat"
I used: /k "C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\vcvarsall.bat"
2. In my implementation of SQL Server 2005, before I enabled CLR, I had to show advanced options first:
EXEC sp_CONFIGURE 'show advanced options' , '1';
GO
RECONFIGURE;
GO
EXEC sp_CONFIGURE 'clr enabled' , '1'
GO
RECONFIGURE;
GO

Thanks again!!
David
madan.bisht
madan.bisht
Grasshopper
Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)

Group: General Forum Members
Points: 11 Visits: 4
Thanks....
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search