主题
生成随机字节 - GenerateRandomBytes
函数简介
生成指定长度和类型的随机字节字符串。
接口名称
GenerateRandomBytesDLL调用
c
long GenerateRandomBytes(long instance, int length, int type);参数说明
| 参数名 | 类型 | 说明 |
|---|---|---|
| instance | 长整数型 | OLAPlug对象的指针,由 CreateCOLAPlugInterFace 接口生成。 |
| length | 整数型 | 要生成的随机字节长度。 |
| type | 整数型 | 字符类型:0-十六进制字符(0-9A-F);1-数字+大写字母(0-9A-Z);2-数字+大小写字母(0-9A-Za-z);3-可打印ASCII字符(包含特殊字符);4-Base64字符集(A-Za-z0-9+/)。 |
示例
SDK 调用
cpp
#include "OLAPlugServer.h"
OLAPlugServer ola;
string randomKey = ola.GenerateRandomBytes(16, 0);csharp
using OLAPlug;
var ola = new OLAPlugServer();
string randomKey = ola.GenerateRandomBytes(16, 0);python
from OLAPlugServer import OLAPlugServer
ola = OLAPlugServer()
randomKey = ola.GenerateRandomBytes(16, 0)java
import com.olaplug.OLAPlugServer;
OLAPlugServer ola = new OLAPlugServer();
String randomKey = ola.GenerateRandomBytes(16, 0);cpp
var ola = com("OlaPlug.OlaSoft")
var randomKey = ola.GenerateRandomBytes(16, 0)vbscript
Set ola = CreateObject("OlaPlug.OlaSoft")
randomKey = ola.GenerateRandomBytes(16, 0)text
.局部变量 ola, OLAPlug
ola.创建 ()
randomKey = ola.GenerateRandomBytes(16, 0)aardio
import OLAPlugServer;
var ola = OLAPlugServer();
var randomKey = ola.GenerateRandomBytes(16, 0);text
变量 ola <类型 = OLAPlugServer>
ola = 新建 OLAPlugServer
文本型 randomKey = ola.GenerateRandomBytes(16, 0)cpp
#include "OLAPlugServer.h"
OLAPlugServer ola;
std::string randomKey = ola.GenerateRandomBytes(16, 0);原生 DLL 调用
cpp
long instance = CreateCOLAPlugInterFace();
long ptr = GenerateRandomBytes(instance, 16, 0);
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 GenerateRandomBytes(long ola, int length, int type);
long instance = CreateCOLAPlugInterFace();
long ptr = GenerateRandomBytes(instance, 16, 0);
if (ptr != 0) {
StringBuilder sb = new StringBuilder(GetStringSize(ptr) + 1);
GetStringFromPtr(ptr, sb, sb.Capacity);
FreeStringPtr(ptr);
string randomKey = 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.GenerateRandomBytes(instance, 16, 0)返回值
| 返回值 | 说明 |
|---|---|
非 0 | 成功,返回 随机字节字符串的指针。 |
0 | 失败。 |
注意事项
| 项目 | 说明 |
|---|---|
| 可直接用作AES密钥 | 可直接用作AES密钥,推荐长度:16/24/32。 |
