| 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] |