faceless 0.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 28783323f888bede9c89a04c90c4f43f8da79fc5
4
+ data.tar.gz: 6ac7a0c1d913529856e90f5a0cf87e1241e04759
5
+ SHA512:
6
+ metadata.gz: 79c45c66504f419282a1fefcb7332d416510d54fcf7ce514d31cf5ba970afd249559cd7bbc93e219124f848e41d604581b600eba91738ecb7269c2573d7f3793
7
+ data.tar.gz: 8e6e5b588aa6aa9f44284fc7c542f1ef427cf2503993f9dc9a38e96ed1fb96329bbc08f293661ec21843dbf23cc014f18d3fa071f2bd8493a900b5d199dcfde6
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .rvmrc
5
+ .config
6
+ .yardoc
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2016 Alex S
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,52 @@
1
+ # Faceless
2
+
3
+ A cool implementation of encryption/decryption in Ruby, borrowed from [UCenter Authcode(comsenz)](http://www.comsenz.com/downloads/install/ucenter).
4
+
5
+ For me, the coolest thing is, everytime it encodes a same string and generates different result, this makes it more secure.
6
+
7
+ ## Installation
8
+
9
+ Just install the gem:
10
+
11
+ ```
12
+ gem install faceless
13
+ ```
14
+
15
+ Or add it to your Gemfile:
16
+
17
+ ```
18
+ gem 'faceless'
19
+ ```
20
+
21
+ ## Configuration
22
+
23
+ ```
24
+ Faceless.configure do |config|
25
+ config.auth_token = 'whatever-token-you-want'
26
+ end
27
+ ```
28
+
29
+ ## Usage
30
+
31
+ #### To encrypt
32
+ ```
33
+ encrypted = Faceless::Authcode.encode("encrypt-me")
34
+ ```
35
+
36
+ #### To decrypt
37
+ ```
38
+ Faceless::Authcode.decode encrypted
39
+ ```
40
+
41
+ ## Note
42
+ The original algorithm implementation was in PHP by comsenz.
43
+
44
+ Thanks to them. :beers:
45
+
46
+ The name of this project is from
47
+ [http://gameofthrones.wikia.com/wiki/Faceless_Men](http://gameofthrones.wikia.com/wiki/Faceless_Men)
48
+
49
+ ## License
50
+
51
+ [MIT](http://opensource.org/licenses/MIT)
52
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/faceless.gemspec ADDED
@@ -0,0 +1,18 @@
1
+ require File.expand_path('../lib/faceless/version', __FILE__)
2
+
3
+ Gem::Specification.new do |gem|
4
+ gem.authors = ["Alex S"]
5
+ gem.email = ["hlcfan.yan@gmail.com.com"]
6
+ gem.description = %q{Ruby Implementation of UCenter Authcode}
7
+ gem.summary = %q{A cool implementation of encryption/decryption in Ruby, borrowed from UCenter(comsenz)}
8
+ gem.homepage = "https://github.com/hlcfan/faceless"
9
+
10
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
11
+ gem.files = `git ls-files`.split("\n")
12
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
13
+ gem.name = "faceless"
14
+ gem.require_paths = ["lib"]
15
+ gem.version = Faceless::VERSION
16
+
17
+ gem.add_development_dependency "bundler", "~> 1.0"
18
+ end
@@ -0,0 +1,145 @@
1
+ require 'digest/md5'
2
+ require 'base64'
3
+ require 'cgi'
4
+
5
+ module Faceless
6
+ class Authcode
7
+
8
+ KEY_LENGTH = 4
9
+
10
+ def initialize string, operation, expiry
11
+ @string = string
12
+ @operation = operation || ''
13
+ @expiry = expiry || 0
14
+ end
15
+
16
+ def self.encode string, expiry = 0
17
+ new(string, 'ENCODE', expiry).authcode
18
+ end
19
+
20
+ def self.decode string, expiry = 0
21
+ new(string, 'DECODE', expiry).authcode
22
+ end
23
+
24
+ def authcode
25
+ key_length = generate_encrypt_key.size
26
+
27
+ string = determine_string
28
+ string_ords = ords(string)
29
+ string_length = string_ords.size
30
+
31
+ result = ''
32
+ box = (0..255).to_a
33
+
34
+ random_key = 0.upto(255).map do |i|
35
+ (generate_encrypt_key[i % key_length]).ord
36
+ end
37
+
38
+ j = i = 0
39
+ while i < 256 do
40
+ j = (j + box[i] + random_key[i]) % 256
41
+ box[i], box[j] = box[j], box[i]
42
+ i += 1
43
+ end
44
+
45
+ a = j = i = 0
46
+ while i < string_length
47
+ a = (a + 1) % 256
48
+ j = (j + box[a]) % 256
49
+ box[a], box[j] = box[j], box[a]
50
+ result += (string_ords[i] ^ (box[(box[a] + box[j]) % 256])).chr
51
+ i += 1
52
+ end
53
+
54
+ return_result result
55
+ end
56
+
57
+ private
58
+
59
+ def auth_token
60
+ @auth_token ||= Faceless.configuration.auth_token
61
+ end
62
+
63
+ def result_match? result
64
+ (result[0,10] == '0'*10 || (result[0, 10]).to_i - Time.now.to_i > 0) &&
65
+ result[10, 16] == (md5(result[26..-1] + keyb))[0, 16]
66
+ end
67
+
68
+ def base_key
69
+ @base_key ||= md5(auth_token)
70
+ end
71
+
72
+ def keya
73
+ @keya ||= md5(base_key[0, 16])
74
+ end
75
+
76
+ def keyb
77
+ @keyb ||= md5(base_key[16, 16])
78
+ end
79
+
80
+ def keyc
81
+ @keyc ||= begin
82
+ if KEY_LENGTH > 0
83
+ if @operation == 'DECODE'
84
+ @string[0, KEY_LENGTH]
85
+ else
86
+ (md5(microtime()))[-KEY_LENGTH..-1]
87
+ end
88
+ else
89
+ ''
90
+ end
91
+ end
92
+ end
93
+
94
+ def inject_timestamp
95
+ timestamp = @expiry > 0 ? @expiry + Time.now.to_i : 0
96
+ "#{sprintf('%010d', timestamp)}#{(md5(@string+keyb))[0, 16]}#{@string}"
97
+ end
98
+
99
+ def generate_encrypt_key
100
+ @key ||= keya + md5(keya+keyc)
101
+ end
102
+
103
+ def determine_string
104
+ if @operation == 'DECODE'
105
+ base64_url_decode(@string[KEY_LENGTH..-1])
106
+ else
107
+ inject_timestamp
108
+ end
109
+ end
110
+
111
+ def return_result result
112
+ if @operation == 'DECODE'
113
+ if result_match? result
114
+ result[26..-1]
115
+ else
116
+ ''
117
+ end
118
+ else
119
+ "#{keyc}#{(Base64.encode64(result)).gsub(/\=/, '')}"
120
+ end
121
+ end
122
+
123
+ def md5 s
124
+ Digest::MD5.hexdigest(s)
125
+ end
126
+
127
+ def base64_url_decode str
128
+ str += '=' * (4 - str.length.modulo(4))
129
+ Base64.decode64(str.tr('-_','+/'))
130
+ end
131
+
132
+ def microtime
133
+ epoch_mirco = Time.now.to_f
134
+ epoch_full = Time.now.to_i
135
+ epoch_fraction = epoch_mirco - epoch_full
136
+
137
+ "#{epoch_fraction} #{epoch_full}"
138
+ end
139
+
140
+ def ords(s)
141
+ s.bytes.to_a
142
+ end
143
+
144
+ end
145
+ end
@@ -0,0 +1,11 @@
1
+ module Faceless
2
+ class Configuration
3
+
4
+ attr_accessor :auth_token
5
+
6
+ def initialize
7
+ @auth_token = nil
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ module Faceless
2
+ VERSION = '0.0.1'
3
+ end
data/lib/faceless.rb ADDED
@@ -0,0 +1,28 @@
1
+ require "faceless/configuration"
2
+ require "faceless/authcode"
3
+
4
+ module Faceless
5
+ class << self
6
+
7
+ attr_accessor :configuration
8
+
9
+ def configuration
10
+ @configuration ||= Configuration.new
11
+ end
12
+
13
+ def configure
14
+ yield configuration
15
+ end
16
+
17
+ def authenticator
18
+ Faceless::Authcode
19
+ end
20
+
21
+ private
22
+
23
+ def method_missing method, *args, &block
24
+ return super unless authenticator.respond_to?(method)
25
+ authenticator.send(method, *args, &block)
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,2 @@
1
+ require "faceless"
2
+ require "faceless/rspec
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: faceless
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Alex S
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-04-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ description: Ruby Implementation of UCenter Authcode
28
+ email:
29
+ - hlcfan.yan@gmail.com.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - .gitignore
35
+ - Gemfile
36
+ - LICENSE
37
+ - README.md
38
+ - Rakefile
39
+ - faceless.gemspec
40
+ - lib/faceless.rb
41
+ - lib/faceless/authcode.rb
42
+ - lib/faceless/configuration.rb
43
+ - lib/faceless/version.rb
44
+ - specs/spec_helper.rb
45
+ homepage: https://github.com/hlcfan/faceless
46
+ licenses: []
47
+ metadata: {}
48
+ post_install_message:
49
+ rdoc_options: []
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - '>='
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ requirements: []
63
+ rubyforge_project:
64
+ rubygems_version: 2.4.5
65
+ signing_key:
66
+ specification_version: 4
67
+ summary: A cool implementation of encryption/decryption in Ruby, borrowed from UCenter(comsenz)
68
+ test_files: []
69
+ has_rdoc: