MCPcopy Create free account
hub / github.com/GoogleCloudPlatform/nodejs-docs-samples / main

Function main

kms/getPublicKey.js:17–71  ·  view source on GitHub ↗
(
  projectId = 'my-project',
  locationId = 'us-east1',
  keyRingId = 'my-key-ring',
  keyId = 'my-key',
  versionId = '123'
)

Source from the content-addressed store, hash-verified

15'use strict';
16
17async function main(
18 projectId = 'my-project',
19 locationId = 'us-east1',
20 keyRingId = 'my-key-ring',
21 keyId = 'my-key',
22 versionId = '123'
23) {
24 // [START kms_get_public_key]
25 //
26 // TODO(developer): Uncomment these variables before running the sample.
27 //
28 // const projectId = 'my-project';
29 // const locationId = 'us-east1';
30 // const keyRingId = 'my-key-ring';
31 // const keyId = 'my-key';
32
33 // Imports the Cloud KMS library
34 const {KeyManagementServiceClient} = require('@google-cloud/kms');
35
36 // Instantiates a client
37 const client = new KeyManagementServiceClient();
38
39 // Build the key version name
40 const versionName = client.cryptoKeyVersionPath(
41 projectId,
42 locationId,
43 keyRingId,
44 keyId,
45 versionId
46 );
47
48 async function getPublicKey() {
49 const [publicKey] = await client.getPublicKey({
50 name: versionName,
51 });
52
53 // Optional, but recommended: perform integrity verification on publicKey.
54 // For more details on ensuring E2E in-transit integrity to and from Cloud KMS visit:
55 // https://cloud.google.com/kms/docs/data-integrity-guidelines
56 const crc32c = require('fast-crc32c');
57 if (publicKey.name !== versionName) {
58 throw new Error('GetPublicKey: request corrupted in-transit');
59 }
60 if (crc32c.calculate(publicKey.pem) !== Number(publicKey.pemCrc32c.value)) {
61 throw new Error('GetPublicKey: response corrupted in-transit');
62 }
63
64 console.log(`Public key pem: ${publicKey.pem}`);
65
66 return publicKey;
67 }
68
69 return getPublicKey();
70 // [END kms_get_public_key]
71}
72module.exports.main = main;
73
74/* c8 ignore next 10 */

Callers 1

getPublicKey.jsFile · 0.70

Calls 1

getPublicKeyFunction · 0.85

Tested by

no test coverage detected