@@ -1075,8 +1075,9 @@ public IRubyObject update(final ThreadContext context, final IRubyObject arg) {
10751075
10761076 checkCipherNotNull (runtime );
10771077
1078- final byte [] data = arg .asString ().getBytes ();
1079- if ( data .length == 0 ) {
1078+ final ByteList data = arg .asString ().getByteList ();
1079+ final int length = data .length ();
1080+ if ( length == 0 ) {
10801081 throw runtime .newArgumentError ("data must not be empty" );
10811082 }
10821083
@@ -1088,12 +1089,17 @@ public IRubyObject update(final ThreadContext context, final IRubyObject arg) {
10881089
10891090 final ByteList str ;
10901091 try {
1091- final byte [] out = cipher .update (data );
1092+ final byte [] in = data .getUnsafeBytes ();
1093+ final int offset = data .begin ();
1094+ final byte [] out = cipher .update (in , offset , length );
10921095 if ( out != null ) {
10931096 str = new ByteList (out , false );
1094- if ( realIV != null ) setLastIVIfNeeded ( encryptMode ? out : data );
1097+ if ( realIV != null ) {
1098+ if ( encryptMode ) setLastIVIfNeeded ( out );
1099+ else setLastIVIfNeeded ( in , offset , length );
1100+ }
10951101
1096- processedDataBytes += data . length ;
1102+ processedDataBytes += length ;
10971103 }
10981104 else {
10991105 str = new ByteList (ByteList .NULL_ARRAY );
@@ -1160,10 +1166,14 @@ public IRubyObject do_final(final ThreadContext context) {
11601166 }
11611167
11621168 private void setLastIVIfNeeded (final byte [] tmpIV ) {
1163- final int len = ivLength ;
1164- if ( lastIV == null ) lastIV = new byte [len ];
1165- if ( tmpIV .length >= len ) {
1166- System .arraycopy (tmpIV , tmpIV .length - len , lastIV , 0 , len );
1169+ setLastIVIfNeeded (tmpIV , 0 , tmpIV .length );
1170+ }
1171+
1172+ private void setLastIVIfNeeded (final byte [] tmpIV , final int offset , final int length ) {
1173+ final int ivLen = this .ivLength ;
1174+ if ( lastIV == null ) lastIV = new byte [ivLen ];
1175+ if ( length >= ivLen ) {
1176+ System .arraycopy (tmpIV , offset + (length - ivLen ), lastIV , 0 , ivLen );
11671177 }
11681178 }
11691179
0 commit comments