1# Secure Random Number Generation
2
3> **NOTE**
4>
5> Since API version 12, wearable devices support operations related to obtaining random numbers.
6
7Random 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.
8
9
10You can call APIs to:
11
12
13- Generate a secure random number of the specified length and uses it to generate a key.
14
15- Generate a series of random sequences based on a seed.
16
17
18It will be helpful if you have basic knowledge of encryption and decryption and understand the following basic concepts:
19
20
21- Internal state
22
23  A value in the random number generator memory. The same internal state produces a random number of the same sequence.
24
25- Random seed
26
27  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.
28
29  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.
30
31
32## Supported Algorithms and Specifications
33
34The random number generation algorithm uses the **RAND_priv_bytes** interface of OpenSSL to generate secure random numbers.
35
36| Algorithm| Length (Byte)|
37| -------- | -------- |
38| CTR_DRBG | [1, INT_MAX] |
39
40
41## How to Develop
42
431. Use [cryptoFramework.createRandom](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#cryptoframeworkcreaterandom) to create a **Random** instance.
44
452. (Optional) Use [Random.setSeed](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#setseed) to set a seed for the random number generation pool.
46
473. 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.
48
49   The length of the random number to generate ranges from **1** to **INT_MAX**, in bytes.
50
51- Return the result using **await**:
52  ```ts
53  import { cryptoFramework } from '@kit.CryptoArchitectureKit';
54
55  async function doRand() {
56    let rand = cryptoFramework.createRandom();
57    let seed = new Uint8Array([1, 2, 3]);
58    rand.setSeed({ data: seed });
59    let len = 12;
60    let randOutput = await rand.generateRandom(len);
61    console.info('rand output:' + randOutput.data);
62  }
63  ```
64
65- Return the result synchronously:
66  ```ts
67  import { cryptoFramework } from '@kit.CryptoArchitectureKit';
68  import { BusinessError } from '@kit.BasicServicesKit';
69
70  function doRandBySync() {
71    let rand = cryptoFramework.createRandom();
72    let len = 24; // Generate a 24-byte random number.
73    try {
74      let randData = rand.generateRandomSync(len);
75      if (randData != null) {
76        console.info("[Sync]: rand result: " + randData.data);
77      } else {
78        console.error("[Sync]: get rand result fail!");
79      }
80    } catch (error) {
81      let e: BusinessError = error as BusinessError;
82      console.error(`do rand failed, ${e.code}, ${e.message}`);
83    }
84  }
85  ```
86