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

Function wrapAndImportKey

kms/importManuallyWrappedKey.js:54–81  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

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,
75 algorithm: 'GOOGLE_SYMMETRIC_ENCRYPTION',
76 wrappedKey: wrappedTargetKey,
77 });
78
79 console.log(`Imported key version: ${version.name}`);
80 return version;
81 }
82
83 return wrapAndImportKey();
84 // [END kms_import_manually_wrapped_key]

Callers 1

mainFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected