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 java.security.MessageDigest;
23  import java.security.NoSuchAlgorithmException;
24  
25  /**
26   * A utility class mainly for use by web sockets
27   */
28  final class WebSocketUtil {
29  
30      /**
31       * Performs a MD5 hash on the specified data
32       *
33       * @param data The data to hash
34       * @return The hashed data
35       */
36      static byte[] md5(byte[] data) {
37          try {
38              //Try to get a MessageDigest that uses MD5
39              MessageDigest md = MessageDigest.getInstance("MD5");
40              //Hash the data
41              return md.digest(data);
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       * Performs a SHA-1 hash on the specified data
50       *
51       * @param data The data to hash
52       * @return The hashed data
53       */
54      static byte[] sha1(byte[] data) {
55          try {
56              //Attempt to get a MessageDigest that uses SHA1
57              MessageDigest md = MessageDigest.getInstance("SHA1");
58              //Hash the data
59              return md.digest(data);
60          } catch (NoSuchAlgorithmException e) {
61              //Alright, you might have an old system.
62              throw new InternalError("SHA-1 is not supported on this platform - Outdated?");
63          }
64      }
65  
66      /**
67       * Performs base64 encoding on the specified data
68       *
69       * @param data The data to encode
70       * @return An encoded string containing the data
71       */
72      static String base64(byte[] data) {
73          ByteBuf encodedData = Unpooled.wrappedBuffer(data);
74          ByteBuf encoded = Base64.encode(encodedData);
75          String encodedString = encoded.toString(CharsetUtil.UTF_8);
76          encoded.release();
77          return encodedString;
78      }
79  
80      /**
81       * Creates an arbitrary number of random bytes
82       *
83       * @param size the number of random bytes to create
84       * @return An array of random bytes
85       */
86      static byte[] randomBytes(int size) {
87          byte[] bytes = new byte[size];
88  
89          for (int index = 0; index < size; index++) {
90              bytes[index] = (byte) randomNumber(0, 255);
91          }
92  
93          return bytes;
94      }
95  
96      /**
97       * Generates a pseudo-random number
98       *
99       * @param minimum The minimum allowable value
100      * @param maximum The maximum allowable value
101      * @return A pseudo-random number
102      */
103     static int randomNumber(int minimum, int maximum) {
104         return (int) (Math.random() * maximum + minimum);
105     }
106 
107     /**
108      * A private constructor to ensure that instances of this class cannot be made
109      */
110     private WebSocketUtil() {
111         // Unused
112     }
113 }