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