ucenter_authcode 0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/ucenter_authcode.rb +78 -0
- metadata +46 -0
@@ -0,0 +1,78 @@
|
|
1
|
+
# coding: UTF-8
|
2
|
+
|
3
|
+
require 'digest/md5'
|
4
|
+
require 'base64'
|
5
|
+
|
6
|
+
module UCenter
|
7
|
+
def self.decode key, string
|
8
|
+
AuthCode.new.authcode(key, string)
|
9
|
+
end
|
10
|
+
|
11
|
+
# 用类包装来防止方法间变量修改
|
12
|
+
class AuthCode
|
13
|
+
KeyLength = 4
|
14
|
+
|
15
|
+
# 参考 https://github.com/daxingplay/dokuwiki_ucenter/blob/master/api/uc.php#L295
|
16
|
+
def authcode key, string
|
17
|
+
# 拆分key,并各自做md5
|
18
|
+
key = md5(key)
|
19
|
+
key_left, key_right = md5(key[0, 16]), md5(key[16, 16])
|
20
|
+
|
21
|
+
# 拆分string auth和content部分
|
22
|
+
string_auth, string_content = string[0, KeyLength], string[KeyLength..-1]
|
23
|
+
|
24
|
+
# 从key_crypt抽取box
|
25
|
+
key_crypt = key_left + md5(key_left + string_auth)
|
26
|
+
key_crypt_length = key_crypt.length
|
27
|
+
rndkey = []
|
28
|
+
0.upto(255) do |i|
|
29
|
+
rndkey[i] = (key_crypt[i % key_crypt_length]).ord
|
30
|
+
end
|
31
|
+
a = b = 0
|
32
|
+
box = (0..255).to_a
|
33
|
+
while b < 256 do
|
34
|
+
a = (a + box[b] + rndkey[b]) % 256
|
35
|
+
box[b], box[a] = box[a], box[b]
|
36
|
+
b +=1
|
37
|
+
end
|
38
|
+
|
39
|
+
# 联合key_crypt和key_content解密出result
|
40
|
+
string_content_ords = base64_url_decode(string_content).bytes.to_a
|
41
|
+
string_content_ords_length = string_content_ords.length
|
42
|
+
a = b = string_idx = 0
|
43
|
+
result = ""
|
44
|
+
while string_idx < string_content_ords_length
|
45
|
+
a = (a + 1) % 256
|
46
|
+
b = (b + box[a]) % 256
|
47
|
+
box[a], box[b] = box[b], box[a]
|
48
|
+
result << (string_content_ords[string_idx] ^ (box[(box[a] + box[b]) % 256])).chr
|
49
|
+
string_idx +=1
|
50
|
+
end
|
51
|
+
|
52
|
+
result_time_valided = (result[0, 10] == '0'*10) || (result[0, 10].to_i - Time.now.to_i > 0)
|
53
|
+
result_string_valided = result[10, 16] == (md5(result[26..-1] + key_right))[0, 16] # 重新加密和string对比验证
|
54
|
+
if (result_time_valided && result_string_valided)
|
55
|
+
return result[26..-1]
|
56
|
+
else
|
57
|
+
return ''
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def microtime
|
62
|
+
epoch_mirco = Time.now.to_f
|
63
|
+
epoch_full = Time.now.to_i
|
64
|
+
epoch_fraction = epoch_mirco - epoch_full
|
65
|
+
epoch_fraction.to_s + ' ' + epoch_full.to_s
|
66
|
+
end
|
67
|
+
|
68
|
+
private
|
69
|
+
def md5 str; Digest::MD5.hexdigest str.to_s end
|
70
|
+
|
71
|
+
def base64_url_decode str
|
72
|
+
mod = str.to_s.length.modulo(4)
|
73
|
+
str2 = "#{str}#{'=' * (4 - mod)}".tr('-_','+/')
|
74
|
+
Base64.decode64 str2
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
78
|
+
end
|
metadata
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ucenter_authcode
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '0.1'
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- iceskysl
|
9
|
+
- David Chen
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
date: 2013-04-25 00:00:00.000000000 Z
|
14
|
+
dependencies: []
|
15
|
+
description:
|
16
|
+
email: mvjome@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- lib/ucenter_authcode.rb
|
22
|
+
homepage: http://rubygems.org/gems/ucenter_authcode
|
23
|
+
licenses: []
|
24
|
+
post_install_message:
|
25
|
+
rdoc_options: []
|
26
|
+
require_paths:
|
27
|
+
- lib
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
29
|
+
none: false
|
30
|
+
requirements:
|
31
|
+
- - ! '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
35
|
+
none: false
|
36
|
+
requirements:
|
37
|
+
- - ! '>='
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
requirements: []
|
41
|
+
rubyforge_project:
|
42
|
+
rubygems_version: 1.8.25
|
43
|
+
signing_key:
|
44
|
+
specification_version: 3
|
45
|
+
summary: decode ucenter user info auth
|
46
|
+
test_files: []
|