Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
This repository was archived by the owner on Feb 28, 2024. It is now read-only.
/obfuscatePublic archive

Simple way to provide elegant obfuscated ids and text

License

NotificationsYou must be signed in to change notification settings

mguymon/obfuscate

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

RDoc

Install

gem install obfuscate

Ruby Usage

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"

Rails Integration

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 )

Benchmarks

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)

License

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp