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 io.netty.util.concurrent.FastThreadLocal;
23
24 import java.security.MessageDigest;
25 import java.security.NoSuchAlgorithmException;
26
27 /**
28 * A utility class mainly for use by web sockets
29 */
30 final class WebSocketUtil {
31
32 private static final FastThreadLocal<MessageDigest> MD5 = new FastThreadLocal<MessageDigest>() {
33 @Override
34 protected MessageDigest initialValue() throws Exception {
35 try {
36 //Try to get a MessageDigest that uses MD5
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 return MessageDigest.getInstance("SHA1");
51 } catch (NoSuchAlgorithmException e) {
52 //This shouldn't happen! How old is the computer?
53 throw new InternalError("SHA-1 not supported on this platform - Outdated?");
54 }
55 }
56 };
57
58 /**
59 * Performs a MD5 hash on the specified data
60 *
61 * @param data The data to hash
62 * @return The hashed data
63 */
64 static byte[] md5(byte[] data) {
65 // TODO(normanmaurer): Create md5 method that not need MessageDigest.
66 return digest(MD5, data);
67 }
68
69 /**
70 * Performs a SHA-1 hash on the specified data
71 *
72 * @param data The data to hash
73 * @return The hashed data
74 */
75 static byte[] sha1(byte[] data) {
76 // TODO(normanmaurer): Create sha1 method that not need MessageDigest.
77 return digest(SHA1, data);
78 }
79
80 private static byte[] digest(FastThreadLocal<MessageDigest> digestFastThreadLocal, byte[] data) {
81 MessageDigest digest = digestFastThreadLocal.get();
82 digest.reset();
83 return digest.digest(data);
84 }
85
86 /**
87 * Performs base64 encoding on the specified data
88 *
89 * @param data The data to encode
90 * @return An encoded string containing the data
91 */
92 static String base64(byte[] data) {
93 ByteBuf encodedData = Unpooled.wrappedBuffer(data);
94 ByteBuf encoded = Base64.encode(encodedData);
95 String encodedString = encoded.toString(CharsetUtil.UTF_8);
96 encoded.release();
97 return encodedString;
98 }
99
100 /**
101 * Creates an arbitrary number of random bytes
102 *
103 * @param size the number of random bytes to create
104 * @return An array of random bytes
105 */
106 static byte[] randomBytes(int size) {
107 byte[] bytes = new byte[size];
108
109 for (int index = 0; index < size; index++) {
110 bytes[index] = (byte) randomNumber(0, 255);
111 }
112
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 return (int) (Math.random() * maximum + minimum);
125 }
126
127 /**
128 * A private constructor to ensure that instances of this class cannot be made
129 */
130 private WebSocketUtil() {
131 // Unused
132 }
133 }