View Javadoc
1   /*
2    * Copyright 2012 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  package io.netty.handler.codec.http.websocketx;
17  
18  import io.netty.buffer.ByteBuf;
19  import io.netty.buffer.Unpooled;
20  import io.netty.handler.codec.base64.Base64;
21  import io.netty.util.CharsetUtil;
22  import io.netty.util.concurrent.FastThreadLocal;
23  import io.netty.util.internal.PlatformDependent;
24  
25  import java.security.MessageDigest;
26  import java.security.NoSuchAlgorithmException;
27  
28  /**
29   * A utility class mainly for use by web sockets
30   */
31  final class WebSocketUtil {
32  
33      private static final FastThreadLocal<MessageDigest> MD5 = new FastThreadLocal<MessageDigest>() {
34          @Override
35          protected MessageDigest initialValue() throws Exception {
36              try {
37                  //Try to get a MessageDigest that uses MD5
38                  return MessageDigest.getInstance("MD5");
39              } catch (NoSuchAlgorithmException e) {
40                  //This shouldn't happen! How old is the computer?
41                  throw new InternalError("MD5 not supported on this platform - Outdated?");
42              }
43          }
44      };
45  
46      private static final FastThreadLocal<MessageDigest> SHA1 = new FastThreadLocal<MessageDigest>() {
47          @Override
48          protected MessageDigest initialValue() throws Exception {
49              try {
50                  //Try to get a MessageDigest that uses SHA1
51                  return MessageDigest.getInstance("SHA1");
52              } catch (NoSuchAlgorithmException e) {
53                  //This shouldn't happen! How old is the computer?
54                  throw new InternalError("SHA-1 not supported on this platform - Outdated?");
55              }
56          }
57      };
58  
59      /**
60       * Performs a MD5 hash on the specified data
61       *
62       * @param data The data to hash
63       * @return The hashed data
64       */
65      static byte[] md5(byte[] data) {
66          // TODO(normanmaurer): Create md5 method that not need MessageDigest.
67          return digest(MD5, data);
68      }
69  
70      /**
71       * Performs a SHA-1 hash on the specified data
72       *
73       * @param data The data to hash
74       * @return The hashed data
75       */
76      static byte[] sha1(byte[] data) {
77          // TODO(normanmaurer): Create sha1 method that not need MessageDigest.
78          return digest(SHA1, data);
79      }
80  
81      private static byte[] digest(FastThreadLocal<MessageDigest> digestFastThreadLocal, byte[] data) {
82          MessageDigest digest = digestFastThreadLocal.get();
83          digest.reset();
84          return digest.digest(data);
85      }
86  
87      /**
88       * Performs base64 encoding on the specified data
89       *
90       * @param data The data to encode
91       * @return An encoded string containing the data
92       */
93      static String base64(byte[] data) {
94          ByteBuf encodedData = Unpooled.wrappedBuffer(data);
95          ByteBuf encoded = Base64.encode(encodedData);
96          String encodedString = encoded.toString(CharsetUtil.UTF_8);
97          encoded.release();
98          return encodedString;
99      }
100 
101     /**
102      * Creates an arbitrary number of random bytes
103      *
104      * @param size the number of random bytes to create
105      * @return An array of random bytes
106      */
107     static byte[] randomBytes(int size) {
108         byte[] bytes = new byte[size];
109         PlatformDependent.threadLocalRandom().nextBytes(bytes);
110         return bytes;
111     }
112 
113     /**
114      * Generates a pseudo-random number
115      *
116      * @param minimum The minimum allowable value
117      * @param maximum The maximum allowable value
118      * @return A pseudo-random number
119      */
120     static int randomNumber(int minimum, int maximum) {
121         assert minimum < maximum;
122         double fraction = PlatformDependent.threadLocalRandom().nextDouble();
123 
124         // the idea here is that nextDouble gives us a random value
125         //
126         //              0 <= fraction <= 1
127         //
128         // the distance from min to max declared as
129         //
130         //              dist = max - min
131         //
132         // satisfies the following
133         //
134         //              min + dist = max
135         //
136         // taking into account
137         //
138         //         0 <= fraction * dist <= dist
139         //
140         // we've got
141         //
142         //       min <= min + fraction * dist <= max
143         return (int) (minimum + fraction * (maximum - minimum));
144     }
145 
146     /**
147      * A private constructor to ensure that instances of this class cannot be made
148      */
149     private WebSocketUtil() {
150         // Unused
151     }
152 }