class Tls::Sig::Api::V2::Server

class Error < StandardError; end Your code goes here…

Attributes

key[RW]
sdkappid[RW]

Public Class Methods

new(sdkappid, key) click to toggle source
# File lib/tls/sig/api/v2.rb, line 18
def initialize(sdkappid, key)
  @sdkappid = sdkappid
  @key = key
end

Public Instance Methods

gen_sig( identifier, expire) click to toggle source

/**

* 生成 usersig
* @param identifier 用户账号
* @param expire 有效期,单位秒
* @returns {string} 返回的 sig 值
*/
# File lib/tls/sig/api/v2.rb, line 29
def gen_sig( identifier,  expire) 
   gen_sig_with_userbuf(identifier, expire, nil);
end
gen_sig_with_userbuf( identifier, expire, userbuf) click to toggle source

/**

  • 生成带 userbuf 的 usersig

  • @param identifier 用户账号

  • @param expire 有效期,单位秒

  • @param userBuf 用户数据

  • @returns {string} 返回的 sig 值

*/

# File lib/tls/sig/api/v2.rb, line 40
def gen_sig_with_userbuf(  identifier,   expire,   userbuf)
  currTime = Time.now.to_i
  sigDoc = {
    "TLS.ver": "2.0",
    "TLS.identifier": identifier,
    "TLS.sdkappid": sdkappid ,
    "TLS.expire": expire ,
    "TLS.time": currTime
  }
  base64UserBuf = nil
  if userbuf != nil 
    base64UserBuf = Base64.strict_encode64(userbuf)
    sigDoc["TLS.userbuf"] = base64UserBuf
  end
  sig = hmacsha256(identifier, currTime, expire, base64UserBuf);
  if sig == ""
    return "";
  end       
  sigDoc["TLS.sig"] = sig
  json = sigDoc.to_json
  data_compressed = Zlib::Deflate.deflate(json)
  base64str = Base64.strict_encode64(data_compressed) 
  escape(base64str)
end

Protected Instance Methods

escape(str) click to toggle source
# File lib/tls/sig/api/v2.rb, line 78
def escape(str)
  str.gsub(/\+/, '*').gsub(/\//, '-').gsub(/=/, '_')
end
hmacsha256( identifier, currTime, expire, base64Userbuf) click to toggle source
# File lib/tls/sig/api/v2.rb, line 67
 def hmacsha256(  identifier,   currTime,   expire,   base64Userbuf)
   contentToBeSigned = "TLS.identifier:#{identifier}\n" + "TLS.sdkappid:#{sdkappid}\n" + "TLS.time:#{currTime}\n" + "TLS.expire:#{expire}\n";
  if base64Userbuf != nil 
      contentToBeSigned += "TLS.userbuf:" + base64Userbuf + "\n";
  end
  res = OpenSSL::HMAC.digest("sha256", key, contentToBeSigned)
  res = Base64.strict_encode64(res) 
  res
end