Some links for 2011-05-20

Restoring Installed Packages in Debian

I have an older laptop that runs Ubuntu Linux and one of the issues that comes up regularly is that Ubuntu releases a new version every 6 months and I usually flatten the disks on that machine and do a fresh install. The base system is great and very usable, but there are several packages that I install manually after the fact, most of them being programming tools. There is a quick way for you to list out all installed packages and then restore them after upgrading and I will cover that technique here.

Note: The instructions listed here are for Debian based distributions that use .deb based packages.

Backing up a list of installed packages

In order to list out all installed packages and save them to a text file, you can do this from the shell:

$ dpkg --get-selections > installed-packages.txt

Let's deconstruct the above command:

  • dpkg is the Debian package manager
  • --get-selections is the parameter or action that lists all the currently installed packages. You can also pass a pattern to this action to generate different types of package lists. Look at the man pages for details.
  • > textfilename.txt will redirect the standard output to the text file specified.

Restoring the list of installed packages

When you need to restore the installed packages from a previous state, you can run the following command from the shell:

$ dpkg --set-selections < installed-packages.txt

The command above is a little different and those differences mean:

  • --set-selections is the action that sets the package selections using a file read in from standard in.
  • < textfilename.txt will read the contents of the file and send them to standard in.

Conclusion

Backing up and restoring your package installation list is a really simple procedure in Debian based distributions. You can even script it out and run it on a schedule and then save it to a cloud storage service such as Dropbox. Let me know what you think and if you have a better process.

A Scytale Algorithm in CSharp

I wrote about a simple reciprocal substitution cipher before and today I will write about a simple transposition cipher. In a substitution cipher, the letters stay in their positions, but they change their identities. They may be changed to another letter or symbol. The ROT-13 algorithm (see previous link) and the Caesar cipher are two examples of a substitution cipher.

In a transposition cipher, the letters keep their identities, but their positions are switched (or transposed). An example of a transposition cipher is the Scytale (rhymes with Italy) tool used in ancient Greece to encrypt messages sent to troops and officers during battle.

The Scytale is a rod in which the sender wraps a strip of parchment paper and writes the message on that strip. The messenger then carries the message to the receiver, who must have another Scytale of the same diameter of the one used to create the encrypted message. The receiver wraps the strip on his Scytale and is able to read the message.

Scytale

The Scytale points to an interesting concept in cryptography: it's really difficult to keep an algorithm secret. The security of a cipher should not be dependent on keeping the method of encryption a secret. In the case of Scytale, the method of encryption is the rod used, but there is another aspect: the diameter of the rod is also important. The diameter is the key of the algorithm.

The Implementation

The implementation of the algorithm is really simple and there are many methods one can use to achieve the same goal. The approach I used breaks down the plain text into a list of characters and then splits this list into chunks of size equal to the key. The next step is to read the first character of each chunk and then the second character of each chunk and so on until all characters are read. The algorithm look like this in CSharp:

public string Encrypt(string plaintext, int key) {
    var chars = plaintext.ToList();
    var chunks = (int)Math.Ceiling(((double)chars.Count() / (double)key));
    var inters = new List<list>();
    int i = 0, j = 0;

    while (i = inters[l].Count())
                buff.Append("+");
            else
                buff.Append(inters[l][k]);
            l++;
        }
        k++;
    }

    return buff.ToString();
}

Note that we have to pad the last chunk if the chunk size is not equal to the key size. Also not that this is not a reciprocal algorithm, as is the case with the ROT-13 implementation. In order to decrypt a message to plain text you need to run it through another function that does things backwards. Note how this code is very similar to the encryption code, but things are down in a different order:

public string Decrypt(string ciphertext, int key) {
    var chars = ciphertext.ToList();
    var chunks = (int)Math.Ceiling(((double)chars.Count() / (double)key));
    var inters = new List<list>();
    int i = 0, j = 0;

    while (i < key) {
        inters.Add(chars.Skip(j).Take(chunks).ToList());
        ++i; j += chunks;
    }

    StringBuilder buff = new StringBuilder();
    int k = 0;

    while (k < chunks) {
        int l = 0;
        while (l < inters.Count()) {
            buff.Append(inters[l][k]);
            l++;
        }
        k++;
    }

    return buff.ToString();
}

Using the Algorithm

To use the Scytale cipher algorithm, you simply have to call the Encrypt and Decrypt functions of the Scytale class and send it a message to encrypt or decrypt along with the key size. In the example code below the Main function uses command line arguments passed in:

