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

Function signAsymmetric

kms/signAsymmetric.js:51–98  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

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
75 // Optional, but recommended: perform integrity verification on signResponse.
76 // For more details on ensuring E2E in-transit integrity to and from Cloud KMS visit:
77 // https://cloud.google.com/kms/docs/data-integrity-guidelines
78 if (signResponse.name !== versionName) {
79 throw new Error('AsymmetricSign: request corrupted in-transit');
80 }
81 if (!signResponse.verifiedDigestCrc32c) {
82 throw new Error('AsymmetricSign: request corrupted in-transit');
83 }
84 if (
85 crc32c.calculate(signResponse.signature) !==
86 Number(signResponse.signatureCrc32c.value)
87 ) {
88 throw new Error('AsymmetricSign: response corrupted in-transit');
89 }
90
91 // Example of how to display signature. Because the signature is in a binary
92 // format, you need to encode the output before printing it to a console or
93 // displaying it on a screen.
94 const encoded = signResponse.signature.toString('base64');
95 console.log(`Signature: ${encoded}`);
96
97 return signResponse.signature;
98 }
99
100 return signAsymmetric();
101 // [END kms_sign_asymmetric]

Callers 1

mainFunction · 0.85

Calls 2

toStringMethod · 0.80
updateMethod · 0.45

Tested by

no test coverage detected