if ((getUsername() == null) || (key == null)) {
throw new AuthenticationProtocolException(
"You must supply a username and a key");
}
ByteArrayWriter baw = new ByteArrayWriter();
log.info("Generating data to sign");
SshPublicKey pub = key.getPublicKey();
log.info("Preparing public key authentication request");
// Now prepare and send the message
baw.write(1);
baw.writeString(pub.getAlgorithmName());
baw.writeBinaryString(pub.getEncoded());
// Create the signature data
ByteArrayWriter data = new ByteArrayWriter();
data.writeBinaryString(authentication.getSessionIdentifier());
data.write(SshMsgUserAuthRequest.SSH_MSG_USERAUTH_REQUEST);
data.writeString(getUsername());
data.writeString(serviceToStart);
data.writeString(getMethodName());
data.write(1);
data.writeString(pub.getAlgorithmName());
data.writeBinaryString(pub.getEncoded());
// Generate the signature
baw.writeBinaryString(key.generateSignature(data.toByteArray()));
SshMsgUserAuthRequest msg = new SshMsgUserAuthRequest(getUsername(),
serviceToStart, getMethodName(), baw.toByteArray());
authentication.sendMessage(msg);
}