lib/jwe/enc/aes_cbc_hs.rb in jwe-0.1.0 vs lib/jwe/enc/aes_cbc_hs.rb in jwe-0.1.1

- old
+ new

@@ -20,32 +20,32 @@ ciphertext = cipher.update(cleartext) + cipher.final length = [ciphertext.length * 8].pack('Q>') # 64bit big endian to_sign = authenticated_data + iv + ciphertext + length signature = OpenSSL::HMAC.digest(OpenSSL::Digest.new(hash_name), mac_key, to_sign) - self.tag = signature[0 ... mac_key.length] + self.tag = signature[0...mac_key.length] ciphertext end def decrypt(ciphertext, authenticated_data) raise JWE::BadCEK.new("The supplied key is invalid. Required length: #{key_length}") if cek.length != key_length length = [ciphertext.length * 8].pack('Q>') # 64bit big endian to_sign = authenticated_data + iv + ciphertext + length signature = OpenSSL::HMAC.digest(OpenSSL::Digest.new(hash_name), mac_key, to_sign) - if signature[0 ... mac_key.length] != tag - raise JWE::InvalidData.new("Authentication tag verification failed") + if signature[0...mac_key.length] != tag + raise JWE::InvalidData.new('Authentication tag verification failed') end cipher.decrypt cipher.key = enc_key cipher.iv = iv cipher.update(ciphertext) + cipher.final rescue OpenSSL::Cipher::CipherError - raise JWE::InvalidData.new("Invalid ciphertext or authentication tag") + raise JWE::InvalidData.new('Invalid ciphertext or authentication tag') end def iv @iv ||= SecureRandom.random_bytes(16) end @@ -53,24 +53,24 @@ def cek @cek ||= SecureRandom.random_bytes(key_length) end def mac_key - cek[0 ... key_length / 2] + cek[0...key_length / 2] end def enc_key - cek[key_length / 2 .. -1 ] + cek[key_length / 2..-1] end def cipher @cipher ||= OpenSSL::Cipher.new(cipher_name) rescue RuntimeError raise JWE::NotImplementedError.new("The version of OpenSSL linked to your Ruby does not support the cipher #{cipher_name}.") end def tag - @tag || "" + @tag || '' end def self.included(base) base.extend(ClassMethods) end