为 Google oAuth2 (Javascript) 计算 JWS 签名

Computing JWS Signature for Google oAuth2 (Javascript)

我正在为我的 React 应用程序设置服务器到服务器的身份验证,以便能够从 Google 电子表格 API 中提取数据,而无需用户进行身份验证。

我正在查看文档:https://developers.google.com/identity/protocols/OAuth2ServiceAccount

我遇到了这个问题:

Sign the UTF-8 representation of the input using SHA256withRSA (also known as RSASSA-PKCS1-V1_5-SIGN with the SHA-256 hash function) with the private key obtained from the Google API Console. The output will be a byte array.

使用 javascript 会是什么样子?

我已经从我的 Google API 控制台获得私钥,并且我已经准备好输入的 UTF-8 表示形式。

我只是不确定如何使用带有私钥的 SHA256withRSA 对其进行签名。

谢谢!

好的,所有的原料都准备好了,现在你要做的就是制作产品!

您可以通过以下方式签署输入:

使用加密库:https://github.com/thenativeweb/crypto2

找到正确的散列算法(它们列在 github 上)然后开始。这是一个例子

crypto2.sign(YOUR_INPUT_HERE, your_private_key, (err, signature) => {
    console.log (signature); // View the contents
});