| 45 | const plaintextCrc32c = crc32c.calculate(plaintextBuffer); |
| 46 | |
| 47 | async function encryptSymmetric() { |
| 48 | const [encryptResponse] = await client.encrypt({ |
| 49 | name: keyName, |
| 50 | plaintext: plaintextBuffer, |
| 51 | plaintextCrc32c: { |
| 52 | value: plaintextCrc32c, |
| 53 | }, |
| 54 | }); |
| 55 | |
| 56 | const ciphertext = encryptResponse.ciphertext; |
| 57 | |
| 58 | // Optional, but recommended: perform integrity verification on encryptResponse. |
| 59 | // For more details on ensuring E2E in-transit integrity to and from Cloud KMS visit: |
| 60 | // https://cloud.google.com/kms/docs/data-integrity-guidelines |
| 61 | if (!encryptResponse.verifiedPlaintextCrc32c) { |
| 62 | throw new Error('Encrypt: request corrupted in-transit'); |
| 63 | } |
| 64 | if ( |
| 65 | crc32c.calculate(ciphertext) !== |
| 66 | Number(encryptResponse.ciphertextCrc32c.value) |
| 67 | ) { |
| 68 | throw new Error('Encrypt: response corrupted in-transit'); |
| 69 | } |
| 70 | |
| 71 | console.log(`Ciphertext: ${ciphertext.toString('base64')}`); |
| 72 | return ciphertext; |
| 73 | } |
| 74 | |
| 75 | return encryptSymmetric(); |
| 76 | // [END kms_encrypt_symmetric] |