Create SHA512 Hash in Access VBA

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?
erm4Asked:
Who is Participating?
 
erm4Connect With a Mentor Author Commented:
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
 
rockiroadsCommented:
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
 
erm4Author Commented:
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
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
rockiroadsCommented:
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
 
erm4Author Commented:
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
 
erm4Author Commented:
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
 
rockiroadsCommented:
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
 
erm4Author Commented:
Ahh.  Destroy in class.  Will try in AM.

Regards.
0
 
erm4Author Commented:
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
 
rockiroadsCommented:
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
 
rockiroadsCommented:
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
 
erm4Author Commented:
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
 
erm4Author Commented:
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
 
rockiroadsCommented:
Would it make a difference running on 32bit and 64bit? My setup is Win XP 32bit.

0
 
erm4Author Commented:
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
 
erm4Author Commented:
Actually this class is using the full 4.0 framework.
0
 
rockiroadsCommented:
I was using v2.0. Dont know if it makes a difference though, but you never know with upgrades and their new features!
0
 
rockiroadsConnect With a Mentor Commented:
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
 
erm4Author Commented:
Awarding points to rockiroads.
0
 
rockiroadsCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.