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