org.eclipse.jetty.websocket.common.AcceptHash Maven / Gradle / Ivy
//
// ========================================================================
// Copyright (c) 1995-2012 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.websocket.common;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import org.eclipse.jetty.util.B64Code;
import org.eclipse.jetty.util.StringUtil;
/**
* Logic for working with the Sec-WebSocket-Key
and Sec-WebSocket-Accept
headers.
*
* This is kept separate from Connection objects to facilitate difference in behavior between client and server, as well as making testing easier.
*/
public class AcceptHash
{
/**
* Globally Unique Identifier for use in WebSocket handshake within Sec-WebSocket-Accept
and Sec-WebSocket-Key
http headers.
*
* See Opening Handshake (Section 1.3)
*/
private final static byte[] MAGIC;
static
{
try
{
MAGIC = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11".getBytes(StringUtil.__ISO_8859_1);
}
catch (UnsupportedEncodingException e)
{
throw new RuntimeException(e);
}
}
/**
* Concatenate the provided key with the Magic GUID and return the Base64 encoded form.
*
* @param key
* the key to hash
* @return the Sec-WebSocket-Accept
header response (per opening handshake spec)
*/
public static String hashKey(String key)
{
try
{
MessageDigest md = MessageDigest.getInstance("SHA1");
md.update(key.getBytes("UTF-8"));
md.update(MAGIC);
return new String(B64Code.encode(md.digest()));
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}
}