2323import java .util .Arrays ;
2424import javax .crypto .Mac ;
2525import javax .crypto .SecretKey ;
26- import javax .crypto .ShortBufferException ;
2726import javax .crypto .spec .SecretKeySpec ;
2827
2928/**
@@ -193,36 +192,7 @@ public byte[] deriveKey(final String info, final int length) throws IllegalState
193192 */
194193 public byte [] deriveKey (final byte [] info , final int length ) throws IllegalStateException {
195194 byte [] result = new byte [length ];
196- try {
197- deriveKey (info , length , result , 0 );
198- } catch (ShortBufferException ex ) {
199- // This exception is impossible as we ensure the buffer is long
200- // enough
201- throw new RuntimeException (ex );
202- }
203- return result ;
204- }
205-
206- /**
207- * Derives a pseudorandom key of <code>length</code> bytes and stores the result in <code>output
208- * </code>.
209- *
210- * @param info optional context and application specific information (can be a zero-length array).
211- * @param length the length of the output key in bytes
212- * @param output the buffer where the pseudorandom key will be stored
213- * @param offset the offset in <code>output</code> where the key will be stored
214- * @throws ShortBufferException if the given output buffer is too small to hold the result
215- * @throws IllegalStateException if this object has not been initialized
216- */
217- public void deriveKey (final byte [] info , final int length , final byte [] output , final int offset )
218- throws ShortBufferException , IllegalStateException {
219195 assertInitialized ();
220- if (length < 0 ) {
221- throw new IllegalArgumentException ("Length must be a non-negative value." );
222- }
223- if (output .length < offset + length ) {
224- throw new ShortBufferException ();
225- }
226196 Mac mac = createMac ();
227197
228198 if (length > 255 * mac .getMacLength ()) {
@@ -241,14 +211,15 @@ public void deriveKey(final byte[] info, final int length, final byte[] output,
241211 t = mac .doFinal ();
242212
243213 for (int x = 0 ; x < t .length && loc < length ; x ++, loc ++) {
244- output [loc ] = t [x ];
214+ result [loc ] = t [x ];
245215 }
246216
247217 i ++;
248218 }
249219 } finally {
250220 Arrays .fill (t , (byte ) 0 ); // Zeroize temporary array
251221 }
222+ return result ;
252223 }
253224
254225 private Mac createMac () {
0 commit comments