@Test(expected = CayenneCryptoException.class) public void testCreate_WithKeyName_TooLong() { StringBuilder buf = new StringBuilder(); for (int i = 0; i < Byte.MAX_VALUE + 1; i++) { buf.append("a"); } Header.create(buf.toString(), false, false); }
public static Header create(String keyName, boolean compressed, boolean withHMAC) { byte[] keyNameBytes; try { keyNameBytes = keyName.getBytes(KEY_NAME_CHARSET); } catch (UnsupportedEncodingException e) { throw new CayenneCryptoException("Can't encode in " + KEY_NAME_CHARSET, e); } if (keyNameBytes.length > KEY_NAME_MAX_SIZE) { throw new CayenneCryptoException("Key name '" + keyName + "' is too long. Its UTF8-encoded form should not exceed " + KEY_NAME_MAX_SIZE + " bytes"); } int n = MAGIC_NUMBER.length + 1 + 1 + keyNameBytes.length; byte[] data = new byte[n]; System.arraycopy(MAGIC_NUMBER, 0, data, MAGIC_NUMBER_POSITION, MAGIC_NUMBER.length); data[SIZE_POSITION] = (byte) n; if (compressed) { data[FLAGS_POSITION] = bitOn(data[FLAGS_POSITION], COMPRESS_BIT); } if (withHMAC) { data[FLAGS_POSITION] = bitOn(data[FLAGS_POSITION], HMAC_BIT); } System.arraycopy(keyNameBytes, 0, data, KEY_NAME_OFFSET, keyNameBytes.length); return create(data, 0); }
Header { public static Header create(String keyName, boolean compressed, boolean withHMAC) { byte[] keyNameBytes; try { keyNameBytes = keyName.getBytes(KEY_NAME_CHARSET); } catch (UnsupportedEncodingException e) { throw new CayenneCryptoException("Can't encode in " + KEY_NAME_CHARSET, e); } if (keyNameBytes.length > KEY_NAME_MAX_SIZE) { throw new CayenneCryptoException("Key name '" + keyName + "' is too long. Its UTF8-encoded form should not exceed " + KEY_NAME_MAX_SIZE + " bytes"); } int n = MAGIC_NUMBER.length + 1 + 1 + keyNameBytes.length; byte[] data = new byte[n]; System.arraycopy(MAGIC_NUMBER, 0, data, MAGIC_NUMBER_POSITION, MAGIC_NUMBER.length); data[SIZE_POSITION] = (byte) n; if (compressed) { data[FLAGS_POSITION] = bitOn(data[FLAGS_POSITION], COMPRESS_BIT); } if (withHMAC) { data[FLAGS_POSITION] = bitOn(data[FLAGS_POSITION], HMAC_BIT); } System.arraycopy(keyNameBytes, 0, data, KEY_NAME_OFFSET, keyNameBytes.length); return create(data, 0); } }
Header { public static Header create(String keyName, boolean compressed, boolean withHMAC) { byte[] keyNameBytes; try { keyNameBytes = keyName.getBytes(KEY_NAME_CHARSET); } catch (UnsupportedEncodingException e) { throw new CayenneCryptoException("Can't encode in " + KEY_NAME_CHARSET, e); } if (keyNameBytes.length > KEY_NAME_MAX_SIZE) { throw new CayenneCryptoException("Key name '" + keyName + "' is too long. Its UTF8-encoded form should not exceed " + KEY_NAME_MAX_SIZE + " bytes"); } int n = MAGIC_NUMBER.length + 1 + 1 + keyNameBytes.length; byte[] data = new byte[n]; System.arraycopy(MAGIC_NUMBER, 0, data, MAGIC_NUMBER_POSITION, MAGIC_NUMBER.length); data[SIZE_POSITION] = (byte) n; if (compressed) { data[FLAGS_POSITION] = bitOn(data[FLAGS_POSITION], COMPRESS_BIT); } if (withHMAC) { data[FLAGS_POSITION] = bitOn(data[FLAGS_POSITION], HMAC_BIT); } System.arraycopy(keyNameBytes, 0, data, KEY_NAME_OFFSET, keyNameBytes.length); return create(data, 0); } private  Header(byte[] data, int offset); }
Header { public static Header create(String keyName, boolean compressed, boolean withHMAC) { byte[] keyNameBytes; try { keyNameBytes = keyName.getBytes(KEY_NAME_CHARSET); } catch (UnsupportedEncodingException e) { throw new CayenneCryptoException("Can't encode in " + KEY_NAME_CHARSET, e); } if (keyNameBytes.length > KEY_NAME_MAX_SIZE) { throw new CayenneCryptoException("Key name '" + keyName + "' is too long. Its UTF8-encoded form should not exceed " + KEY_NAME_MAX_SIZE + " bytes"); } int n = MAGIC_NUMBER.length + 1 + 1 + keyNameBytes.length; byte[] data = new byte[n]; System.arraycopy(MAGIC_NUMBER, 0, data, MAGIC_NUMBER_POSITION, MAGIC_NUMBER.length); data[SIZE_POSITION] = (byte) n; if (compressed) { data[FLAGS_POSITION] = bitOn(data[FLAGS_POSITION], COMPRESS_BIT); } if (withHMAC) { data[FLAGS_POSITION] = bitOn(data[FLAGS_POSITION], HMAC_BIT); } System.arraycopy(keyNameBytes, 0, data, KEY_NAME_OFFSET, keyNameBytes.length); return create(data, 0); } private  Header(byte[] data, int offset); static Header create(String keyName, boolean compressed, boolean withHMAC); static Header create(byte[] data, int offset); static byte setCompressed(byte bits, boolean compressed); static byte setHaveHMAC(byte bits, boolean haveHMAC); int size(); boolean isCompressed(); boolean haveHMAC(); byte getFlags(); void store(byte[] output, int outputOffset, byte flags); String getKeyName(); }
Header { public static Header create(String keyName, boolean compressed, boolean withHMAC) { byte[] keyNameBytes; try { keyNameBytes = keyName.getBytes(KEY_NAME_CHARSET); } catch (UnsupportedEncodingException e) { throw new CayenneCryptoException("Can't encode in " + KEY_NAME_CHARSET, e); } if (keyNameBytes.length > KEY_NAME_MAX_SIZE) { throw new CayenneCryptoException("Key name '" + keyName + "' is too long. Its UTF8-encoded form should not exceed " + KEY_NAME_MAX_SIZE + " bytes"); } int n = MAGIC_NUMBER.length + 1 + 1 + keyNameBytes.length; byte[] data = new byte[n]; System.arraycopy(MAGIC_NUMBER, 0, data, MAGIC_NUMBER_POSITION, MAGIC_NUMBER.length); data[SIZE_POSITION] = (byte) n; if (compressed) { data[FLAGS_POSITION] = bitOn(data[FLAGS_POSITION], COMPRESS_BIT); } if (withHMAC) { data[FLAGS_POSITION] = bitOn(data[FLAGS_POSITION], HMAC_BIT); } System.arraycopy(keyNameBytes, 0, data, KEY_NAME_OFFSET, keyNameBytes.length); return create(data, 0); } private  Header(byte[] data, int offset); static Header create(String keyName, boolean compressed, boolean withHMAC); static Header create(byte[] data, int offset); static byte setCompressed(byte bits, boolean compressed); static byte setHaveHMAC(byte bits, boolean haveHMAC); int size(); boolean isCompressed(); boolean haveHMAC(); byte getFlags(); void store(byte[] output, int outputOffset, byte flags); String getKeyName();  }