blob: 046ac13b88e6160fd9d429a7703bee80a38b0ec0 [file] [log] [blame]
<?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>SecureRandomNumberGenerator.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 :: Core</a> &gt; <a href="index.source.html" class="el_package">org.apache.shiro.crypto</a> &gt; <span class="el_source">SecureRandomNumberGenerator.java</span></div><h1>SecureRandomNumberGenerator.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
* &quot;License&quot;); 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
* &quot;AS IS&quot; 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.ByteSource;
import java.security.SecureRandom;
/**
* Default implementation of the {@link RandomNumberGenerator RandomNumberGenerator} interface, backed by a
* {@link SecureRandom SecureRandom} instance.
* &lt;p/&gt;
* This class is a little easier to use than using the JDK's {@code SecureRandom} class directly. It also
* allows for JavaBeans-style of customization, convenient for Shiro's INI configuration or other IoC configuration
* mechanism.
*
* @since 1.1
*/
public class SecureRandomNumberGenerator implements RandomNumberGenerator {
protected static final int DEFAULT_NEXT_BYTES_SIZE = 16; //16 bytes == 128 bits (a common number in crypto)
private int defaultNextBytesSize;
private SecureRandom secureRandom;
/**
* Creates a new instance with a default backing {@link SecureRandom SecureRandom} and a
* {@link #getDefaultNextBytesSize() defaultNextBytesSize} of {@code 16}, which equals 128 bits, a size commonly
* used in cryptographic algorithms.
*/
<span class="fc" id="L47"> public SecureRandomNumberGenerator() {</span>
<span class="fc" id="L48"> this.defaultNextBytesSize = DEFAULT_NEXT_BYTES_SIZE;</span>
<span class="fc" id="L49"> this.secureRandom = new SecureRandom();</span>
<span class="fc" id="L50"> }</span>
/**
* Seeds the backing {@link SecureRandom SecureRandom} instance with additional seed data.
*
* @param bytes the seed bytes
* @see SecureRandom#setSeed(byte[])
*/
public void setSeed(byte[] bytes) {
<span class="nc" id="L59"> this.secureRandom.setSeed(bytes);</span>
<span class="nc" id="L60"> }</span>
/**
* Returns the {@link SecureRandom SecureRandom} backing this instance.
*
* @return the {@link SecureRandom SecureRandom} backing this instance.
*/
public SecureRandom getSecureRandom() {
<span class="nc" id="L68"> return secureRandom;</span>
}
/**
* Sets the {@link SecureRandom SecureRandom} to back this instance.
*
* @param random the {@link SecureRandom SecureRandom} to back this instance.
* @throws NullPointerException if the method argument is null
*/
public void setSecureRandom(SecureRandom random) throws NullPointerException {
<span class="pc bpc" id="L78" title="1 of 2 branches missed."> if (random == null) {</span>
<span class="fc" id="L79"> throw new NullPointerException(&quot;SecureRandom argument cannot be null.&quot;);</span>
}
<span class="nc" id="L81"> this.secureRandom = random;</span>
<span class="nc" id="L82"> }</span>
/**
* Returns the size of the generated byte array for calls to {@link #nextBytes() nextBytes()}. Defaults to
* {@code 16}, which equals 128 bits, a size commonly used in cryptographic algorithms.
*
* @return the size of the generated byte array for calls to {@link #nextBytes() nextBytes()}.
*/
public int getDefaultNextBytesSize() {
<span class="fc" id="L91"> return defaultNextBytesSize;</span>
}
/**
* Sets the size of the generated byte array for calls to {@link #nextBytes() nextBytes()}. Defaults to
* {@code 16}, which equals 128 bits, a size commonly used in cryptographic algorithms.
*
* @param defaultNextBytesSize the size of the generated byte array for calls to {@link #nextBytes() nextBytes()}.
* @throws IllegalArgumentException if the argument is 0 or negative
*/
public void setDefaultNextBytesSize(int defaultNextBytesSize) throws IllegalArgumentException {
<span class="fc bfc" id="L102" title="All 2 branches covered."> if ( defaultNextBytesSize &lt;= 0) {</span>
<span class="fc" id="L103"> throw new IllegalArgumentException(&quot;size value must be a positive integer (1 or larger)&quot;);</span>
}
<span class="fc" id="L105"> this.defaultNextBytesSize = defaultNextBytesSize;</span>
<span class="fc" id="L106"> }</span>
public ByteSource nextBytes() {
<span class="fc" id="L109"> return nextBytes(getDefaultNextBytesSize());</span>
}
public ByteSource nextBytes(int numBytes) {
<span class="fc bfc" id="L113" title="All 2 branches covered."> if (numBytes &lt;= 0) {</span>
<span class="fc" id="L114"> throw new IllegalArgumentException(&quot;numBytes argument must be a positive integer (1 or larger)&quot;);</span>
}
<span class="fc" id="L116"> byte[] bytes = new byte[numBytes];</span>
<span class="fc" id="L117"> this.secureRandom.nextBytes(bytes);</span>
<span class="fc" id="L118"> return ByteSource.Util.bytes(bytes);</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>