title: Google Cloud Key Management Service to sign JSON Web Tokens
body: <p>First of all I tried the solution at: <a href="https://stackoverflow.com/questions/54250511/using-google-cloud-key-management-service-to-sign-json-web-tokens">Using Google Cloud Key Management Service to sign JSON Web Tokens</a>
  But it doesn't work.</p>
  
  <p>Creating signature:</p>
  
  <pre class="lang-js prettyprint-override"><code>const TimeStamp = Math.floor(new Date().getTime() / 1000)
  
  let body = base64url(
      JSON.stringify({
          alg: 'RS256',
          typ: 'JWT'
      })
  )
  body += '.'
  body += base64url(
      JSON.stringify({
          iss: 'some-iss',
          aud: 'some-aud',
          iat: TimeStamp,
          exp: TimeStamp + parseInt(process.env.TOKEN_EXPIRY, 10)
      })
  )
  
  const hashedMessage = crypto
                  .createHash('sha256')
                  .update(body)
                  .digest('base64')
  const digest = { sha256: hashedMessage }
  
  const [signatureObj] = await client
      .asymmetricSign({ name, digest })
      .catch(console.error)
  
  const signature = base64url(signatureObj.signature)
  const token = `${body}.${signature}`
  </code></pre>
  
  <p>Then verifying:</p>
  
  <pre class="lang-js prettyprint-override"><code>const[publicKeyObject] = await client.getPublicKey({ name }).catch(console.error)
  const publicKey = publicKeyObject.pem
  
  const verify = crypto.createVerify('sha256')
  verify.write(body)
  verify.end()
  verify.verify(publicKey, base64url.decode(signature), 'base64')
  </code></pre>
  
  <p>I'm not able to figure what is wrong with the code.</p>