org.eclipse.jetty.websocket.common.AcceptHash Maven / Gradle / Ivy
//
// ========================================================================
// Copyright (c) 1995-2022 Mort Bay Consulting Pty Ltd and others.
// ------------------------------------------------------------------------
// 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.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.Base64;
/**
* 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 static final byte[] MAGIC = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11".getBytes(StandardCharsets.ISO_8859_1);
/**
* 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(StandardCharsets.UTF_8));
md.update(MAGIC);
return Base64.getEncoder().encodeToString(md.digest());
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}
}