( projectId = 'my-project', locationId = 'us-east1', numBytes = 256 )
| 15 | 'use strict'; |
| 16 | |
| 17 | async function main( |
| 18 | projectId = 'my-project', |
| 19 | locationId = 'us-east1', |
| 20 | numBytes = 256 |
| 21 | ) { |
| 22 | // [START kms_generate_random_bytes] |
| 23 | // |
| 24 | // TODO(developer): Uncomment these variables before running the sample. |
| 25 | // |
| 26 | // const projectId = 'my-project'; |
| 27 | // const locationId = 'us-east1'; |
| 28 | // const numBytes = 256; |
| 29 | |
| 30 | // Imports the Cloud KMS library |
| 31 | const {KeyManagementServiceClient} = require('@google-cloud/kms'); |
| 32 | |
| 33 | // Instantiates a client |
| 34 | const client = new KeyManagementServiceClient(); |
| 35 | |
| 36 | // Build the location name |
| 37 | const locationName = client.locationPath(projectId, locationId); |
| 38 | |
| 39 | async function generateRandomBytes() { |
| 40 | const [randomBytesResponse] = await client.generateRandomBytes({ |
| 41 | location: locationName, |
| 42 | lengthBytes: numBytes, |
| 43 | protectionLevel: 'HSM', |
| 44 | }); |
| 45 | |
| 46 | console.log(`Random bytes: ${randomBytesResponse.data.toString('base64')}`); |
| 47 | return randomBytesResponse; |
| 48 | } |
| 49 | |
| 50 | return generateRandomBytes(); |
| 51 | // [END kms_generate_random_bytes] |
| 52 | } |
| 53 | module.exports.main = main; |
| 54 | |
| 55 | /* c8 ignore next 10 */ |
no test coverage detected