View Javadoc

1   /*
2    * Copyright 2014 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  
17  package io.netty.handler.ssl.util;
18  
19  import io.netty.util.internal.PlatformDependent;
20  
21  import java.security.SecureRandom;
22  import java.util.Random;
23  
24  /**
25   * Insecure {@link SecureRandom} which relies on {@link PlatformDependent#threadLocalRandom()} for random number
26   * generation.
27   */
28  final class ThreadLocalInsecureRandom extends SecureRandom {
29  
30      private static final long serialVersionUID = -8209473337192526191L;
31  
32      private static final SecureRandom INSTANCE = new ThreadLocalInsecureRandom();
33  
34      static SecureRandom current() {
35          return INSTANCE;
36      }
37  
38      private ThreadLocalInsecureRandom() { }
39  
40      @Override
41      public String getAlgorithm() {
42          return "insecure";
43      }
44  
45      @Override
46      public void setSeed(byte[] seed) { }
47  
48      @Override
49      public void setSeed(long seed) { }
50  
51      @Override
52      public void nextBytes(byte[] bytes) {
53          random().nextBytes(bytes);
54      }
55  
56      @Override
57      public byte[] generateSeed(int numBytes) {
58          byte[] seed = new byte[numBytes];
59          random().nextBytes(seed);
60          return seed;
61      }
62  
63      @Override
64      public int nextInt() {
65          return random().nextInt();
66      }
67  
68      @Override
69      public int nextInt(int n) {
70          return random().nextInt(n);
71      }
72  
73      @Override
74      public boolean nextBoolean() {
75          return random().nextBoolean();
76      }
77  
78      @Override
79      public long nextLong() {
80          return random().nextLong();
81      }
82  
83      @Override
84      public float nextFloat() {
85          return random().nextFloat();
86      }
87  
88      @Override
89      public double nextDouble() {
90          return random().nextDouble();
91      }
92  
93      @Override
94      public double nextGaussian() {
95          return random().nextGaussian();
96      }
97  
98      private static Random random() {
99          return PlatformDependent.threadLocalRandom();
100     }
101 }