Class: VisaNetUy::Cipher

Inherits:
Object
  • Object
show all
Includes:
Encoder
Defined in:
lib/visa_net_uy/cipher.rb

Constant Summary collapse

CIPHER_ALGORITHM =
"DES-EDE3-CBC"

Instance Method Summary collapse

Methods included from Encoder

#custom_base64_urlsafe_decode, #custom_base64_urlsafe_encode

Instance Method Details

#generate_session_keyObject

Generates a session_key



10
11
12
13
# File 'lib/visa_net_uy/cipher.rb', line 10

def generate_session_key
  # Generate random session key with 16 bytes length
  OpenSSL::Random.random_bytes(16)
end

#urlsafe_base64_asymmetric_decrypt(urlsafe_base64_encrypted_data, private_key) ⇒ Object

Decrypt data using the private_key



28
29
30
31
32
33
34
35
36
37
# File 'lib/visa_net_uy/cipher.rb', line 28

def urlsafe_base64_asymmetric_decrypt(urlsafe_base64_encrypted_data, private_key)
  #  Load key
  pkey = OpenSSL::PKey::RSA.new(private_key, nil)
  raise 'Invalid private key.' unless pkey.private?

  # Decode encrypted data with custom decoding
  encrypted_data = custom_base64_urlsafe_decode(urlsafe_base64_encrypted_data)
  # Decrypt encrypted data
  pkey.private_decrypt(encrypted_data)
end

#urlsafe_base64_asymmetric_encrypt(data, public_key) ⇒ Object

Encrypt data using the public_key



16
17
18
19
20
21
22
23
24
25
# File 'lib/visa_net_uy/cipher.rb', line 16

def urlsafe_base64_asymmetric_encrypt(data, public_key)
  #  Load key
  pkey = OpenSSL::PKey::RSA.new(public_key, nil)
  raise 'Invalid public key.' unless pkey.public?

  # Encrypt data
  encrypted_data = pkey.public_encrypt(data)
  # Encode encrypted data with custom Encoder
  custom_base64_urlsafe_encode(encrypted_data)
end

#urlsafe_base64_symmetric_decrypt(urlsafe_base64_encrypted_data, key, iv) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/visa_net_uy/cipher.rb', line 65

def urlsafe_base64_symmetric_decrypt(urlsafe_base64_encrypted_data, key, iv)
  raise 'Initialization Vector must have 16 hexadecimal characters.' unless iv.length == 16
  raise 'Key must have 16 hexadecimal characters.' unless key.length == 16

  bin_iv = [iv].pack('H*')
  raise 'Initialization Vector is not valid, must contain only hexadecimal characters.' if bin_iv.empty?

  # Appends first 8 Bytes to Key
  key += key.byteslice(0,8)

  # Create Cipher
  cipher = OpenSSL::Cipher.new(CIPHER_ALGORITHM)
  # Initialize cipher mode
  cipher.decrypt
  # Set initialization vector
  cipher.iv = bin_iv
  # Set key
  cipher.key = key

  # Decode data
  encrypted_data = custom_base64_urlsafe_decode(urlsafe_base64_encrypted_data)
  # Decrypt data
  data = cipher.update(encrypted_data) + cipher.final
end

#urlsafe_base64_symmetric_encrypt(data, key, iv) ⇒ Object

Encrypt data with key



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/visa_net_uy/cipher.rb', line 40

def urlsafe_base64_symmetric_encrypt(data, key, iv)
  raise 'Initialization Vector must have 16 hexadecimal characters.' unless iv.length == 16
  raise 'Key must have 16 hexadecimal characters.' unless key.length == 16

  bin_iv = [iv].pack('H*')
  raise 'Initialization Vector is not valid, must contain only hexadecimal characters.' if bin_iv.empty?

  # Appends first 8 Bytes to Key
  key += key.byteslice(0,8)

  # Create Cipher
  cipher = OpenSSL::Cipher.new(CIPHER_ALGORITHM)
  # Initialize cipher mode
  cipher.encrypt
  # Set initialization vector
  cipher.iv = bin_iv
  # Set key
  cipher.key = key

  # Encrypt data
  encrypted_data = cipher.update(data) + cipher.final
  # Encode data
  custom_base64_urlsafe_encode(encrypted_data)
end