Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Create SHA512 Hash in Access VBA

Posted on 2010-09-24
20
Medium Priority
?
3,081 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 11
  • 9
20 Comments
 
LVL 65

Expert Comment

by:rockiroads
ID: 33755161
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
ID: 33755529
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
ID: 33755789
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
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 

Author Comment

by:erm4
ID: 33755839
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
ID: 33758613
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
ID: 33758958
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
ID: 33759137
Ahh.  Destroy in class.  Will try in AM.

Regards.
0
 

Author Comment

by:erm4
ID: 33761007
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
ID: 33761973
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
ID: 33762065
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
 

Author Comment

by:erm4
ID: 33762189
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
ID: 33762196
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
ID: 33762214
Would it make a difference running on 32bit and 64bit? My setup is Win XP 32bit.

0
 

Author Comment

by:erm4
ID: 33762240
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
ID: 33762250
Actually this class is using the full 4.0 framework.
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 33762273
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 2000 total points
ID: 33765130
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
ID: 33765456
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
ID: 33765472
Awarding points to rockiroads.
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 33765545
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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

721 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