查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
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 org.jboss.netty.handler.ssl.util;
18  
19  import org.jboss.netty.util.internal.ThreadLocalRandom;
20  
21  import java.security.SecureRandom;
22  import java.util.Random;
23  
24  /**
25   * Insecure {@link SecureRandom} which relies on {@link ThreadLocalRandom} for random number generation.
26   */
27  final class ThreadLocalInsecureRandom extends SecureRandom {
28  
29      private static final long serialVersionUID = -8209473337192526191L;
30  
31      private static final SecureRandom INSTANCE = new ThreadLocalInsecureRandom();
32  
33      static SecureRandom current() {
34          return INSTANCE;
35      }
36  
37      private ThreadLocalInsecureRandom() { }
38  
39      @Override
40      public String getAlgorithm() {
41          return "insecure";
42      }
43  
44      @Override
45      public void setSeed(byte[] seed) { }
46  
47      @Override
48      public void setSeed(long seed) { }
49  
50      @Override
51      public void nextBytes(byte[] bytes) {
52          random().nextBytes(bytes);
53      }
54  
55      @Override
56      public byte[] generateSeed(int numBytes) {
57          byte[] seed = new byte[numBytes];
58          random().nextBytes(seed);
59          return seed;
60      }
61  
62      @Override
63      public int nextInt() {
64          return random().nextInt();
65      }
66  
67      @Override
68      public int nextInt(int n) {
69          return random().nextInt(n);
70      }
71  
72      @Override
73      public boolean nextBoolean() {
74          return random().nextBoolean();
75      }
76  
77      @Override
78      public long nextLong() {
79          return random().nextLong();
80      }
81  
82      @Override
83      public float nextFloat() {
84          return random().nextFloat();
85      }
86  
87      @Override
88      public double nextDouble() {
89          return random().nextDouble();
90      }
91  
92      @Override
93      public double nextGaussian() {
94          return random().nextGaussian();
95      }
96  
97      private static Random random() {
98          return ThreadLocalRandom.current();
99      }
100 }