| 15 | 'use strict'; |
| 16 | |
| 17 | async function main( |
| 18 | projectId = 'your-project-id', |
| 19 | locationId = 'us-east1', |
| 20 | keyRingId = 'my-key-ring', |
| 21 | keyId = 'my-key', |
| 22 | versionId = '123', |
| 23 | data = Buffer.from('...') |
| 24 | ) { |
| 25 | // [START kms_sign_mac] |
| 26 | // |
| 27 | // TODO(developer): Uncomment these variables before running the sample. |
| 28 | // |
| 29 | // const projectId = 'your-project-id'; |
| 30 | // const locationId = 'us-east1'; |
| 31 | // const keyRingId = 'my-key-ring'; |
| 32 | // const keyId = 'my-key'; |
| 33 | // const versionId = '123'; |
| 34 | // const data = Buffer.from('...'); |
| 35 | |
| 36 | // Imports the Cloud KMS library |
| 37 | const {KeyManagementServiceClient} = require('@google-cloud/kms'); |
| 38 | |
| 39 | // Instantiates a client |
| 40 | const client = new KeyManagementServiceClient(); |
| 41 | |
| 42 | // Build the version name |
| 43 | const versionName = client.cryptoKeyVersionPath( |
| 44 | projectId, |
| 45 | locationId, |
| 46 | keyRingId, |
| 47 | keyId, |
| 48 | versionId |
| 49 | ); |
| 50 | |
| 51 | async function signMac() { |
| 52 | // Sign the data with Cloud KMS |
| 53 | const [signResponse] = await client.macSign({ |
| 54 | name: versionName, |
| 55 | data: data, |
| 56 | }); |
| 57 | |
| 58 | // Example of how to display signature. Because the signature is in a binary |
| 59 | // format, you need to encode the output before printing it to a console or |
| 60 | // displaying it on a screen. |
| 61 | const encoded = signResponse.mac.toString('base64'); |
| 62 | console.log(`Signature: ${encoded}`); |
| 63 | |
| 64 | return signResponse; |
| 65 | } |
| 66 | |
| 67 | return signMac(); |
| 68 | // [END kms_sign_mac] |
| 69 | } |
| 70 | module.exports.main = main; |
| 71 | |
| 72 | /* c8 ignore next 10 */ |