obfuscated 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/obfuscated.rb +60 -0
- metadata +65 -0
data/lib/obfuscated.rb
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'digest/sha1'
|
2
|
+
require 'active_record'
|
3
|
+
|
4
|
+
module Obfuscated
|
5
|
+
@@mysql_support = ActiveRecord::Base.connection.class.to_s.downcase.include?('mysql') ? true : false
|
6
|
+
|
7
|
+
def self.append_features(base)
|
8
|
+
super
|
9
|
+
base.extend(ClassMethods)
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.supported?
|
13
|
+
@@mysql_support
|
14
|
+
end
|
15
|
+
|
16
|
+
module ClassMethods
|
17
|
+
def has_obfuscated_id( options={} )
|
18
|
+
class_eval do
|
19
|
+
|
20
|
+
include Obfuscated::InstanceMethods
|
21
|
+
|
22
|
+
# Uses an 12 character string to find the appropriate record
|
23
|
+
def self.find_by_hashed_id( hash, options={} )
|
24
|
+
# Don't bother if there's no hash provided.
|
25
|
+
return nil if hash.blank?
|
26
|
+
|
27
|
+
# If Obfuscated isn't supported, use ActiveRecord's default finder
|
28
|
+
return find_by_id(hash, options) unless Obfuscated::supported?
|
29
|
+
|
30
|
+
# Update the conditions to use the hash calculation
|
31
|
+
options.update(:conditions => ["SUBSTRING(SHA1(CONCAT('---',#{self.table_name}.id,'-WICKED-#{self.table_name}-')),1,12) = ?", hash])
|
32
|
+
|
33
|
+
# Find it!
|
34
|
+
first(options) or raise ActiveRecord::RecordNotFound, "Couldn't find #{self.class.to_s} with Hashed ID=#{hash}"
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
|
42
|
+
module InstanceMethods
|
43
|
+
# Generate an obfuscated 12 character id incorporating the primary key and the table name.
|
44
|
+
def hashed_id
|
45
|
+
raise 'This record does not have a primary key yet!' if id.blank?
|
46
|
+
|
47
|
+
# If Obfuscated isn't supported, just return the normal id
|
48
|
+
return id unless Obfuscated::supported?
|
49
|
+
|
50
|
+
# Use SHA1 to generate a consistent hash based on the id and the table name
|
51
|
+
@hashed_id ||= Digest::SHA1.hexdigest("---#{id}-WICKED-#{self.class.table_name}-")[0..11]
|
52
|
+
end
|
53
|
+
|
54
|
+
def to_param
|
55
|
+
hashed_id
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
metadata
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: obfuscated
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Jon Collier
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2012-03-20 00:00:00 Z
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: Obfuscate your autoincrementing primary key ids.
|
22
|
+
email: github@joncollier.com
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files: []
|
28
|
+
|
29
|
+
files:
|
30
|
+
- lib/obfuscated.rb
|
31
|
+
homepage: https://github.com/imnotquitejack/obfuscated
|
32
|
+
licenses: []
|
33
|
+
|
34
|
+
post_install_message:
|
35
|
+
rdoc_options: []
|
36
|
+
|
37
|
+
require_paths:
|
38
|
+
- lib
|
39
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
hash: 3
|
45
|
+
segments:
|
46
|
+
- 0
|
47
|
+
version: "0"
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
hash: 3
|
54
|
+
segments:
|
55
|
+
- 0
|
56
|
+
version: "0"
|
57
|
+
requirements: []
|
58
|
+
|
59
|
+
rubyforge_project:
|
60
|
+
rubygems_version: 1.8.18
|
61
|
+
signing_key:
|
62
|
+
specification_version: 3
|
63
|
+
summary: Primary Key Obfuscation
|
64
|
+
test_files: []
|
65
|
+
|