2012-01-19 05:12:45 +01:00
|
|
|
/*
|
2012-06-04 22:31:44 +02:00
|
|
|
* Copyright 2012 The Netty Project
|
2012-01-19 05:12:45 +01:00
|
|
|
*
|
|
|
|
* The Netty Project licenses this file to you under the Apache License,
|
|
|
|
* version 2.0 (the "License"); you may not use this file except in compliance
|
|
|
|
* with the License. You may obtain a copy of the License at:
|
|
|
|
*
|
2012-06-04 22:31:44 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2012-01-19 05:12:45 +01:00
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*/
|
|
|
|
package io.netty.handler.codec.http.websocketx;
|
|
|
|
|
2012-06-10 04:08:43 +02:00
|
|
|
import io.netty.buffer.ByteBuf;
|
2012-06-11 10:02:00 +02:00
|
|
|
import io.netty.buffer.Unpooled;
|
2012-01-19 05:12:45 +01:00
|
|
|
import io.netty.handler.codec.base64.Base64;
|
|
|
|
import io.netty.util.CharsetUtil;
|
2015-12-18 11:09:59 +01:00
|
|
|
import io.netty.util.concurrent.FastThreadLocal;
|
|
|
|
|
2012-05-31 21:03:01 +02:00
|
|
|
import java.security.MessageDigest;
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
|
|
2012-01-19 05:12:45 +01:00
|
|
|
/**
|
2012-06-29 07:12:59 +02:00
|
|
|
* A utility class mainly for use by web sockets
|
2012-01-19 05:12:45 +01:00
|
|
|
*/
|
|
|
|
final class WebSocketUtil {
|
|
|
|
|
2015-12-18 11:09:59 +01:00
|
|
|
private static final FastThreadLocal<MessageDigest> MD5 = new FastThreadLocal<MessageDigest>() {
|
|
|
|
@Override
|
|
|
|
protected MessageDigest initialValue() throws Exception {
|
|
|
|
try {
|
|
|
|
//Try to get a MessageDigest that uses MD5
|
|
|
|
return MessageDigest.getInstance("MD5");
|
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
//This shouldn't happen! How old is the computer?
|
|
|
|
throw new InternalError("MD5 not supported on this platform - Outdated?");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
private static final FastThreadLocal<MessageDigest> SHA1 = new FastThreadLocal<MessageDigest>() {
|
|
|
|
@Override
|
|
|
|
protected MessageDigest initialValue() throws Exception {
|
|
|
|
try {
|
|
|
|
//Try to get a MessageDigest that uses SHA1
|
|
|
|
return MessageDigest.getInstance("SHA1");
|
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
//This shouldn't happen! How old is the computer?
|
|
|
|
throw new InternalError("SHA-1 not supported on this platform - Outdated?");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-01-19 05:12:45 +01:00
|
|
|
/**
|
2012-06-29 07:12:59 +02:00
|
|
|
* Performs a MD5 hash on the specified data
|
2012-01-19 05:12:45 +01:00
|
|
|
*
|
2012-06-29 07:12:59 +02:00
|
|
|
* @param data The data to hash
|
|
|
|
* @return The hashed data
|
2012-01-19 05:12:45 +01:00
|
|
|
*/
|
2012-06-29 07:12:59 +02:00
|
|
|
static byte[] md5(byte[] data) {
|
2015-12-18 11:09:59 +01:00
|
|
|
// TODO(normanmaurer): Create md5 method that not need MessageDigest.
|
2016-01-04 14:29:21 +01:00
|
|
|
return digest(MD5, data);
|
2012-01-19 05:12:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-06-29 07:12:59 +02:00
|
|
|
* Performs a SHA-1 hash on the specified data
|
2012-01-19 05:12:45 +01:00
|
|
|
*
|
2012-06-29 07:12:59 +02:00
|
|
|
* @param data The data to hash
|
|
|
|
* @return The hashed data
|
2012-01-19 05:12:45 +01:00
|
|
|
*/
|
2012-06-29 07:12:59 +02:00
|
|
|
static byte[] sha1(byte[] data) {
|
2015-12-18 11:09:59 +01:00
|
|
|
// TODO(normanmaurer): Create sha1 method that not need MessageDigest.
|
2016-01-04 14:29:21 +01:00
|
|
|
return digest(SHA1, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static byte[] digest(FastThreadLocal<MessageDigest> digestFastThreadLocal, byte[] data) {
|
|
|
|
MessageDigest digest = digestFastThreadLocal.get();
|
|
|
|
digest.reset();
|
|
|
|
return digest.digest(data);
|
2012-01-19 05:12:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-06-29 07:12:59 +02:00
|
|
|
* Performs base64 encoding on the specified data
|
2012-01-19 05:12:45 +01:00
|
|
|
*
|
2012-06-29 07:12:59 +02:00
|
|
|
* @param data The data to encode
|
|
|
|
* @return An encoded string containing the data
|
2012-01-19 05:12:45 +01:00
|
|
|
*/
|
2012-06-29 07:12:59 +02:00
|
|
|
static String base64(byte[] data) {
|
|
|
|
ByteBuf encodedData = Unpooled.wrappedBuffer(data);
|
2013-07-15 19:12:24 +02:00
|
|
|
ByteBuf encoded = Base64.encode(encodedData);
|
|
|
|
String encodedString = encoded.toString(CharsetUtil.UTF_8);
|
|
|
|
encoded.release();
|
|
|
|
return encodedString;
|
2012-01-19 05:12:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-06-29 07:12:59 +02:00
|
|
|
* Creates an arbitrary number of random bytes
|
2012-01-19 05:12:45 +01:00
|
|
|
*
|
2012-06-29 07:12:59 +02:00
|
|
|
* @param size the number of random bytes to create
|
|
|
|
* @return An array of random bytes
|
2012-01-19 05:12:45 +01:00
|
|
|
*/
|
|
|
|
static byte[] randomBytes(int size) {
|
|
|
|
byte[] bytes = new byte[size];
|
|
|
|
|
2012-06-29 07:12:59 +02:00
|
|
|
for (int index = 0; index < size; index++) {
|
|
|
|
bytes[index] = (byte) randomNumber(0, 255);
|
2012-01-19 05:12:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-06-29 07:12:59 +02:00
|
|
|
* Generates a pseudo-random number
|
2012-01-19 05:12:45 +01:00
|
|
|
*
|
2012-06-29 07:12:59 +02:00
|
|
|
* @param minimum The minimum allowable value
|
|
|
|
* @param maximum The maximum allowable value
|
|
|
|
* @return A pseudo-random number
|
2012-01-19 05:12:45 +01:00
|
|
|
*/
|
2012-06-29 07:12:59 +02:00
|
|
|
static int randomNumber(int minimum, int maximum) {
|
|
|
|
return (int) (Math.random() * maximum + minimum);
|
2012-01-19 05:12:45 +01:00
|
|
|
}
|
|
|
|
|
2012-06-29 07:12:59 +02:00
|
|
|
/**
|
|
|
|
* A private constructor to ensure that instances of this class cannot be made
|
|
|
|
*/
|
2012-01-19 05:12:45 +01:00
|
|
|
private WebSocketUtil() {
|
|
|
|
// Unused
|
|
|
|
}
|
|
|
|
}
|