/** * Decrypts the value of a {@link GuardedByteArray}. * * @param guardedByteArray the guarded byte array value. * @return the clear byte array value. * @since 1.4 */ public static byte[] decrypt(GuardedByteArray guardedByteArray) { byte[][] clearByte = new byte[1][]; guardedByteArray.access((clearBytes) -> { clearByte[0] = clearBytes; }); return clearByte[0]; } }
/** * Decrypts the value of a {@link GuardedByteArray}. * * @param guardedByteArray the guarded byte array value. * @return the clear byte array value. * @since 1.4 */ public static byte[] decrypt(GuardedByteArray guardedByteArray) { byte[][] clearByte = new byte[1][]; guardedByteArray.access((clearBytes) -> { clearByte[0] = clearBytes; }); return clearByte[0]; } }
@Override public void serialize(final Object object, final ObjectEncoder encoder) { final GuardedByteArray val = (GuardedByteArray) object; val.access(new GuardedByteArray.Accessor() { @Override public void access(byte[] clearBytes) { byte[] encryptedBytes = null; try { encryptedBytes = EncryptorFactory.getInstance().getDefaultEncryptor().encrypt(clearBytes); encoder.writeByteArrayContents(encryptedBytes); } finally { SecurityUtil.clear(encryptedBytes); } } }); } });
/** * Highly insecure method! Do not do this in production * code. This is only for test purposes */ private byte[] decryptToBytes(GuardedByteArray bytes) { final ByteArrayOutputStream out = new ByteArrayOutputStream(); bytes.access((byte[] bytes1) -> { out.write(bytes1, 0, bytes1.length); }); return out.toByteArray(); } }
@Override public void serialize(final Object object, final ObjectEncoder encoder) { final GuardedByteArray val = (GuardedByteArray) object; val.access((byte[] clearBytes) -> { byte[] encryptedBytes = null; try { encryptedBytes = EncryptorFactory.getInstance().getDefaultEncryptor().encrypt(clearBytes); encoder.writeByteArrayContents(encryptedBytes); } finally { SecurityUtil.clear(encryptedBytes); } }); } });
@Override public void serialize(final Object object, final ObjectEncoder encoder) { final GuardedByteArray val = (GuardedByteArray) object; val.access((byte[] clearBytes) -> { byte[] encryptedBytes = null; try { encryptedBytes = EncryptorFactory.getInstance().getDefaultEncryptor().encrypt(clearBytes); encoder.writeByteArrayContents(encryptedBytes); } finally { SecurityUtil.clear(encryptedBytes); } }); } });
@Test public void testRange() { for (int i = -128; i < 128; i++) { final byte expected = (byte) i; GuardedByteArray bytes = new GuardedByteArray(new byte[] { (byte) i }); bytes.access((byte[] clearBytes) -> { byte v = clearBytes[0]; assertEquals(v, expected); }); } }