hideous 0.0.3

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.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'rake'
4
+
5
+ # Specify your gem's dependencies in hideous.gemspec
6
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Morton Jonuschat
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,29 @@
1
+ # Hideous
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'hideous'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install hideous
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,17 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/hideous/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Morton Jonuschat"]
6
+ gem.email = ["yabawock@gmail.com"]
7
+ gem.description = %q{Hide/Obfuscate ActiveRecord IDs in a url using a modified Knuth hash}
8
+ gem.summary = %q{A simple Rails plugin to obfuscate ActiveRecord IDs a bit}
9
+ gem.homepage = "https://github.com/yabawock/hideous"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "hideous"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Hideous::VERSION
17
+ end
@@ -0,0 +1,60 @@
1
+ require "hideous/version"
2
+
3
+ module Hideous
4
+
5
+ def obfuscate_id(options = {})
6
+ require 'hideous/xor_knuth_hash'
7
+ extend ClassMethods
8
+ include InstanceMethods
9
+ cattr_accessor :hideous_prime
10
+ cattr_accessor :hideous_prime_inverse
11
+ cattr_accessor :hideous_rndxor
12
+ self.hideous_prime = (options[:prime] || hideous_default_prime)
13
+ self.hideous_prime_inverse = (options[:prime_inverse] || hideous_default_prime_inverse)
14
+ self.hideous_rndxor = (options[:rndxor] || hideous_default_rndxor)
15
+ end
16
+
17
+ def self.hide(id, hideous_prime, hideous_prime_inverse, hideous_rndxor)
18
+ XorKnuthHash.hash(id, hideous_prime, hideous_prime_inverse, hideous_rndxor)
19
+ end
20
+
21
+ def self.show(id, hideous_prime, hideous_prime_inverse, hideous_rndxor)
22
+ XorKnuthHash.reverse_hash(id, hideous_prime, hideous_prime_inverse, hideous_rndxor)
23
+ end
24
+
25
+
26
+ module ClassMethods
27
+ def find(*args)
28
+ if has_obfuscated_id?
29
+ args[0] = Hideous.show(args[0], self.hideous_prime, self.hideous_prime_inverse, self.hideous_rndxor)
30
+ end
31
+ super(*args)
32
+ end
33
+
34
+ def has_obfuscated_id?
35
+ true
36
+ end
37
+
38
+ def hideous_default_prime
39
+ 413_158_511
40
+ end
41
+
42
+ def hideous_default_prime_inverse
43
+ 1_711_444_623
44
+ end
45
+
46
+ def hideous_default_rndxor
47
+ 652_109_907
48
+ end
49
+ end
50
+
51
+ module InstanceMethods
52
+ def to_param
53
+ Hideous.hide(self.id, self.hideous_prime, self.hideous_prime_inverse, self.hideous_rndxor)
54
+ end
55
+
56
+ end
57
+
58
+ end
59
+
60
+ ActiveRecord::Base.extend Hideous
@@ -0,0 +1,3 @@
1
+ module Hideous
2
+ VERSION = "0.0.3"
3
+ end
@@ -0,0 +1,59 @@
1
+ class XorKnuthHash
2
+ MAXID = 2147483647
3
+ # Convience class method pointing to the instance method
4
+ def self.hash(record_id, prime, prime_inverse, rndxor)
5
+ new(record_id, prime, prime_inverse, rndxor).hash
6
+ end
7
+
8
+ # Convience class method pointing to the instance method
9
+ def self.reverse_hash(hash, prime, prime_inverse, rndxor)
10
+ new(hash, prime, prime_inverse, rndxor).reverse_hash
11
+ end
12
+
13
+ def self.calculate_prime_inverse(prime)
14
+ self.modinv(prime, MAXID+1)
15
+ end
16
+
17
+ def initialize(id_or_hash, prime, prime_inverse, rndxor)
18
+ @id_or_hash = id_or_hash
19
+ @prime = prime
20
+ @prime_inverse = prime_inverse
21
+ @rndxor = rndxor
22
+ end
23
+
24
+ # obfuscates an integer
25
+ def hash
26
+ (((@id_or_hash * @prime) & MAXID) ^ @rndxor).to_s(16)
27
+ end
28
+
29
+ # de-obfuscates an integer
30
+ def reverse_hash
31
+ ((@id_or_hash.to_i(16) ^ @rndxor) * @prime_inverse) & MAXID
32
+ end
33
+
34
+ private
35
+
36
+ # Modular inverse
37
+ # Returns x so that ax ≡ 1 (mod m)
38
+ def self.modinv(a, m)
39
+ x, y, g = egcd(a, m)
40
+ if g==1
41
+ return x%m
42
+ else
43
+ return nil
44
+ end
45
+ end
46
+
47
+ # Extended great common divisor
48
+ # Returns x, y and gcd(a,b) so that ax + by = gcd(a,b)
49
+ def self.egcd(a, b)
50
+ x, x1, y, y1 = 1, 0, 0, 1
51
+ while b!=0
52
+ q = a/b
53
+ x, x1 = x1, x-q*x1
54
+ y, y1 = y1, y-q*y1
55
+ a, b = b, a-q*b
56
+ end
57
+ return x, y, a
58
+ end
59
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hideous
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Morton Jonuschat
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-07-05 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Hide/Obfuscate ActiveRecord IDs in a url using a modified Knuth hash
15
+ email:
16
+ - yabawock@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - LICENSE
24
+ - README.md
25
+ - Rakefile
26
+ - hideous.gemspec
27
+ - lib/hideous.rb
28
+ - lib/hideous/version.rb
29
+ - lib/hideous/xor_knuth_hash.rb
30
+ homepage: https://github.com/yabawock/hideous
31
+ licenses: []
32
+ post_install_message:
33
+ rdoc_options: []
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ! '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ segments:
43
+ - 0
44
+ hash: -488321943809870856
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ none: false
47
+ requirements:
48
+ - - ! '>='
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ segments:
52
+ - 0
53
+ hash: -488321943809870856
54
+ requirements: []
55
+ rubyforge_project:
56
+ rubygems_version: 1.8.23
57
+ signing_key:
58
+ specification_version: 3
59
+ summary: A simple Rails plugin to obfuscate ActiveRecord IDs a bit
60
+ test_files: []