@@ -1075,8 +1075,9 @@ public IRubyObject update(final ThreadContext context, final IRubyObject arg) {
1075
1075
1076
1076
checkCipherNotNull (runtime );
1077
1077
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 ) {
1080
1081
throw runtime .newArgumentError ("data must not be empty" );
1081
1082
}
1082
1083
@@ -1088,12 +1089,17 @@ public IRubyObject update(final ThreadContext context, final IRubyObject arg) {
1088
1089
1089
1090
final ByteList str ;
1090
1091
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 );
1092
1095
if ( out != null ) {
1093
1096
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
+ }
1095
1101
1096
- processedDataBytes += data . length ;
1102
+ processedDataBytes += length ;
1097
1103
}
1098
1104
else {
1099
1105
str = new ByteList (ByteList .NULL_ARRAY );
@@ -1160,10 +1166,14 @@ public IRubyObject do_final(final ThreadContext context) {
1160
1166
}
1161
1167
1162
1168
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 );
1167
1177
}
1168
1178
}
1169
1179
0 commit comments