title: JSON generate unique hash value (SHA-512)
body: <p>I'm searching for a way to generate a SHA-512 hash from a json string in Ruby, independent from the positions of the elements in it, and independent from nestings, arrays, nested arrays and so on. I just want to hash the raw data along with its keys.</p>
  
  <p>I tried some approaches with converting the JSON into a ruby hash, deep sort them by their keys, append everything into one, long string and hash it. But I bet that my solution isn't the most efficient one, and that there must be a better way to do this.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>So far, I convert JSON into a Ruby hash. Then I try to use this function to get a canonical representation:</p>
  
  <pre><code>  def self.canonical_string_from_hash value, key=nil
      str = ""
      if value.is_a? Hash
        value.keys.sort.each do |k|
          str += canonical_string_from_hash(value[k], k)
        end
      elsif value.is_a? Array
        str += key.to_s
        value.each do |v|
          str += canonical_string_from_hash(v)
        end
      else
        str += key ? "#{key}#{value}" : value.to_s
      end
      return str
    end
  </code></pre>
  
  <p>But I'm not sure, if this is a good and efficient way to do this.</p>
  
  <p>For example, this hash</p>
  
  <pre><code>hash = {
    id: 3,
    zoo: "test",
    global: [
      {ukulele: "ringding", blub: 3},
      {blub: nil, ukulele: "rangdang", guitar: "stringstring"}
    ],
    foo: {
      ids: [3,4,5],
      bar: "asdf"
    }
  }
  </code></pre>
  
  <p>gets converted to this string:</p>
  
  <pre><code>barasdfids345globalblub3ukuleleringdingblubguitarstringstringukulelerangdangid3zootest
  </code></pre>