📄 authentication.java
字号:
public final int getAuthManner() { return _manner; } // /** // * Returns input stream of socket to be connected/bound // * @return input stream of socket to be connected/bound // */ // public final InputStream getInputStream() { // return _inputStream; // } // /** * Returns data input of packet * @return data input of packet */ public final DataInput getDataInput() { return _dataInput; } /** * Returns local IP address of socket to be connected/bound * @return local IP address of socket to be connected/bound */ public final InetAddress getLocalAddress() { return _localAddr; } /** * Returns output stream of socket to be connected/bound * @return output stream of socket to be connected/bound */ public final OutputStream getOutputStream() { return _outputStream; } /** * Returns remote IP address of socket to be connected/bound * @return remote IP address of socket to be connected/bound */ public final InetAddress getRemoteAddress() { return _remoteAddr; } /** * Returns selected security domain name. * @return selected security domain name */ public final String getSelectedDomainName() { return _selectedDomainname; } /** * Returns shared secret for selected security domain. * @return shared secret for selected security domain */ public final SharedSecret getSelectedSecret() { return _selectedSecret; } /** * Returns socket to be connected/bound. * @return socket to be connected/bound */ public final Socket getSocket() { return _socket; } /** * Returns turn of protocol. * @return turn of protocol */ public final int getTurn() { return _turn; } public boolean isAuthenticatedMyself() { return _authenticatedMyself; } public boolean isAuthenticatedOpponent() { return _authenticatedOpponent; } /** * Sets authentication manner. * @param manner authentication manner * @exception java.lang.IllegalArgumentException */ private final void setAuthManner(int manner) throws IllegalArgumentException { if (manner == AtpConstants.AUTHENTICATION_MANNER_DIGEST || manner == AtpConstants.AUTHENTICATION_MANNER_SIGNATURE) { _manner = manner; } else { _manner = AtpConstants.NO_AUTHENTICATION_MANNER; throw new IllegalArgumentException("Illegal manner : " + manner); } } /** * Sets data input of packet. * @param di data input of packet */ private final void setDataInput(DataInput di) { _dataInput = di; } /** * Sets socket to be connected/bound. * @param socket socket to be connected/bound */ private final void setSocket(Socket socket) { _socket = socket; _localAddr = _socket.getLocalAddress(); _remoteAddr = _socket.getInetAddress(); _serverIdentifier = new ServerIdentifier(_socket); try { // _inputStream = _socket.getInputStream(); _outputStream = _socket.getOutputStream(); } catch (IOException excpt) { // _inputStream = null; _outputStream = null; } } /** * Sets status */ private final void setStatus(int status) throws IllegalArgumentException { switch (status) { case STATUS_NORMAL: case STATUS_AUTHENTICATION_FAILED: case STATUS_ERROR: _status = status; break; default: throw new IllegalArgumentException("Illegal status : " + status); } } // - // - /** // - * Gets file name of private key for challenge-response authentication with digital signature. // - * @return file name of private key for challenge-response authentication with digital signature // - */ // - public static String getPrivateKeyFilename() { // - final String securityDir = FileUtils.getSecurityDirectory(); // - final String default_file = securityDir + File.separator + "private.key"; // - Resource res = Resource.getResourceFor("atp"); // - if(res==null) { // - return default_file; // - } // - return res.getString("atp.auth.privatekey", default_file); // - } // - // - /** // - * Gets private key for challenge-response authentication with digital signature by loading private key file. // - * If the file is already loaded, return it. // - * If the private key file does not exist, return null. // - * @return private key for challenge-response authentication with digital signature. // - */ // - public synchronized static PrivateKey getPrivateKey() { // - if(_privateKey!=null) { // - return _privateKey; // - } // - // - loadPrivateKey(); // - // - return _privateKey; // - } // - // - /** // - * Loads private key for challenge-response authentication with digital signature. // - * If the private key file does not exist or something wrong, sets null. // - */ // - public synchronized static void loadPrivateKey() { // - FileInputStream fstprivate = null; // - try { // - final String file = getPrivateKeyFilename(); // - System.out.print("[Reading from private key file "+file+" ... "); // - fstprivate = new FileInputStream(file); // - } // - catch(FileNotFoundException excpt) { // - // private key file does not exist // - System.out.println("no private key file.]"); // - System.err.println(excpt); // - _privateKey = null; // - return; // - } // - // - try { // - ObjectInputStream istprivate = new ObjectInputStream(fstprivate); // - _privateKey = (PrivateKey)istprivate.readObject(); // - fstprivate.close(); // - } // - catch(ClassNotFoundException excpt) { // - // something wrong // - System.out.println("file seems to be broken.]"); // - System.err.println(excpt); // - _privateKey = null; // - return; // - } // - catch(IOException excpt) { // - // something wrong // - System.out.println("file has something wrong.]"); // - System.err.println(excpt); // - _privateKey = null; // - return; // - } // - // - System.out.println("done.]"); // - } // - // - /** // - * Gets file name of public key for challenge-response authentication with digital signature. // - * @return file name of public key for challenge-response authentication with digital signature // - */ // - public static String getPublicKeyFilename() { // - final String securityDir = FileUtils.getSecurityDirectory(); // - final String default_file = securityDir + File.separator + "public.key"; // - Resource res = Resource.getResourceFor("atp"); // - if(res==null) { // - return default_file; // - } // - return res.getString("atp.auth.publickey", default_file); // - } // - // - /** // - * Gets public key for challenge-response authentication with digital signature by loading public key file. // - * If the file is already loaded, return it. // - * If the public key file does not exist, return null. // - * @return public key for challenge-response authentication with digital signature. // - */ // - public synchronized static PublicKey getPublicKey() { // - if(_publicKey!=null) { // - return _publicKey; // - } // - // - loadPublicKey(); // - // - return _publicKey; // - } // - // - /** // - * Loads public key for challenge-response authentication with digital signature. // - * If the public key file does not exist or something wrong, sets null. // - */ // - public synchronized static void loadPublicKey() { // - FileInputStream fstpublic = null; // - try { // - final String file = getPublicKeyFilename(); // - System.out.print("[Reading from public key file "+file+" ... "); // - fstpublic = new FileInputStream(file); // - } // - catch(FileNotFoundException excpt) { // - // public key file does not exist // - System.out.println("no public key file.]"); // - System.err.println(excpt); // - _publicKey = null; // - return; // - } // - // - try { // - ObjectInputStream istpublic = new ObjectInputStream(fstpublic); // - _publicKey = (PublicKey)istpublic.readObject(); // - fstpublic.close(); // - } // - catch(ClassNotFoundException excpt) { // - // something wrong // - System.out.println("file seems to be broken.]"); // - System.err.println(excpt); // - _publicKey = null; // - return; // - } // - catch(IOException excpt) { // - // something wrong // - System.out.println("file has something wrong.]"); // - System.err.println(excpt); // - _publicKey = null; // - return; // - } // - // - System.out.println("done.]"); // - } // - // - /** // - * Creates private/public key pair for challenge-response authentication // - * with digital signature and // - * write it into the private/public key file. // - * @return public key for challenge-response authentication with digital signature // - */ // - public synchronized static PublicKey createKeyPair() { // - if(_keyPairGen==null) { // - try { // - /* Get a Key Pair Generator */ // - _keyPairGen = KeyPairGenerator.getInstance(KEYPAIRGENERATORALGORITHM); // - /* Initialize Key Pair Generator with randomize seed */ // - _keyPairGen.initialize(KEYSTRENGTH, Randoms.getRandomGenerator(SEEDLENGTH)); // - } // - catch(NoSuchAlgorithmException excpt) { // - System.err.println(excpt); // - _privateKey = null; // - _publicKey = null; // - return null; // - } // - } // - // - /* Generate a Key Pair */ // - KeyPair pair = _keyPairGen.generateKeyPair(); // - _privateKey = pair.getPrivate(); // - _publicKey = pair.getPublic(); // - // - writePrivateKey(_privateKey); // - writePublicKey(_publicKey); // - // - return _publicKey; // - } // - // - /** // - * Writes private key for challenge-response authentication // - * with digital signature into a file. // - * @param privateKey private key // - */ // - public synchronized static void writePrivateKey(PrivateKey privateKey) { // - FileOutputStream fstprivate = null; // - ObjectOutputStream ostprivate = null; // - try { // - final String file = getPrivateKeyFilename(); // - fstprivate = new FileOutputStream(file); // - ostprivate = new ObjectOutputStream(fstprivate); // - ostprivate.writeObject(privateKey); // - ostprivate.flush(); // - fstprivate.close(); // - } // - catch(IOException excpt) { // - // something wrong // - } // - } // - // - /** // - * Writes public key for challenge-response authentication // - * with digital signature into a file. // - * @param publicKey public key // - */ // - public synchronized static void writePublicKey(PublicKey publicKey) { // - FileOutputStream fstpublic = null; // - ObjectOutputStream ostpublic = null; // - try { // - final String file = getPublicKeyFilename(); // - fstpublic = new FileOutputStream(file); // - ostpublic = new ObjectOutputStream(fstpublic); // - ostpublic.writeObject(publicKey); // - ostpublic.flush(); // - fstpublic.close(); // - } // - catch(IOException excpt) { // - // something wrong // - } // - } // - // - /** // - * Sets public key of opponent for challenge-response authentication // - * with digital signature. // - */ // - public static void setPublicKeyOpponent(PublicKey publicKey) { // - _publicKeyOpponent = publicKey; // - } /** * Sets turn of protocol. * @param turn turn of protocol * @exception java.lang.IllegalArgumentException */ private final void setTurn(int turn) throws IllegalArgumentException { if (turn == Auth.FIRST_TURN || turn == Auth.SECOND_TURN) { _turn = turn; } else { _turn = Auth.NO_TURNS; throw new IllegalArgumentException("Illegal turn : " + turn); } } private static final void verboseOut(String msg) { AuthPacket.verboseOut(msg); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -