Tuesday, February 5, 2013

Java: Base64 Encode

I used this function to encode a string using base64 encoding for my java card applet.

public static String base64Encode(String s) {
    String table="ABCDEFGHIJKLMNOPQRSTUVWXYZ";

            + "abcdefghijklmnopqrstuvwxyz0123456789+/";
    String b="";
    String r="";
    int pad;
       
    for (int c : s.toCharArray()) {
        b+=rjust(Integer.toBinaryString(c), 8, "0");
    }
           
    for (int i=0; i<b.length(); i+=6) {
        r+=table.charAt(Integer.parseInt(ljust(b.substring(i, i+6), 6, "0"), 2));
        pad=6-b.substring(i, i+6).length();
        switch (pad) {
            case 0: r+=""; break;
            case 4: r+="=="; break;
            case 2: r+="="; break; }                  
    }
    return r;

}

Both rjust and ljust that you can find in the function above are custom functions similar to python's rjust and ljust.

Monday, January 28, 2013

SQL Server notes

Rename a field (or column) in a table:
EXEC sp_rename 'table_name.old_name', 'new_name', 'COLUMN';

sp_rename is a SQL Server function name.

Friday, December 21, 2012

Fedora 17: Open port for TCP connection

To open a port for TCP connection you have to edit /etc/sysconfig/iptables and append the following line before the COMMIT line

-A INPUT -m state --state NEW -m tcp -p tcp --dport <PORT> -j ACCEPT

And then restart the iptables service by executing

service iptables restart

*EDIT*
I created this post before knowing that you could actually use iptables directly as below :P :

# iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport <PORT> -j ACCEPT

You should list your iptables rules first to check whether there's any DROP rule. If your new rule comes after a rule that DROPs any connection on you target port, the port you're trying to open will stay closed. If there's a DROP rule, use -I (insert) instead:

# iptables -I INPUT <rulenum> -m state --state NEW -m tcp -p tcp --dport <PORT> -j ACCEPT

where <rulenum> is the rule index in the chain. and <PORT> is your target port.

Friday, December 14, 2012

Python: Fun with sockets (remote desktop)

Server
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

#!/usr/bin/python

import os
import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(5)

client, addr = s.accept()

cmd = s.recv(1024)
try:
    os.system(cmd)
    client.sendall("OK")
except:
    client.sendall("FAIL")
client.close()

Client
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/usr/bin/python

import socket
import sys

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
s.sendall(sys.argv[1])

data = s.recv(32)

print data
s.close()
print "Received: '%s'"%data

Wednesday, November 28, 2012

Web programming with python and apache: Webserver index page (index.py)

To use python in a web server instead of php, you need to enable cgi-script in your httpd.conf using
AddHandler cgi-script .cgi .py
and you use
ScriptAlias /cgi-bin/ "/your/own/dir/for/cgi-bin/"
to tell apache the script the location of the scripts to be executed.

But if you want the script to handle the webserver index page as index.html, index.php does, follow the following steps:
  1. Edit your root directory settings to execute cgi scripts and handle .py files by writing:
    <Directory /your/root/dir>
      Options +ExecCGI
      AddHandler cgi-script .cgi .py
    </Directory>
  2.  Then set your DirectoryIndex as index.py as so:
    DirectoryIndex index.py

 

index.py "Hello, World!"

#!/usr/bin/python

import cgitb
cgitb.enable()

print "Content-Type: text/html;charset=utf-8\r\n" #tells the respond type to the client
print """<html><head><title>FooBarBaz</title></head>
<body>Hello world!</body>
</html>
"""

Saturday, September 29, 2012

Gtk+ tips: Set value of properties in C

In C, if you want to set the value of certain property of a Gtk+ object, you can do it like so
gtk_<type>_set_<property> (GTK_<TYPE> (var_name), <value>);
For example, to set the title of a GtkWindow to "Hello World", do would write
gtk_window_set_title (GTK_WINDOW (window), "Hello World");

This is fine, but what if you want to set the value of a bunch of properties, say for GtkWindow; "app-paintable", "default-width", "default-height", "resizable", and "modal", most beginner would write:

GtkWidget *window = gtk_window_new ();
gtk_widget_set_app_paintable (window), TRUE);
gtk_window_set_default_size (GTK_WINDOW (window), 100,100);
gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
gtk_window_set_modal (GTK_WINDOW (window), TRUE);

Too many characters to type (229 characters excluding spaces and newlines).

I prefer the GObject way  to do this task. The following is the code which does the same thing as the code above, but by using g_object_set:

GtkWidget *window = gtk_window_new ();
g_object_set (G_OBJECT (window),
        "app-paintable", TRUE,
        "default-width", 100, "default-height", 100,
        "resizable", FALSE,
        "modal", TRUE,
        NULL); //the arguments must end with NULL

You have to type much less characters, only 130 characters.  You can also remove the G_OBJECT macro from the code above. Moreover, your code would look more beautiful (in my eyes at least). :)

Friday, September 14, 2012

Fedora 17: Slow recreate volatile files and directories during boot

One of the reason for this is that there's a broken entry in /etc/fstab. Removing the entry would solve the problem and booting would be faster.