Commit f305130a authored by O'Reilly Media, Inc.'s avatar O'Reilly Media, Inc.
Browse files

Initial commit

parents
Loading
Loading
Loading
Loading

9781565924024

0 → 120000
+1 −0
Changes for 9781565924024: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
9781565924024
 No newline at end of file
+1.43 KiB

File added.

No diff preview for this file type.

+41 −0
Changes for Authentication/DoubleClient.java: 41 added lines, 0 removed lines.
Original line number Diff line number Diff line
import java.io.*;
import java.net.*;
import java.security.*;
import java.util.Date;

import Protection;

public class DoubleClient {
  public void sendAuthentication(String user, String password,
      OutputStream outStream) throws IOException, NoSuchAlgorithmException {
    DataOutputStream out = new DataOutputStream(outStream);
    long t1 = (new Date()).getTime();
    double q1 = Math.random();
    byte[] protected1 = Protection.makeDigest(user, password, t1, q1);
    long t2 = (new Date()).getTime();
    double q2 = Math.random();
    byte[] protected2 = Protection.makeDigest(protected1, t2, q2);

    out.writeUTF(user);
    out.writeLong(t1);
    out.writeDouble(q1);
    out.writeLong(t2);
    out.writeDouble(q2);
    out.writeInt(protected2.length);
    out.write(protected2);
    out.flush();
  }

  public static void main(String[] args) throws Exception {
    String host = args[0];
    int port = 7999;
    String user = "Jonathan";
    String password = "buendia";
    Socket s = new Socket(host, port);

    DoubleClient client = new DoubleClient();
    client.sendAuthentication(user, password, s.getOutputStream());

    s.close();
  }
}
 No newline at end of file
+1.6 KiB

File added.

No diff preview for this file type.

+42 −0
Changes for Authentication/DoubleServer.java: 42 added lines, 0 removed lines.
Original line number Diff line number Diff line
import java.io.*;
import java.net.*;
import java.security.*;

import Protection;

public class DoubleServer {
  public boolean authenticate(InputStream inStream)
      throws IOException, NoSuchAlgorithmException {
    DataInputStream in = new DataInputStream(inStream);

    String user = in.readUTF();
    long t1 = in.readLong();
    double q1 = in.readDouble();
    long t2 = in.readLong();
    double q2 = in.readDouble();
    int length = in.readInt();
    byte[] protected2 = new byte[length];
    in.readFully(protected2);

    String password = lookupPassword(user);
    byte[] local1 = Protection.makeDigest(user, password, t1, q1);
    byte[] local2 = Protection.makeDigest(local1, t2, q2);
    return MessageDigest.isEqual(protected2, local2);
  }

  protected String lookupPassword(String user) { return "buendia"; }

  public static void main(String[] args) throws Exception {
    int port = 7999;
    ServerSocket s = new ServerSocket(port);
    Socket client = s.accept();

    DoubleServer server = new DoubleServer();
    if (server.authenticate(client.getInputStream()))
      System.out.println("Client logged in.");
    else
      System.out.println("Client failed to log in.");

    s.close();
  }
}
 No newline at end of file
Loading