Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 74
Default Hash (MD5) in Excel

Hi Ale,

i need i cell to get the Hash (MD5) value of a group of cells..


I just knocked this up from some similar code I did a while ago. The function can be called from a worksheet, giving it a range of cells.
Note that this routine puts the contents of each cell in a Variant, then adds the byte representation of the variant to the MD5 hash - so
for example if you calculate an MD5 hash for a specific string in another manner, it won't agree with this one, as this includes all the
extra stuff that's contained in a Variant. And watch out for word-wrap!

Regards

Stephen Bullen
Microsoft MVP - Excel
www.BMSLtd.co.uk


======================================
Option Explicit

Declare Function CryptAcquireContext Lib "advapi32" Alias "CryptAcquireContextA" (ByRef hProv As Long, ByVal sContainer As String, _
ByVal sProvider As String, ByVal lProvType As Long, ByVal lFlags As Long) As Long

Declare Function CryptCreateHash Lib "advapi32" (ByVal hProv As Long, ByVal lALG_ID As Long, _
ByVal hKey As Long, ByVal lFlags As Long, ByRef hHash As Long) As Long

Declare Function CryptHashData Lib "advapi32" (ByVal hHash As Long, ByVal lDataPtr As Long, ByVal lLen As Long, ByVal lFlags As Long) As
Long

Declare Function CryptGetHashParam Lib "advapi32" (ByVal hHash As Long, ByVal lParam As Long, ByVal sBuffer As String, _
ByRef lLen As Long, ByVal lFlags As Long) As Long

Declare Function CryptDestroyHash Lib "advapi32" (ByVal hHash As Long) As Long

Declare Function CryptReleaseContext Lib "advapi32" (ByVal hProv As Long, ByVal lFlags As Long) As Long

Const MS_DEF_PROV = "Microsoft Base Cryptographic Provider v1.0"
Const PROV_RSA_FULL As Long = 1
Const CRYPT_NEWKEYSET As Long = 8
Const CALG_MD5 As Long = 32771
Const HP_HASHVAL As Long = 2

Public Function GetMD5Hash(rngData As Range) As String

Dim hProv As Long
Dim hHash As Long
Dim lLen As Long
Dim oCell As Range
Dim baData() As Byte
Dim sBuffer As String
Dim vValue As Variant
Dim lResult As Long

'Get/create a cryptography context
CryptAcquireContext hProv, vbNullString, MS_DEF_PROV, PROV_RSA_FULL, 0
If hProv = 0 Then
CryptAcquireContext hProv, vbNullString, MS_DEF_PROV, PROV_RSA_FULL, CRYPT_NEWKEYSET
End If

'If we got one...
If hProv < 0 Then

'Create an MD5 Hash
CryptCreateHash hProv, CALG_MD5, 0, 0, hHash

'If that was OK...
If hHash < 0 Then

'Fill it with the contents of the range
For Each oCell In rngData.Cells
If Not IsEmpty(oCell.Value) Then
vValue = oCell.Value
lResult = CryptHashData(hHash, VarPtr(vValue), LenB(vValue), 0&)
End If
Next

'Create a buffer to store the hash value
sBuffer = Space(30)
lLen = 30

'Get the hash value
CryptGetHashParam hHash, HP_HASHVAL, sBuffer, lLen, 0

'Return the hash value
GetMD5Hash = Left$(sBuffer, lLen)

'Tidy up
CryptDestroyHash hHash
End If

'Tidy up
CryptReleaseContext hProv, 0
End If

End Function


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Hash (MD5) in Excel

Thank you for this solution!

However, this doesn't appear to work for Win7x64 or 64bit office (not sure which is causing the problem, the OS or the Office suite).

Is a simple search & replace on 32 - 64 likely to work?

On Thursday, February 5, 2004 1:50:20 PM UTC-7, Stephen Bullen wrote:
Hi Ale,

i need i cell to get the Hash (MD5) value of a group of cells..


