|
| 1 | +// Copyright 2019 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +'use strict'; |
| 16 | + |
| 17 | +// [START kms_asymmetric_encrypt] |
| 18 | +async function asymmetricEncrypt( |
| 19 | + projectId = 'your-project-id', // Your GCP projectId |
| 20 | + keyRingId = 'my-key-ring', // Name of the crypto key's key ring |
| 21 | + cryptoKeyId = 'my-key', // Name of the crypto key, e.g. "my-key" |
| 22 | + cryptoKeyVersionId = '1', // Version of the crypto key to use |
| 23 | + plaintext = 'my data to encrypt' // Plaintext data to encrypt |
| 24 | +) { |
| 25 | + // Import the library and create a client |
| 26 | + const kms = require('@google-cloud/kms'); |
| 27 | + const client = new kms.KeyManagementServiceClient(); |
| 28 | + |
| 29 | + // The location of the crypto key's key ring, e.g. "global" |
| 30 | + const locationId = 'global'; |
| 31 | + |
| 32 | + // Construct the cyrpto key version ID |
| 33 | + const name = client.cryptoKeyVersionPath( |
| 34 | + projectId, |
| 35 | + locationId, |
| 36 | + keyRingId, |
| 37 | + cryptoKeyId, |
| 38 | + cryptoKeyVersionId |
| 39 | + ); |
| 40 | + |
| 41 | + // Get public key from Cloud KMS |
| 42 | + const [publicKey] = await client.getPublicKey({name: name}); |
| 43 | + |
| 44 | + // Import and setup crypto |
| 45 | + const crypto = require('crypto'); |
| 46 | + const plaintextBuffer = Buffer.from(plaintext); |
| 47 | + |
| 48 | + // Encrypt plaintext locally using the public key. This example uses a key |
| 49 | + // that was configured with sha256 hash with OAEP padding. Update these values |
| 50 | + // to match the Cloud KMS key. |
| 51 | + // |
| 52 | + // NOTE: In Node < 12, this function does not properly consume the OAEP |
| 53 | + // padding and thus produces invalid ciphertext. If you are using Node to do |
| 54 | + // public key encryption, please use version 12+. |
| 55 | + const encryptedBuffer = crypto.publicEncrypt( |
| 56 | + { |
| 57 | + key: publicKey.pem, |
| 58 | + oaepHash: 'sha256', |
| 59 | + padding: crypto.constants.RSA_PKCS1_OAEP_PADDING, |
| 60 | + }, |
| 61 | + plaintextBuffer |
| 62 | + ); |
| 63 | + |
| 64 | + // Example of how to display ciphertext. Because the ciphertext is in a binary |
| 65 | + // format, you need to encode the output before printing it to a console or |
| 66 | + // displaying it on a screen. |
| 67 | + const encoded = encryptedBuffer.toString('base64'); |
| 68 | + console.log(`Encrypted ciphertext: ${encoded}`); |
| 69 | + |
| 70 | + // Return the ciphertext buffer |
| 71 | + return encryptedBuffer; |
| 72 | +} |
| 73 | +// [END kms_asymmetric_encrypt] |
| 74 | + |
| 75 | +const args = process.argv.slice(2); |
| 76 | +asymmetricEncrypt(...args).catch(console.error); |
0 commit comments