Ensure that Nostr::Crypto#descrypt_text always returns a string

Fixes another Steep/RBS violation
This commit is contained in:
Wilson Silva 2023-11-18 16:38:03 +07:00
parent 3077aa67a7
commit 4b630c678b
No known key found for this signature in database
GPG Key ID: 65135F94E23F82C8
2 changed files with 19 additions and 4 deletions

View File

@ -62,6 +62,10 @@ module Nostr
# #
def decrypt_text(recipient_private_key, sender_public_key, encrypted_text) def decrypt_text(recipient_private_key, sender_public_key, encrypted_text)
base64_encoded_text, iv = encrypted_text.split('?iv=') base64_encoded_text, iv = encrypted_text.split('?iv=')
# Ensure iv and base64_encoded_text are not nil
return '' unless iv && base64_encoded_text
cipher = OpenSSL::Cipher.new(CIPHER_ALGORITHM).decrypt cipher = OpenSSL::Cipher.new(CIPHER_ALGORITHM).decrypt
cipher.iv = Base64.decode64(iv) cipher.iv = Base64.decode64(iv)
cipher.key = compute_shared_key(recipient_private_key, sender_public_key) cipher.key = compute_shared_key(recipient_private_key, sender_public_key)

View File

@ -69,11 +69,22 @@ RSpec.describe Nostr::Crypto do
) )
end end
it 'decrypts an encrypted text' do context 'when the encrypted text includes an iv query string' do
encrypted_text = crypto.encrypt_text(sender_keypair.private_key, recipient_keypair.public_key, 'Twitter Files') it 'decrypts an encrypted text' do
decrypted_text = crypto.decrypt_text(recipient_keypair.private_key, sender_keypair.public_key, encrypted_text) encrypted_text = crypto.encrypt_text(sender_keypair.private_key, recipient_keypair.public_key, 'Twitter Files')
decrypted_text = crypto.decrypt_text(recipient_keypair.private_key, sender_keypair.public_key, encrypted_text)
expect(decrypted_text).to eq('Twitter Files') expect(decrypted_text).to eq('Twitter Files')
end
end
context 'when the encrypted text does not include an iv query string' do
it 'returns an empty string' do
encrypted_text = 'wrYQaHDfpOEvyJELSCg1vzsywmlJTz8NqH03eFW44s8iQs869jtSb26Lr4s23gmY?it=v38vAJ3LlJAGZxbmWU4qAg=='
decrypted_text = crypto.decrypt_text(recipient_keypair.private_key, sender_keypair.public_key, encrypted_text)
expect(decrypted_text).to eq('')
end
end end
end end
end end