I just knocked this up from some similar code I did a while ago. The function can be called from a worksheet, giving it a range of cells.
Note that this routine puts the contents of each cell in a Variant, then adds the byte representation of the variant to the MD5 hash - so
for example if you calculate an MD5 hash for a specific string in another manner, it won't agree with this one, as this includes all the
extra stuff that's contained in a Variant. And watch out for word-wrap!

Regards

Stephen Bullen
Microsoft MVP - Excel
www.BMSLtd.co.uk


======================================
Option Explicit

Declare Function CryptAcquireContext Lib "advapi32" Alias "CryptAcquireContextA" (ByRef hProv As Long, ByVal sContainer As String, _
ByVal sProvider As String, ByVal lProvType As Long, ByVal lFlags As Long) As Long

Declare Function CryptCreateHash Lib "advapi32" (ByVal hProv As Long, ByVal lALG_ID As Long, _
ByVal hKey As Long, ByVal lFlags As Long, ByRef hHash As Long) As Long

Declare Function CryptHashData Lib "advapi32" (ByVal hHash As Long, ByVal lDataPtr As Long, ByVal lLen As Long, ByVal lFlags As Long) As
Long

Declare Function CryptGetHashParam Lib "advapi32" (ByVal hHash As Long, ByVal lParam As Long, ByVal sBuffer As String, _
ByRef lLen As Long, ByVal lFlags As Long) As Long

Declare Function CryptDestroyHash Lib "advapi32" (ByVal hHash As Long) As Long

Declare Function CryptReleaseContext Lib "advapi32" (ByVal hProv As Long, ByVal lFlags As Long) As Long

Const MS_DEF_PROV = "Microsoft Base Cryptographic Provider v1.0"
Const PROV_RSA_FULL As Long = 1
Const CRYPT_NEWKEYSET As Long = 8
Const CALG_MD5 As Long = 32771
Const HP_HASHVAL As Long = 2

Public Function GetMD5Hash(rngData As Range) As String

Dim hProv As Long
Dim hHash As Long
Dim lLen As Long
Dim oCell As Range
Dim baData() As Byte
Dim sBuffer As String
Dim vValue As Variant
Dim lResult As Long

'Get/create a cryptography context
CryptAcquireContext hProv, vbNullString, MS_DEF_PROV, PROV_RSA_FULL, 0
If hProv = 0 Then
CryptAcquireContext hProv, vbNullString, MS_DEF_PROV, PROV_RSA_FULL, CRYPT_NEWKEYSET
End If

'If we got one...
If hProv < 0 Then

'Create an MD5 Hash
CryptCreateHash hProv, CALG_MD5, 0, 0, hHash

'If that was OK...
If hHash < 0 Then

'Fill it with the contents of the range
For Each oCell In rngData.Cells
If Not IsEmpty(oCell.Value) Then
vValue = oCell.Value
lResult = CryptHashData(hHash, VarPtr(vValue), LenB(vValue), 0&)
End If
Next

'Create a buffer to store the hash value
sBuffer = Space(30)
lLen = 30

'Get the hash value
CryptGetHashParam hHash, HP_HASHVAL, sBuffer, lLen, 0

'Return the hash value
GetMD5Hash = Left$(sBuffer, lLen)

'Tidy up
CryptDestroyHash hHash
End If

'Tidy up
CryptReleaseContext hProv, 0
End If

End Function

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,182
Default Hash (MD5) in Excel

As noted at the top, this is Robert Hubley's creation NOT mine. Glad,
though, that you were able to make use of it!

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Are hash tables in Excel possible? Mahurshi Akilla Excel Discussion (Misc queries) 4 May 10th 23 07:44 PM
Excel cell shows and prints as hash marks (#) Phil Mead Excel Discussion (Misc queries) 3 December 3rd 09 03:15 PM
How do I restore my hash key on my keyboard? Nanny B Excel Worksheet Functions 2 September 4th 06 05:13 PM
how to create a psuedo hash in excel nlscb New Users to Excel 0 January 20th 05 03:07 AM
Hash (MD5) in Excel Tom Ogilvy Excel Programming 0 February 5th 04 03:20 PM


All times are GMT +1. The time now is 10:20 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"