Module: ClaimToken::Encryptor

Defined in:
lib/claim_token/encryptor.rb

Class Method Summary collapse

Class Method Details

.build_cipherObject



31
32
33
34
35
36
37
# File 'lib/claim_token/encryptor.rb', line 31

def build_cipher
  cipher = OpenSSL::Cipher::Cipher.new(cipher_type)
  cipher.encrypt
  cipher.key = encryption_key
  cipher.iv = iv = cipher.random_iv
  return [cipher, iv]
end

.cipher_typeObject



39
40
41
# File 'lib/claim_token/encryptor.rb', line 39

def cipher_type
  ClaimToken.configuration.cipher_type
end

.encode(raw_string) ⇒ Object



51
52
53
# File 'lib/claim_token/encryptor.rb', line 51

def encode raw_string
  Base64.urlsafe_encode64 raw_string
end

.encrypt(message) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/claim_token/encryptor.rb', line 11

def encrypt message
  message_json = JSON.dump(message)

  cipher, iv = build_cipher

  encrypted_data = encrypt_message_with_cipher message_json, cipher

  encoded_encrypted_data = encode(encrypted_data)

  signature = ClaimToken::Signer.sign(encoded_encrypted_data)

  {
    "type" => "EncryptedMessage",
    "cipher" => cipher_type,
    "data" => encoded_encrypted_data,
    "iv" => encode(iv),
    "signature" => signature,
  }
end

.encrypt_message_with_cipher(message, cipher) ⇒ Object



47
48
49
# File 'lib/claim_token/encryptor.rb', line 47

def encrypt_message_with_cipher message, cipher
  cipher.update(message.encode("UTF-8")) + cipher.final
end

.encryption_keyObject



43
44
45
# File 'lib/claim_token/encryptor.rb', line 43

def encryption_key
  ClaimToken.configuration.shared_encryption_key
end