JetBrains Rider 2025.1 Help

HTTP 客户端 密码学 API 参考

crypto 对象提供对 HTTP 客户端 Crypto API 的访问权限,允许您使用加密哈希函数和 HMAC 生成 HTTP 签名。 然后,您可以在预请求脚本中将这些签名用作变量来签署您的 HTTP 请求。

crypto 接受一种方法,该方法可以是以下哈希函数之一(sha1sha256sha512md5 ),或 hmac

哈希方法

方法

参数

描述

updateWithText

textInput (字符串)

编码 (字符串): textInput 的编码。 默认值是 UTF8

将字符串更新为哈希值。

updateWithHex

hexInput (字符串)

将十六进制字符串更新为哈希值。

updateWithBase64

base64Input (字符串)

urlSafe (布尔值):如果字符串是使用 Base64 的 URL 安全变体编码的,请输入 true

将一个 Base64 字符串更新为哈希。

digest().toHex()

生成哈希并将其转换为十六进制格式。

digest().toBase64()

urlSafe (布尔):如果您想使用 URL 安全的 Base64 变体,请输入 true

生成哈希,并将其转换为 Base64 格式。

HMAC 方法

crypto.hmac 对象使您能够使用 HMAC 对 HTTP 请求进行签名。 它可以访问所有 hash 方法来生成哈希,并且还有方法可以获取令牌的秘密部分。

方法

形参

描述

withTextSecret

文本保密 (字符串)

编码 (字符串): 文本保密 的编码。 默认值是 UTF8

将用于 HMAC 的密钥放入。

withHexSecret

hexSecret (字符串)

将密钥置于十六进制格式。

withBase64Secret

base64Input (字符串)

urlSafe (布尔值):如果字符串是使用 Base64 的 URL 安全变体编码的,请输入 true

将密钥置于 Base64 格式。

示例:

< {% const signature = crypto.hmac.sha256() .withTextSecret(request.environment.get("secret")) // get variable from http-client.private.env.json .updateWithText(request.body.tryGetSubstituted()) .digest().toHex(); request.variables.set("signature", signature) const hash = crypto.sha256() .updateWithText(request.body.tryGetSubstituted()) .digest().toHex(); request.variables.set("hash", hash) %} POST https://httpbin.org/post X-My-Signature: {{signature}} X-My-Hash: {{hash}} Content-Type: application/json { "prop": "value" }

示例:生成 JWT

以下是创建 JSON Web Token (JWT)的示例,它由三部分组成:base64url 编码的头部、base64url 编码的有效载荷(声明)以及使用 SHA-256 算法生成的加密签名。 然后将此令牌保存为 jwt_token 预请求变量,可用于验证请求。

< {% const CLIENT_ID = request.environment.get("client_id"); // get variable from http-client.private.env.json const SECRET = request.environment.get("secret"); // get variable from http-client.private.env.json function base64UrlEncode(input) { const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; let binary = ""; for (let i = 0; i < input.length; i++) { binary += input.charCodeAt(i).toString(2).padStart(8, "0"); } let base64 = ""; for (let i = 0; i < binary.length; i += 6) { const chunk = binary.substring(i, i + 6); const index = parseInt(chunk.padEnd(6, "0"), 2); base64 += chars[index]; } return base64.replace(/=/g, "").replace(/\+/g, "-").replace(/\//g, "_"); } const header = { alg: "HS256", typ: "JWT" }; const payload = { iat: Math.floor(Date.now() / 1000), client_id: CLIENT_ID, iss: CLIENT_ID, user_id: "dev-user", user_representation: "dev user" }; const encodedHeader = base64UrlEncode(JSON.stringify(header)); const encodedPayload = base64UrlEncode(JSON.stringify(payload)); const unsignedToken = `${encodedHeader}.${encodedPayload}`; const signature = crypto.hmac.sha256() .withTextSecret(SECRET) .updateWithText(unsignedToken) .digest().toBase64(true); const token = `${unsignedToken}.${signature}`; request.variables.set("jwt_token", token); %} GET https://example.com/api/path Authorization: Bearer {{jwt_token}}
最后修改日期: 2025年 6月 25日