Mar 09
2016

Clone Windows 10 Installation

Its not 100% easy to do a Windows 10 clone, eg. from a ordinary drive to an SSD drive but the steps below will do the trick. The boot process will normally not work out of the box without these steps.

  • Make a backup of the partition with Drive Image XML
  • Restore the backup with Drive Image XML onto the new disk
  • Make sure partition is the first, its primary and active.
  • It might not be possible to set the active on the partition, if its greyed out in Windows Disk Management you need to set it manually (*)
  • Now create a System Restore Drive on a USB stick, you do that from Windows Control Panel and it burns onto the USB
  • Install the new disk and repair the boot process with the USB stick
  • You are ready to go

(*): If its not possible to set the primary partition to active do this:

Open a cmd prompt with administrator priviledges and do this:

  • DISKPART     (to open the partition utility)
  • LIST DISK     (disk number(s) will be shown)
  • SELECT DISK n     (where n is the number of the disk – probably 0)
  • LIST PARTITION     (partition number(s) will be shown)
  • SELECT PARTITION n     (where n is the number of the Primary partition you wish to make Active)
  • ACTIVE     (the selected partition on the selected disk will be made Active)
  • EXIT     (to exit DiskPart)
  • EXIT     (to exit the Command Prompt)
Posted in hardware | Leave a comment
Feb 05
2016

RSA encryption in Java – complete step by step tutorial

rsa

Generate the RSA keys

Open your favourite Linux and make sure you have openSSL installed.

Generate the private and public key. The public key is used by any interested senders, to use for encryption. The private key must be kept, surprise, private and is used to decrypt. Write this in the linux command line:

openssl genrsa -out private.pem 1024

Now you got a private key called private.pem.

You need to generate the public key from the private key, as we want to use it in Java we generate it as der instead of pem, its much easier to load into Java.

Now write the following to get a public key in der format:

openssl rsa -in private.pem -pubout -outform DER -out public_key.der

Now you got a public key called public_key.der ready to load into Java.

Useful information

If you want to see the information in the private key use this line:

openssl rsa -in private.pem -noout -text

The modulus is the public key. Please note, it can be padded with a 0, giving 1 byte more than the key size.

The public exponent is usually 65537 – not all tools let you change this.

The privateExponent is the private key. In our case we needed the private key inside an embedded system, we just copied the bytes from the private Exponent and used that in an appropriate RSA algorithm in the embedded device (remember to use same public exponent there). If you need help in getting RSA to work in an embedded system feel free to contact me.

A side note, if you are not familiar with RSA. The data you want to encrypt must be same size as your key. Some padding schemes exist, where data is padded, in some cases you can use less data.

If you want to generate a public pem key, use this line in linux:

openssl rsa -in private.pem -outform PEM -pubout -out public.pem

If you want to encrypt some string (must be same size as the key, this example does not use padding):

cat text.txt | openssl rsautl -raw -encrypt -pubin -inkey public.pem > encrypted.txt

To decrypt use this line:

openssl rsautl -raw -decrypt -in encrypted.txt -out decrypted.txt -inkey private.pem

A few words on the key length

Please do not use less than 512 bits key size. 256 bit keys takes less than 30 minutes to crack on a standard desktop pc. Java does not allow you to use key sizes less than 256 bits, but you might find other algorithms that do not care. A rule of thumb is that if you want it totally uncrackable, you need 2048 bits, if you want it very secure use 1024 bits and if you want it ok secure, 512 bits are ok. But never less.

Encrypt the stuff in Java

Its pretty easy, just use the code below. Its almost trivial to write similar code for encryption.

package RSAHelper;
import java.security.*;
import java.io.*;
import java.security.spec.*;
import javax.crypto.Cipher;

public class RSAHelperPlain {

 public PublicKey loadPubKey(String filename) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
    File file = new File(filename);
    int fileLength = (int)file.length();
    DataInputStream dis = new DataInputStream(new FileInputStream(file));
    byte[] keyBytes = new byte[fileLength];
    dis.readFully(keyBytes);
    dis.close();

