主题
HMAC消息认证码 - HMAC
函数简介
使用HMAC算法生成消息认证码,用于验证数据的完整性和真实性。
接口名称
HMACDLL调用
c
long HMAC(long instance, string source, string key, int shaType);参数说明
| 参数名 | 类型 | 说明 |
|---|---|---|
| instance | 长整数型 | OLAPlug对象的指针,由 CreateCOLAPlugInterFace 接口生成。 |
| source | 字符串 | 要进行HMAC计算的源数据。 |
| key | 字符串 | HMAC密钥。 |
| shaType | 整数型 | 哈希类型:0-MD5;1-SHA1;2-SHA256;3-SHA384;4-SHA512;5-SHA3-256;6-SHA3-384;7-SHA3-512。 |
示例
SDK 调用
cpp
#include "OLAPlugServer.h"
OLAPlugServer ola;
string mac = ola.HMAC("hello ola", "secret", 2);csharp
using OLAPlug;
var ola = new OLAPlugServer();
string mac = ola.HMAC("hello ola", "secret", 2);python
from OLAPlugServer import OLAPlugServer
ola = OLAPlugServer()
mac = ola.HMAC("hello ola", "secret", 2)java
import com.olaplug.OLAPlugServer;
OLAPlugServer ola = new OLAPlugServer();
String mac = ola.HMAC("hello ola", "secret", 2);cpp
var ola = com("OlaPlug.OlaSoft")
var mac = ola.HMAC("hello ola", "secret", 2)vbscript
Set ola = CreateObject("OlaPlug.OlaSoft")
mac = ola.HMAC("hello ola", "secret", 2)text
.局部变量 ola, OLAPlug
ola.创建 ()
mac = ola.HMAC("hello ola", "secret", 2)aardio
import OLAPlugServer;
var ola = OLAPlugServer();
var mac = ola.HMAC("hello ola", "secret", 2);text
变量 ola <类型 = OLAPlugServer>
ola = 新建 OLAPlugServer
文本型 mac = ola.HMAC("hello ola", "secret", 2)cpp
#include "OLAPlugServer.h"
OLAPlugServer ola;
std::string mac = ola.HMAC("hello ola", "secret", 2);原生 DLL 调用
cpp
long instance = CreateCOLAPlugInterFace();
long ptr = HMAC(instance, "hello ola", "secret", 2);
if (ptr != 0) {
char buffer[4096] = {0};
GetStringFromPtr(ptr, buffer, sizeof(buffer));
FreeStringPtr(ptr);
}csharp
using System.Runtime.InteropServices;
using System.Text;
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int GetStringFromPtr(long ptr, StringBuilder lpString, int size);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int FreeStringPtr(long ptr);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int GetStringSize(long ptr);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long CreateCOLAPlugInterFace();
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long HMAC(long ola, string source, string key, int shaType);
long instance = CreateCOLAPlugInterFace();
long ptr = HMAC(instance, "hello ola", "secret", 2);
if (ptr != 0) {
StringBuilder sb = new StringBuilder(GetStringSize(ptr) + 1);
GetStringFromPtr(ptr, sb, sb.Capacity);
FreeStringPtr(ptr);
string mac = sb.ToString();
}python
from ctypes import CDLL, c_int, c_int64
ola = CDLL("OLAPlug_x64.dll")
ola.CreateCOLAPlugInterFace.restype = c_int64
instance = ola.CreateCOLAPlugInterFace()
ptr = ola.HMAC(instance, b"hello ola", b"secret", 2)返回值
| 返回值 | 说明 |
|---|---|
非 0 | 成功,返回 HMAC值的字符串指针。 |
0 | 失败。 |
注意事项
| 项目 | 说明 |
|---|---|
| 密钥应保持安全 | 密钥应保持安全,不要泄露。 |
