1 /* 2 * Copyright (C) 2019 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.server.backup.encryption.tasks; 18 19 import java.io.Closeable; 20 import java.io.IOException; 21 import java.security.InvalidKeyException; 22 import java.security.NoSuchAlgorithmException; 23 24 /** 25 * Accepts the plaintext bytes of decrypted chunks and writes them to some output. Also keeps track 26 * of the message digest of the chunks. 27 */ 28 public interface DecryptedChunkOutput extends Closeable { 29 /** 30 * Opens whatever output the implementation chooses, ready to process chunks. 31 * 32 * @return {@code this}, to allow use with try-with-resources 33 */ open()34 DecryptedChunkOutput open() throws IOException, NoSuchAlgorithmException; 35 36 /** 37 * Writes the plaintext bytes of chunk to whatever output the implementation chooses. Also 38 * updates the digest with the chunk. 39 * 40 * <p>You must call {@link #open()} before this method, and you may not call it after calling 41 * {@link Closeable#close()}. 42 * 43 * @param plaintextBuffer An array containing the bytes of the plaintext of the chunk, starting 44 * at index 0. 45 * @param length The length in bytes of the plaintext contained in {@code plaintextBuffer}. 46 */ processChunk(byte[] plaintextBuffer, int length)47 void processChunk(byte[] plaintextBuffer, int length) 48 throws IOException, InvalidKeyException, NoSuchAlgorithmException; 49 50 /** 51 * Returns the message digest of all the chunks processed by {@link #processChunk}. 52 * 53 * <p>You must call {@link Closeable#close()} before calling this method. 54 */ getDigest()55 byte[] getDigest() throws NoSuchAlgorithmException; 56 } 57