    X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes);
    KeyFactory kf = KeyFactory.getInstance("RSA");
    return kf.generatePublic(spec);
 }

 public byte[] encrypt(byte[] bs, PublicKey key) throws NoSuchAlgorithmException, GeneralSecurityException {
    byte[] cipherText = null;
    final Cipher cipher = Cipher.getInstance("RSA/ECB/NOPADDING");
    cipher.init(Cipher.ENCRYPT_MODE, key);
    cipherText = cipher.doFinal(bs);
    return cipherText;
 }
}

To conclude it can be quite overwhelming, if you are not used to crypto work. Not all tutorials out there makes it easier to grasp the idea. But its is in fact quite simple.

Posted in Uncategorized | Leave a comment
Dec 04
2015

WordPress, from slow to fast in a few steps

We took over a site written in WordPress recently, it was very, very slow. Ocassionally it took more than 10 seconds to load a page, normally after no one had visited the site for a while.

Here is what we did to change it from super slow, to super fast.

  • Install WP Super Cache, make sure to enable it, and check that paths are SEO friendly, or it will not work
  • Disable the way a default WordPress runs cron jobs. It intercepts the user hits from users, and do the tasks while the user is waiting. Obviously, that makes the site feel slow now and then: Find wp-config.php and add this line: define(‘DISABLE_WP_CRON’, true);
  • Now setup a cron job calling the site regulary, like every 15 minutes, on a linux system the command is like this (replace yoursite.com with your site name):wget -q -O – http://yoursite.com/wp-cron.php?doing_wp_cron >/dev/null 2>&1
  • Set preload to 60 minutes

Thats it, the site is now super fast :-)

 

Posted in database, php, wordpress | Leave a comment
Nov 09
2015

Linux shuts down after approximately two minutes

Recently i installed a S6760 notebook from MM Vision. Its a rebranded Clevo Notebook.

I installed Ubuntu 14.04 on it, but to unfortunately the notebook restarted itself after around two minutes. Looking in the logs a line suggested that it had to do with the nouveau driver for Nvidia graphics.

Here is what i did to get rid of the problem.

  1. Hold down shift during boot, now you get to the grub menu press ‘e’ for edit
  2. Find the line starting with linux, at the end add “nouveau.modeset=0″ without quotes
  3. Press F10 and the system boots and voila it does not shutdown

Now you need to add this permanently to linux:

  1. Open a command prompt
  2. Edit the file /etc/default/grub for example with nano
  3. Edit one line, it could look like this: GRUB_CMDLINE_LINUX_DEFAULT=”quiet splash”
  4. And after you have edited, it looks like this GRUB_CMDLINE_LINUX_DEFAULT=”quiet splash foo=bar nouveau.modset=0″
  5. Now save the settings (ctrl+x in nano)
  6. And save with the command “sudo update-grub”

And restart.

This does not give you a good driver for your Nvidia Geforce GTX card, but it lets you start and find a better suited driver without having only two minutes to fix it before the system reboots.

 

Posted in Uncategorized | Leave a comment
Aug 15
2015

Magento – is it not working as expected? Do you have module conflicts?

Magento is a great webshop with endless possibilites. However, its dynamic structure, and the way modules are written can give some headache.

The problem can be that two or more modules inherit the same core class. Also called Magento module conflict. When that happens, one of the modules wins, and the rest looses. You do not really which one wins, and the result can be strange behaviour and features not working as expected.

How to detect a Magento module conflict?

Its quite easy to detect, if you have any conflicts in Magento, without waiting for some customer reporting that something is not working. You can install this module http://www.magentocommerce.com/magento-connect/modules-conflict-detector.html

It will tell you if you have any conflicts, if everything is green and fine you do not have any conflicts.

How to fix a Magento module conflict?

Several solutions exist. But you need to get your fingers into the code, unless you uninstall one of the conflicting modules and use something else.

The solution can be to let the one conflicting class inherit the other conflicting class. You need to use the Magento dependency concept, where you can make sure that one module loads before another, to make sure which one should inherit the other.

Another less attractive approach is to look at the two classes that conflicts, and write them together into one class. Its not very nice, and if one of the modules gets updated the result can be the conflict getting back.

You can read more about Magento module conflict resolving here http://www.webshopapps.com/blog/2010/11/resolving-magento-extension-conflicts/

Conclusion is, that it can be tricky to fix a Magento module conflict, you might need to consult an expert, or find other alternative modules. However, its easy to detect if you have a potential problem, you can do that with the module described in the first paragraph of this blog.

