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
| Name | typedef Keyword | Description |
|---|---|---|
| OH_CryptoRand | OH_CryptoRand | Defines a struct for a random number generator. |
Functions
| Name | Description |
|---|---|
| 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
| Name | Description |
|---|---|
| OH_CryptoRand **ctx | Pointer to the random number generator instance. |
Returns
| Type | Description |
|---|---|
| OH_Crypto_ErrCode | CRYPTO_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
| Name | Description |
|---|---|
| OH_CryptoRand *ctx | Random number generator instance. |
| int len | Length of the random number to generate, in bytes. The value range is [1, INT_MAX]. |
| Crypto_DataBlob *out | Pointer to the random number. |
Returns
| Type | Description |
|---|---|
| OH_Crypto_ErrCode | CRYPTO_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
| Name | Description |
|---|---|
| OH_CryptoRand *ctx | Pointer to the random number generator instance. |
Returns
| Type | Description |
|---|---|
| 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
| Name | Description |
|---|---|
| OH_CryptoRand *ctx | Random number generator instance. |
| Crypto_DataBlob *seed | Seed data. |
Returns
| Type | Description |
|---|---|
| OH_Crypto_ErrCode | CRYPTO_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
| Name | Description |
|---|---|
| OH_CryptoRand *ctx | Random number generator instance. |
Returns
| Type | Description |
|---|---|
| OH_Crypto_ErrCode | CRYPTO_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
| Name | Description |
|---|---|
| OH_CryptoRand *ctx | Random number generator instance. |