January 27, 2010

Gource

Det här är en video från ett litet projekt jag jobbat på de senaste veckorna. Projektet heter “Fältrevision virkestillredning”, inte speciellt spännande namn kanske. Videon är däremot rätt cool tycker jag. Den är genererad med gource, ett litet program som kan skapa den här typen av animerade grafer från git-loggar (det kan även använda svn eller http-loggar mm.)

Tack för tipset Alex.

Den lilla gubben som flyger omkring symboliserar mig, bollarna är filer och när gubben skjuter på bollarna med laser (eller vad det ska föreställa) så är det jag som ändrar en fil. Hrm.

Det är väldigt enkelt att använda gource, även att generera videofiler (annars visas animationen bara på skärmen). Jag gjorde så här:

$ gource /sökväg/till/gitförrådet -s 3 \
    --disable-progress --output-ppm-stream /sökväg/till/utdatafil.ppm
$ ffmpeg -y -b  3000K -r 60 -f image2pipe -vcodec ppm \
    -i /sökväg/till/utdatafil.ppm -vcodec libx264 \
    -vpre default /sökväg/till/video.mp4
  • ska snart berätta vad jag gör just nu...
January 8, 2010

PHP implementation of the MySQL old_password function

MySQL has a built in function called password that calculates the hash of a password for secure storage in a database. In MySQL versions older than 4.1 the hashing function was very basic so all newer versions uses the cryptograpichally secure SHA-1 hashing algorithm (twice?).

It comes as no surprise that many older databases are full of hashes calculated using the older algorithm. Fortunately it is still available under the name old_password. I needed the hashes from the old_password function but I didn’t want to connect to a database server each time. I looked up the old_password (it’s actually called my_make_scrambled_password_323 internally) from the MySQL source code. It’s written in C so a rewrite in PHP was pretty trivial. PHP doesn’t have the unsigned integer concept so I had to do some adding in the end to make the results come out right. It hasn’t been tested much and it probably only works for plain ASCII so use it at your own risk. Here it goes.

function old_password($password) {
  if ($password == '')
    return '';
  $nr = 1345345333;
  $add = 7;
  $nr2 = 0x12345671;
  foreach(str_split($password) as $c) {
    if ($c == ' ' or $c == "\t")
      continue;
    $tmp = ord($c);
    $nr ^= ((($nr & 63) + $add) * $tmp) + ($nr << 8);
    $nr2 += ($nr2 << 8) ^ $nr;
    $add += $tmp;
  }

  if ($nr2 > PHP_INT_MAX)
    $nr2 += PHP_INT_MAX + 1;

  return sprintf("%x%x", $nr, $nr2);
}
November 4, 2008

Principerna bakom Erlang

Robert Virding, en av de ursprungliga utvecklarna bakom programmeringsspråket Erlang har just släppt ett intressant dokument där han beskriver hur de tänkte när de skapade språket. Vilka krav de hade, vilka designbeslut som togs och varför vissa saker ser ut som de gör. Han beskriver också ganska utförligt hur språket kunde sett ut om de hade valt andra vägar, och varför det inte hade varit lika bra. Immutable (oföränderliga) data till exempel.

Immutable data suits a high-level language, having mutable data gets you in to all sorts of trouble and difficulties, just read descriptions of other languages which have it and the difficulties in describing what gets changed and when, for example Python’s copy and deep_copy. Mutable data, however, is much easier to comprehend in a low-level language like C, K&R C, where you directly see which data is passed by value and which is passed by reference, i.e. mutable.”

Ett annat intressant parti är när han förklarar hur deras pragmatiska inställning till programspråksdesign har gett upphov till makron och records - fula programkonstruktioner kan gå an om de ger stora fördelar.

May 17, 2008
May 8, 2008

How to use UTF-8 with MySQL and PHP Data Objects (PDO)

If you are using PHP Data Objects (PDO) and are having trouble getting UTF-8 out of your MySQL server, even though everything is set to UTF-8 explicitly, you might want to try this:

$pdo = new PDO(
    'mysql:host=mysql.example.com;dbname=example_db',
    "username",
    "password",
    array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));

The last parameter of the PDO constructor call is the driver specific options, given as an array with key => value pairs. The MySQL driver have a “PDO::MYSQL_ATTR_INIT_COMMAND” option where you can specify a command that is executed every time you connect to the database. The MySQL specific query “SET NAMES utf8” is simply telling MySQL to use UTF-8 as the character set for our connection.

Read more: