| <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../../jacoco-resources/report.gif" type="image/gif"/><title>DefaultBlockCipherService.java</title><link rel="stylesheet" href="../../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../../index.html" class="el_report">Apache Shiro :: Jar Bundle</a> > <a href="../index.html" class="el_bundle">shiro-core</a> > <a href="index.source.html" class="el_package">org.apache.shiro.crypto</a> > <span class="el_source">DefaultBlockCipherService.java</span></div><h1>DefaultBlockCipherService.java</h1><pre class="source lang-java linenums">/* |
| * Licensed to the Apache Software Foundation (ASF) under one |
| * or more contributor license agreements. See the NOTICE file |
| * distributed with this work for additional information |
| * regarding copyright ownership. The ASF licenses this file |
| * to you under the Apache License, Version 2.0 (the |
| * "License"); you may not use this file except in compliance |
| * with the License. You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, |
| * software distributed under the License is distributed on an |
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| * KIND, either express or implied. See the License for the |
| * specific language governing permissions and limitations |
| * under the License. |
| */ |
| package org.apache.shiro.crypto; |
| |
| import org.apache.shiro.util.StringUtils; |
| |
| /** |
| * Base abstract class for block cipher algorithms. |
| * |
| * <h2>Usage</h2> |
| * Note that this class exists mostly to simplify algorithm-specific subclasses. Unless you understand the concepts of |
| * cipher modes of operation, block sizes, and padding schemes, and you want direct control of these things, you should |
| * typically not uses instances of this class directly. Instead, algorithm-specific subclasses, such as |
| * {@link AesCipherService}, {@link BlowfishCipherService}, and others are usually better suited for regular use. |
| * <p/> |
| * However, if you have the need to create a custom block cipher service where no sufficient algorithm-specific subclass |
| * exists in Shiro, this class would be very useful. |
| * |
| * <h2>Configuration</h2> |
| * Block ciphers can accept configuration parameters that direct how they operate. These parameters concatenated |
| * together in a single String comprise what the JDK JCA documentation calls a |
| * <a href="http://java.sun.com/javase/6/docs/technotes/guides/security/crypto/CryptoSpec.html#trans">transformation |
| * string</a>. We think that it is better for Shiro to construct this transformation string automatically based on its |
| * constituent parts instead of having the end-user construct the string manually, which may be error prone or |
| * confusing. To that end, Shiro {@link DefaultBlockCipherService}s have attributes that can be set individually in |
| * a type-safe manner based on your configuration needs, and Shiro will build the transformation string for you. |
| * <p/> |
| * The following sections typically document the configuration options for block (byte array) |
| * {@link #encrypt(byte[], byte[])} and {@link #decrypt(byte[], byte[])} method invocations. Streaming configuration |
| * for those same attributes are done via mirrored {@code streaming}* attributes, and their purpose is identical, but |
| * they're only used during streaming {@link #encrypt(java.io.InputStream, java.io.OutputStream, byte[])} and |
| * {@link #decrypt(java.io.InputStream, java.io.OutputStream, byte[])} methods. See the &quot;Streaming&quot; |
| * section below for more. |
| * |
| * <h3>Block Size</h3> |
| * The block size specifies the number of bits (not bytes) that the cipher operates on when performing an operation. |
| * It can be specified explicitly via the {@link #setBlockSize blockSize} attribute. If not set, the JCA Provider |
| * default will be used based on the cipher algorithm. Block sizes are usually very algorithm specific, so set this |
| * value only if you know you don't want the JCA Provider's default for the desired algorithm. For example, the |
| * AES algorithm's Rijndael implementation <em>only</em> supports a 128 bit block size and will not work with any other |
| * size. |
| * <p/> |
| * Also note that the {@link #setInitializationVectorSize initializationVectorSize} is usually the same as the |
| * {@link #setBlockSize blockSize} in block ciphers. If you change either attribute, you should ensure that the other |
| * attribute is correct for the target cipher algorithm. |
| * |
| * <h3>Operation Mode</h3> |
| * You may set the block cipher's<a href="http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation">mode of |
| * operation</a> via the {@link #setMode(OperationMode) mode} attribute, which accepts a type-safe |
| * {@link OperationMode OperationMode} enum instance. This type safety helps avoid typos when specifying the mode and |
| * guarantees that the mode name will be recognized by the underlying JCA Provider. |
| * <p/> |
| * <b>*</b>If no operation mode is specified, Shiro defaults all of its block {@code CipherService} instances to the |
| * {@link OperationMode#CBC CBC} mode, specifically to support auto-generation of initialization vectors during |
| * encryption. This is different than the JDK's default {@link OperationMode#ECB ECB} mode because {@code ECB} does |
| * not support initialization vectors, which are necessary for strong encryption. See the |
| * {@link org.apache.shiro.crypto.JcaCipherService JcaCipherService parent class} class JavaDoc for an extensive |
| * explanation on why we do this and why we do not use the Sun {@code ECB} default. You also might also want read |
| * the <a href="http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Electronic_codebook_.28ECB.29">Wikipedia |
| * section on ECB<a/> and look at the encrypted image to see an example of why {@code ECB} should not be used in |
| * security-sensitive environments. |
| * <p/> |
| * In the rare case that you need to override the default with a mode not represented |
| * by the {@link OperationMode} enum, you may specify the raw mode name string that will be recognized by your JCA |
| * provider via the {@link #setModeName modeName} attribute. Because this is not type-safe, it is recommended only to |
| * use this attribute if the {@link OperationMode} enum does not represent your desired mode. |
| * <p/> |
| * <b>NOTE:</b> If you change the mode to one that does not support initialization vectors (such as |
| * {@link OperationMode#ECB ECB} or {@link OperationMode#NONE NONE}), you <em>must</em> turn off auto-generated |
| * initialization vectors by setting {@link #setGenerateInitializationVectors(boolean) generateInitializationVectors} |
| * to {@code false}. Abandoning initialization vectors significantly weakens encryption, so think twice before |
| * disabling this feature. |
| * |
| * <h3>Padding Scheme</h3> |
| * Because block ciphers process messages in fixed-length blocks, if the final block in a message is not equal to the |
| * block length, <a href="http://en.wikipedia.org/wiki/Padding_(cryptography)">padding</a> is applied to match that |
| * size to maintain the total length of the message. This is good because it protects data patterns from being |
| * identified - when all chunks look the same length, it is much harder to infer what that data might be. |
| * <p/> |
| * You may set a padding scheme via the {@link #setPaddingScheme(PaddingScheme) paddingScheme} attribute, which |
| * accepts a type-safe {@link PaddingScheme PaddingScheme} enum instance. Like the {@link OperationMode} enum, |
| * this enum offers type safety to help avoid typos and guarantees that the mode will be recongized by the underlying |
| * JCA provider. |
| * <p/> |
| * <b>*</b>If no padding scheme is specified, this class defaults to the {@link PaddingScheme#PKCS5} scheme, specifically |
| * to be compliant with the default behavior of auto-generating initialization vectors during encryption (see the |
| * {@link org.apache.shiro.crypto.JcaCipherService JcaCipherService parent class} class JavaDoc for why). |
| * <p/> |
| * In the rare case that you need to override the default with a scheme not represented by the {@link PaddingScheme} |
| * enum, you may specify the raw padding scheme name string that will be recognized by your JCA provider via the |
| * {@link #setPaddingScheme paddingSchemeName} attribute. Because this is not type-safe, it is recommended only to |
| * use this attribute if the {@link PaddingScheme} enum does not represent your desired scheme. |
| * |
| * <h2>Streaming</h2> |
| * Most people don't think of using block ciphers as stream ciphers, since their name implies working |
| * with block data (i.e. byte arrays) only. However, block ciphers can be turned into byte-oriented stream ciphers by |
| * using an appropriate {@link OperationMode operation mode} with a {@link #getStreamingBlockSize() streaming block size} |
| * of 8 bits. This is why the {@link CipherService} interface provides both block and streaming operations. |
| * <p/> |
| * Because this streaming 8-bit block size rarely changes across block-cipher algorithms, default values have been set |
| * for all three streaming configuration parameters. The defaults are: |
| * <ul> |
| * <li>{@link #setStreamingBlockSize(int) streamingBlockSize} = {@code 8} (bits)</li> |
| * <li>{@link #setStreamingMode streamingMode} = {@link OperationMode#CBC CBC}</li> |
| * <li>{@link #setStreamingPaddingScheme(PaddingScheme) streamingPaddingScheme} = {@link PaddingScheme#PKCS5 PKCS5}</li> |
| * </ul> |
| * <p/> |
| * These attributes have the same meaning as the {@code mode}, {@code blockSize}, and {@code paddingScheme} attributes |
| * described above, but they are applied during streaming method invocations only ({@link #encrypt(java.io.InputStream, java.io.OutputStream, byte[])} |
| * and {@link #decrypt(java.io.InputStream, java.io.OutputStream, byte[])}). |
| * |
| * @see BlowfishCipherService |
| * @see AesCipherService |
| * @see <a href="http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation">Wikipedia: Block Cipher Modes of Operation</a> |
| * @since 1.0 |
| */ |
| public class DefaultBlockCipherService extends AbstractSymmetricCipherService { |
| |
| private static final int DEFAULT_BLOCK_SIZE = 0; |
| |
| private static final String TRANSFORMATION_STRING_DELIMITER = "/"; |
| private static final int DEFAULT_STREAMING_BLOCK_SIZE = 8; //8 bits (1 byte) |
| |
| private String modeName; |
| private int blockSize; //size in bits (not bytes) - i.e. a blockSize of 8 equals 1 byte. negative or zero value = use system default |
| private String paddingSchemeName; |
| |
| private String streamingModeName; |
| private int streamingBlockSize; |
| private String streamingPaddingSchemeName; |
| |
| private String transformationString; //cached value - rebuilt whenever any of its constituent parts change |
| private String streamingTransformationString; //cached value - rebuilt whenever any of its constituent parts change |
| |
| |
| /** |
| * Creates a new {@link DefaultBlockCipherService} using the specified block cipher {@code algorithmName}. Per this |
| * class's JavaDoc, this constructor also sets the following defaults: |
| * <ul> |
| * <li>{@code streamingMode} = {@link OperationMode#CBC CBC}</li> |
| * <li>{@code streamingPaddingScheme} = {@link PaddingScheme#NONE none}</li> |
| * <li>{@code streamingBlockSize} = 8</li> |
| * </ul> |
| * All other attributes are null/unset, indicating the JCA Provider defaults will be used. |
| * |
| * @param algorithmName the block cipher algorithm to use when encrypting and decrypting |
| */ |
| public DefaultBlockCipherService(String algorithmName) { |
| <span class="fc" id="L165"> super(algorithmName);</span> |
| |
| <span class="fc" id="L167"> this.modeName = OperationMode.CBC.name();</span> |
| <span class="fc" id="L168"> this.paddingSchemeName = PaddingScheme.PKCS5.getTransformationName();</span> |
| <span class="fc" id="L169"> this.blockSize = DEFAULT_BLOCK_SIZE; //0 = use the JCA provider's default</span> |
| |
| <span class="fc" id="L171"> this.streamingModeName = OperationMode.CBC.name();</span> |
| <span class="fc" id="L172"> this.streamingPaddingSchemeName = PaddingScheme.PKCS5.getTransformationName();</span> |
| <span class="fc" id="L173"> this.streamingBlockSize = DEFAULT_STREAMING_BLOCK_SIZE;</span> |
| <span class="fc" id="L174"> }</span> |
| |
| /** |
| * Returns the cipher operation mode name (as a String) to be used when constructing |
| * {@link javax.crypto.Cipher Cipher} transformation string or {@code null} if the JCA Provider default mode for |
| * the specified {@link #getAlgorithmName() algorithm} should be used. |
| * <p/> |
| * This attribute is used <em>only</em> when constructing the transformation string for block (byte array) |
| * operations ({@link #encrypt(byte[], byte[])} and {@link #decrypt(byte[], byte[])}). The |
| * {@link #getStreamingModeName() streamingModeName} attribute is used when the block cipher is used for |
| * streaming operations. |
| * <p/> |
| * The default value is {@code null} to retain the JCA Provider default. |
| * |
| * @return the cipher operation mode name (as a String) to be used when constructing the |
| * {@link javax.crypto.Cipher Cipher} transformation string, or {@code null} if the JCA Provider default |
| * mode for the specified {@link #getAlgorithmName() algorithm} should be used. |
| */ |
| public String getModeName() { |
| <span class="fc" id="L193"> return modeName;</span> |
| } |
| |
| /** |
| * Sets the cipher operation mode name to be used when constructing the |
| * {@link javax.crypto.Cipher Cipher} transformation string. A {@code null} value indicates that the JCA Provider |
| * default mode for the specified {@link #getAlgorithmName() algorithm} should be used. |
| * <p/> |
| * This attribute is used <em>only</em> when constructing the transformation string for block (byte array) |
| * operations ({@link #encrypt(byte[], byte[])} and {@link #decrypt(byte[], byte[])}). The |
| * {@link #getStreamingModeName() streamingModeName} attribute is used when the block cipher is used for |
| * streaming operations. |
| * <p/> |
| * The default value is {@code null} to retain the JCA Provider default. |
| * <p/> |
| * <b>NOTE:</b> most standard mode names are represented by the {@link OperationMode OperationMode} enum. That enum |
| * should be used with the {@link #setMode mode} attribute when possible to retain type-safety and reduce the |
| * possibility of errors. This method is better used if the {@link OperationMode} enum does not represent the |
| * necessary mode. |
| * |
| * @param modeName the cipher operation mode name to be used when constructing |
| * {@link javax.crypto.Cipher Cipher} transformation string, or {@code null} if the JCA Provider |
| * default mode for the specified {@link #getAlgorithmName() algorithm} should be used. |
| * @see #setMode |
| */ |
| public void setModeName(String modeName) { |
| <span class="nc" id="L219"> this.modeName = modeName;</span> |
| //clear out the transformation string so the next invocation will rebuild it with the new mode: |
| <span class="nc" id="L221"> this.transformationString = null;</span> |
| <span class="nc" id="L222"> }</span> |
| |
| /** |
| * Sets the cipher operation mode of operation to be used when constructing the |
| * {@link javax.crypto.Cipher Cipher} transformation string. A {@code null} value indicates that the JCA Provider |
| * default mode for the specified {@link #getAlgorithmName() algorithm} should be used. |
| * <p/> |
| * This attribute is used <em>only</em> when constructing the transformation string for block (byte array) |
| * operations ({@link #encrypt(byte[], byte[])} and {@link #decrypt(byte[], byte[])}). The |
| * {@link #setStreamingMode streamingMode} attribute is used when the block cipher is used for |
| * streaming operations. |
| * <p/> |
| * If the {@link OperationMode} enum cannot represent your desired mode, you can set the name explicitly |
| * via the {@link #setModeName modeName} attribute directly. However, because {@link OperationMode} represents all |
| * standard JDK mode names already, ensure that your underlying JCA Provider supports the non-standard name first. |
| * |
| * @param mode the cipher operation mode to be used when constructing |
| * {@link javax.crypto.Cipher Cipher} transformation string, or {@code null} if the JCA Provider |
| * default mode for the specified {@link #getAlgorithmName() algorithm} should be used. |
| */ |
| public void setMode(OperationMode mode) { |
| <span class="nc" id="L243"> setModeName(mode.name());</span> |
| <span class="nc" id="L244"> }</span> |
| |
| /** |
| * Returns the cipher algorithm padding scheme name (as a String) to be used when constructing |
| * {@link javax.crypto.Cipher Cipher} transformation string or {@code null} if the JCA Provider default mode for |
| * the specified {@link #getAlgorithmName() algorithm} should be used. |
| * <p/> |
| * This attribute is used <em>only</em> when constructing the transformation string for block (byte array) |
| * operations ({@link #encrypt(byte[], byte[])} and {@link #decrypt(byte[], byte[])}). The |
| * {@link #getStreamingPaddingSchemeName() streamingPaddingSchemeName} attribute is used when the block cipher is |
| * used for streaming operations. |
| * <p/> |
| * The default value is {@code null} to retain the JCA Provider default. |
| * |
| * @return the padding scheme name (as a String) to be used when constructing the |
| * {@link javax.crypto.Cipher Cipher} transformation string, or {@code null} if the JCA Provider default |
| * padding scheme for the specified {@link #getAlgorithmName() algorithm} should be used. |
| */ |
| public String getPaddingSchemeName() { |
| <span class="fc" id="L263"> return paddingSchemeName;</span> |
| } |
| |
| /** |
| * Sets the padding scheme name to be used when constructing the |
| * {@link javax.crypto.Cipher Cipher} transformation string, or {@code null} if the JCA Provider default mode for |
| * the specified {@link #getAlgorithmName() algorithm} should be used. |
| * <p/> |
| * This attribute is used <em>only</em> when constructing the transformation string for block (byte array) |
| * operations ({@link #encrypt(byte[], byte[])} and {@link #decrypt(byte[], byte[])}). The |
| * {@link #getStreamingPaddingSchemeName() streamingPaddingSchemeName} attribute is used when the block cipher is |
| * used for streaming operations. |
| * <p/> |
| * The default value is {@code null} to retain the JCA Provider default. |
| * <p/> |
| * <b>NOTE:</b> most standard padding schemes are represented by the {@link PaddingScheme PaddingScheme} enum. |
| * That enum should be used with the {@link #setPaddingScheme paddingScheme} attribute when possible to retain |
| * type-safety and reduce the possibility of errors. Calling this method however is suitable if the |
| * {@code PaddingScheme} enum does not represent the desired scheme. |
| * |
| * @param paddingSchemeName the padding scheme name to be used when constructing |
| * {@link javax.crypto.Cipher Cipher} transformation string, or {@code null} if the JCA |
| * Provider default padding scheme for the specified {@link #getAlgorithmName() algorithm} |
| * should be used. |
| * @see #setPaddingScheme |
| */ |
| public void setPaddingSchemeName(String paddingSchemeName) { |
| <span class="nc" id="L290"> this.paddingSchemeName = paddingSchemeName;</span> |
| //clear out the transformation string so the next invocation will rebuild it with the new padding scheme: |
| <span class="nc" id="L292"> this.transformationString = null;</span> |
| <span class="nc" id="L293"> }</span> |
| |
| /** |
| * Sets the padding scheme to be used when constructing the |
| * {@link javax.crypto.Cipher Cipher} transformation string. A {@code null} value indicates that the JCA Provider |
| * default padding scheme for the specified {@link #getAlgorithmName() algorithm} should be used. |
| * <p/> |
| * This attribute is used <em>only</em> when constructing the transformation string for block (byte array) |
| * operations ({@link #encrypt(byte[], byte[])} and {@link #decrypt(byte[], byte[])}). The |
| * {@link #setStreamingPaddingScheme streamingPaddingScheme} attribute is used when the block cipher is used for |
| * streaming operations. |
| * <p/> |
| * If the {@link PaddingScheme PaddingScheme} enum does represent your desired scheme, you can set the name explicitly |
| * via the {@link #setPaddingSchemeName paddingSchemeName} attribute directly. However, because |
| * {@code PaddingScheme} represents all standard JDK scheme names already, ensure that your underlying JCA Provider |
| * supports the non-standard name first. |
| * |
| * @param paddingScheme the padding scheme to be used when constructing |
| * {@link javax.crypto.Cipher Cipher} transformation string, or {@code null} if the JCA Provider |
| * default padding scheme for the specified {@link #getAlgorithmName() algorithm} should be used. |
| */ |
| public void setPaddingScheme(PaddingScheme paddingScheme) { |
| <span class="nc" id="L315"> setPaddingSchemeName(paddingScheme.getTransformationName());</span> |
| <span class="nc" id="L316"> }</span> |
| |
| /** |
| * Returns the block cipher's block size to be used when constructing |
| * {@link javax.crypto.Cipher Cipher} transformation string or {@code 0} if the JCA Provider default block size |
| * for the specified {@link #getAlgorithmName() algorithm} should be used. |
| * <p/> |
| * This attribute is used <em>only</em> when constructing the transformation string for block (byte array) |
| * operations ({@link #encrypt(byte[], byte[])} and {@link #decrypt(byte[], byte[])}). The |
| * {@link #getStreamingBlockSize() streamingBlockSize} attribute is used when the block cipher is used for |
| * streaming operations. |
| * <p/> |
| * The default value is {@code 0} which retains the JCA Provider default. |
| * |
| * @return the block cipher block size to be used when constructing the |
| * {@link javax.crypto.Cipher Cipher} transformation string, or {@code 0} if the JCA Provider default |
| * block size for the specified {@link #getAlgorithmName() algorithm} should be used. |
| */ |
| public int getBlockSize() { |
| <span class="fc" id="L335"> return blockSize;</span> |
| } |
| |
| /** |
| * Sets the block cipher's block size to be used when constructing |
| * {@link javax.crypto.Cipher Cipher} transformation string. {@code 0} indicates that the JCA Provider default |
| * block size for the specified {@link #getAlgorithmName() algorithm} should be used. |
| * <p/> |
| * This attribute is used <em>only</em> when constructing the transformation string for block (byte array) |
| * operations ({@link #encrypt(byte[], byte[])} and {@link #decrypt(byte[], byte[])}). The |
| * {@link #getStreamingBlockSize() streamingBlockSize} attribute is used when the block cipher is used for |
| * streaming operations. |
| * <p/> |
| * The default value is {@code 0} which retains the JCA Provider default. |
| * <p/> |
| * <b>NOTE:</b> block cipher block sizes are very algorithm-specific. If you change this value, ensure that it |
| * will work with the specified {@link #getAlgorithmName() algorithm}. |
| * |
| * @param blockSize the block cipher block size to be used when constructing the |
| * {@link javax.crypto.Cipher Cipher} transformation string, or {@code 0} if the JCA Provider |
| * default block size for the specified {@link #getAlgorithmName() algorithm} should be used. |
| */ |
| public void setBlockSize(int blockSize) { |
| <span class="nc" id="L358"> this.blockSize = Math.max(DEFAULT_BLOCK_SIZE, blockSize);</span> |
| //clear out the transformation string so the next invocation will rebuild it with the new block size: |
| <span class="nc" id="L360"> this.transformationString = null;</span> |
| <span class="nc" id="L361"> }</span> |
| |
| /** |
| * Same purpose as the {@link #getModeName modeName} attribute, but is used instead only for for streaming |
| * operations ({@link #encrypt(java.io.InputStream, java.io.OutputStream, byte[])} and |
| * {@link #decrypt(java.io.InputStream, java.io.OutputStream, byte[])}). |
| * <p/> |
| * Note that unlike the {@link #getModeName modeName} attribute, the default value of this attribute is not |
| * {@code null} - it is {@link OperationMode#CBC CBC} for reasons described in the class-level JavaDoc in the |
| * {@code Streaming} section. |
| * |
| * @return the transformation string mode name to be used for streaming operations only. |
| */ |
| public String getStreamingModeName() { |
| <span class="fc" id="L375"> return streamingModeName;</span> |
| } |
| |
| private boolean isModeStreamingCompatible(String modeName) { |
| <span class="nc bnc" id="L379" title="All 2 branches missed."> return modeName != null &&</span> |
| <span class="nc bnc" id="L380" title="All 2 branches missed."> !modeName.equalsIgnoreCase(OperationMode.ECB.name()) &&</span> |
| <span class="nc bnc" id="L381" title="All 2 branches missed."> !modeName.equalsIgnoreCase(OperationMode.NONE.name());</span> |
| } |
| |
| /** |
| * Sets the transformation string mode name to be used for streaming operations only. The default value is |
| * {@link OperationMode#CBC CBC} for reasons described in the class-level JavaDoc in the {@code Streaming} section. |
| * |
| * @param streamingModeName transformation string mode name to be used for streaming operations only |
| */ |
| public void setStreamingModeName(String streamingModeName) { |
| <span class="nc bnc" id="L391" title="All 2 branches missed."> if (!isModeStreamingCompatible(streamingModeName)) {</span> |
| <span class="nc" id="L392"> String msg = "mode [" + streamingModeName + "] is not a valid operation mode for block cipher streaming.";</span> |
| <span class="nc" id="L393"> throw new IllegalArgumentException(msg);</span> |
| } |
| <span class="nc" id="L395"> this.streamingModeName = streamingModeName;</span> |
| //clear out the streaming transformation string so the next invocation will rebuild it with the new mode: |
| <span class="nc" id="L397"> this.streamingTransformationString = null;</span> |
| <span class="nc" id="L398"> }</span> |
| |
| /** |
| * Sets the transformation string mode to be used for streaming operations only. The default value is |
| * {@link OperationMode#CBC CBC} for reasons described in the class-level JavaDoc in the {@code Streaming} section. |
| * |
| * @param mode the transformation string mode to be used for streaming operations only |
| */ |
| public void setStreamingMode(OperationMode mode) { |
| <span class="nc" id="L407"> setStreamingModeName(mode.name());</span> |
| <span class="nc" id="L408"> }</span> |
| |
| public String getStreamingPaddingSchemeName() { |
| <span class="fc" id="L411"> return streamingPaddingSchemeName;</span> |
| } |
| |
| public void setStreamingPaddingSchemeName(String streamingPaddingSchemeName) { |
| <span class="nc" id="L415"> this.streamingPaddingSchemeName = streamingPaddingSchemeName;</span> |
| //clear out the streaming transformation string so the next invocation will rebuild it with the new scheme: |
| <span class="nc" id="L417"> this.streamingTransformationString = null;</span> |
| <span class="nc" id="L418"> }</span> |
| |
| public void setStreamingPaddingScheme(PaddingScheme scheme) { |
| <span class="nc" id="L421"> setStreamingPaddingSchemeName(scheme.getTransformationName());</span> |
| <span class="nc" id="L422"> }</span> |
| |
| public int getStreamingBlockSize() { |
| <span class="nc" id="L425"> return streamingBlockSize;</span> |
| } |
| |
| public void setStreamingBlockSize(int streamingBlockSize) { |
| <span class="nc" id="L429"> this.streamingBlockSize = Math.max(DEFAULT_BLOCK_SIZE, streamingBlockSize);</span> |
| //clear out the streaming transformation string so the next invocation will rebuild it with the new block size: |
| <span class="nc" id="L431"> this.streamingTransformationString = null;</span> |
| <span class="nc" id="L432"> }</span> |
| |
| /** |
| * Returns the transformation string to use with the {@link javax.crypto.Cipher#getInstance} call. If |
| * {@code streaming} is {@code true}, a block-cipher transformation string compatible with streaming operations will |
| * be constructed and cached for re-use later (see the class-level JavaDoc for more on using block ciphers |
| * for streaming). If {@code streaming} is {@code false} a normal block-cipher transformation string will |
| * be constructed and cached for later re-use. |
| * |
| * @param streaming if the transformation string is going to be used for a Cipher performing stream-based encryption or not. |
| * @return the transformation string |
| */ |
| protected String getTransformationString(boolean streaming) { |
| <span class="fc bfc" id="L445" title="All 2 branches covered."> if (streaming) {</span> |
| <span class="fc bfc" id="L446" title="All 2 branches covered."> if (this.streamingTransformationString == null) {</span> |
| <span class="fc" id="L447"> this.streamingTransformationString = buildStreamingTransformationString();</span> |
| } |
| <span class="fc" id="L449"> return this.streamingTransformationString;</span> |
| } else { |
| <span class="fc bfc" id="L451" title="All 2 branches covered."> if (this.transformationString == null) {</span> |
| <span class="fc" id="L452"> this.transformationString = buildTransformationString();</span> |
| } |
| <span class="fc" id="L454"> return this.transformationString;</span> |
| } |
| } |
| |
| private String buildTransformationString() { |
| <span class="fc" id="L459"> return buildTransformationString(getModeName(), getPaddingSchemeName(), getBlockSize());</span> |
| } |
| |
| private String buildStreamingTransformationString() { |
| <span class="fc" id="L463"> return buildTransformationString(getStreamingModeName(), getStreamingPaddingSchemeName(), 0);</span> |
| } |
| |
| private String buildTransformationString(String modeName, String paddingSchemeName, int blockSize) { |
| <span class="fc" id="L467"> StringBuilder sb = new StringBuilder(getAlgorithmName());</span> |
| <span class="pc bpc" id="L468" title="1 of 2 branches missed."> if (StringUtils.hasText(modeName)) {</span> |
| <span class="fc" id="L469"> sb.append(TRANSFORMATION_STRING_DELIMITER).append(modeName);</span> |
| } |
| <span class="pc bpc" id="L471" title="1 of 2 branches missed."> if (blockSize > 0) {</span> |
| <span class="nc" id="L472"> sb.append(blockSize);</span> |
| } |
| <span class="pc bpc" id="L474" title="1 of 2 branches missed."> if (StringUtils.hasText(paddingSchemeName)) {</span> |
| <span class="fc" id="L475"> sb.append(TRANSFORMATION_STRING_DELIMITER).append(paddingSchemeName);</span> |
| } |
| <span class="fc" id="L477"> return sb.toString();</span> |
| } |
| |
| /** |
| * Returns {@code true} if the specified cipher operation mode name supports initialization vectors, |
| * {@code false} otherwise. |
| * |
| * @param modeName the raw text name of the mode of operation |
| * @return {@code true} if the specified cipher operation mode name supports initialization vectors, |
| * {@code false} otherwise. |
| */ |
| private boolean isModeInitializationVectorCompatible(String modeName) { |
| <span class="pc bpc" id="L489" title="1 of 2 branches missed."> return modeName != null &&</span> |
| <span class="pc bpc" id="L490" title="1 of 2 branches missed."> !modeName.equalsIgnoreCase(OperationMode.ECB.name()) &&</span> |
| <span class="pc bpc" id="L491" title="1 of 2 branches missed."> !modeName.equalsIgnoreCase(OperationMode.NONE.name());</span> |
| } |
| |
| /** |
| * Overrides the parent implementation to ensure initialization vectors are always generated if streaming is |
| * enabled (block ciphers <em>must</em> use initialization vectors if they are to be used as a stream cipher). If |
| * not being used as a stream cipher, then the value is computed based on whether or not the currently configured |
| * {@link #getModeName modeName} is compatible with initialization vectors as well as the result of the configured |
| * {@link #setGenerateInitializationVectors(boolean) generateInitializationVectors} value. |
| * |
| * @param streaming whether or not streaming is being performed |
| * @return {@code true} if streaming or a value computed based on if the currently configured mode is compatible |
| * with initialization vectors. |
| */ |
| @Override |
| protected boolean isGenerateInitializationVectors(boolean streaming) { |
| <span class="pc bpc" id="L507" title="2 of 6 branches missed."> return streaming || super.isGenerateInitializationVectors() && isModeInitializationVectorCompatible(getModeName());</span> |
| } |
| |
| @Override |
| protected byte[] generateInitializationVector(boolean streaming) { |
| <span class="fc bfc" id="L512" title="All 2 branches covered."> if (streaming) {</span> |
| <span class="fc" id="L513"> String streamingModeName = getStreamingModeName();</span> |
| <span class="pc bpc" id="L514" title="1 of 2 branches missed."> if (!isModeInitializationVectorCompatible(streamingModeName)) {</span> |
| <span class="nc" id="L515"> String msg = "streamingMode attribute value [" + streamingModeName + "] does not support " +</span> |
| "Initialization Vectors. Ensure the streamingMode value represents an operation mode " + |
| "that is compatible with initialization vectors."; |
| <span class="nc" id="L518"> throw new IllegalStateException(msg);</span> |
| } |
| <span class="fc" id="L520"> } else {</span> |
| <span class="fc" id="L521"> String modeName = getModeName();</span> |
| <span class="pc bpc" id="L522" title="1 of 2 branches missed."> if (!isModeInitializationVectorCompatible(modeName)) {</span> |
| <span class="nc" id="L523"> String msg = "mode attribute value [" + modeName + "] does not support " +</span> |
| "Initialization Vectors. Ensure the mode value represents an operation mode " + |
| "that is compatible with initialization vectors."; |
| <span class="nc" id="L526"> throw new IllegalStateException(msg);</span> |
| } |
| } |
| <span class="fc" id="L529"> return super.generateInitializationVector(streaming);</span> |
| } |
| } |
| </pre><div class="footer"><span class="right">Created with <a href="http://www.eclemma.org/jacoco">JaCoCo</a> 0.7.7.201606060606</span></div></body></html> |