Skip to content
This repository was archived by the owner on Jan 18, 2023. It is now read-only.

Commit 0cb2e42

Browse files
committed
Don't blindly assume inputs are strings
1 parent 7aa5595 commit 0cb2e42

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

lib/aead/cipher.rb

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,14 @@ def nonce_len
109109
# @return [String] the generated AEAD
110110
#
111111
def encrypt(nonce, aad, plaintext)
112-
if nonce.respond_to?(:force_encoding)
113-
nonce .force_encoding('ASCII-8BIT')
114-
aad .force_encoding('ASCII-8BIT')
115-
plaintext.force_encoding('ASCII-8BIT')
116-
end
112+
nonce.force_encoding('ASCII-8BIT') if
113+
nonce.respond_to?(:force_encoding)
114+
115+
aad.force_encoding('ASCII-8BIT') if
116+
aad.respond_to?(:force_encoding)
117+
118+
plaintext.force_encoding('ASCII-8BIT') if
119+
plaintext.respond_to?(:force_encoding)
117120

118121
_verify_nonce_bytesize(nonce, self.nonce_len)
119122
_verify_plaintext_presence(plaintext)
@@ -135,11 +138,14 @@ def encrypt(nonce, aad, plaintext)
135138
# @return [String] the original plaintext
136139
#
137140
def decrypt(nonce, aad, aead)
138-
if nonce.respond_to?(:force_encoding)
139-
nonce.force_encoding('ASCII-8BIT')
140-
aad .force_encoding('ASCII-8BIT')
141-
aead .force_encoding('ASCII-8BIT')
142-
end
141+
nonce.force_encoding('ASCII-8BIT') if
142+
nonce.respond_to?(:force_encoding)
143+
144+
aad.force_encoding('ASCII-8BIT') if
145+
aad.respond_to?(:force_encoding)
146+
147+
aead.force_encoding('ASCII-8BIT') if
148+
aead.respond_to?(:force_encoding)
143149

144150
_verify_nonce_bytesize(nonce, self.nonce_len)
145151

0 commit comments

Comments
 (0)