public AuthenticatedSocket(InetAddress ia, int port, MessageDigest md, byte[] secret) throws IOException, AuthenticationException { super(ia, port); try { OutputStream output = this.getOutputStream(); InputStream input = this.getInputStream(); // Get challenge length byte[] challengeSize = new byte[4]; input.read(challengeSize); // Receive random challenge string byte[] challenge = new byte[Bytes.toInt(challengeSize)]; input.read(challenge); // Generate MD5 hash byte[] append = Bytes.append(challenge, secret); byte[] hash = md.digest(append); // Send time and hash strings output.write(hash); } catch (Exception e) { throw new AuthenticationException("Authentication failed: " + e.getMessage()); } }