obfuscated 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/obfuscated.rb +9 -12
- metadata +2 -1
data/lib/obfuscated.rb
CHANGED
@@ -11,10 +11,9 @@ module Obfuscated
|
|
11
11
|
end
|
12
12
|
|
13
13
|
def self.supported?
|
14
|
-
|
15
|
-
@@db_support ||= db.include?('mysql') || db.include?('postgresql') ? true : false
|
14
|
+
@@mysql_support ||= ActiveRecord::Base.connection.class.to_s.downcase.include?('mysql') ? true : false
|
16
15
|
end
|
17
|
-
|
16
|
+
|
18
17
|
module Finder
|
19
18
|
def find( *primary_key )
|
20
19
|
# Sale.find( '7e2d2c4da1b0' )
|
@@ -47,18 +46,14 @@ module Obfuscated
|
|
47
46
|
return find_by_id(hash, options) unless Obfuscated::supported?
|
48
47
|
|
49
48
|
# Update the conditions to use the hash calculation
|
50
|
-
|
51
|
-
|
52
|
-
options.update(:conditions => ["substring(encode(digest(concat('---',id::text,'-WICKED-#{self.table_name}-#{Obfuscated::salt}'), 'sha1'), 'hex'), 1, 12) = ?", hash])
|
53
|
-
elsif db.include?('mysql')
|
54
|
-
options.update(:conditions => ["SUBSTRING(SHA1(CONCAT('---',#{self.table_name}.id,'-WICKED-#{self.table_name}-#{Obfuscated::salt}')),1,12) = ?", hash])
|
55
|
-
end
|
49
|
+
options.update(:conditions => ["SUBSTRING(SHA1(CONCAT('---',#{self.table_name}.id,'-WICKED-#{self.table_name}-#{Obfuscated::salt}')),1,12) = ?", hash])
|
50
|
+
|
56
51
|
# Find it!
|
57
52
|
first(options) or raise ActiveRecord::RecordNotFound, "Couldn't find #{self.class.to_s} with Hashed ID=#{hash}"
|
58
53
|
end
|
59
54
|
end
|
60
55
|
end
|
61
|
-
|
56
|
+
|
62
57
|
end
|
63
58
|
|
64
59
|
module InstanceMethods
|
@@ -70,7 +65,9 @@ module Obfuscated
|
|
70
65
|
return id unless Obfuscated::supported?
|
71
66
|
|
72
67
|
# Use SHA1 to generate a consistent hash based on the id and the table name
|
73
|
-
@hashed_id ||= Digest::SHA1.hexdigest(
|
68
|
+
@hashed_id ||= Digest::SHA1.hexdigest(
|
69
|
+
"---#{id}-WICKED-#{self.class.table_name}-#{Obfuscated::salt}"
|
70
|
+
)[0..11]
|
74
71
|
end
|
75
72
|
|
76
73
|
def to_param
|
@@ -79,4 +76,4 @@ module Obfuscated
|
|
79
76
|
end
|
80
77
|
end
|
81
78
|
|
82
|
-
ActiveRecord::Base.class_eval { include Obfuscated }
|
79
|
+
ActiveRecord::Base.class_eval { include Obfuscated }
|
metadata
CHANGED