[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5147
  • Last Modified:

Calculating an hmac sha256 in vba

Hi All,

I need to calculate an RFC 2104-compliant HMAC (SHA256 ideally) using the string and key I have.

Using VBA please.

Many thanks!

Katerina
0
katerina-p
Asked:
katerina-p
  • 7
  • 6
  • 3
  • +1
1 Solution
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
There's not enough information in your question.  

>RFC 2104-compliant HMAC (SHA256 ideally)
Most people would have no idea what this is, or how to calculate it.

>using the string and key I have.
Define 'string and key'.
0
 
katerina-pAuthor Commented:
Hi,

Thanks for the response. Ignore my reference to key, sorry!

I need to encypt a string using a HMAC hash algorithm (preferably SHA256 but SHA1 if much easier).

I don't think any more information is needed?

Example:

"The quick brown fox jumps over the lazy dog"
is
d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592

http://en.wikipedia.org/wiki/SHA-256
http://www.online-convert.com/result/174e5d4be41ae900cb192d0ede767e35

Thanks very much!

K.
0
 
aikimarkCommented:
0
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
katerina-pAuthor Commented:
Hi aikimark,

Thanks for your comment. I only have MS Access (and so VBA) to work with, so unfortunately I'm struggling to make use of that download.

KR

K.
0
 
aikimarkCommented:
Since it is written for the VB6 standard of the language, it should be compatible with your VBA environment.  Have you imported the modules and class files into your Access project?
0
 
katerina-pAuthor Commented:
I can't import either of the forms because 'the form class contained [within] is not supported in VBE'. I've imported all the bas modules and cls class modules.
0
 
aikimarkCommented:
You don't import the forms.  You only use the code.
0
 
katerina-pAuthor Commented:
Forgive me if I'm being dense - which function(s)?

Katerina.
0
 
aikimarkCommented:
I instantiate one of the class objects (SHA) and use its hashing method to generate a hash.  There are different flavors of SHA hash.  I convert the hash into a hex string, but it could be converted into a Base64 string.
0
 
katerina-pAuthor Commented:
I'm afraid that doesn't mean much to me.

I do, actually, eventually need it in Base64.

Can you please just post, what I presume is, the few lines I'll need to convert a string to SHA256 and then into Base64? Thank you so much. I've got a base64 converter elsewhere so just the hash would be fine
0
 
Ryan McCauleyCommented:
Just a point of clarification here - "hashing" and "encrypting" aren't the same thing. Encryption uses a password to transform the data in a reversable way, where a hash algorythm takes data and produces a series of bytes that signify that data, and there's no way to modify the source data in a predictable way to make the hash algorythm generate the same has for two different sets of data. SHA256 is a hashing algorythm, not an encryption algorythm.

That said, hashing algorythms are really, really complicated (CRC or MD5 are pretty straightforward and I've seen direct VB implementations, but the SHA2 ones can get pretty long). While you could implement something like this in VBA, it would be complicated, and I imagine pretty slow as well.

In the code module somebody linked to above, you'll be interested in clsSHA2.cls - that's the one that contains the SHA2 implementations, including SHA256. Since VBA can reference external COM DLLs, I'd consider compiling and registering a library like this one and then referring to it from VBA - that's probably the easiest route. The next might be to take his VB code and make the slight modifications you'd need to so you can run it in VBA - it should be pretty close and require only minor changes.

Good luck!
0
 
aikimarkCommented:
@katerina-p

What kind of VBA programming have you done?
0
 
Ryan McCauleyCommented:
I bit more digging turned up this SHA256 implementation in VBScript:

http://bytes.com/topic/access/insights/906938-sha2-cryptographic-hash-algorithm-vba-vbscript

It's not VBA but it's pretty close - you should be able to make any minor changes necessary to run it, but if you get stuck anywhere in particular, post some details here and maybe we can help.
0
 
katerina-pAuthor Commented:
@aikimark - lots, mainly higher-level stuff though - self-taught (yes I know...!) - 40k lines behind an Access system for online retail company. I'm just not very familiar with using class modules etc and assume it would take me hours to figure out what, if one knows what to do, should take minutes!

@ryanmccauley Thanks! Just what I was looking for! (I think!)
?SHA("The quick brown fox jumps over the lazy dog") gives me
 215  168  251  179  7  215  128  148  105  202  154  188  176  8  46  79  141  86  81  228  109  60  219  118  45  2  208  191  55  201  229  146
Which when in hex is
0x d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592, i.e., correct according to the sources I linked to above! Thank you.
0
 
Ryan McCauleyCommented:
You'll have to convert the INTs to hex, but that should be pretty straightforward. If you need any other help, please let us know.
0
 
aikimarkCommented:
Think about a class as an object type.  You have probably used common data-related object types, such as recordset, querydef, field, and tabledef.

As with these objects, you will have a Dim statement that declares a variable as a data type.  In this case, it will be the name of the class.

Example:
Dim oSHA As New clsSHA

Open in new window


Then, the intellisense will show you the properties and methods of your oSHA variable when you type a period following the variable name.
0
 
katerina-pAuthor Commented:
aikimark, ok - thanks for the help. Makes a bit more sense now.

ryanmccauley - VBA has a Hex() function and I've got base64 conversion functions around somewhere too. Thanks very much.
0

Featured Post

2017 Webroot Threat Report

MSPs: Get the facts you need to protect your clients.
The 2017 Webroot Threat Report provides a uniquely insightful global view into the analysis and discoveries made by the Webroot® Threat Intelligence Platform to provide insights on key trends and risks as seen by our users.

  • 7
  • 6
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now