Module: Legate::Auth::Encryption
- Defined in:
- lib/legate/auth/encryption.rb
Overview
Provides encryption and decryption utilities for sensitive authentication data. Uses the rbnacl gem for authenticated encryption.
Constant Summary collapse
- ENV_KEY_NAME =
Environment variable name for the encryption key
'LEGATE_AUTH_ENCRYPTION_KEY'- ENCRYPTION_HEADER =
Header added to encrypted data for identification
'LGTAUTH'
Class Method Summary collapse
-
.decrypt(encrypted_data, key = nil) ⇒ String
Decrypts sensitive data.
-
.encrypt(data, key = nil) ⇒ String
Encrypts sensitive data.
-
.encrypted?(data) ⇒ Boolean
Checks if the encrypted data is in the expected format.
-
.generate_key ⇒ String
Generates a new random encryption key.
Class Method Details
.decrypt(encrypted_data, key = nil) ⇒ String
Decrypts sensitive data
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/legate/auth/encryption.rb', line 38 def decrypt(encrypted_data, key = nil) require_rbnacl encryption_key = key || get_encryption_key # Check format and remove header raise ArgumentError, 'Invalid encrypted data format' unless encrypted_data.to_s.start_with?(ENCRYPTION_HEADER) encoded = encrypted_data.to_s[ENCRYPTION_HEADER.length..-1] require 'base64' encrypted = Base64.strict_decode64(encoded) box = create_box(encryption_key) box.decrypt(encrypted) rescue RbNaCl::CryptoError => e raise ArgumentError, "Decryption failed: #{e.}" rescue ArgumentError => e raise ArgumentError, "Invalid Base64 encoding: #{e.}" end |
.encrypt(data, key = nil) ⇒ String
Encrypts sensitive data
22 23 24 25 26 27 28 29 30 |
# File 'lib/legate/auth/encryption.rb', line 22 def encrypt(data, key = nil) require_rbnacl encryption_key = key || get_encryption_key require 'base64' box = create_box(encryption_key) encrypted = box.encrypt(data.to_s) "#{ENCRYPTION_HEADER}#{Base64.strict_encode64(encrypted)}" end |
.encrypted?(data) ⇒ Boolean
Checks if the encrypted data is in the expected format
70 71 72 |
# File 'lib/legate/auth/encryption.rb', line 70 def encrypted?(data) data.to_s.start_with?(ENCRYPTION_HEADER) end |
.generate_key ⇒ String
Generates a new random encryption key
60 61 62 63 64 65 |
# File 'lib/legate/auth/encryption.rb', line 60 def generate_key require_rbnacl require 'base64' raw_key = RbNaCl::Random.random_bytes(RbNaCl::SecretBox.key_bytes) Base64.strict_encode64(raw_key) end |