niedziela, 29 maja 2011

Ubuntu: reduce memory consumption - HP ZE5501US with 512 MB

Running Ubuntu 11.04 on my old laptop (although equipped with Pentium 4 and 512 MB of RAM) becomes difficult since the OS is terribly slow. After some analysis it came out that the reason is the 512 MB of RAM (64 MB consumed by the graphic card) which is far too less for the default configuration. Fortunately there is an easy solution to free ~100 MB of additional RAM by disabling the startup applications for gnome environment. You can do it:
  1. Ubuntu Classic: System->Preferences->Startup Applications
  2. Unity: you can just type 'startup' in the search field
In my case I disabled most of the apps (apart from power manager and few other ones). Below you can see how the window looks like:

Ubuntu: remove service from automatic startup

I have recently installed a BOINC client on my Ubuntu box. However later on I decided to turn it off but all I could do is to turn off the service and not disable it at the automatic startup.

After some search on the web I found this link - which is in my opinion very helpful: http://www.unixtutorial.org/2009/01/disable-service-startup-in-ubuntu/

Example from my box:

root@ubuntu:~# update-rc.d -f boinc-client remove
 Removing any system startup links for /etc/init.d/boinc-client ...
   /etc/rc0.d/K20boinc-client
   /etc/rc1.d/K20boinc-client
   /etc/rc2.d/S20boinc-client
   /etc/rc3.d/S20boinc-client
   /etc/rc4.d/S20boinc-client
   /etc/rc5.d/S20boinc-client
   /etc/rc6.d/K20boinc-client

sobota, 28 maja 2011

Ruby: Logging to STDOUT and file using standard ruby Logger

Have you ever wanted to log the message from your application to both standard output and file? If you have and you are using in your application the Logger that comes together with Ruby this article will be helpful for you.

First of all the description of the Logger can be found in the rdoc (http://www.ruby-doc.org/stdlib/libdoc/logger/rdoc/). Some of the features are listed below:
  • logging on multiple levels: FATAL, ERROR, WARNING, INFO, DEBUG
  • logging to a user defined device
  • custom formatting
But it does not provide functionalities similar to log4j like appenders, categories etc. (if you would like to use these the logging gem will be helpful: http://logging.rubyforge.org/).

The standard ruby Logger unfortunately does not provide functionality to log to multiple devices out of the box. In my opinion the simplest approach is to define an extension to the Logger like the one below (actually this can be used by you 1:1).

require 'logger'
class EnhancedLogger < Logger

  def initialize(logdev, shift_age = 10, shift_size = 1048576)
    super(logdev, shift_age, shift_size)
    @stdout = nil
    if (logdev == STDOUT)
      @stdout = STDOUT
    end
  end

  def logStd(severity, message = nil, progname = nil, &block)
    msg = format_message(format_severity(severity), Time.now, progname, message)
    self<<(msg)
    if (@stdout != nil)
      @stdout.puts(msg)
    end
  end
end


The EnhancedLogger class extends the standard Logger with an additional function called logStd, which in addition to the standard log device defined in the constructor displays the message also on the STDOUT. One can also think about similar function for STDERR (if required of course).

środa, 25 maja 2011

Encryption/decryption of data in Java

Recently I came to the problem that the one of my colleagues utilities had to store some sensitive data (password) in it's configuration file (property file). Storing it it plain text would be a serious security flaw and therefore we had to find a way to store the password in an encrypted form using some key. Fortunately it is not that difficult in java - the apache common codec library becomes useful here. It provides much more functionality but what we used is the base64 encoding/decoding.

Below I enclose the example logic for encrypting the password:
    public byte[] encryptPassword(String pass, String key) throws Exception {
        DESKeySpec keySpec = new DESKeySpec(key.getBytes("UTF8"));
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey secKey = keyFactory.generateSecret(keySpec);
        byte[] cleartext = pass.getBytes("UTF8");     

        Cipher cipher = Cipher.getInstance("DES"); // cipher is not thread safe
        cipher.init(Cipher.ENCRYPT_MODE, secKey);
        return Base64.encodeBase64(cipher.doFinal(cleartext));
    }


And to decrypt the password one can use the following method:
    public byte[] decryptPassword(String encryptedPass, String key) throws Exception {
        DESKeySpec keySpec = new DESKeySpec(key.getBytes("UTF8"));
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey secKey = keyFactory.generateSecret(keySpec);

        byte[] encrypedPwdBytes = Base64.decodeBase64(encryptedPass);

        Cipher cipher = Cipher.getInstance("DES");// cipher is not thread safe
        cipher.init(Cipher.DECRYPT_MODE, secKey);
        return (cipher.doFinal(encrypedPwdBytes));
       
    }


I hope that these code snippets would be helpful also for you.