openharmony 鸿蒙 capi-crypto-rand-h

2026-08-25 浏览 (1)

crypto_rand.h

Overview

Defines APIs for a random number generator.

Header file: <CryptoArchitectureKit/crypto_rand.h>

Library: libohcrypto.so

System capability: SystemCapability.Security.CryptoFramework

Since: 20

Related module: CryptoRandApi

Summary

Structs

Nametypedef KeywordDescription
OH_CryptoRandOH_CryptoRandDefines a struct for a random number generator.

Functions

NameDescription
OH_Crypto_ErrCode OH_CryptoRand_Create(OH_CryptoRand **ctx)Creates a random number generator.
Note: The created resource must be destroyed by calling OH_CryptoRand_Destroy.
OH_Crypto_ErrCode OH_CryptoRand_GenerateRandom(OH_CryptoRand *ctx, int len, Crypto_DataBlob *out)Generates random numbers.
Note: After the use is complete, the memory for storing the out parameter must be released by calling OH_Crypto_FreeDataBlob.
const char *OH_CryptoRand_GetAlgoName(OH_CryptoRand *ctx)Obtains the algorithm name of a random number generator instance.
OH_Crypto_ErrCode OH_CryptoRand_SetSeed(OH_CryptoRand *ctx, Crypto_DataBlob *seed)Sets the seed of a random number generator.
OH_Crypto_ErrCode OH_CryptoRand_EnableHardwareEntropy(OH_CryptoRand *ctx)Enables the hardware entropy source.
void OH_CryptoRand_Destroy(OH_CryptoRand *ctx)Destroys a random number generator instance.

Function Description

OH_CryptoRand_Create()

OH_Crypto_ErrCode OH_CryptoRand_Create(OH_CryptoRand **ctx)

Description

Creates a random number generator.
Note: The created resource must be destroyed by calling OH_CryptoRand_Destroy.

Since: 20

Parameters

NameDescription
OH_CryptoRand **ctxPointer to the random number generator instance.

Returns

TypeDescription
OH_Crypto_ErrCodeCRYPTO_SUCCESS: The operation is successful.
CRYPTO_NOT_SUPPORTED: The operation is not supported.
CRYPTO_MEMORY_ERROR: A memory error occurs.
CRYPTO_PARAMETER_CHECK_FAILED: The parameter check failed.
CRYPTO_OPERTION_ERROR: Failed to call an API of a third-party algorithm library.

OH_CryptoRand_GenerateRandom()

OH_Crypto_ErrCode OH_CryptoRand_GenerateRandom(OH_CryptoRand *ctx, int len, Crypto_DataBlob *out)

Description

Generates random numbers.
Note: After the use is complete, the memory for storing the out parameter must be released by calling OH_Crypto_FreeDataBlob.

Since: 20

Parameters

NameDescription
OH_CryptoRand *ctxRandom number generator instance.
int lenLength of the random number to generate, in bytes. The value range is [1, INT_MAX].
Crypto_DataBlob *outPointer to the random number.

Returns

TypeDescription
OH_Crypto_ErrCodeCRYPTO_SUCCESS: The operation is successful.
CRYPTO_NOT_SUPPORTED: The operation is not supported.
CRYPTO_MEMORY_ERROR: A memory error occurs.
CRYPTO_PARAMETER_CHECK_FAILED: The parameter check failed.
CRYPTO_OPERTION_ERROR: Failed to call an API of a third-party algorithm library.

OH_CryptoRand_GetAlgoName()

const char *OH_CryptoRand_GetAlgoName(OH_CryptoRand *ctx)

Description

Obtains the algorithm name of a random number generator instance.

Since: 20

Parameters

NameDescription
OH_CryptoRand *ctxPointer to the random number generator instance.

Returns

TypeDescription
const char *Algorithm name of a random number generator instance.

OH_CryptoRand_SetSeed()

OH_Crypto_ErrCode OH_CryptoRand_SetSeed(OH_CryptoRand *ctx, Crypto_DataBlob *seed)

Description

Sets the seed of a random number generator.

Since: 20

Parameters

NameDescription
OH_CryptoRand *ctxRandom number generator instance.
Crypto_DataBlob *seedSeed data.

Returns

TypeDescription
OH_Crypto_ErrCodeCRYPTO_SUCCESS: The operation is successful.
CRYPTO_NOT_SUPPORTED: The operation is not supported.
CRYPTO_MEMORY_ERROR: A memory error occurs.
CRYPTO_PARAMETER_CHECK_FAILED: The parameter check failed.
CRYPTO_OPERTION_ERROR: Failed to call an API of a third-party algorithm library.

OH_CryptoRand_EnableHardwareEntropy()

OH_Crypto_ErrCode OH_CryptoRand_EnableHardwareEntropy(OH_CryptoRand *ctx)

Description

Enables the hardware entropy source.

Since: 21

Parameters

NameDescription
OH_CryptoRand *ctxRandom number generator instance.

Returns

TypeDescription
OH_Crypto_ErrCodeCRYPTO_SUCCESS: The operation is successful.
CRYPTO_NOT_SUPPORTED: The operation is not supported.
CRYPTO_MEMORY_ERROR: A memory error occurs.
CRYPTO_PARAMETER_CHECK_FAILED: The parameter check failed.
CRYPTO_OPERTION_ERROR: Failed to call an API of a third-party algorithm library.

OH_CryptoRand_Destroy()

void OH_CryptoRand_Destroy(OH_CryptoRand *ctx)

Description

Destroys a random number generator instance.

Since: 20

Parameters

NameDescription
OH_CryptoRand *ctxRandom number generator instance.

你可能感兴趣的鸿蒙文章

openharmony 鸿蒙 capi-cryptosymkeyapi-oh-cryptosymkey

openharmony 鸿蒙 capi-cryptoasymkeyapi-oh-cryptoasymkeygeneratorwithspec

openharmony 鸿蒙 capi-cryptoasymcipherapi-oh-cryptoasymcipher

openharmony 鸿蒙 capi-cryptokdfapi-oh-cryptokdfparams

openharmony 鸿蒙 capi-cryptomacapi

openharmony 鸿蒙 capi-cryptodigestapi-oh-cryptodigest

openharmony 鸿蒙 capi-cryptomacapi-oh-cryptomac

openharmony 鸿蒙 capi-cryptosignatureapi-oh-cryptoeccsignaturespec

openharmony 鸿蒙 capi-cryptocommonapi-crypto-datablob

openharmony 鸿蒙 capi-cryptoasymcipherapi-oh-cryptosm2ciphertextspec

  • 所属分类: 后端技术
  • 本文标签: 鸿蒙 软件
  • 版权声明: 本文链接 https://seaxiang.com/blog/MJdN9btV