Sha256: 7c43112b5eed79a54f008aa27b73f36eaf8c8d3403411834180a6957de0777a9

Contents?: true

Size: 1.29 KB

Versions: 1

Compression:

Stored size: 1.29 KB

Contents

= HashHash

Generates MD5 digests of Hashes (and Arrays) indifferent to key type and ordering.

Useful for hashing rows in a 2-dimensional table.

Used by the [remote_table](https://github.com/seamusabshere/remote_table) gem.

== Example

=== Indifferent to key type

    HashDigest.hexdigest(:a => 1)  #=> '3872c9ae3f427af0be0ead09d07ae2cf'
    HashDigest.hexdigest('a' => 1) #=> '3872c9ae3f427af0be0ead09d07ae2cf'

=== Indifferent to key order

    HashDigest.hexdigest(:a => 1, 'b' => 2) == HashDigest.hexdigest('a' => 1, :b => 2) # true
    HashDigest.hexdigest(:a => 1, 'b' => 2) == HashDigest.hexdigest(:b => 2, 'a' => 1) # true

== Algorithm

Basically represent the hash as a URL querystring, ordered by key, and MD5 that.

1. Stringify keys
2. Create an array of strings like "url_encode(key)=url_encode(value)" by going through each key in alphabetical order
3. Join the array together with "&"
4. MD5 hexdigest the joined string

To digest an array, just pretend it's a hash with keys like 1, 2, 3, etc.

== Potential issues

* Uses MD5 (not cryptographically awesome)
* Uses ActiveSupport's <tt>#to_query</tt> method to create a digestible string like "foo=bar&baz=bam" (slow)
* Meant for flat hashes, e.g. { :a => 1, :b => 2 } and not { :x => { :y => :z } }

== Copyright

Copyright 2011 Seamus Abshere

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hash_digest-1.0.0 README.markdown