Class: SyrupPay::Jwa::AesKeyWrap

Inherits:
Object
  • Object
show all
Defined in:
lib/jose/jwa/alg/aes_key_wrap.rb

Direct Known Subclasses

A128Kw, A256Kw

Defined Under Namespace

Classes: InvalidKeyLengthError

Instance Method Summary collapse

Constructor Details

#initialize(length) ⇒ AesKeyWrap

Returns a new instance of AesKeyWrap.



10
11
12
# File 'lib/jose/jwa/alg/aes_key_wrap.rb', line 10

def initialize(length)
  @length = length
end

Instance Method Details

#decryption(key, wrapped_cek) ⇒ Object



32
33
34
35
# File 'lib/jose/jwa/alg/aes_key_wrap.rb', line 32

def decryption(key, wrapped_cek)
  key_binary = str_to_binary(key)
  AESKeyWrap.unwrap(wrapped_cek, key_binary)
end

#encryption(key, cek) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/jose/jwa/alg/aes_key_wrap.rb', line 14

def encryption(key, cek)
  valid_key_length!(key, @length)

  if cek.instance_of? SyrupPay::Jwa::ContentEncryptionKeyGenerator
    kek = cek.generate_random_key
  elsif cek.is_a? String
    kek = cek
  elsif cek.is_a? Array
    kek = cek.pack('C*')
  end

  key_binary = str_to_binary(key)
  cek_binary = str_to_binary(kek)

  wrapped_key = AESKeyWrap.wrap(cek_binary, key_binary)
  [kek, wrapped_key]
end