The most viewed posts on Spiration http://www.spiration.co.uk/rss/popular The most viewed posts on Spiration en-us Torkalot christo@uk.com christo@uk.com Java md5 example with MessageDigest http://www.spiration.co.uk/post/1199/Java md5 example with MessageDigest This is a quick tip for implementing md5 encryption in java. We use the MessageDigest class in the java.security package and some string manipulation to turn the plain text into a byte array. The digest is then updated from the bytes from the byte array and a hash computation is conducted upon them. To quote from the Sun java api docs, The MessageDigest class provides applications the functionality of a message digest algorithm, such as MD5 or SHA. Message digests are secure one-way hash functions that take arbitrary-sized data and output a fixed-length hash value. Anyway the md5 example code below takes a session id (this is just a string which I wanted to encrypt - It could just as easily be a document, say a lump of xml or any old bit of text). This session id is pulled into the bytes array, defaultBytes and then the MessageDigest is instantiated as an instance of an md5 encryption. Java developers who have come over from PERL or PHP often get frustrated with such a longwinded means of running what could simply be a single line of code, however the snippet below could be wrapped into an md5sum class which conducts the encryption and simply returns a string of cipher text. [code] include java.security.*; ... etc sessionid="12345"; byte[] defaultBytes = sessionid.getBytes(); try{ MessageDigest algorithm = MessageDigest.getInstance("MD5"); algorithm.reset(); algorithm.update(defaultBytes); byte messageDigest[] = algorithm.digest(); StringBuffer hexString = new StringBuffer(); for (int i=0;i<messageDigest.length;i++) { hexString.append(Integer.toHexString(0xFF & messageDigest[i])); } String foo = messageDigest.toString(); System.out.println("sessionid "+sessionid+" md5 version is "+hexString.toString()); sessionid=hexString+""; }catch(NoSuchAlgorithmException nsae){ } [/code] Remember to import the java.security package. The original plain text and cipher text strings are echod to the java console just to demonstrate what has happened. Hope that's useful, christo follow me on twitter: [url]http://www.twitter.com/planet_guru[/url] C# Confirmation Dialog Box example (ok, cancel) http://www.spiration.co.uk/post/1205/C# Confirmation Dialog Box example (ok, cancel) Somtimes you need to obtain a quick confirmation from a user to confirm an action - say a close, or delete event. This is very simple in Windows Forms UI programming, but is slightly confused by the number of Dialog classes available to the designer. In fact if all you need is a simple confirmation, you will not require a Dialog at all. All you need to do is show a MessageBox with two buttons, 'ok' and 'cancel'. In this example, a user has clicked on a 'delete' button and I wish to confirm the deletion first: [code] private void button3_Click(object sender, System.EventArgs e) { if (MessageBox.Show("Really delete?","Confirm delete", MessageBoxButtons.YesNo) == DialogResult.Yes) { // a 'DialogResult.Yes' value was returned from the MessageBox // proceed with your deletion } } [/code] That's it - what could be simpler? christo follow me on twitter: [url]http://www.twitter.com/planet_guru[/url] Unix / Linux change a user's home directory - usermod http://www.spiration.co.uk/post/1294/Unix / Linux change a user's home directory - usermod I watched somebody change a user's home directory today by deleting the user using 'userdel' and then re-creating the user with 'adduser' and entering a different path for their home directory. This is not the simplest way to do it.. To change the user's home directory, just use the 'usermod' command, which exists on all unices. It works like this: usermod -d /path/to/new/homedir/ username easy christo follow me on twitter: [url]http://www.twitter.com/planet_guru[/url] Get Windows username of current user - C# http://www.spiration.co.uk/post/1367/Get Windows username of current user - C# If you need to retrieve the username and domain of the logged-in user in your .Net application, you can access it in a comple of simple steps. First, include the System.Security.Princial namespace at the top of your class file. eg [code] using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.IO; using System.Xml; using System.Security.Principal; // here is the security namespace you need [/code] You then call the getCurrent() method against WindowsIdentity, and then reference it's Name property like so: [code] this.nametext = WindowsIdentity.GetCurrent().Name; [/code] That will return the name you need, so in my case it looks like 'DSclacy' If you want to retrieve just the username without the domain portion and the '' delimeter, you can use a String.split() method in the same line of code like this: [code] this.nametext = WindowsIdentity.GetCurrent().Name.Split('')[1]; [/code] So in the example above that would simply return 'clacy' christo You don't have permission to access root on this serv http://www.spiration.co.uk/post/1195/You don't have permission to access root on this serv Forbidden You don't have permission to access / on this server. This is common error with new Apache installations and I often get asked how to fix it. The answer is simple, but first just to clarify what it means.. This doesn't mean that apache is trying to access '/' on the unix machine - it's referring to the DocumentRoot. The reason that it's trying to access the document root is that you have asked it for a file that doesn't exist, so instead it's trying to pull up a directory listing. Apache is then failing to list the directory contents, because it hasn't been set up to do so. There are two fixes to this. The first (and safest) fix is to fix the bad link into the site, or the wrongly posted URL or whatever is causing the missing file in the first place. With new Apache installations, this is usually just a simple case of making sure your index page is called index.html and not index.htm or index.jsp or index.php or whatever. A Vanilla build of apache looks for index.html and complains if it can't find it. The second fix is to enable alternative index page naming conventions on your webserver. This requires a simple change to the httpd.conf (so you must be logged in with superuser permissions). All you need to do is add the following line to the httpd.conf: DirectoryIndex index.html If you like you can really go to town by adding all sorts of other index name options like so: DirectoryIndex index.html index.php index.sh default.jsp Note that if more than one possible convention is permitted, Apache will look for each on in turn from the beginnning of the list, so in the example given in the line above, if both index.php and index.jsp exist, the webserver will load index.php. Overcome this simply by adjusting their order of occurenc in the httpd.conf. And don't forget to restart the webserver after the changes have been made. First check the syntax of your httpd.conf with apachectl configtest and then restart with apachectl graceful Hope that's useful, christo follow me on twitter: [url]http://www.twitter.com/planet_guru[/url] convert hex to dec or dec to hex conversion http://www.spiration.co.uk/post/1211/convert hex to dec or dec to hex conversion Well here's a dead simple way of converting betwen hex and dec in perl.. [code] #!/usr/bin/perl $foo = 10; $hexval = sprintf("%x", $foo); $decval = hex($hexval); print "n$foo in hex is $hexval and in dec is $decvaln"; # end [/code] Cut and paste the above code into a new file, save it as hexdec.pl or dechex.pl or whatever and ensure that you have execute permissions. The script creates a variable, $foo with a value of your choice, (in this case 10). It will then print out it's value in hexadecimal and in decimal. Christo follow me on twitter to catch all my tech updates: [url]http://www.twitter.com/planet_guru[/url] Ubuntu Linux - Bluetooth and GPRS dialup connection http://www.spiration.co.uk/post/1307/Ubuntu Linux - Bluetooth and GPRS dialup connection This article explains how to set up a bluetooth connection between your ubuntu laptop and phone and get PPP working with BT's mobile service in the UK. It probably isn't so hard to repeat this for other network providers. I'm sure that if you do a Yahoo search for the GPRS settings of your mobile telco, you'll be up and runnin with little more than a few mintues of poking around. First you need to install the bluetooth packages on your linux machine: [code] $sudo apt-get install bluez-utils $sudo apt-get install blues-pin [/code] Now you need to make sure you have the ppp package installed $sudo apt-get install ppp At this point you already have enough software on your machine to do a scan of the local area and see what devices are available. this is done using hcitool. When I run this command, the output looks like this: [code] $chris@snackerjack-lx:~$ hcitool scan Scanning ... 00:07:3A:08:EE:74 n/a 00:18:13:50:0C:EB Christo Yahoo! chris@snackerjack-lx:~$ [/code] That means that my laptop can see my phone - it's reporting the MAC address and the device name. For more information on hcitool, you can run 'man hcitool'. Basically hcitool is a handy bluetooth utility which allows you to scan for and query local bluetooth devices. At this stage you can use hcitool to pair your bluetooth-enabled laptop with your phone. Use the following commands to achieve this: [code] $sudo hcitool cc 00:18:13:50:0C:EB [/code] Note, you should use the MAC address of your own phone in this command - as reported by the hcitool scan command earlier. This creates a baseband connection to your phone. The next step is: [code] $sudo hcitool auth 00:18:13:50:0C:EB [/code] This will request authentication with your phone - this is known as 'pairing' and will allow your computer to communicate with your bluetooth phone. Note again, you should replace the mac address with that of your phone. Don't use mine! The next stage is to use the Service Discovery Protocol to ask your device what bluetooth services it is offering. At this point, if you're feeling really curious, you could re-run your hcitool scan and then run an SDP discovery search on all the listed devices. You never know - it might be interesting. However, this article is about setting up a Dial-up connection through your phone, so let's keep focussed.. The command you need to issue looks like this: [code] $sdptool browse 00:18:13:50:0C:EB [/code] Again, use your own phone's MAC address - This will list all sorts of bluetooth services that your phone offers. This is what the output looks like when I run this command against my own phone.. I know it's a lot to paste, but it's worth seeing the kind of services which might be on offer: [code] chris@snackerjack-lx:~$ sdptool browse 00:18:13:50:0C:EB Browsing 00:18:13:50:0C:EB ... Service Description: Sony Ericsson K750 Service RecHandle: 0x10000 Service Class ID List: "PnP Information" (0x1200) Service Name: Dial-up Networking Service RecHandle: 0x10001 Service Class ID List: "Dialup Networking" (0x1103) "Generic Networking" (0x1201) Protocol Descriptor List: "L2CAP" (0x0100) "RFCOMM" (0x0003) Channel: 1 Profile Descriptor List: "Dialup Networking" (0x1103) Version: 0x0100 Service Name: Serial Port Service RecHandle: 0x10002 Service Class ID List: "Serial Port" (0x1101) Protocol Descriptor List: "L2CAP" (0x0100) "RFCOMM" (0x0003) Channel: 2 Service Name: HF Voice Gateway Service RecHandle: 0x10003 Service Class ID List: "Handfree Audio Gateway" (0x111f) "Generic Audio" (0x1203) Protocol Descriptor List: "L2CAP" (0x0100) "RFCOMM" (0x0003) Channel: 3 Profile Descriptor List: "Handsfree" (0x111e) Version: 0x0101 Service Name: HS Voice Gateway Service RecHandle: 0x10004 Service Class ID List: "Headset Audio Gateway" (0x1112) "Generic Audio" (0x1203) Protocol Descriptor List: "L2CAP" (0x0100) "RFCOMM" (0x0003) Channel: 4 Profile Descriptor List: "Headset" (0x1108) Version: 0x0100 Service Name: OBEX Object Push Service RecHandle: 0x10005 Service Class ID List: "OBEX Object Push" (0x1105) Protocol Descriptor List: "L2CAP" (0x0100) "RFCOMM" (0x0003) Channel: 5 "OBEX" (0x0008) Profile Descriptor List: "OBEX Object Push" (0x1105) Version: 0x0100 Service Name: OBEX File Transfer Service RecHandle: 0x10006 Service Class ID List: "OBEX File Transfer" (0x1106) Protocol Descriptor List: "L2CAP" (0x0100) "RFCOMM" (0x0003) Channel: 6 "OBEX" (0x0008) Profile Descriptor List: "OBEX File Transfer" (0x1106) Version: 0x0100 Service Name: OBEX SyncML Client Service RecHandle: 0x10007 Service Class ID List: "Error: This is UUID-128" (0x00000002-0000-1000-8000-0002ee000002) Protocol Descriptor List: "L2CAP" (0x0100) "RFCOMM" (0x0003) Channel: 7 "OBEX" (0x0008) Service Name: OBEX IrMC Sync Server Service RecHandle: 0x10008 Service Class ID List: "IrMC Sync" (0x1104) Protocol Descriptor List: "L2CAP" (0x0100) "RFCOMM" (0x0003) Channel: 8 "OBEX" (0x0008) Profile Descriptor List: "IrMC Sync" (0x1104) Version: 0x0100 Service Name: Mouse & Keyboard Service Description: Remote Control Service Provider: Sony Ericsson Service RecHandle: 0x10009 Service Class ID List: "Human Interface Device" (0x1124) Protocol Descriptor List: "L2CAP" (0x0100) PSM: 17 "HIDP" (0x0011) Language Base Attr List: code_ISO639: 0x656e encoding: 0x6a base_offset: 0x100 Profile Descriptor List: "Human Interface Device" (0x1124) Version: 0x0100 chris@snackerjack-lx:~$ [/code] That's pretty cool - First you can see that I'm using a Sony Ericsson K750 phone and then on every bluetooth 'Service Name:' line, you can see the name of a service on offer. The one we are most interested in is the 'Dial-up Networking' service, however, we can also use this device as a voice gateway, and it will support OBEX transfer requests (ie the exchange of binary objects from other bluetooth devices). I can also use this phone as an input device (service 'Mouse & Keyboard') - note that this last service describes itself as 'Remote Control' and yes, it can be used in precisely that way - so the phone will suddenly become a pointer/mouse within the PAN. I will explain this in another article. Okay, so where are we now? Well the next step is simply to tell your system which channel you want to talk RFCOMM. RFCOMM is a special bluetooth serial port emulation over radio frequency (hence the name rf communication). It is quite literally an implementation of the RS232 serial protocol over radio. Bluetooth can handle several rfcomm channels consecutively. We just have to decide which one we're going to use for this exercise.. This is dead easy. Just look at the output of the sdp search which you ran just now and look at the RFCOMM channel number in the 'Dial up Networking' service section. In the case above, it's channel 1. All you do now is specify that channel in your rfcomm.conf - so on my system that looks like this: [code] rfcomm0 { bind yes; device 00:18:13:50:0C:EB; channel 1; comment "PPP connect"; } [/code] At this point, you can set up /dev/rfcomm0 by running the following command: [code] $sudo /etc/init.d/bluez-utils restart [/code] We're nearly there. Now that you have your rfcomm node fully configured and your phone and computer are paired, all you need to do is set up the PPP and chat settings on your computer. Chat sets up a ppp connection between your modem and a remote ppp service based on rules which you define in a chatscript. First create the file /etc/ppp/peers/bluetoothconn and put the following into it: [code] debug noauth connect "/usr/sbin/chat -v -f /etc/chatscripts/bluetoothconn" usepeerdns /dev/rfcomm0 115200 defaultroute crtscts lcp-echo-failure 0 [/code] Now edit the file /etc/chatscripts/bluetoothconn and make sure it contains the following: [code] TIMEOUT 35 ECHO ON ABORT 'nBUSYr' ABORT 'nERRORr' ABORT 'nNO ANSWERr' ABORT 'nNO CARRIERr' ABORT 'nNO DIALTONEr' ABORT 'nRINGINGrnrnRINGINGr' '' rAT OK 'AT+CGDCONT=2,"IP","btmobile.bt.com"' OK ATD*99***2# CONNECT "" [/code] It took me a while to get this configuration to work. I tried several settings for the data profile number, without any real idea of what it should be - and found that ***2 worked. You might have to play around with this for your own telco - try swapping the '2' for any integer between '1' and '4', or even remove the whole lot and just terminate with a '#'. To bring the connection up, just run the command: [code] $pon bluetoothconn [/code] and to turn it off again: [code] $poff Bluetoothconn [/code] It's as simple as that. If you still have questions about your bluetooth and GPRS setup with linux on your laptop and trying to talk to BT, please reply to this and let me know. Hope that helps. christo Follow me on twitter to catch all my tech updates [url]http://www.twitter.com/planet_guru[/url]