Thursday, November 13, 2014

Working with SHA1 on SQL Server

There are times you might need to convert some information to MD5 on your SQL Server in HEX format. As it is for now there is no out of the box function to do it on SQL Server. But you can easily accomplish this task by using the following function:
create FUNCTION [dbo].[sha1]
        @value varchar(4000)
RETURNS varchar(40)
    case when @value is not null then
      SUBSTRING(master.dbo.fn_varbintohexstr(HashBytes('SHA1', @value)), 3, 40)
    else null end

Testing it is quite straight forward. For example:
select dbo.sha1('123') 

And here is the output:


Wednesday, November 12, 2014

Working with MD5 on SQL Server

There are times you might need to convert some information to MD5 on your SQL Server in HEX format. As it is for now there is no out of the box function to do it on SQL Server. But you can easily accomplish this task by using the following function:
create FUNCTION [dbo].[md5]
        @value varchar(4000)
RETURNS varchar(40)
    case when @value is not null then
      (SUBSTRING(master.dbo.fn_varbintohexstr(HashBytes('MD5', @value)), 3, 32))
    else null end

Testing it is quite straight forward. For example:
select dbo.md5('123') 

And here is the output:


Tuesday, April 9, 2013

PC speaker beeps with no apparent reason

I had this problem for about few months: from time to time with no warning, my PC started beeping from its speakers for no apparent reason.
Sometimes it started with several beeps, sometimes it started with a single long beep. When I restarted my computer the problem seemed to pass till the next time.
First, I suspected I had a virus. I made sure I updated my Kaspersky to the latest version and scanned for viruses with no luck.

I then went to Windows device drivers manager and stopped the speakers service - but still, the beep used to come whenever it wanted. Eventually, I came with the theory that my machine is getting too hot and therefore I get a warning beep. So I opened the case of the PC to make sure my ventilation is working properly - all seemed right.

So, I decided to download this small app named Core Temp. It can give detailed about the temperature of each core of your CPU. It uses information from the CPU itself and not sensors that measure temperature outside the CPU.

After loading the app I noticed the temperature was around 70 degrees. It seems to me a bit high, but nothing unreasonable. So I decided to wait for the next beep to come.
After few days the beep came again. I loaded the Core Temp and was amazed to see that the CPU temperature was on 95 degrees!
I quickly opened my PC case and noticed both ventilation are working properly. So I decided to give a closer look at the hit sink below the CPU ventilation. I noticed a huge amount of dust that filled the hit sink.

I started cleaning all the dust that accumulated there along the time. Then started my computer again and loaded the Core Temp. Then I noticed the temperature was around 35 degrees!
Eventually the mystery was solved: my PC was beeping like crazy, since the CPU was hitting too much and I couldn't tell there was a problem, because ventilation seemed to work properly.
So if your PC starts to beep with no apparent reason, do yourself a favor and give it attention. It might say that something is seriously wrong. Probably your CPU is over hitting and the blame might be more then a low performing ventilation: DUST.
I assume that the dust may have caused 2 major things:
  • Less effective hit dispensation.
  • Increases the drag of the ventilation and therefore cause it to rotate slower.
When my computer was not working too hard: temperature was around 70 degrees, which was not so good, but not so bad.

When my computer was working hard, CPU temperature started climbing higher and higher till  it reached around 95 degrees and the computer started beeping to warn about the hit.
The small difference of removing the excessive dust made the difference for more than 50 degrees! Worth the effort!

Monday, February 4, 2013

Change address bar back to Google on FireFox

Nowadays, when you install new software on your machine, you can never know if it comes with some other bundled packages that during installation simply change the behavior of your browser/system. During the installation process you get a small check box selected by default, usually on the bottom of the window so you do not notice that you are going to install some additional software.

Lately this thing happened to me, even tough I always pay attention to these kind of things. I even do not remember what I installed. But as a result of the installation I was not longer able to directly do search on my FireFox address bar.
Companies that usually change the address bar for you without you wanting it are:
I was able to repair this issue very easily by doing the following steps:
  1. Write on FireFox address bar: "about:config". It will take you to advanced settings of FireFox.
  2. Use the "search" text field on the top to filter: "keyword.URL".
  3. Right click on the resulted row and select: "Reset".
That's it. As simply as that.

It is also a good thing if you will try to filter the list by the strings: "ask" and "conduit" in order to reset other things they might have changed in you browser.
Please make sure that you do not do mistakes in the process.

Thursday, July 12, 2012

Use Google photos screen saver without installing Picasa

Google photo screen saver used to be part of Google Pack. Then Google decided to move it under Picasa. That means, that if you want to use Google photo screen saver you have to install Picasa.

I use Adobe Light Room to edit my photos and I love it. I love Google's photo screen saver. It shows photos much more nicely then Microsoft's built-in photos screen saver and also ads more sources to get photos (like RSS feeds from the internet).

I wanted to install Google's photo screen saver without needing to install Picasa. I managed to do it easily by installing Picasa, grabbing it's screen saver file: GPhoto.scr and then uninstalling Picasa.

If you would like to use Google photo screen saver without needing to install Picasa, you can simply download it here, then place this file under Windows folder: c:\windows (or you own windows installation folder).

When going to screen savers selection on Windows (the location of this option depends on your Windows version, but can be easily found) you should see Google's photos screen saver on the screen savers list.

Enjoy it!

Monday, June 6, 2011

Configure Timeout for Apache HttpClient 4.0

Adding HTTP request timeout support to HttpClient 4.0  of Appache is quite an easy task. But not as straight forward as one might think. I needed to add such timeout for one of my HTTP requests. I expected to have a method named: "setTimout" or "setConnectionTimeout". But couldn't find any method regarding timeout. After a little digging I found how it can be done. Hope it will save you some time.
In general, all you have to do is create an instance of HttpParams and use it in order to define the connection timeout.
Let's see how the code looks:
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
 * @author Bashan
public class HttpRequest {
  public static void main(String[] args) throws Exception {
    // Set connection timeout
    HttpParams params = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(params, 5000);
    HttpConnectionParams.setSoTimeout(params, 5000);
    HttpClient httpClient = new DefaultHttpClient(params);
    HttpResponse response = httpClient.execute(new HttpGet(""));

You can see in the 3 lines marked in yellow, how the HttpParams class is used in order to define the connection timeout. Later an HttpClient instance is created with params that is passed to it.

You can download this class here.

Friday, May 27, 2011

Show timer with seconds using NSTimer on iPhone and Objective C

I am currently developing an iPhone App using X-Code and Objective-C. I had to show a simple timer showing the elapsed number of seconds and minutes. I don’t know much about Objective-C, but from my past experience I knew this task can be easily accomplished using a Timer. The timer should be called every second, and a simple int variable will hold the seconds. The variable is incremented on every call of the timer.
This is how I the timer is defined:
NSTimer *mainTimer = [NSTimer scheduledTimerWithTimeInterval:1 

Note that the timer is set to be activated every one second and the function that will be called every one second is named “timerController”.

In the “timerController” function I wrote the code that will increment the seconds variable as well as update a label on the screen. In order to update the label with the minutes and seconds I wrote a small function. The function gets seconds and returns a string of the format: “mm:ss” (for example, for input of 90 the function will return: “01:30”. Let’s see the function that returns the time:

- (NSString*)getTimeStr : (int) secondsElapsed {
  int seconds = secondsElapsed % 60;
  int minutes = secondsElapsed / 60;
  return [NSString stringWithFormat:@"%02d:%02d", minutes, seconds];

And let’s have a look at the “timeController” function that use it:

- (void)timerController {
  [[self timeLabel] setText:[self getTimeStr]];

The “timeLable” is a UILabel control that is connected with a label that is show on the iPhone screen.

Thursday, May 26, 2011

Add Facebook Like Button to your (BlogSpot)

Facebook Like button is a great and easy way of sharing things that you like with your friends. If you have a blog on (BlogSpot) you can easily add a Facebook like button to any of your posts. Each post will have it's own Like button. This can help increasing the traffic of your blog.

Let's see how we can easily add Facebook like button:

Step 1: Editing out blog template as HTML:

  • press on: "Design" on your blog main menu.
  • Then press on "Edit HTML" on the upper sub menu.
  • Check "Expand Widget Templates" checkbox.
Step 2: Find the place in which Facebook Like button code should be entered:
Search for the String:
Facebook Like button should be placed before this code.

Step 3: Put the following code in your blog template:
<b:if cond='data:blog.pageType == &quot;item&quot;'>
<iframe allowTransparency='true' expr:src='&quot;; + data:post.url + &quot;&amp;layout=standard&amp;show_faces=false&amp;width=100&amp;action=like&amp;font=arial&amp;colorscheme=light&quot;' frameborder='0' scrolling='no' style='border:none; overflow:hidden; width:450px; height:40px;'/>

After you save your template, go to one of your posts. You could see that a Facebook Like button was added right under the name of your post.

Monday, May 16, 2011

Log4j SMTPAppender and deadlocks - Adding Timeout support

On the post: Sending Email alerts with Log4j we saw how we can easily send mails whenever our system has an exception. But we have to remember that using Apache Log4j SMTPAppender, can be very risky if not used cautiously. In general, the Log4j SMTPAppender has 2 main problems:

  1. The part that send the mail, is synchronous. In fact, it is synchronous between all Log4j appenders. It means that when you log an error and a mail is being sent, all log commands are locked. This is quite risky. Especially when your system has many errors (for whatever reason). This may cause your whole system to get stuck. This issue can be easily handled by using: AsyncAppender. I may write about AsyncAppender in more detail in the future.
  2. The code responsible for sending the mail, which is written by Apache developers, doesn't has a Timeout. That mean, a mail being sent can be stuck forever and simply cause your entire system to go into a deadlock.
    In this post, we will improve the Apache SMTPAppender to include a Timeout property. The Timout property will make sure, that if a mail is being sent using SMTP connection for too long, it will be dropped. We may loose a report about an exception, but we will make sure our system won't be stuck indefinitely.

We will create a new class name: SMTPAppenderTimeout, that extends SMTPAppender. This new class will override the SMTPAppender method: createSession.
The new createSession method will make sure to add the following 2 properties to the mail session: 

  • mail.smtp.connectiontimeout
  • mail.smtp.timeout

These 2 properties instruct the Java mail framework to set a timeout on the SMTP connection.
Let's have a look on the SMTPAppenderTimeout class:

import javax.mail.Authenticator; 
import javax.mail.PasswordAuthentication; 
import javax.mail.Session; 
import java.util.Properties; 
* @author Bashan 
public class SMTPAppenderTimeout extends SMTPAppender { 
  private int timeout; 
  public int getTimeout() { 
    return timeout; 
  public void setTimeout(int timeout) { 
    this.timeout = timeout; 
  protected Session createSession() { 
    Properties props; 
    try { 
      props = new Properties(System.getProperties()); 
    } catch (SecurityException ex) { 
      props = new Properties(); 
    if (timeout > 0) { 
      String timeoutStr = Integer.toString(timeout); 
      props.setProperty("mail.smtp.connectiontimeout", timeoutStr); 
      props.setProperty("mail.smtp.timeout", timeoutStr); 
    if (getSMTPHost() != null) { 
      props.put("", getSMTPHost()); 
    Authenticator auth = null; 
    if (getSMTPPassword() != null && getSMTPUsername() != null) { 
      props.put("mail.smtp.auth", "true"); 
      auth = new Authenticator() { 
        protected PasswordAuthentication getPasswordAuthentication() { 
          return new PasswordAuthentication(getSMTPUsername(), getSMTPPassword()); 
    Session session = Session.getInstance(props, auth); 
    if (getSMTPDebug()) { 
    return session; 

You can also download the SMTPAppenderTimeout.

Let's see an example of a log4j.xml file which use SMTPAppenderTimeout to allow timeout of 5 seconds (5000ms):       

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="">
	<appender name="RollFile" class="org.apache.log4j.RollingFileAppender">
		<param name="File" value="C:\\testlog.txt" />
		<param name="MaxFileSize" value="10MB" />
		<param name="MaxBackupIndex" value="1" />
		<layout class="org.apache.log4j.PatternLayout">
			<param name="ConversionPattern" value="%d{HH:mm:ss} %-5p [%c{1}]: %m%n" />
	<appender name="Email" class="">
		<param name="BufferSize" value="10" />
		<param name="SMTPHost" value="" />
		<param name="SMTPUsername" value="test_user" />
		<param name="SMTPPassword" value="test_password" />
		<param name="Timeout" value="5000" />
		<param name="From" value="" />
		<param name="To" value="" />
		<param name="Subject" value="System Error Notification" />
		<layout class="org.apache.log4j.PatternLayout">
			<param name="ConversionPattern" value="%d [%t] %-5p %c %x - %m%n" />
		<priority value="info" />
		<appender-ref ref="RollFile" />
		<appender-ref ref="Email" />

You can also download the log4j.xml.

Sunday, April 24, 2011

Nikon D7000 vs Nikon D300 or should I wait for the Nikon D400?

I was waiting for the Nikon D400 to come out for too long. It has been more than 4 years since I bought my Nikon D300. I love this camera. But it has gotten too old. I wanted something new and the Nikon D400 doesn’t seem to show any signs of coming out. So I got tired of waiting, and just bought the Nikon D7000. I wanted to share some of my feelings of my new Nikon D7000 and do a small comparison with my good and old Nikon D300, so those of you who own the Nikon D300 could have an easier decision regarding purchasing the Nikon D7000. So, before comparing the Nikon D7000 and Nikon D300, I can spare you from reading and tell you that I simply love my new Nikon D7000 and I do recommend you go and buy one. If you own a Nikon D300, you won’t regret it. Although the Nikon D7000 is less pro, it’s newer technology definitely puts it in a better place than Nikon D300 and even Nikon D300s and all for that for less price. OK, so let’s start to see what is so good on the Nikon D7000:

Focus System:

Nikon D300 has 51 focus points from which 15 are cross-type. 51 focus points are a lot, and the Nikon D300 has great focus system. It is fast, accurate and allows you to do continuous and even tracking focus. But, although the Nikon D7000 has less focus points: 39 from which 9 are cross-type, it has very fast and accurate focus system. I think even faster and better than the one of the Nikon D300. You don’t have anything to worry about, all the focus options from the Nikon D300 exist in the Nikon D7000.


That’s one of the things I worried about when I wanted to buy the Nikon D7000. After so much time that I got used to all the fast and easy on-camera controls of the Nikon D300, I worried that I will really miss them on the Nikon D7000. Well, now that I got the Nikon D7000, I can tell you that I don’t miss those buttons much. I mean, it was nice to have these buttons there, but you can easily manage without them on the Nikon D7000. Switching the ISO (I usually choose the ISO manually), White Balance (I almost never change it from Automatic), and metering mode are all all done same as the Nikon D90/D80. But, there is a new button, placed on the focus AF/M selection switch. This new button along with the two front and back wheels allows you to quickly and easily select your desired focus mode. This new cool button, makes it easier to forget those loveable focus switches on the Nikon D300.


The Nikon D7000 has 100% viewfinder coverage. comparing to the 95% viewfinder coverage of the Nikon D300, you can see entirely what you are going to shoot. It’s the first time I am using a 100% coverage viewfinder and I like it. Better image


The Nikon D7000 has better image quality. You have to look at the results and decide for yourself. In my opinion the results are better. Although the Nikon D7000 has 16.2 MP sensor (comparing to the Nikon D300), the image quality is better at all ISO ranges. A sensor containing more pixels, means smaller pixels. Smaller pixels mean more noise at higher ISOs. But the D7000 seems to be dealing better with noise than the D300. The Nikon D7000 has better color depth: 24bit comparing to 22bit. That’s about x2.5 more colors. In addition, the Nikon D7000 has better dynamic range: 14EV comparing to 12EV of the Nikon D300. That’s more 2 f-stops on the D7000.

Continuous Shooting:

The Nikon D300 shoots 7 frames per second. The Nikon D7000 shoots 6 frames per second. That makes the Nikon D300 a bit better when it comes to continuous shooting. There are times when continuous shooting can be a great help, but the difference between 6 to 7 frames per second is not that big. In addition, when you set the Nikon D300 D-Lightning feature, or you set your ISO to be 800 or higher (when high ISO cleaning option is turned on), the processing is too slow, so after about 8 photos, the shooting rate goes down dramatically. The D7000 doesn’t seem to suffer from this issue.

Shutter Noise:

One of the most annoying things of the Nikon D300 is it’s shutter noise. This is really an extreme noise. I don’t know how Nikon released a camera with such a strong shutter noise. Well, on the Nikon D7000 you don’t have to worry about noise. The shutter makes much less noise, even when shooting continuously. If you want even less noise you have a special “quite” mode that will reduce the noise even further.

Memory Slots:

The Nikon D7000 has 2 memory slots. Additional memory slot means more memory. More memory means you can store more photos. This is good when you shoot on RAW mode which takes considerably more space for each photo, or you shoot 1080p video which takes a lot of space. You can also use the 2 slots to function as backup (each photo is stored on 2 cards). Video: Well, I don’t shoot video. But with the Nikon D7000 you can easily shoot 1080p Video with 24 fps. You shoot the video using your good Nikon lenses, so you can get great results with shallow depth of field. The video is shown on the back screen and there is an automatic focusing system that can identify faces. It is working very nice. You can also edit the video on the camera.

Size and Weight:

The Nikon D7000 is smaller in size and lighter in weight than the Nikon D300, but it still has a strong grip and it feels good in your hands. It is not well built as the Nikon D300, but it still has a very satisfying built quality Price: Event today, after more than 4 years that the Nikon D300 exists in the market (I believe Nikon has already stopped producing it), it is sold for about more than $200 than the Nikon D7000.


I own the Nikon D7000 for only 2 days. I have taken with it only 200 photos. It may be a short period for solid opinion, but so far I am very satisfied with it. If you own a Nikon D300 or Nikon D300s I think the Nikon D7000 is a good upgrade choice for a decent price. Since the Nikon D400 is still not on the horizon (with the recent earthquake on Japan, it will probably event take longer), and it seems like it is going to be with the same sensor of the Nikon D7000 but with more focus points and stronger body, I think it is not worth the time and money waiting for the Nikon D400.