class Wechat::Validation::Signature
Signature
是参数签名类,用于生成参数签名。如: nonce = # Get the nonce from the request parameters. timestamp = # Get the timestamp from the request parameters. token = # Load the token from the configuration file. actual_signature = # Get the actual signature from the request parameters. expected_signature = Wechat::Validation::Signature.create
nonce, timestamp, token if expected_signature==actual_signature
# The signature is matched.
else
# The signature is not matched.
end
Public Class Methods
create(nonce, timestamp, token)
click to toggle source
开发者通过检验signature对请求进行校验(下面有校验方式)。 若确认此次GET请求来自微信服务器,请原样返回echostr参数内容,则接入生效,成为开发者成功,否则接入失败。 加密/校验流程如下:
-
将token、timestamp、nonce三个参数进行字典序排序
-
将三个参数字符串拼接成一个字符串进行sha1加密
-
开发者获得加密后的字符串可与signature对比,标识该请求来源于微信
# File lib/wechat/validation/signature.rb, line 28 def self.create(nonce, timestamp, token) assert_present! :nonce, nonce assert_present! :timestamp, timestamp assert_present! :token, token Digest::SHA1.hexdigest [ nonce.to_s, timestamp.to_s, token.to_s ].sort.join end