API login and MD5

it looks like basic and i do not know much about that, so i will give you example how i did it in java all what i need to login

mainly check login() method and other what i posted here is what i used in that login method

here is the thread:
http://forum.mikrotik.com/t/api-routerosv3/11568/40

so here it goes

this method is used to send last string of the command to the RouterOS, of course, it can be implemented some more elegant way, this is used to finish a command but:

    private void writeln(String s){
        try {
            out.writeByte( new Integer(s.length()).byteValue());
            out.writeBytes( s + "\0");
            System.out.println(s);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }


same as above, just sends one line of the command



    private void write(String s){
        try {
            Integer t = new Integer(s.length());
            out.writeByte(t.byteValue());
            out.writeBytes(s);
            System.out.println(s);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

this reads stuff that is returned by routeros API

    private String[] read(){
        String s="";
        boolean haveSymbols = true;
        try {
            char ch;
            while (true){
                while (haveSymbols){
                    byte b = in.readByte();
                    if (b!=0x00){
                        s+="\n";
                        
                        for (int count=0;count<b;count++){
                            ch = (char)(in.readByte() & 0xFF);
                            s+=ch;
                        }
                    }else haveSymbols=false;
                    
                }
                if (s.contains("!done")) return  s.split("\n");
                
                haveSymbols = true;
                
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return null;
    }

this is how to make connection using java

    public boolean connect(String ipAddress, int ipPort){
        boolean ret = false;
        try {
            socket = new Socket(ipAddress,ipPort);
            in = new DataInputStream(socket.getInputStream());
            out = new DataOutputStream(socket.getOutputStream());
            ret = true;
        } catch (UnknownHostException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return ret;
    }

this is how i managed the login

    
    public boolean login(String login, String password){
        String[] readIn;
        String transition ="";
        boolean ret = false;
        writeln("/login");
        readIn = read();
        for (int i=0; i<readIn.length;i++){
            System.out.println(readIn[i]);
        }
        String[] tmp = readIn[2].split("=ret=");
        transition = tmp[tmp.length-1];
        String chal="";
        chal=MessageDigestHelper.myHexStrToStr("00") + password + MessageDigestHelper.myHexStrToStr(transition);
        chal = MessageDigestHelper.myMD5Helper(chal);
        write("/login");
        write("=name="+login);
        writeln("=response=00"+chal);
        readIn = read();
        for (int i=0; i<readIn.length;i++){
            System.out.println(readIn[i]);
        }
        if (readIn[readIn.length-1].contains("!done")){
            if (readIn[1].contains("!trap")){
                return false;
            } else
                return true;
        }
        return ret;
    }

and now missing peaces that i used in login method (hope they are self explanatory:

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 *
 * @author janisk
 */
public class MessageDigestHelper {
    
    /** Creates a new instance of MessageDigestHelper */
    public MessageDigestHelper() {
    }
    static public String myMD5Helper(String s){
        String md5val = "";
        MessageDigest algorithm = null;
        try {
            algorithm = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException nsae) {
            System.out.println("Cannot find digest algorithm");
            System.exit(1);
        }
        byte[] defaultBytes = new byte[s.length()];
        for (int i=0;i<s.length();i++){
            defaultBytes[i] = (byte)(0xFF & s.charAt(i));
        }
        algorithm.reset();
        algorithm.update(defaultBytes);
        byte messageDigest[] = algorithm.digest();
        StringBuffer hexString = new StringBuffer();
        
        for (int i = 0; i < messageDigest.length; i++) {
            String hex = Integer.toHexString(0xFF & messageDigest[i]);
            if (hex.length() == 1) {
                hexString.append('0');
            }
            hexString.append(hex);
        }
        return hexString.toString();
    }
    static public String myHexStrToStr(String s){
        String ret ="";
        for (int i = 0; i<s.length();i+=2){
            ret += (char)Integer.parseInt(s.substring(i,i+2),16);
        }
        return ret;
    }
}