MCPcopy Create free account
hub / github.com/GoogleCloudPlatform/nodejs-docs-samples / main

Function main

kms/verifyAsymmetricRsa.js:17–78  ·  view source on GitHub ↗
(
  projectId = 'your-project-id',
  locationId = 'us-east1',
  keyRingId = 'my-key-ring',
  keyId = 'my-key',
  versionId = '1',
  message = 'my message to verify',
  signatureBuffer = Buffer.from('...')
)

Source from the content-addressed store, hash-verified

15'use strict';
16
17async function main(
18 projectId = 'your-project-id',
19 locationId = 'us-east1',
20 keyRingId = 'my-key-ring',
21 keyId = 'my-key',
22 versionId = '1',
23 message = 'my message to verify',
24 signatureBuffer = Buffer.from('...')
25) {
26 // [START kms_verify_asymmetric_signature_rsa]
27 //
28 // TODO(developer): Uncomment these variables before running the sample.
29 //
30 // const projectId = 'your-project-id';
31 // const locationId = 'us-east1';
32 // const keyRingId = 'my-key-ring';
33 // const keyId = 'my-key';
34 // const versionId = '1';
35 // const message = 'my message to verify';
36 // const signatureBuffer = Buffer.from('...');
37
38 // Imports the Cloud KMS library
39 const {KeyManagementServiceClient} = require('@google-cloud/kms');
40
41 // Instantiates a client
42 const client = new KeyManagementServiceClient();
43
44 // Build the key name
45 const versionName = client.cryptoKeyVersionPath(
46 projectId,
47 locationId,
48 keyRingId,
49 keyId,
50 versionId
51 );
52
53 async function verifyAsymmetricSignatureRsa() {
54 // Get public key
55 const [publicKey] = await client.getPublicKey({
56 name: versionName,
57 });
58
59 // Create the verifier. The algorithm must match the algorithm of the key.
60 const crypto = require('crypto');
61 const verify = crypto.createVerify('sha256');
62 verify.update(message);
63 verify.end();
64
65 // Build the key object
66 const key = {
67 key: publicKey.pem,
68 padding: crypto.constants.RSA_PKCS1_PSS_PADDING,
69 };
70
71 // Verify the signature using the public key
72 const verified = verify.verify(key, signatureBuffer);
73 return verified;
74 }

Callers 1

Calls 1

Tested by

no test coverage detected