// Iterate the agents keys, find an acceptable key and authenticate
Map keys = agent.listKeys();
Iterator it = keys.entrySet().iterator();
boolean acceptable = false;
SshPublicKey key = null;
String description;
Map.Entry entry;
while (it.hasNext() && !acceptable) {
entry = (Map.Entry) it.next();
key = (SshPublicKey) entry.getKey();
description = (String) entry.getValue();
acceptable = acceptsKey(authentication, getUsername(),
serviceToStart, key);
log.info("Agent authentication with key " + key.getFingerprint() +
" [" + description + "] is " +
(acceptable ? " acceptable" : " not acceptable"));
if (acceptable) {
ByteArrayWriter baw = new ByteArrayWriter();
log.info("Generating data to sign");
log.info("Preparing public key authentication request");
// Now prepare and send the message
baw.write(1);
baw.writeString(key.getAlgorithmName());
baw.writeBinaryString(key.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(key.getAlgorithmName());
data.writeBinaryString(key.getEncoded());
// Generate the signature
baw.writeBinaryString(agent.hashAndSign(key, data.toByteArray()));
SshMsgUserAuthRequest msg = new SshMsgUserAuthRequest(getUsername(),