lib/fernet/generator.rb in fernet-1.4 vs lib/fernet/generator.rb in fernet-1.5

- old
+ new

@@ -1,7 +1,6 @@ require 'base64' -require 'yajl' require 'openssl' require 'date' module Fernet class Generator @@ -14,17 +13,17 @@ @data = {} end def generate yield self if block_given? - data.merge!(:issued_at => DateTime.now) + data.merge!(:issued_at => DateTime.now.to_s) if encrypt? iv = encrypt_data! @payload = "#{base64(data)}|#{base64(iv)}" else - @payload = base64(Yajl::Encoder.encode(data)) + @payload = base64(Fernet::OkJson.encode(stringify_hash_keys(data))) end mac = OpenSSL::HMAC.hexdigest('sha256', payload, signing_key) "#{payload}|#{mac}" end @@ -45,11 +44,11 @@ cipher = OpenSSL::Cipher.new('AES-128-CBC') cipher.encrypt iv = cipher.random_iv cipher.iv = iv cipher.key = encryption_key - @data = cipher.update(Yajl::Encoder.encode(data)) + cipher.final + @data = cipher.update(Fernet::OkJson.encode(stringify_hash_keys(data))) + cipher.final iv end def base64(chars) Base64.urlsafe_encode64(chars) @@ -65,7 +64,13 @@ def encrypt? @encrypt end + def stringify_hash_keys(hash) + hash.inject({}) do |result, (k, v)| + result[k.to_s] = v + result + end + end end end