static void Main(string[] args) {
    Scytale scytale = new Scytale();

    string msg = args[0]; //"HELPMEIAMUNDERATTACK";
    int key = Int32.Parse(args[1]);

    string cipher = scytale.Encrypt(msg, key);
    string plain = scytale.Decrypt(cipher, key); ;

    Console.WriteLine("Message: " + msg);
    Console.WriteLine("Cipher:  " + cipher);
    Console.WriteLine("Plain:   " + plain);

    Console.ReadKey();
}

You can also call the scytale.exe program like this:

d:\> scytale.exe "HELPMEIAMUNDERATTACK" 5

The output should be the following:

Message: HELPMEIAMUNDERATTACK
Cipher:  HENTEIDTLAEAPMRCMUAK
Plain:   HELPMEIAMUNDERATTACK

[delicious | grep links | blogger] for 2011-01-03

New Year's Resolution - Lose 100 Lbs.

About a year and a half ago, Tim Ferriss (of 4 Hour Work Week fame) was looking for subjects to test some of the theories in his newly published book The Four Hour Body. I immediately volunteered for the experiments, but never received a reply from Tim or his people. About a year later I remember receiving a thank you email explaining that the pool of candidates was overwhelming and that they would not pick me. They were really nice about it, but I was a little disappointed nonetheless.

Fast forward another year to 12/19/2010 or thereabouts and I receive a package in the mail from a book publisher. First thought I had was That's weird, I didn't order anything recently.... The actual contents of the package were 2 hardcover copies of The 4 Hour Body. I started reading it immediately and through the Holidays, and I had an epiphany: I can fix my weight problem the same way that I fix any other problem I have at work.

I have been fighting with my weight for years, ever since I stopped playing basketball competitively in college and my exercise regimen went down the gutter. Today I am a six foot eight inches man who weights in at 400 plus pounds (the scale at home tops off at 400). I graduated from college in 1997 and have been working in a sit down job in the technology industry ever since. I worked as a technical writer and then as a programmer for several startups during the Internet boom years and I put on the pounds during that time. Then in early 2003 I went to work for Microsoft and that was not much of a change in the health department. Then the kids were born in 2005 and I got even less exercise than before. But the funny thing is that I think of myself as a person who can figure out a solution for any problem, and yet I have not been able to crack the weight nut.

This is were Tim's book has inspired me. I will approach my weight problem the same way that I approach any technical problem at work:

  • Root cause analysis: With most technical problems, you need to first find out what and where the problem is, gather baseline data (if there is any), and then figure out how to fix it. In the case of my weight problem and general lack of well-being, the root cause is a poor diet and complete lack of exercise.
  • Baseline measurements: Based on Tim's advice, I am taking measurements of my body at several locations: belly, arms, legs, etc. I am also keeping track of weight. I am not tracking my BMI at this point because I don't have a good way to measure it. I wrote a quick Django app that allows me to quickly gather the data no matter where I am. All I need is a connection to the Internet. Later I can easily export the data to a spreadsheet for slicing and dicing and graphing.
  • How to fix it: I will be using Tim's slow-carb diet and an exercise regimen that includes weights and swimming. Body measurements will be taken weekly to compare to baseline and that way track progress. A food diary will also be used to track food intake and portion size and I will use a web-based photo gallery app for that.
  • The goal: The goal is to lose 100 Lbs. by 12/31/2011
  • Progress: I will be tracking my progress with the tools I described above and with periodical posts to this blog.

I shared my plan and ideas with my family over the Holidays and they were all very encouraging. But the obvious question was asked and I should address it here also: You have tried to lose weight before, what is different about this time? The key thing that is so obvious in hind-sight, but it never occurred to me until I started reading Tim's book is that I can break down and fix the weight problem in the same way that I tackle a programming or other technical problem. Break down the problem in its component parts, measure the parts of the problem consistently to track progress, tweak the approach to get optimal results... This is just like hacking on a programming project. I have years of experience doing this and I have had many more successes than failures doing it this way with code. I am certain that I can apply this approach to the weight problem and be successful.

I will keep you posted on the progress...

[delicious | grep links | blogger] for 2010-11-20

How to find the longest string in a varchar column in SQL Server

If you need to find the longest string in a column in a table in SQL Server which has a datatype of varchar(X) or nvarchar(X), where X is a valid integer value for the datatype, then you will need to use the following query:

select MyVarcharColumnName
from MyTableName
where len(MyVarcharColumnName) =
    (select max(len(MyVarcharColumnName))
     from MyTableName)

In the query above you will need to substitute MyVarcharColumnName and MyTableName by your respective column and table names.

