在Access VBA中创建SHA1哈希和xml规范化 [英] Creating SHA1 hash and xml Canonicalization in Access VBA

查看:155
本文介绍了在Access VBA中创建SHA1哈希和xml规范化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是编程和使用Access 2010 VBA的新手.我需要将一组规则应用于xml文件,以便可以访问API.我研究过不同的方法,但是由于我的知识有限,我被困在每种方法上.如果有人能指出我正确的方向/为此提供一些建议,那就太好了.

I am new to programming and using Access 2010 VBA. I need to apply a set of rules to an xml file so that I can access an API. I have looked at different ways of doing it but have got stuck at each one due to my limited knowledge. If anyone could point me down the right path / offer some advice for doing this that would be great.

规则:

  1. 将xml文件标准化为W3C C14N规范.
  2. SHA1将xml哈希为二进制以进一步编码
  3. Base64和Base32对SHA1哈希进行编码

我的第一个问题是我找不到在VBA中以C14n格式显示XML的任何方法.因此,我跳过了这一步,因为我可以确保xml预先规范化.尽管理想情况下,这将在编码过程中完成,以防万一有人更改了先行的xml.

My first problem is that I can not find any way to c14n the xml in VBA. So I skipped this step as I can make sure the xml is canonicalised up front. Though ideally this would be done as part of the encoding process in case someone changes the precedent xml.

然后我开始查看SHA1哈希并找到以下示例代码:

I then started looking at the SHA1 hash and found this sample code:

http://vb.wikia.com/wiki/SHA-1.bas

这里的问题是输出哈希是十六进制的,我认为在转换为base64之前我需要转换为字节数组.我找不到任何示例代码可以做到这一点.

The problem here is that the output hash is in hexadecimal which I think I need to convert into a byte array before converting to base64. I cant find any sample code to do this.

然后我看到了这篇文章: VBA中的Base64 HMAC SHA1字符串

I then came across this post: Base64 HMAC SHA1 String in VBA

这正在为HMACSHA1调用.net cyptography库,但是有一个针对SHA1的库:

This is calling the .net cyptography library for HMACSHA1 but there is a library for SHA1:

http://msdn.microsoft.com /en-us/library/system.security.cryptography.sha1.aspx

这里的问题是我不知道如何调用.net库的工作.我需要在办公室的10台不同Windows机器上运行此软件,所以我需要了解它.

The problem here is I have no idea how calling .net library's work. I need to run this software on 10 different windows machines in the office so I need to understand it.

最后我找到了我需要在vb.net中编写的确切功能:

Lastly I found the exact function I need written in vb.net:

http://blog.kuffs .co.uk/2009/07/calculating-irmark-for-hmrc-gateway.html

同样,我没有使用.net的经验,并且不确定如何将其构建到可访问的库中.

Again i've got no experience with .net and am unsure how to build this into a library that is accessible to access.

推荐答案

在这里:

Public Function SHA1Base64(ByVal sTextToHash As String)

    Dim asc As Object, enc As Object
    Dim TextToHash() As Byte
    Set asc = CreateObject("System.Text.UTF8Encoding")
    Set enc = CreateObject("System.Security.Cryptography.SHA1CryptoServiceProvider")
    TextToHash = asc.Getbytes_4(sTextToHash)
    Dim bytes() As Byte
    bytes = enc.ComputeHash_2((TextToHash))
    SHA1Base64 = EncodeBase64(bytes)
    Set asc = Nothing
    Set enc = Nothing

End Function

Private Function EncodeBase64(ByRef arrData() As Byte) As String

    Dim objXML As MSXML2.DOMDocument
    Dim objNode As MSXML2.IXMLDOMElement

    Set objXML = New MSXML2.DOMDocument

    ' byte array to base64
    Set objNode = objXML.createElement("b64")
    objNode.DataType = "bin.base64"
    objNode.nodeTypedValue = arrData
    EncodeBase64 = objNode.Text

    Set objNode = Nothing
    Set objXML = Nothing

End Function

请注意,并不是几乎每个.NET Framework库都可以在VBA中使用(无论如何都不要将它们包装在COM可调用包装dll中).碰巧的是,可以直接从VBA调用所需的功能.是的,的确,我了解的文档确实很少,而且除了直观之外,什么都没有. GetBytes_4和ComputerHash_2实际上是.NET中的重载"函数,在VBA中不存在.在基本上,您可以多次声明相同的函数,但是每次都使用不同数量和/或不同类型的参数.

Be aware that not nearly every .NET Framework library is available to be used in VBA (without wrapping them in COM callable wrapper dll's anyway). It just so happens that the functions you need can be called directly from VBA. And yes, it's true that there's really not much documentation on this that I'm aware of and it's anything but intuitive. GetBytes_4 and ComputerHash_2 are actually "overloaded" functions in .NET, something that doesn't exist in VBA. It's basically where you declare the same function multiple times, but each time it takes different quantity and/or different types of arguments.

您可能想将这些函数的输出与其他地方的一些已知良好输出进行比较.我之所以这么说,是因为我知道这段代码正在返回某些内容,但我不知道这是否是您要查找的内容.

You may want to compare the output of these functions to some known good output from somewhere else. I say that only because I know this code is returning something but I don't know if it's what you're looking for.

这篇关于在Access VBA中创建SHA1哈希和xml规范化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