Class: Fadada::Digest

Inherits:
Object
  • Object
show all
Defined in:
lib/fadada/digest.rb

Class Method Summary collapse

Class Method Details

.generate(timestamp, options = {}) ⇒ Object

生成摘要 Base64(SHA1(app_id + MD5(timestamp + 参数集合1) + SHA1(app_secret + 参数集合) + 参数集合2 )) 示例: options = {

_params: { a: 1, b: 2 },
_md5_params: { a: 1, b: 2 },
_extend_params: { a: 1, b: 2 }

}



13
14
15
16
17
18
19
20
# File 'lib/fadada/digest.rb', line 13

def self.generate(timestamp, options = {})
  _biz_data = Fadada::config.app_secret + params_transform(options[:_params])
  _sha1_biz_data = ::Digest::SHA1.hexdigest(_biz_data).upcase
  _md5 = ::Digest::MD5.hexdigest(params_transform(options[:_md5_params]) + timestamp.to_s).upcase
  _extend = params_transform(options[:_extend_params])
  _data = ::Digest::SHA1.hexdigest(::Fadada::config.app_id + _md5 + _sha1_biz_data + _extend).upcase
  Base64.strict_encode64(_data)
end

.verify?(params = {}) ⇒ Boolean

验证摘要

Returns:

  • (Boolean)


24
25
26
27
28
29
# File 'lib/fadada/digest.rb', line 24

def self.verify?(params = {})
  options = params.transform_keys(&:to_sym)
  _timestamp = options[:timestamp]
  digest_params = { _params: { transaction_id: options[:transaction_id] } }
  options[:msg_digest].to_s == generate(_timestamp, digest_params)
end