(
projectId = 'your-project-id',
locationId = 'us-east1',
keyRingId = 'my-key-ring',
keyId = 'my-key',
versionId = '123',
message = Buffer.from('...')
)
| 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 | message = Buffer.from('...') |
| 24 | ) { |
| 25 | // [START kms_sign_asymmetric] |
| 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 message = 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 signAsymmetric() { |
| 52 | // Create a digest of the message. The digest needs to match the digest |
| 53 | // configured for the Cloud KMS key. |
| 54 | const crypto = require('crypto'); |
| 55 | const hash = crypto.createHash('sha256'); |
| 56 | hash.update(message); |
| 57 | const digest = hash.digest(); |
| 58 | |
| 59 | // Optional but recommended: Compute digest's CRC32C. |
| 60 | // Ensure fast-crc32c has been installed, `npm i fast-crc32c`. |
| 61 | const crc32c = require('fast-crc32c'); |
| 62 | const digestCrc32c = crc32c.calculate(digest); |
| 63 | |
| 64 | // Sign the message with Cloud KMS |
| 65 | const [signResponse] = await client.asymmetricSign({ |
| 66 | name: versionName, |
| 67 | digest: { |
| 68 | sha256: digest, |
| 69 | }, |
| 70 | digestCrc32c: { |
| 71 | value: digestCrc32c, |
| 72 | }, |
| 73 | }); |
| 74 |
no test coverage detected