lib/fernet/generator.rb in fernet-1.5 vs lib/fernet/generator.rb in fernet-1.6
- old
+ new
@@ -1,6 +1,7 @@
require 'base64'
+require 'yajl'
require 'openssl'
require 'date'
module Fernet
class Generator
@@ -13,17 +14,17 @@
@data = {}
end
def generate
yield self if block_given?
- data.merge!(:issued_at => DateTime.now.to_s)
+ data.merge!(:issued_at => DateTime.now)
if encrypt?
iv = encrypt_data!
@payload = "#{base64(data)}|#{base64(iv)}"
else
- @payload = base64(Fernet::OkJson.encode(stringify_hash_keys(data)))
+ @payload = base64(Yajl::Encoder.encode(data))
end
mac = OpenSSL::HMAC.hexdigest('sha256', payload, signing_key)
"#{payload}|#{mac}"
end
@@ -44,11 +45,11 @@
cipher = OpenSSL::Cipher.new('AES-128-CBC')
cipher.encrypt
iv = cipher.random_iv
cipher.iv = iv
cipher.key = encryption_key
- @data = cipher.update(Fernet::OkJson.encode(stringify_hash_keys(data))) + cipher.final
+ @data = cipher.update(Yajl::Encoder.encode(data)) + cipher.final
iv
end
def base64(chars)
Base64.urlsafe_encode64(chars)
@@ -64,13 +65,7 @@
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