Permalink
Browse files

add hasher and serializer options

  • Loading branch information...
1 parent 183898f commit dd58af05c8b2c23b731d220b277f0ed00d7d0fe9 @grosser committed Jan 19, 2010
Showing with 48 additions and 9 deletions.
  1. +11 −1 README.markdown
  2. +11 −2 lib/url_store.rb
  3. +14 −6 lib/url_store/compact_encoder.rb
  4. +12 −0 spec/url_store_spec.rb
View
@@ -1,11 +1,16 @@
Data securely stored in urls.
+ - url-save output
+ - short codes through GZip
+ - serializing through :marshal :yaml
+ - hashing through DSS DSS1 MD2 MD4 MD5 MDC2 RIPEMD160 SHA SHA1 SHA224 SHA256 SHA384 SHA512
+
+
Install
=======
- As gem: ` sudo gem install url_store `
- As Rails plugin: ` script/plugin install git://github.com/grosser/url_store.git `
-
Usage
=====
# config (e.g environment.rb)
@@ -21,6 +26,11 @@ Usage
raise 'FRAUD!'
end
+### Options
+ UrlStore.secret = 'something random'
+ UrlStore.hasher = 'MD5' # default: 'SHA1'
+ UrlStore.serializer = :yaml # default: :marshal
+
Author
=======
[Michael Grosser](http://pragmatig.wordpress.com)
View
@@ -3,7 +3,8 @@
class UrlStore
VERSION = File.read( File.join(File.dirname(__FILE__),'..','VERSION') ).strip
SECRET = 'asdkasjlwqjdqaccxnjkasdfh2313'
- METHOD = 'SHA1'
+ HASHER = 'SHA1'
+ SERIALIZER = :marshal
# (convert to base64url <-> RFC4648) and '|'
# which is not url-safe if you ask ERB/CGI, but browsers accept it
@@ -14,6 +15,14 @@ class UrlStore
def self.secret=(x); @@secret=x; end
def self.secret; @@secret; end
+ @@hasher = HASHER
+ def self.hasher=(x); @@hasher=x; end
+ def self.hasher; @@hasher; end
+
+ @@serializer = SERIALIZER
+ def self.serializer=(x); @@serializer=x; end
+ def self.serializer; @@serializer; end
+
def self.encode(data)
string = encoder.encode(data)
string.to_s.tr(IN,OUT)
@@ -30,6 +39,6 @@ def self.encoder
if secret == SECRET
warn "WARNING: you should not use the default secret! use UrlStore.secret='something'"
end
- UrlStore::CompactEncoder.new(secret, METHOD)
+ UrlStore::CompactEncoder.new(secret, :hasher => hasher, :serializer => serializer)
end
end
@@ -3,8 +3,10 @@
class UrlStore
class CompactEncoder
- def initialize(secret, algorithm)
- @secret = secret; @algorithm = algorithm
+ def initialize(secret, options={})
+ @secret = secret
+ @hasher = options[:hasher] || 'SHA1'
+ @serializer = options[:serializer] || :marshal
end
def encode(data)
@@ -26,15 +28,21 @@ def decode(data)
private
def serialize(data)
- Marshal.dump data
+ case @serializer.to_sym
+ when :yaml then data.to_yaml
+ when :marshal then Marshal.dump(data)
+ end
end
def deserialize(data)
- Marshal.load data
+ case @serializer.to_sym
+ when :yaml then YAML.load(data)
+ when :marshal then Marshal.load(data)
+ end
end
def compress(data)
- Base64.encode64( Zlib::Deflate.deflate data ).gsub("\n",'')
+ Base64.encode64( Zlib::Deflate.deflate(data)).gsub("\n",'')
end
def extract(data)
@@ -48,7 +56,7 @@ def hash_length
# stolen from ActiveSupport
def digest(data)
require 'openssl' unless defined?(OpenSSL)
- OpenSSL::HMAC.hexdigest(OpenSSL::Digest::Digest.new(@algorithm), @secret, data)
+ OpenSSL::HMAC.hexdigest(OpenSSL::Digest::Digest.new(@hasher.to_s), @secret, data)
end
end
end
@@ -46,6 +46,18 @@
UrlStore.encode(x).size.should <= x.size
end
+ it "can serialize using a different method" do
+ old = UrlStore.encode(@data)
+ UrlStore.serializer = :yaml
+ UrlStore.encode(@data).size.should_not == old.size
+ end
+
+ it "can serialize using different hasher" do
+ old = UrlStore.encode(@data)
+ UrlStore.hasher = 'MD5'
+ UrlStore.encode(@data).size.should_not == old.size
+ end
+
it "has a VERSION" do
UrlStore::VERSION.should =~ /^\d+\.\d+\.\d+$/
end

0 comments on commit dd58af0

Please sign in to comment.