• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Secure Random Number Generation
2
3
4Random numbers are used to generate temporary session keys and asymmetric encryption algorithm keys. In encryption and decryption, a secure random number generator must feature randomness, unrepeatability, and unpredictability. The random numbers generated by the system meet the requirements of cryptography security pseudo-randomness.
5
6
7You can call APIs to:
8
9
10- Generate a secure random number of the specified length and uses it to generate a key.
11
12- Generate a series of random sequences based on a seed.
13
14
15It will be helpful if you have basic knowledge of encryption and decryption and understand the following basic concepts:
16
17
18- Internal state
19
20  A value in the random number generator memory. The same internal state produces a random number of the same sequence.
21
22- Random seed
23
24  A number used to initialize the internal state of a pseudorandom number generator. The random number generator generates a series of random sequences based on the seeds.
25
26  In the OpenSSL implementation, the internal state of the random number generator changes continuously. Therefore, the generated random number sequences are different even if the same seed is used.
27
28
29## Supported Algorithms and Specifications
30
31The random number generation algorithm uses the **RAND_priv_bytes** interface of OpenSSL to generate secure random numbers.
32
33| Algorithm| Length (Byte)|
34| -------- | -------- |
35| CTR_DRBG | [1, INT_MAX] |
36
37
38## How to Develop
39
401. Use [cryptoFramework.createRandom](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#cryptoframeworkcreaterandom) to create a **Random** instance.
41
422. (Optional) Use [Random.setSeed](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#setseed) to set a seed for the random number generation pool.
43
443. Use [Random.generateRandom](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#generaterandom) or [Random.generateRandomSync](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#generaterandomsync10) to generate a secure random number.
45
46   The length of the random number to generate ranges from **1** to **INT_MAX**, in bytes.
47
48- Return the result using **await**:
49  ```ts
50  import { cryptoFramework } from '@kit.CryptoArchitectureKit';
51
52  async function doRand() {
53    let rand = cryptoFramework.createRandom();
54    let seed = new Uint8Array([1, 2, 3]);
55    rand.setSeed({ data: seed });
56    let len = 12;
57    let randOutput = await rand.generateRandom(len);
58    console.info('rand output:' + randOutput.data);
59  }
60  ```
61
62- Return the result synchronously:
63  ```ts
64  import { cryptoFramework } from '@kit.CryptoArchitectureKit';
65  import { BusinessError } from '@kit.BasicServicesKit';
66
67  function doRandBySync() {
68    let rand = cryptoFramework.createRandom();
69    let len = 24; // Generate a 24-byte random number.
70    try {
71      let randData = rand.generateRandomSync(len);
72      if (randData != null) {
73        console.info("[Sync]: rand result: " + randData.data);
74      } else {
75        console.error("[Sync]: get rand result fail!");
76      }
77    } catch (error) {
78      let e: BusinessError = error as BusinessError;
79      console.error(`do rand failed, ${e.code}, ${e.message}`);
80    }
81  }
82  ```
83