Module: JWE::Enc::AesGcm
Defined Under Namespace
Modules: ClassMethods
Instance Attribute Summary collapse
-
#cek ⇒ Object
Returns the value of attribute cek.
-
#iv ⇒ Object
Returns the value of attribute iv.
-
#tag ⇒ Object
Returns the value of attribute tag.
Class Method Summary collapse
Instance Method Summary collapse
- #cipher ⇒ Object
- #decrypt(ciphertext, authenticated_data) ⇒ Object
- #encrypt(cleartext, authenticated_data) ⇒ Object
- #initialize(cek = nil, iv = nil) ⇒ Object
Instance Attribute Details
#cek ⇒ Object
Returns the value of attribute cek.
4 5 6 |
# File 'lib/jwe/enc/aes_gcm.rb', line 4 def cek @cek end |
#iv ⇒ Object
Returns the value of attribute iv.
5 6 7 |
# File 'lib/jwe/enc/aes_gcm.rb', line 5 def iv @iv end |
#tag ⇒ Object
Returns the value of attribute tag.
6 7 8 |
# File 'lib/jwe/enc/aes_gcm.rb', line 6 def tag @tag end |
Class Method Details
.included(base) ⇒ Object
59 60 61 |
# File 'lib/jwe/enc/aes_gcm.rb', line 59 def self.included(base) base.extend(ClassMethods) end |
Instance Method Details
#cipher ⇒ Object
49 50 51 52 53 |
# File 'lib/jwe/enc/aes_gcm.rb', line 49 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 |
#decrypt(ciphertext, authenticated_data) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/jwe/enc/aes_gcm.rb', line 27 def decrypt(ciphertext, authenticated_data) raise JWE::BadCEK.new("The supplied key is too short. Required length: #{key_length}") if cek.length < key_length cipher.decrypt cipher.key = cek cipher.iv = iv cipher.auth_tag = tag cipher.auth_data = authenticated_data cipher.update(ciphertext) + cipher.final rescue OpenSSL::Cipher::CipherError raise JWE::InvalidData.new("Invalid ciphertext or authentication tag") end |
#encrypt(cleartext, authenticated_data) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/jwe/enc/aes_gcm.rb', line 13 def encrypt(cleartext, authenticated_data) raise JWE::BadCEK.new("The supplied key is too short. Required length: #{key_length}") if cek.length < key_length cipher.encrypt cipher.key = cek cipher.iv = iv cipher.auth_data = authenticated_data ciphertext = cipher.update(cleartext) + cipher.final self.tag = cipher.auth_tag ciphertext end |
#initialize(cek = nil, iv = nil) ⇒ Object
8 9 10 11 |
# File 'lib/jwe/enc/aes_gcm.rb', line 8 def initialize(cek = nil, iv = nil) self.iv = iv self.cek = cek end |