/** * Returns the bit length of the specified byte length, preventing * integer overflow. * * @param byteLength The byte length. * * @return The bit length. * * @throws IntegerOverflowException On a integer overflow. */ public static int safeBitLength(final int byteLength) throws IntegerOverflowException { long longResult = (long)byteLength * (long)8; if((long)((int)longResult) != longResult) { throw new IntegerOverflowException(); } else { return (int)longResult; } }
@Override public int size() { try { return ByteUtils.safeBitLength(k.decode()); } catch (IntegerOverflowException e) { throw new ArithmeticException(e.getMessage()); } }
@Override public int size() { try { return ByteUtils.safeBitLength(n.decode()); } catch (IntegerOverflowException e) { throw new ArithmeticException(e.getMessage()); } }
/** * Checks the length of the Content Encryption Key (CEK) according to * the encryption method. * * @param cek The CEK. Must not be {@code null}. * @param enc The encryption method. Must not be {@code null}. * * @throws KeyLengthException If the CEK length doesn't match the * encryption method. */ private static void checkCEKLength(final SecretKey cek, final EncryptionMethod enc) throws KeyLengthException { try { if (enc.cekBitLength() != ByteUtils.safeBitLength(cek.getEncoded())) { throw new KeyLengthException("The Content Encryption Key (CEK) length for " + enc + " must be " + enc.cekBitLength() + " bits"); } } catch (IntegerOverflowException e) { throw new KeyLengthException("The Content Encryption Key (CEK) is too long: " + e.getMessage()); } }