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

Function main

kms/importManuallyWrappedKey.js:17–85  ·  view source on GitHub ↗
(
  projectId = 'my-project',
  locationId = 'us-east1',
  keyRingId = 'my-key-ring',
  cryptoKeyId = 'my-imported-key',
  importJobId = 'my-import-job'
)

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 cryptoKeyId = 'my-imported-key',
22 importJobId = 'my-import-job'
23) {
24 // [START kms_import_manually_wrapped_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 cryptoKeyId = 'my-imported-key';
32 // const importJobId = 'my-import-job';
33
34 // Imports the Cloud KMS library
35 const {KeyManagementServiceClient} = require('@google-cloud/kms');
36
37 // Instantiates a client
38 const client = new KeyManagementServiceClient();
39
40 // Build the crypto key and importjob resource names
41 const cryptoKeyName = client.cryptoKeyPath(
42 projectId,
43 locationId,
44 keyRingId,
45 cryptoKeyId
46 );
47 const importJobName = client.importJobPath(
48 projectId,
49 locationId,
50 keyRingId,
51 importJobId
52 );
53
54 async function wrapAndImportKey() {
55 // Generate a 32-byte key to import.
56 const crypto = require('crypto');
57 const targetKey = crypto.randomBytes(32);
58
59 const [importJob] = await client.getImportJob({name: importJobName});
60
61 // Wrap the target key using the import job key
62 const wrappedTargetKey = crypto.publicEncrypt(
63 {
64 key: importJob.publicKey.pem,
65 oaepHash: 'sha256',
66 padding: crypto.constants.RSA_PKCS1_OAEP_PADDING,
67 },
68 targetKey
69 );
70
71 // Import the target key version
72 const [version] = await client.importCryptoKeyVersion({
73 parent: cryptoKeyName,
74 importJob: importJobName,

Callers 1

Calls 1

wrapAndImportKeyFunction · 0.85

Tested by

no test coverage detected