use of javax.crypto.ShortBufferException in project j2objc by google.
the class myCipherSpi method testCipherSpi06.
/**
* Test for <code>engineDoFinal(ByteBuffer, ByteBuffer)</code> method
* Assertions:
* throws NullPointerException if one of these buffers is null;
* throws ShortBufferException is there is no space in output to hold result
*/
public void testCipherSpi06() throws BadPaddingException, ShortBufferException, IllegalBlockSizeException {
Mock_CipherSpi cSpi = new Mock_CipherSpi();
int len = 10;
byte[] bbuf = new byte[len];
for (int i = 0; i < bbuf.length; i++) {
bbuf[i] = (byte) i;
}
ByteBuffer bb1 = ByteBuffer.wrap(bbuf);
ByteBuffer bbNull = null;
try {
cSpi.engineDoFinal(bbNull, bb1);
fail("NullPointerException must be thrown");
} catch (NullPointerException e) {
}
try {
cSpi.engineDoFinal(bb1, bbNull);
fail("NullPointerException must be thrown");
} catch (NullPointerException e) {
}
ByteBuffer bb2 = ByteBuffer.allocate(len);
bb1.position(bb1.limit());
assertEquals("Incorrect result", 0, cSpi.engineDoFinal(bb1, bb2));
bb1.position(0);
bb2.position(len - 2);
try {
cSpi.engineDoFinal(bb1, bb2);
fail("ShortBufferException must be thrown. Output buffer remaining: ".concat(Integer.toString(bb2.remaining())));
} catch (ShortBufferException e) {
}
int pos = 5;
bb1.position(pos);
bb2.position(0);
assertTrue("Incorrect result", cSpi.engineDoFinal(bb1, bb2) > 0);
}
use of javax.crypto.ShortBufferException in project wildfly by wildfly.
the class CipherAdapter method engineUpdate.
@Override
protected int engineUpdate(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset) throws ShortBufferException {
int result = engineGetOutputSize(inputLen);
if ((output.length - outputOffset) < result)
throw new ShortBufferException();
byte[] buf = engineUpdate(input, inputOffset, inputLen);
result = buf.length;
if ((output.length - outputOffset) < result)
throw new ShortBufferException();
System.arraycopy(buf, 0, output, outputOffset, result);
return result;
}
use of javax.crypto.ShortBufferException in project wildfly by wildfly.
the class CipherAdapter method engineDoFinal.
@Override
protected int engineDoFinal(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset) throws ShortBufferException, IllegalBlockSizeException, BadPaddingException {
int result = engineGetOutputSize(inputLen);
if ((output.length - outputOffset) < result)
throw new ShortBufferException();
byte[] buf = engineDoFinal(input, inputOffset, inputLen);
result = buf.length;
if ((output.length - outputOffset) < result)
throw new ShortBufferException();
System.arraycopy(buf, 0, output, outputOffset, result);
return result;
}
use of javax.crypto.ShortBufferException in project elastic-core-maven by OrdinaryDude.
the class Scrypt method hash.
public byte[] hash(final byte[] input) {
int i, j, k;
System.arraycopy(input, 0, B, 0, input.length);
try {
mac.init(new SecretKeySpec(B, 0, 40, "HmacSHA256"));
} catch (InvalidKeyException e) {
throw new IllegalStateException(e);
}
B[40] = 0;
B[41] = 0;
B[42] = 0;
for (i = 0; i < 4; i++) {
B[43] = (byte) (i + 1);
mac.update(B, 0, 44);
try {
mac.doFinal(H, 0);
} catch (ShortBufferException e) {
throw new IllegalStateException(e);
}
for (j = 0; j < 8; j++) {
X[i * 8 + j] = (H[j * 4 + 0] & 0xff) << 0 | (H[j * 4 + 1] & 0xff) << 8 | (H[j * 4 + 2] & 0xff) << 16 | (H[j * 4 + 3] & 0xff) << 24;
}
}
for (i = 0; i < 1024; i++) {
System.arraycopy(X, 0, V, i * 32, 32);
xorSalsa8(0, 16);
xorSalsa8(16, 0);
}
for (i = 0; i < 1024; i++) {
k = (X[16] & 1023) * 32;
for (j = 0; j < 32; j++) X[j] ^= V[k + j];
xorSalsa8(0, 16);
xorSalsa8(16, 0);
}
for (i = 0; i < 32; i++) {
B[i * 4 + 0] = (byte) (X[i] >> 0);
B[i * 4 + 1] = (byte) (X[i] >> 8);
B[i * 4 + 2] = (byte) (X[i] >> 16);
B[i * 4 + 3] = (byte) (X[i] >> 24);
}
B[128 + 3] = 1;
mac.update(B, 0, 128 + 4);
try {
mac.doFinal(H, 0);
} catch (ShortBufferException e) {
throw new IllegalStateException(e);
}
return H;
}
Aggregations