- Notifications
You must be signed in to change notification settings - Fork6
Simple way to provide elegant obfuscated ids and text
License
mguymon/obfuscate
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
A simple way to obfuscate ids and text. Useful when you have to make ids visibleto users. Integrates directly with Rails 3.
The goal is to make simple obfuscated ids that are not huge. This is achieved by using animplementation ofBlowfishand encrypting a single block. This produces a nice id of 11 characters (11 since thetrailing= is removed by default), for example3NINgAbOhPc
. The caveat is, the idmust be within99,999,999, e.g. a max length of8.
Text can be obfuscated using Blowfish's string encryption as well, but than it producesoutput that is larger than the elegant 11 character from single block encryption.
https://github.com/mguymon/obfuscate
gem install obfuscate
A simple example
Obfuscate.setup do |config| config.salt = "A weak salt ..." # Length must be between 1-56 config.mode = :string # defaults to :stringendobfuscated = Obfuscate.obfuscate( "test" ) # "HoDruKtafqyLxZxu9s-kYQ=="clarified = Obfuscate.clarify( obfuscated ) # "test"
Create an initializer inconfig/initializers
with:
require 'obfuscate/obfuscatable'Obfuscate.setup do |config| config.salt = "A weak salt ..." # Length must be between 1-56 end
Now add to models that you want to beObfuscatable
, withpossible config options:
class Message < ActiveRecord::Base obfuscatable # a hash of config options can be passed.end
To get the 11 characterobfuscated_id
, which usesmode :block
for the Blowfish single block encryption:
message = Message.find(1)obfuscated = message.obfuscated_id # "NuwhZTtHnko"clarified = message.clarify_id( obfuscated ) # "1"Message.find_obfuscated( obfuscated ) # raises an ActiveRecord::RecordNotFound if the found record does not existMessage.find_by_obfuscated_id( obfuscated ) # returns nil if the found record does not exist
Orobfuscate
a block of text, defaults to mode :string which uses Blowfish string encryption, allowing longerblocks of text to be obfuscated.
obfuscated = message.obfuscate( "if you use your imagination, this is a long block of text" ) # "GoxjVCCuBQgaLvttm7mXNEN9U6A_xxBjM3CYWBrsWs640PVXmkuypo7S8rBHEv_z1jP3hhFqQzlI9L1s2DTQ6FYZwfop-xlA"clarified = message.clarify( obfuscated )
Some benchmarks from my dev machine, with cpu clocking in at 2.53ghz:
Obfuscate.setup :salt => 'a very weak salt indead.'puts Benchmark.bm { |bm| ids = [] bm.report("obfuscate x1000:") { 1000.times { ids << Obfuscate.obfuscate( rand(1...99999999), {:mode => :block} ) } } bm.report("clarify x1000:") { ids.each { |id| Obfuscate.clarify( id, {:mode => :block} ) } }}user system total realobfuscate x1000: 27.110000 0.000000 27.110000 ( 27.145539)clarify x1000: 27.270000 0.000000 27.270000 ( 27.304084)
Licensed to the Apache Software Foundation (ASF) under one or morecontributor license agreements. See the NOTICE file distributed with thiswork for additional information regarding copyright ownership. The ASFlicenses this file to you under the Apache License, Version 2.0 (the"License"); you may not use this file except in compliance with the License.You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, softwaredistributed under the License is distributed on an "AS IS" BASIS, WITHOUTWARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See theLicense for the specific language governing permissions and limitations underthe License.
About
Simple way to provide elegant obfuscated ids and text