Posted in magento, webshop | Leave a comment
Aug 04
2015

Storing your app data in the cloud

Do you want to store data in the cloud, but do not want to install Linux servers, load balancers and do server maintenance on a daily basis the rest of your life?

One solution is to use Amazons AWS services.

For storing data you can use the NoSQL database DynamoDB. See a guide here. However accessing data directly from an app is not a good option. You will need to store your keys and database credentials directly in the app. Hackers can easily decompile the app and reveal the credentials.

You need a middle layer to handle secure access to the database without exposing credentials to the DynamoDB. You can write your own in a various languages, but that again requires a real server somewhere or what can you do?

The solution is to use Amazon Cognito. It handles the middle layer logic without writing any backend code or managing any infrastructure. Take a look here for Amazon Cognito.

unnamed-4-825x380

 

Posted in android, database, ios | Leave a comment
Sep 30
2014

Are your PHP webserver scripts consuming too many resources?

If you have a hard loaded webserver, with php scripts, you might consider optimizing the PHP setup before investing in more hardware.

Here are three tips for a production system, there are lot of other tweaks, but these three really makes a difference.

1. Use PHP5-fpm instead of fast-cgi, its balancing resources better and better to automatically recover after a crash.

2. Use APC cache. Avoid PHP generating opcode for each request, with the APC cache the opcode is cached, and retrieved from cache at each hits. It gives a dramatic performance boost.

3. Xdebug is very cpu intensive. Make sure you do not load xdebug in your php.ini. Its not sufficient to disable xdebug, you need to make sure it never loads. Below is an image of the cpu resources on one of our production systems before (xdebug loaded, but disabled) and after (xdebug never loaded). Its quite easy to spot when the change were implemented. It reduced cpu consumption from around average 180% to average 120%.

cpu-week

Posted in php | Leave a comment
Aug 21
2014

Free non-blocking hot backup of your MySQL database

Are you tired of mysqldump, but not willing to pay for the enterprise tools for Mysql? That was my situation a few years ago, and i started to look around for alternatives.

No problem, you can do hot backup of your MySQL database with open source tools, even without locking any tables during the backup.

Take a look at http://www.percona.com/doc/percona-xtrabackup/2.1/

Percona Xtrabackup

It allows you to hot backup your mysql database, and its very, very fast. Its free, and now after using it for years, we can conclude that it works perfectly and every time we are still amazed how fast it backs up even very large databases.

Give it a try, it saves you money and is way beyond the capabilities of mysqldump which is slow, locks tables, and is almost useless for large databases.

Posted in database | Leave a comment
Aug 21
2014

Scalable php server jobs with Gearman

The load on our servers are increasing, more and more users are joining us, and they all send updates on a regular basis. On every update we need to consider if we need to raise an alarm and send an email or sms. Recently we are starting to extend this functionality with customized rules that trigges alarms dependent on the users needs.

The alarm function is not in itself a heavy job,but summing up every request its loads the server heavily.

Here is what we did to distribute the load and make it scalable with Gearman:

Install http://gearman.org/ and use workers to handle the alarms. On every request we start a gearman worker after saving the updates from the client. The worker can easily extract and analyze the submitted data, and raise any alarms, send email/sms/api alerts in the background.

In this way we can add complex functionality without any performance troubles. Needing more computing power,  we just add servers until we are happy with the distribution of load.

Here is an overview of the Gearman framework, its not limited to PHP, but can be used with C, C++, Perl, Python etc. you name it.

Gearman stack

Posted in php | Leave a comment
Jan 29
2014

Magento redirects to old domain when domain is changed

If Magento redirects to your old domain, and you just changed the domain, you need to make a small modification to the Magento database.

Find the table core_config_data and insert your new domain to rows where path is web/unsecure/base_url and web/secure/base_url

You can also update your Magento database with these two queries:

UPDATE core_config_data SET value="http://www.newdomain.com/" 
WHERE path="web/unsecure/base_url"
 UPDATE core_config_data SET value="https://www.newdomain.com/" 
 WHERE path="web/secure/base_url"

Remember the trailing slash (‘/’) or content is not loaded properly.

That will fix the problem.

This is tested on Magento 1.8.1.0 but will likely work on all newer versions.

Posted in magento | Tagged , , | Leave a comment