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();
InetAddress addr = InetAddress.getLocalHost();
String hostname = addr.getHostName();
log.info("Preparing hostbased authentication request for " + hostname);
// Now prepare and send the message
baw.writeString(pub.getAlgorithmName());
baw.writeBinaryString(pub.getEncoded());
baw.writeString(hostname);
if (clientUser != null) {
baw.writeString(clientUser);
} else {
baw.writeString(getUsername());
}
// 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.writeString(pub.getAlgorithmName());
data.writeBinaryString(pub.getEncoded());
data.writeString(hostname);
if (clientUser != null) {
data.writeString(clientUser);
} else {
data.writeString(getUsername());
}
// Generate the signature
baw.writeBinaryString(key.generateSignature(data.toByteArray()));
SshMsgUserAuthRequest msg = new SshMsgUserAuthRequest(getUsername(),
serviceToStart, getMethodName(), baw.toByteArray());
authentication.sendMessage(msg);
}