Java Code Examples for com.amazonaws.encryptionsdk.CryptoAlgorithm#deserialize()

The following examples show how to use com.amazonaws.encryptionsdk.CryptoAlgorithm#deserialize() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CiphertextHeaders.java    From aws-encryption-sdk-java with Apache License 2.0 3 votes vote down vote up
/**
 * Parse the algorithm identifier in the provided bytes. It looks for 2
 * bytes representing a short primitive type in the provided bytes starting
 * at the specified off.
 * 
 * <p>
 * If successful, it returns the number of parsed bytes which is the size of
 * the short primitive type. On failure, it throws a parse exception.
 * 
 * @param b
 *            the byte array to parse.
 * @param off
 *            the offset in the byte array to use when parsing.
 * @return
 *         the number of parsed bytes which is the size of the short
 *         primitive type.
 * @throws ParseException
 *             if there are not sufficient bytes to parse the algorithm
 *             identifier.
 */
private int parseAlgoId(final byte[] b, final int off) throws ParseException {
    cryptoAlgoVal_ = PrimitivesParser.parseShort(b, off);
    if (CryptoAlgorithm.deserialize(cryptoAlgoVal_) == null) {
        throw new BadCiphertextException("Invalid algorithm identifier in ciphertext");
    }
    return Short.SIZE / Byte.SIZE;
}
 
Example 2
Source File: CiphertextHeaders.java    From aws-encryption-sdk-java with Apache License 2.0 3 votes vote down vote up
/**
 * Parse the header tag in the provided bytes. It uses the crypto algorithm
 * identifier to determine the length of the tag to parse. It looks for
 * bytes of size defined by the tag length in the provided bytes starting at
 * the specified off.
 * 
 * <p>
 * If successful, it returns the size of the parsed bytes which is the tag
 * length determined from the crypto algorithm identifier. On failure, it
 * throws a parse exception.
 * 
 * @param b
 *            the byte array to parse.
 * @param off
 *            the offset in the byte array to use when parsing.
 * @return
 *         the size of the parsed bytes which is the tag length determined
 *         from the crypto algorithm identifier.
 * @throws ParseException
 *             if there are not sufficient bytes to parse the header tag.
 */
private int parseHeaderTag(final byte[] b, final int off) throws ParseException {
    final int len = b.length - off;
    final CryptoAlgorithm cryptoAlgo = CryptoAlgorithm.deserialize(cryptoAlgoVal_);
    final int tagLen = cryptoAlgo.getTagLen();
    if (len >= tagLen) {
        headerTag_ = Arrays.copyOfRange(b, off, off + tagLen);
        return tagLen;
    } else {
        throw new ParseException("Not enough bytes to parse header tag");
    }
}
 
Example 3
Source File: CiphertextHeaders.java    From aws-encryption-sdk-java with Apache License 2.0 2 votes vote down vote up
/**
 * Return the crypto algorithm identifier set in the header.
 * 
 * @return
 *         the CryptoAlgorithm enum value representing the identifier set in
 *         the header.
 */
public CryptoAlgorithm getCryptoAlgoId() {
    return CryptoAlgorithm.deserialize(cryptoAlgoVal_);
}