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       * @deprecated use {@link #md5(ChannelBuffer)}
33       */
34      @Deprecated
35      static byte[] md5(byte[] bytes) {
36          try {
37              MessageDigest md = MessageDigest.getInstance("MD5");
38              return md.digest(bytes);
39          } catch (NoSuchAlgorithmException e) {
40              throw new InternalError("MD5 not supported on this platform");
41          }
42      }
43  
44      /**
45       * Performs an MD5 hash
46       *
47       * @param buffer
48       *            buffer to hash
49       * @return Hashed data
50       */
51      static ChannelBuffer md5(ChannelBuffer buffer) {
52          try {
53              MessageDigest md = MessageDigest.getInstance("MD5");
54              if (buffer.hasArray()) {
55                  int offset = buffer.arrayOffset() + buffer.readerIndex();
56                  int length = buffer.readableBytes();
57                  md.update(buffer.array(), offset, length);
58              } else {
59                  md.update(buffer.toByteBuffer());
60              }
61              return ChannelBuffers.wrappedBuffer(md.digest());
62          } catch (NoSuchAlgorithmException e) {
63              throw new InternalError("MD5 not supported on this platform");
64          }
65      }
66  
67      /**
68       * @deprecated use {@link #sha1(ChannelBuffer)}
69       */
70      @Deprecated
71      static byte[] sha1(byte[] bytes) {
72          try {
73              MessageDigest md = MessageDigest.getInstance("SHA1");
74              return md.digest(bytes);
75          } catch (NoSuchAlgorithmException e) {
76              throw new InternalError("SHA-1 not supported on this platform");
77          }
78      }
79  
80      /**
81       * Performs an SHA-1 hash
82       *
83       * @param buffer
84       *            buffer to hash
85       * @return Hashed data
86       */
87      static ChannelBuffer sha1(ChannelBuffer buffer) {
88          try {
89              MessageDigest md = MessageDigest.getInstance("SHA1");
90              if (buffer.hasArray()) {
91                  int offset = buffer.arrayOffset() + buffer.readerIndex();
92                  int length = buffer.readableBytes();
93                  md.update(buffer.array(), offset, length);
94              } else {
95                  md.update(buffer.toByteBuffer());
96              }
97              return ChannelBuffers.wrappedBuffer(md.digest());
98          } catch (NoSuchAlgorithmException e) {
99              throw new InternalError("SHA-1 not supported on this platform");
100         }
101     }
102 
103     /**
104      * @deprecated use {@link #base64(ChannelBuffer)}
105      */
106     @Deprecated
107     static String base64(byte[] bytes) {
108         ChannelBuffer hashed = ChannelBuffers.wrappedBuffer(bytes);
109         return Base64.encode(hashed).toString(CharsetUtil.UTF_8);
110     }
111 
112     /**
113      * Base 64 encoding
114      *
115      * @param buffer
116      *            Bytes to encode
117      * @return encoded string
118      */
119     static String base64(ChannelBuffer buffer) {
120         return Base64.encode(buffer).toString(CharsetUtil.UTF_8);
121     }
122 
123     /**
124      * Creates some random bytes
125      *
126      * @param size
127      *            Number of random bytes to create
128      * @return random bytes
129      */
130     static byte[] randomBytes(int size) {
131         byte[] bytes = new byte[size];
132 
133         for (int i = 0; i < size; i++) {
134             bytes[i] = (byte) randomNumber(0, 255);
135         }
136 
137         return bytes;
138     }
139 
140     /**
141      * Generates a random number
142      *
143      * @param min
144      *            Minimum value
145      * @param max
146      *            Maximum value
147      * @return Random number
148      */
149     static int randomNumber(int min, int max) {
150         return (int) (Math.random() * max + min);
151     }
152 
153     private WebSocketUtil() {
154         // Unused
155     }
156 }