Update: [2013-05-18] fixed the code above as suggested by Andy in the comments

[delicious | grep links | blogger] for 2010-11-13

Topre Realforce 103UB Review

Anyone who follows my blog (hi mom!) knows that I have been on a multi-year quest to find the perfect keyboard and I talked about it here and here before. Well, the next stage of my quest is upon me and the choice I have made is the Topre Realforce 103UB Black keyboard. This is the model SE02B0 from Elite Keyboards.

I have had the Realforce board for a little over a month and I have been waiting to write this review until I had used it for a while and had the time to form an opinion about it. Then I read a post by Jeff Atwood on Coding Horror and I was inspired to write my review now.

Topre Realforce 103UB

Topre Realforce 103UB

Construction

The Topre Realforce 103UB is a solidly constructed board. The 103UB part of the official board name indicates that it has 103 black keys. This is somewhat different than most keyboards used in the U.S. today which have 104 or 105 keys. You will notice right away that the right side Windows key is missing on the Topre Realforce 103UB.

There are many different ways to apply letter/symbol markings to key caps and Topre uses a method that produces a really high-quality marking that never disappears. It uses a dye-sublimation process, which means that the dye (ink) goes from a solid to a gas state without becoming a liquid first in the process. The result is a marking on the key cap that has a molecular bond with the plastic. Even if you scrape off a layer of plastic from the top of a key cap, your will still be able to see the marking of the letter or symbol. One of the first things I did when I got the board was to apply the shake test. There were no loose parts inside and the whole thing felt pretty solid. But after about a week or so I noticed that the rubber grips on the bottom of the board were not as effective as they were when new. That's one area that they need to improve.

The Key Switches

Essentially, the Topre Realforce 103UB uses rubber dome keys. However, that statement needs some qualifications: This is not your rubber dome board that comes free with the latest Dell computer. They are a little better than that. Topre claims that the key switch life cycle is rated to at least 30 million key presses. This is the standard for mechanical boards and it looks pretty good when compared to a meager 1 million key press rating of the cheap rubber dome boards. Some people went as far as putting this claim to the test.

The technical term for the type of switch used on the Topre boards is electrostatic capacitive switch which means that the switch is basically a sensor that can tell the proximity (the capacitive piece) of an electrical charge (the electrostatic piece) to an object. The object in question is the electrode that sits on the printed circuit board. A second electrode, which takes the form of a conical spring, sits on top of the initial electrode on the PCB. Once the key is pressed by the user, the spring is flattened down and this changes the capacitance between the spring and the PCB, therefore registering a key press. Below are a couple of illustrations that help you better understand what is going on:

Topre Realforce key switch

Topre Realforce key switch

Regular rubber dome key switch

Regular rubber dome key switch

Capacitive sensing such as explained above is normally used in touch screen applications, but Topre is able to make use of it in their keyswitches by changing the mechanics of it a little bit. Another board using these Topre key switches is the Happy Hacking Pro 2 board.

I think that the spring inside the rubber dome makes the key press feel very uniform and smooth. And this is something that the clicky keyboard fanatics will notice: there is no clicky feel to these key switches. Or at least none that I can sense.

The other aspect of the switches that is worth mentioning is that different keys on the board require different weight pressures in order to register a key press. This is determined by the position of the keys on the board, where the keys positioned for the strong fingers (index and middle fingers) need 45 grams of force, while the keys positioned for the weak fingers (ring and pinky fingers) require 35 grams of force. The ESC key needs 55 grams of force. I think that this feature allows the user to keep a light touch accross all keys on the board.

Conclusion

The price of the Realforce board is a big determining factor as to whether you should get this board or not. But I think that you should not base your purchasing decision on price alone. Here are some facts about this board that you should think about:

  1. This is not a clicky, tactile board - This is, for all intents and purposes, a rubber dome board. It feels much better than those rubber dome boards that come free with new machines. The key caps of the Realforce feel better than anything I tried to date. But it's not clicky. If you need clicky, this is not the board for you.
  2. If you are not a touch typist, you will struggle with this board, at least initially. If you look at the keyboard while typing, you will find that it's really hard to see the black symbols on the black keys. If you are trying to improve your touch typing, then this board has the potential of helping you tremendously.

I don't recommend this board to those who are looking for a first mechanical keyboard. You should have at least one or two mechanical boards under your belt before you make the Realforce your main typing tool. At the end of the day, the Realforce is a really solid board that feels great on your fingers (even thought it's not clicky) and I highly recommend it to those who know what they are getting into. To me it was worth every penny of the $245 that I paid for it.