loggable_activity 0.1.47 → 0.1.48
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +1 -1
- data/lib/loggable_activity/encryption.rb +25 -18
- data/lib/loggable_activity/encryption_key.rb +4 -1
- data/lib/loggable_activity/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ee3e20a374ac054a343ce5ff1c35c5fcce765fb631788fb6d9b738e52161a16a
|
4
|
+
data.tar.gz: 042cc1db90cf6b99eeb2c8d5ab3f16a5473178c00085f42400c4eec344a99731
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1f4063eb4cd98a16061d151759735e0b5cc98e7747151dc2c56856f2010f23936c2c338991c5cc85dfd3c72af4f60133a4e39271f29ec76e5ee04383ad6dd073
|
7
|
+
data.tar.gz: 8be57a52cfd948825e273648ffaec5809975dc14863f49206ee8f364f07b58a193758d80d867155f21760522a24bc2cf49d183ba757c503af1fdf51317b78202
|
data/CHANGELOG.md
CHANGED
@@ -19,17 +19,21 @@ module LoggableActivity
|
|
19
19
|
# Returns:
|
20
20
|
# "SOME_ENCRYPTED_STRING"
|
21
21
|
#
|
22
|
-
def self.encrypt(data,
|
23
|
-
return nil if data.nil?
|
24
|
-
|
25
|
-
|
22
|
+
def self.encrypt(data, encoded_key)
|
23
|
+
return nil if data.nil? || encoded_key.nil?
|
24
|
+
|
25
|
+
encryption_key = Base64.decode64(encoded_key)
|
26
|
+
raise EncryptionError, "Encryption failed: Invalid encryption key length #{encryption_key.bytesize}" unless encryption_key.bytesize == 32
|
27
|
+
|
28
|
+
cipher = OpenSSL::Cipher.new('AES-256-CBC').encrypt
|
29
|
+
cipher.key = encryption_key
|
30
|
+
cipher.iv = iv = cipher.random_iv
|
26
31
|
|
27
|
-
cipher = OpenSSL::Cipher.new('AES-128-CBC').encrypt
|
28
|
-
cipher.key = Digest::SHA1.hexdigest(encryption_key)[0..15]
|
29
32
|
encrypted = cipher.update(data.to_s) + cipher.final
|
30
|
-
Base64
|
33
|
+
# Combine IV with encrypted data, encode with Base64 for storage/transmission
|
34
|
+
Base64.encode64(iv + encrypted)
|
31
35
|
rescue OpenSSL::Cipher::CipherError => e
|
32
|
-
raise EncryptionError, "Encryption failed: #{e.message}
|
36
|
+
raise EncryptionError, "Encryption failed: #{e.message}"
|
33
37
|
end
|
34
38
|
|
35
39
|
# Decrypts the given data using the given encryption key
|
@@ -40,19 +44,22 @@ module LoggableActivity
|
|
40
44
|
# Returns:
|
41
45
|
# "my secret data"
|
42
46
|
#
|
43
|
-
def self.decrypt(data,
|
44
|
-
return '' if data.nil?
|
45
|
-
|
47
|
+
def self.decrypt(data, encoded_key)
|
48
|
+
return '' if data.nil? || encoded_key.nil?
|
49
|
+
|
50
|
+
encryption_key = Base64.decode64(encoded_key)
|
51
|
+
raise EncryptionError, 'Decryption failed: Invalid encryption key length' unless encryption_key.bytesize == 32
|
52
|
+
|
53
|
+
cipher = OpenSSL::Cipher.new('AES-256-CBC').decrypt
|
54
|
+
cipher.key = encryption_key
|
46
55
|
|
47
|
-
|
48
|
-
cipher.
|
49
|
-
decrypted_data =
|
50
|
-
decrypted_output = cipher.update(decrypted_data) + cipher.final
|
51
|
-
raise 'Decryption failed: Invalid UTF-8 output' unless decrypted_output.valid_encoding?
|
56
|
+
raw_data = Base64.decode64(data)
|
57
|
+
cipher.iv = raw_data[0...cipher.iv_len] # Extract IV from the beginning of raw_data
|
58
|
+
decrypted_data = cipher.update(raw_data[cipher.iv_len..]) + cipher.final
|
52
59
|
|
53
|
-
|
60
|
+
decrypted_data.force_encoding('UTF-8')
|
54
61
|
rescue OpenSSL::Cipher::CipherError => e
|
55
|
-
raise EncryptionError, e.message
|
62
|
+
raise EncryptionError, "Decryption failed: #{e.message}"
|
56
63
|
end
|
57
64
|
|
58
65
|
def self.blank?(value)
|
@@ -107,7 +107,10 @@ module LoggableActivity
|
|
107
107
|
# "a8f4774e7f42eb253045a4db7de7b79e"
|
108
108
|
#
|
109
109
|
def self.random_key
|
110
|
-
|
110
|
+
# Generate 32 random bytes (256 bits) directly
|
111
|
+
encryption_key = SecureRandom.random_bytes(32)
|
112
|
+
# Encode the key in Base64 to ensure it's in a transferable format
|
113
|
+
Base64.encode64(encryption_key).strip
|
111
114
|
end
|
112
115
|
end
|
113
116
|
end
|