Skip to content

Commit e13a281

Browse files
authored
Merge pull request #170 from ahorek/buffer
additional buffer argument
2 parents 91c5ad1 + a5abf7b commit e13a281

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

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

+13-1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
import org.jruby.runtime.builtin.IRubyObject;
6969
import org.jruby.runtime.Visibility;
7070
import org.jruby.util.ByteList;
71+
import org.jruby.util.TypeConverter;
7172

7273
import static org.jruby.ext.openssl.OpenSSL.*;
7374

@@ -1104,6 +1105,11 @@ private String getCipherAlgorithm() {
11041105

11051106
@JRubyMethod
11061107
public IRubyObject update(final ThreadContext context, final IRubyObject arg) {
1108+
return update(context, arg, null);
1109+
}
1110+
1111+
@JRubyMethod
1112+
public IRubyObject update(final ThreadContext context, final IRubyObject arg, IRubyObject buffer) {
11071113
final Ruby runtime = context.runtime;
11081114

11091115
if ( isDebug(runtime) ) dumpVars( runtime.getOut(), "update()" );
@@ -1143,7 +1149,13 @@ public IRubyObject update(final ThreadContext context, final IRubyObject arg) {
11431149
debugStackTrace( runtime, e );
11441150
throw newCipherError(runtime, e);
11451151
}
1146-
return RubyString.newString(runtime, str);
1152+
1153+
if( buffer == null ) {
1154+
return RubyString.newString(runtime, str);
1155+
} else {
1156+
buffer = TypeConverter.convertToType(buffer, context.runtime.getString(), "to_str", true);
1157+
return ((RubyString) buffer).replace(RubyString.newString(runtime, str));
1158+
}
11471159
}
11481160

11491161
@JRubyMethod(name = "<<")

src/test/ruby/test_cipher.rb

+23
Original file line numberDiff line numberDiff line change
@@ -475,4 +475,27 @@ 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+
else
487+
expected = "8\xA7\xBE\xB1\xAE\x88j\xCB\xA3\xE9j\x00\xD2W_\x91"
488+
end
489+
assert_equal actual, expected
490+
assert_equal buffer, expected
491+
end
492+
493+
def test_encrypt_aes_256_cbc_invalid_buffer
494+
cipher = OpenSSL::Cipher.new("AES-256-CBC")
495+
cipher.key = "a" * 32
496+
cipher.encrypt
497+
buffer = Object.new
498+
assert_raise(TypeError) { cipher.update('bar' * 10, buffer) }
499+
end
500+
478501
end

0 commit comments

Comments
 (0)