Class: TagAuth::EncryptionHelper
- Inherits:
-
Object
- Object
- TagAuth::EncryptionHelper
- Defined in:
- lib/tag_auth/encryption_helper.rb
Instance Method Summary collapse
- #decrypt(encrypted_string) ⇒ Object
- #encrypt(data) ⇒ Object
-
#initialize(algorithm, key) ⇒ EncryptionHelper
constructor
A new instance of EncryptionHelper.
Constructor Details
#initialize(algorithm, key) ⇒ EncryptionHelper
Returns a new instance of EncryptionHelper.
6 7 8 9 10 11 12 |
# File 'lib/tag_auth/encryption_helper.rb', line 6 def initialize(algorithm, key) raise ArgumentError, 'Invalid algorithm' unless valid_algorithm?(algorithm.downcase) raise ArgumentError, 'Invalid key length' unless valid_key?(key, algorithm) @algorithm = algorithm.downcase @key = key end |
Instance Method Details
#decrypt(encrypted_string) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/tag_auth/encryption_helper.rb', line 25 def decrypt(encrypted_string) decipher = OpenSSL::Cipher.new(@algorithm) decipher.decrypt decipher.key = @key decoded_data = Base64.decode64(encrypted_string) iv_len = decipher.iv_len data_len = decoded_data.length - iv_len encrypted_data = decoded_data[0, data_len] iv = decoded_data[data_len, iv_len] decipher.iv = iv decipher.update(encrypted_data) + decipher.final end |
#encrypt(data) ⇒ Object
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/tag_auth/encryption_helper.rb', line 14 def encrypt(data) cipher = OpenSSL::Cipher.new(@algorithm) cipher.encrypt cipher.key = @key iv = cipher.random_iv encrypted_data = cipher.update(data) + cipher.final Base64.encode64(encrypted_data + iv) end |