Skip to content

Commit e978f21

Browse files
committed
additional buffer argument
1 parent 3f25f72 commit e978f21

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src/main/java/org/jruby/ext/openssl/Cipher.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1104,6 +1104,11 @@ private String getCipherAlgorithm() {
11041104

11051105
@JRubyMethod
11061106
public IRubyObject update(final ThreadContext context, final IRubyObject arg) {
1107+
return update(context, arg, null);
1108+
}
1109+
1110+
@JRubyMethod
1111+
public IRubyObject update(final ThreadContext context, final IRubyObject arg, IRubyObject buffer) {
11071112
final Ruby runtime = context.runtime;
11081113

11091114
if ( isDebug(runtime) ) dumpVars( runtime.getOut(), "update()" );
@@ -1143,7 +1148,11 @@ public IRubyObject update(final ThreadContext context, final IRubyObject arg) {
11431148
debugStackTrace( runtime, e );
11441149
throw newCipherError(runtime, e);
11451150
}
1146-
return RubyString.newString(runtime, str);
1151+
if( buffer != null ) {
1152+
return ((RubyString) buffer).replace(RubyString.newString(runtime, str));
1153+
} else {
1154+
return RubyString.newString(runtime, str);
1155+
}
11471156
}
11481157

11491158
@JRubyMethod(name = "<<")

src/test/ruby/test_cipher.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,4 +475,21 @@ def test_encrypt_aes_cfb_20_incompatibility
475475
end
476476
end
477477

478+
def test_encrypt_aes_256_cbc_modifies_buffer
479+
cipher = OpenSSL::Cipher.new("AES-256-CBC")
480+
cipher.key = "a" * 32
481+
cipher.encrypt
482+
buffer = ''
483+
actual = cipher.update('bar' * 10, buffer)
484+
if jruby?
485+
expected = "\xE6\xD3Y\fc\xEE\xBA\xB2*\x0Fr\xD1\xC2b\x03\xD0"
486+
assert_equal actual, expected
487+
assert_equal buffer, expected
488+
else
489+
expected = "8\xA7\xBE\xB1\xAE\x88j\xCB\xA3\xE9j\x00\xD2W_\x91"
490+
assert_equal actual, expected
491+
assert_equal buffer, expected
492+
end
493+
end
494+
478495
end

0 commit comments

Comments
 (0)