Skip to content

PBKDF2密钥派生函数 - PBKDF2

函数简介

使用PBKDF2算法从密码和盐值派生密钥。

接口名称

PBKDF2

DLL调用

c
long PBKDF2(long instance, string password, string salt, int iterations, int keyLength, int shaType);

参数说明

参数名类型说明
instance长整数型OLAPlug对象的指针,由 CreateCOLAPlugInterFace 接口生成。
password字符串密码。
salt字符串盐值。
iterations整数型迭代次数。
keyLength整数型派生密钥长度。
shaType整数型哈希类型:1-SHA1;2-SHA256;3-SHA384;4-SHA512。

示例

SDK 调用

cpp
#include "OLAPlugServer.h"

OLAPlugServer ola;
string derived = ola.PBKDF2("password", "salt", 10000, 32, 2);
csharp
using OLAPlug;

var ola = new OLAPlugServer();
string derived = ola.PBKDF2("password", "salt", 10000, 32, 2);
python
from OLAPlugServer import OLAPlugServer

ola = OLAPlugServer()
derived = ola.PBKDF2("password", "salt", 10000, 32, 2)
java
import com.olaplug.OLAPlugServer;

OLAPlugServer ola = new OLAPlugServer();
String derived = ola.PBKDF2("password", "salt", 10000, 32, 2);
cpp
var ola = com("OlaPlug.OlaSoft")
var derived = ola.PBKDF2("password", "salt", 10000, 32, 2)
vbscript
Set ola = CreateObject("OlaPlug.OlaSoft")
derived = ola.PBKDF2("password", "salt", 10000, 32, 2)
text
.局部变量 ola, OLAPlug
ola.创建 ()
derived = ola.PBKDF2("password", "salt", 10000, 32, 2)
aardio
import OLAPlugServer;
var ola = OLAPlugServer();
var derived = ola.PBKDF2("password", "salt", 10000, 32, 2);
text
变量 ola <类型 = OLAPlugServer>
ola = 新建 OLAPlugServer
文本型 derived = ola.PBKDF2("password", "salt", 10000, 32, 2)
cpp
#include "OLAPlugServer.h"

OLAPlugServer ola;
std::string derived = ola.PBKDF2("password", "salt", 10000, 32, 2);

原生 DLL 调用

cpp
long instance = CreateCOLAPlugInterFace();
long ptr = PBKDF2(instance, "password", "salt", 10000, 32, 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 PBKDF2(long ola, string password, string salt, int iterations, int keyLength, int shaType);

long instance = CreateCOLAPlugInterFace();
long ptr = PBKDF2(instance, "password", "salt", 10000, 32, 2);
if (ptr != 0) {
    StringBuilder sb = new StringBuilder(GetStringSize(ptr) + 1);
    GetStringFromPtr(ptr, sb, sb.Capacity);
    FreeStringPtr(ptr);
    string derived = 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.PBKDF2(instance, b"password", b"salt", 10000, 32, 2)

返回值

返回值说明
非 0成功,返回 派生密钥的字符串指针。
0失败。

注意事项

项目说明
迭代次数至少为10000次以确保安全性建议迭代次数至少为10000次以确保安全性。