Solved

Create SHA512 Hash in Access VBA

Posted on 2010-09-24
20
2,703 Views
Last Modified: 2012-05-10
I use the following code to create a hash in VB.Net.
 
    Dim shaM As New SHA512Managed()
    tranSig = System.Convert.ToBase64String(shaM.ComputeHash(System.Text.Encoding.UTF8.GetBytes(tranSig)))

How do I accomplish this in VBA using Access 2010?
0
Comment
Question by:erm4
  • 11
  • 9
20 Comments
 
LVL 65

Expert Comment

by:rockiroads
Comment Utility
I dont know what library so unable to try anything. However over at karenware there is Hasher which uses SHA51. It has its own dll which you would need to add in order to use. http://www.karenware.com/powertools/pthasher.asp
0
 

Author Comment

by:erm4
Comment Utility
Here are the referenced Libraries:

 Visual Basic For Applications
 Microsoft Access 14.0 Object Library
 OLE Automation
 Microsoft Office 14.0 Access database engine Objeci
 Microsoft Component Services Typelib
 Microsoft Office 14.0 C
 Microsoft XML. v6.O

I will check out that link and report back.

Thanks.
0
 
LVL 65

Expert Comment

by:rockiroads
Comment Utility
Dont know if any of them use SHA51 though. Do you know the namespace in dotnet that you use in order to use SHA51? Perhaps it was introduced in dotnet as something new.
0
 

Author Comment

by:erm4
Comment Utility
I reference:   Imports System.Security.Cryptography.SHA512

Karen's HASH fails to load on Windows 7 64bit.

Was hoping one of the libraries accessible to VBA would have something to accomplish the same thing.

0
 

Author Comment

by:erm4
Comment Utility
I created a Class in Vb.Net and compiled with the Register for COM interop.  I added a reference to this class in the VBA code in Access and use this code to create the HASH:

Private Sub Command1_Click()
Dim shaM As New CapQHash.CreatHash
Me.myHash = shaM.Hash(Me.transig)

Set shaM = Nothing
End Sub

The HASH is created successfully but it appears the object is never destroyed because when I exit Access after running the code there is still an Access process running.  This only happens when I run this code.  Is their another way to force the process to close in VBA?  I could find no interop commands in its object library.
Public Class CreatHash



    Public Function Hash(ByVal strHash As String) As String

        Dim shaM As New System.Security.Cryptography.SHA512Managed

        Return (System.Convert.ToBase64String(shaM.ComputeHash(System.Text.Encoding.UTF8.GetBytes(strHash))))

    End Function



    Public Sub New()

        MyBase.New()

    End Sub



End Class

Open in new window

0
 
LVL 65

Expert Comment

by:rockiroads
Comment Utility
Clever work.

Now perhaps you should destroy the object and return the value in a variable

You don't necessarily need a return statement because vba functions can have their return set by specifying the value against the function name (if that makes sense!)


    Public Function Hash(ByVal strHash As String) As String
        Dim shaM As New System.Security.Cryptography.SHA512Managed

        Hash = System.Convert.ToBase64String(shaM.ComputeHash(System.Text.Encoding.UTF8.GetBytes(strHash)))

        set shaM = nothing   'Might work without set
    End Function



I was reading this and trying to get mscorlib.dll in my access app then to see if I could use SHA51 bit it kept failing to add
http://www.pcreview.co.uk/forums/thread-3732551.php

0
 

Author Comment

by:erm4
Comment Utility
Ahh.  Destroy in class.  Will try in AM.

Regards.
0
 

Author Comment

by:erm4
Comment Utility
Made the changes and get the same results.

From your note above it appeared you were trying add the .Net class directly to Access.  From my research VBA in Access can directly use a .Net class.  It has to be wrapped with the COM interop.

You'd think MS would have an easy to kill the process.  Any other ideas?
VB.Net Class compiled with COM interop option enable



Public Class CreatHash



    Public Function Hash(ByVal strHash As String) As String

        Dim shaM As New System.Security.Cryptography.SHA512Managed

        '  Return (System.Convert.ToBase64String(shaM.ComputeHash(System.Text.Encoding.UTF8.GetBytes(strHash))))

        Hash = System.Convert.ToBase64String(shaM.ComputeHash(System.Text.Encoding.UTF8.GetBytes(strHash)))



        shaM = Nothing

    End Function



    Public Sub New()

        MyBase.New()

    End Sub



End Class

Open in new window

0
 
LVL 65

Expert Comment

by:rockiroads
Comment Utility
my bad, not thinking right. I kept thinking vba, your code is vb.net not vba, doh! so stick with the return but just the variable to return

VB.Net Class compiled with COM interop option enable

