@Test public void decrypt() throws Exception { HmacDecryptor decryptor = mock(HmacDecryptor.class); decryptor.delegate = SwapBytesTransformer.decryptor(); when(decryptor.createHmac(any(byte[].class))).thenReturn(new byte[]{0, 1, 2, 3, 4, 5, 6, 7}); when(decryptor.decrypt(any(byte[].class), anyInt(), (Key)any())).thenCallRealMethod(); byte[] expectedResult = {-1, -2, -3}; byte[] input1 = {8, 0, 1, 2, 3, 4, 5, 6, 7, -3, -2, -1}; byte[] result1 = decryptor.decrypt(input1, 0, null); assertArrayEquals(expectedResult, result1); byte[] input2 = {0, 0, 0, 8, 0, 1, 2, 3, 4, 5, 6, 7, -3, -2, -1}; byte[] result2 = decryptor.decrypt(input2, 3, null); assertArrayEquals(expectedResult, result2); }
@Override public byte[] decrypt(byte[] input, int inputOffset, Key key) { byte hmacLength = input[inputOffset++]; if(hmacLength <= 0) { throw new CayenneCryptoException("Input is corrupted: invalid HMAC length."); } byte[] receivedHmac = new byte[hmacLength]; byte[] decrypted = delegate.decrypt(input, inputOffset + hmacLength, key); byte[] realHmac = createHmac(decrypted); System.arraycopy(input, inputOffset, receivedHmac, 0, hmacLength); if(!Arrays.equals(receivedHmac, realHmac)) { throw new CayenneCryptoException("Input is corrupted: wrong HMAC."); } return decrypted; }
HmacDecryptor extends HmacCreator implements BytesDecryptor { @Override public byte[] decrypt(byte[] input, int inputOffset, Key key) { byte hmacLength = input[inputOffset++]; if(hmacLength <= 0) { throw new CayenneCryptoException("Input is corrupted: invalid HMAC length."); } byte[] receivedHmac = new byte[hmacLength]; byte[] decrypted = delegate.decrypt(input, inputOffset + hmacLength, key); byte[] realHmac = createHmac(decrypted); System.arraycopy(input, inputOffset, receivedHmac, 0, hmacLength); if(!Arrays.equals(receivedHmac, realHmac)) { throw new CayenneCryptoException("Input is corrupted: wrong HMAC."); } return decrypted; } }
HmacDecryptor extends HmacCreator implements BytesDecryptor { @Override public byte[] decrypt(byte[] input, int inputOffset, Key key) { byte hmacLength = input[inputOffset++]; if(hmacLength <= 0) { throw new CayenneCryptoException("Input is corrupted: invalid HMAC length."); } byte[] receivedHmac = new byte[hmacLength]; byte[] decrypted = delegate.decrypt(input, inputOffset + hmacLength, key); byte[] realHmac = createHmac(decrypted); System.arraycopy(input, inputOffset, receivedHmac, 0, hmacLength); if(!Arrays.equals(receivedHmac, realHmac)) { throw new CayenneCryptoException("Input is corrupted: wrong HMAC."); } return decrypted; } HmacDecryptor(BytesDecryptor delegate, Header header, Key key); }
HmacDecryptor extends HmacCreator implements BytesDecryptor { @Override public byte[] decrypt(byte[] input, int inputOffset, Key key) { byte hmacLength = input[inputOffset++]; if(hmacLength <= 0) { throw new CayenneCryptoException("Input is corrupted: invalid HMAC length."); } byte[] receivedHmac = new byte[hmacLength]; byte[] decrypted = delegate.decrypt(input, inputOffset + hmacLength, key); byte[] realHmac = createHmac(decrypted); System.arraycopy(input, inputOffset, receivedHmac, 0, hmacLength); if(!Arrays.equals(receivedHmac, realHmac)) { throw new CayenneCryptoException("Input is corrupted: wrong HMAC."); } return decrypted; } HmacDecryptor(BytesDecryptor delegate, Header header, Key key); @Override byte[] decrypt(byte[] input, int inputOffset, Key key); }
HmacDecryptor extends HmacCreator implements BytesDecryptor { @Override public byte[] decrypt(byte[] input, int inputOffset, Key key) { byte hmacLength = input[inputOffset++]; if(hmacLength <= 0) { throw new CayenneCryptoException("Input is corrupted: invalid HMAC length."); } byte[] receivedHmac = new byte[hmacLength]; byte[] decrypted = delegate.decrypt(input, inputOffset + hmacLength, key); byte[] realHmac = createHmac(decrypted); System.arraycopy(input, inputOffset, receivedHmac, 0, hmacLength); if(!Arrays.equals(receivedHmac, realHmac)) { throw new CayenneCryptoException("Input is corrupted: wrong HMAC."); } return decrypted; } HmacDecryptor(BytesDecryptor delegate, Header header, Key key); @Override byte[] decrypt(byte[] input, int inputOffset, Key key);  }