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 org.jboss.netty.handler.codec.http.websocketx;
17  
18  import java.security.MessageDigest;
19  import java.security.NoSuchAlgorithmException;
20  
21  import org.jboss.netty.buffer.ChannelBuffer;
22  import org.jboss.netty.buffer.ChannelBuffers;
23  import org.jboss.netty.handler.codec.base64.Base64;
24  import org.jboss.netty.util.CharsetUtil;
25  
26  /**
27   * TODO Document me.
28   */
29  final class WebSocketUtil {
30  
31      /**
32       * Performs an MD5 hash
33       *
34       * @param bytes
35       *            Data to hash
36       * @return Hashed data
37       */
38      static byte[] md5(byte[] bytes) {
39          try {
40              MessageDigest md = MessageDigest.getInstance("MD5");
41              return md.digest(bytes);
42          } catch (NoSuchAlgorithmException e) {
43              throw new InternalError("MD5 not supported on this platform");
44          }
45      }
46  
47      /**
48       * Performs an SHA-1 hash
49       *
50       * @param bytes
51       *            Data to hash
52       * @return Hashed data
53       */
54      static byte[] sha1(byte[] bytes) {
55          try {
56              MessageDigest md = MessageDigest.getInstance("SHA1");
57              return md.digest(bytes);
58          } catch (NoSuchAlgorithmException e) {
59              throw new InternalError("SHA-1 not supported on this platform");
60          }
61      }
62  
63      /**
64       * Base 64 encoding
65       *
66       * @param bytes
67       *            Bytes to encode
68       * @return encoded string
69       */
70      static String base64(byte[] bytes) {
71          ChannelBuffer hashed = ChannelBuffers.wrappedBuffer(bytes);
72          return Base64.encode(hashed).toString(CharsetUtil.UTF_8);
73      }
74  
75      /**
76       * Creates some random bytes
77       *
78       * @param size
79       *            Number of random bytes to create
80       * @return random bytes
81       */
82      static byte[] randomBytes(int size) {
83          byte[] bytes = new byte[size];
84  
85          for (int i = 0; i < size; i++) {
86              bytes[i] = (byte) randomNumber(0, 255);
87          }
88  
89          return bytes;
90      }
91  
92      /**
93       * Generates a random number
94       *
95       * @param min
96       *            Minimum value
97       * @param max
98       *            Maximum value
99       * @return Random number
100      */
101     static int randomNumber(int min, int max) {
102         return (int) (Math.random() * max + min);
103     }
104 
105 
106     private WebSocketUtil() {
107         // Unused
108     }
109 }