Public Class CreatHash

    Public Function Hash(ByVal strHash As String) As String
        Dim shaM As New System.Security.Cryptography.SHA512Managed
        Dim hashedValue as string


        '  Return (System.Convert.ToBase64String(shaM.ComputeHash(System.Text.Encoding.UTF8.GetBytes(strHash))))
        hashedValue = System.Convert.ToBase64String(shaM.ComputeHash(System.Text.Encoding.UTF8.GetBytes(strHash)))

        shaM = Nothing

         return hashedValue
    End Function

    Public Sub New()
        MyBase.New()
    End Sub

End Class


Im assuming the New is the default code generated. You can create a Finalize and that should create MyBase.Finalize


Anyways regarding the vba side of things, how are you using this com object you created. You did destroy it in vba right?

0
 
LVL 65

Expert Comment

by:rockiroads
Comment Utility
Man, I missed your post with the vba code. I think Im distracted too much watching Reece Witherspoon in Legally blonde on usa network!
Sorry about that.


Ok, I gave it a test. Created my com object and registered in Access

then created this sample code in access

Public Sub TestHash()

    Dim x As New CreatHash
   
    Debug.Print x.hash("18284")
   
    Set x = Nothing
End Sub

then ran that and closed access. my msaccess process goes away.


Is this the way you tested?



    Public Function Hash(ByVal strHash As String) As String
        Dim shaM As New System.Security.Cryptography.SHA512Managed
        Dim hashedValue As String


        '  Return (System.Convert.ToBase64String(shaM.ComputeHash(System.Text.Encoding.UTF8.GetBytes(strHash))))
        hashedValue = System.Convert.ToBase64String(shaM.ComputeHash(System.Text.Encoding.UTF8.GetBytes(strHash)))

        shaM = Nothing

        Return hashedValue
    End Function


0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:erm4
Comment Utility
Have it exactly as you do.

Here is the VBA code (VB.Net Calls compiles as COM interop is in the code view below):

    Private Sub Command1_Click()
        Dim shaQ As New CapQHash.CreatHash
        Me.myHash = shaQ.Hash(Me.transig)

       Set shaQ = Nothing
    End Sub

 The attached PDF shows the Task Manager in various states.  Made changes ot the code.  Used the Clean Process and then used Build.
VB.NET Class



Public Class CreatHash



    Public Function Hash(ByVal strHash As String) As String

        Dim shaM As New System.Security.Cryptography.SHA512Managed

        Dim hashvalue As String



        hashvalue = System.Convert.ToBase64String(shaM.ComputeHash(System.Text.Encoding.UTF8.GetBytes(strHash)))



        shaM = Nothing



        Return hashvalue

    End Function



    Public Sub New()

        MyBase.New()

    End Sub



End Class

Open in new window

TaskViewinAccess.pdf
0
 

Author Comment

by:erm4
Comment Utility
For fun I changed the Class to have the function return Bob.

It behaves the same way with leaving a process for Access Open even after it is closed.



 Public Function Hash(ByVal strHash As String) As String

        

        Return "bob"

    End Function

Open in new window

0
 
LVL 65

Expert Comment

by:rockiroads
Comment Utility
Would it make a difference running on 32bit and 64bit? My setup is Win XP 32bit.

0
 

Author Comment

by:erm4
Comment Utility
We're running Windows 7 64bit.  I will try on a 32 bit machine.  Access is 32 bit.

I am using the 4.0 Client Framework.  Perhaps changing to the full 4.0 will make a difference.  I will try those various iterations.

I was trying to do everything in Access so I didn't have to create so many Views in the App.  This is a very simple program to post XML data with a SHA512 transaction signature, parse the response and update some tables.  I am in the process of breaking breaking the code into various classes.
0
 

Author Comment

by:erm4
Comment Utility
Actually this class is using the full 4.0 framework.
0
 
LVL 65

Expert Comment

by:rockiroads
Comment Utility
I was using v2.0. Dont know if it makes a difference though, but you never know with upgrades and their new features!
0
 
LVL 65

Assisted Solution

by:rockiroads
rockiroads earned 500 total points
Comment Utility
Well I changed the assembly compile option to use dotnet4.0 instead of 2.0 and retested and still worked. I even tried not setting item to nothing in vba and it still worked?

This class you created in vb.net, it was a Class Library right? I am assuming thats what you did otherwise how else you get the com object doh!
0
 

Accepted Solution

by:
erm4 earned 0 total points
Comment Utility
Yes.  Created as Class Library.  Perhaps it is Windows 7 or the 64bit or Access 2010 that is having issues.  At this point I am changing directions and will do everything from VB.Net

Have figured out how to Refresh Tables from VB.NET and will only use ACCESS as the back end DB.
0
 

Author Comment

by:erm4
Comment Utility
Awarding points to rockiroads.
0
 
LVL 65

Expert Comment

by:rockiroads
Comment Utility
Your approach makes more sense to use VB.Net and access just as a db.

I was wondering why use Access. I thought it might of been for reporting purposes or something.

0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now