Created
November 17, 2016 06:55
-
-
Save everwanna/bc8664d6d156268cd6629dc33133c173 to your computer and use it in GitHub Desktop.
Agora Dynamic Key 4 in Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "openssl" | |
module DynamicKey | |
def generate_media_channel_key(app_id, app_certificate, channel_name, unix_ts, random_int, uid, expired_ts) | |
channel_name = channel_name.to_s | |
expired_ts = expired_ts.to_s | |
p "in generate_media_channel_key" | |
return do_generate(app_id, app_certificate, channel_name, unix_ts, random_int, uid, expired_ts, 'ACS') | |
end | |
def do_generate app_id, app_certificate, channel_name, unix_ts, random_int, uid, expired_ts, service_type | |
p "in do_generate" | |
uid = uid.to_s | |
version = "004" | |
unix_ts_str = unix_ts.to_s #Unix Time stamp, track time as a running total of seconds | |
rnd_txt = random_int.to_s(16) | |
random_int_str = ("00000000" + rnd_txt)[rnd_txt.length..-1] | |
expired_ts_str = ("0000000000" + expired_ts)[expired_ts.length..-1] | |
uid_str = ("0000000000" + uid)[uid.length..-1] | |
sign = generate_signature4(app_id, app_certificate, channel_name, unix_ts_str, random_int_str, uid_str, expired_ts_str, service_type) | |
p sign | |
return version + sign + app_id + unix_ts_str + random_int_str + expired_ts_str | |
end | |
def generate_signature4(app_id, app_certificate, channel_name, unix_ts_str, random_int_str, uid_str, expired_ts_str, service_type) | |
p "in generate_signature4" | |
data = service_type + app_id + unix_ts_str + random_int_str + channel_name + uid_str + expired_ts_str | |
p uid_str | |
p expired_ts_str | |
p data | |
digest = OpenSSL::Digest::SHA1.new | |
return OpenSSL::HMAC.hexdigest(digest, app_certificate, data) | |
end | |
end | |
class CDynamicKey | |
include DynamicKey | |
end | |
# app_id = "970ca35de60c44645bbae8a215061b33" | |
# app_certificate = "5cfd2fd1755d40ecb72977518be15d3b" | |
# channel = "7d72365eb983485397e3e3f9d460bdda" | |
# ts = 1446455472 | |
# r = 58964981 | |
# uid = 2882341273 | |
# expired_ts = 1446455471 | |
dkey = CDynamicKey.new | |
p dkey.generate_media_channel_key("ae0f2c8945d24128a75dc5ab35c1b6c1", "4df2344d6fb947ea8728d8a028a00b08", "582c578b6300cf00064aef2d", 1479301006, 8901830, 0, 0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment