📄 handshakepacket.java
字号:
/* * Movino J2ME Client * Copyright (C) 2007 Johannes Berg * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */package org.movino.connection;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.IOException;import java.io.UnsupportedEncodingException;import org.movino.md5.MD5;public class HandShakePacket extends MovinoPacket{ public static final int MOVINO_HANDSHAKE_NO_USERNAME = 1; public static final int MOVINO_HANDSHAKE_REQUIRE_USERNAME = 2; public static final int MOVINO_HANDSHAKE_NO_PASSWORD = 1; public static final int MOVINO_HANDSHAKE_MD5_CHALLENGE = 2; private String userName; private String userPassword; private byte[] randomData; public HandShakePacket(String user_name){ super(MovinoPacket.MOVINO_DATA_HANDSHAKE); userName = user_name; userPassword = null; randomData = null; } public HandShakePacket(String user_name, String user_password){ super(MovinoPacket.MOVINO_DATA_HANDSHAKE); userName = user_name; userPassword = user_password; randomData = null; } public HandShakePacket(DataInputStream in, int length) throws IOException{ super(MovinoPacket.MOVINO_DATA_HANDSHAKE); int need_username = in.readByte(); if(need_username==MOVINO_HANDSHAKE_REQUIRE_USERNAME) userName=""; else userName=null; int need_password = in.readByte(); if(need_password==MOVINO_HANDSHAKE_MD5_CHALLENGE) userPassword=""; else userPassword=null; length = length-2; randomData = new byte[length]; int offset=0; while(length>0){ int count = in.read(randomData, offset, length); offset+=count; length-=count; } } public void writePacketData(DataOutputStream out) throws IOException{ if(userName!=null) out.writeByte(MOVINO_HANDSHAKE_REQUIRE_USERNAME); else out.writeByte(MOVINO_HANDSHAKE_NO_USERNAME); if(userPassword!=null) out.writeByte(MOVINO_HANDSHAKE_MD5_CHALLENGE); else out.writeByte(MOVINO_HANDSHAKE_NO_PASSWORD); if(userName!=null) out.writeUTF(userName); else if(userPassword!=null){ out.writeUTF(""); } if(userPassword!=null){ out.writeShort(16); out.write(getPasswordHash()); } } /*public int getPacketLength(){ int len=2; if(userName!=null){ len += 2+userName.length(); } else if(userPassword!=null){ len += 2; } if(userPassword!=null){ len += 2+16; } return len; }*/ public String getUserName(){ return userName; } public String getPassword(){ return userPassword; } public byte[] getPasswordHash(){ byte[] p = new byte[0]; try { p = userPassword.getBytes("UTF-8"); } catch (UnsupportedEncodingException e) {} byte[] p_hash = MD5.toHash(p); MD5 md5 = new MD5(); md5.Update(p_hash); md5.Update(randomData); return md5.Final(); } public byte[] getRandomData(){ return randomData; } public void setRandomData(byte[] rand_data){ randomData=rand_data; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -