private static String sign(String key, String data) throws GuacamoleException {

        try {

            // Attempt to sign UTF-8 bytes of provided data
            Mac mac = Mac.getInstance(SIGNATURE_ALGORITHM);
            mac.init(new SecretKeySpec(key.getBytes("UTF-8"), SIGNATURE_ALGORITHM));

            // Return signature as hex
            return BaseEncoding.base16().lowerCase().encode(mac.doFinal(data.getBytes("UTF-8")));

        }

        // Re-throw any errors which prevent signature
        catch (InvalidKeyException e){
            throw new GuacamoleServerException("Signing key is invalid.", e);
        }

        // Throw hard errors if standard pieces of Java are missing
        catch (UnsupportedEncodingException e) {
            throw new UnsupportedOperationException("Unexpected lack of UTF-8 support.", e);
        }
        catch (NoSuchAlgorithmException e) {
            throw new UnsupportedOperationException("Unexpected lack of support "
                    + "for required signature algorithm "
                    + "\"" + SIGNATURE_ALGORITHM + "\".", e);
        }

    }
private static String sign ( String key , String data ) throws GuacamoleException { try { Mac mac = Mac . getInstance ( SIGNATURE_ALGORITHM ) ; mac . init ( new SecretKeySpec ( key . getBytes ( "UTF-8" ) , SIGNATURE_ALGORITHM ) ) ; return BaseEncoding . base16 ( ) . lowerCase ( ) . encode ( mac . doFinal ( data . getBytes ( "UTF-8" ) ) ) ; } catch ( InvalidKeyException e ) { throw new GuacamoleServerException ( "Signing key is invalid." , e ) ; } catch ( UnsupportedEncodingException e ) { throw new UnsupportedOperationException ( "Unexpected lack of UTF-8 support." , e ) ; } catch ( NoSuchAlgorithmException e ) { throw new UnsupportedOperationException ( "Unexpected lack of support " + "for required signature algorithm " + "\"" + SIGNATURE_ALGORITHM + "\"." , e ) ; } }
Signs the given arbitrary string data with the given key using the
algorithm defined by SIGNATURE_ALGORITHM. Both the data and the key will
be interpreted as UTF-8 bytes.

@param key
The key which should be used to sign the given data.

@param data
The data being signed.

@return
The signature produced by signing the given data with the given key,
encoded as lowercase hexadecimal.

@throws GuacamoleException
If the given signing key is invalid.