| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.content.pm; |
| 18 |
|
| 19 |
import java.io.FilterInputStream; |
| 20 |
import java.io.IOException; |
| 21 |
import java.io.InputStream; |
| 22 |
|
| 23 |
import javax.crypto.Mac; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
public class MacAuthenticatedInputStream extends FilterInputStream { |
| 33 |
private final Mac mMac; |
| 34 |
|
| 35 |
public MacAuthenticatedInputStream(InputStream in, Mac mac) { |
| 36 |
super(in); |
| 37 |
|
| 38 |
mMac = mac; |
| 39 |
} |
| 40 |
|
| 41 |
public boolean isTagEqual(byte[] tag) { |
| 42 |
final byte[] actualTag = mMac.doFinal(); |
| 43 |
|
| 44 |
if (tag == null || actualTag == null || tag.length != actualTag.length) { |
| 45 |
return false; |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
int value = 0; |
| 54 |
for (int i = 0; i < tag.length; i++) { |
| 55 |
value |= tag[i] ^ actualTag[i]; |
| 56 |
} |
| 57 |
|
| 58 |
return value == 0; |
| 59 |
} |
| 60 |
|
| 61 |
@Override |
| 62 |
public int read() throws IOException { |
| 63 |
final int b = super.read(); |
| 64 |
if (b >= 0) { |
| 65 |
mMac.update((byte) b); |
| 66 |
} |
| 67 |
return b; |
| 68 |
} |
| 69 |
|
| 70 |
@Override |
| 71 |
public int read(byte[] buffer, int offset, int count) throws IOException { |
| 72 |
int numRead = super.read(buffer, offset, count); |
| 73 |
if (numRead > 0) { |
| 74 |
mMac.update(buffer, offset, numRead); |
| 75 |
} |
| 76 |
return numRead; |
| 77 |
} |
| 78 |
} |