Konda.eu

Blog

No comments

C# - Dependency Injections and Automatic Registration with Autofac

Dependency Injections are an awesome design pattern for adding a layer of abstraction into your application for various services and providers. Those can be quickly changed by registering another class and an instance is simply acquired by specifying desired interface as a parameter in constructor therefore decreasing or completely removing factory design pattern.

The only thing I find disturbing is whenever a new dependent class is added it must be registered with dependency injection provider, manual by default. As I consider everything that must be changed or added on a completely different place bad practice, I help myself with the following snippet of code:

var builder = new Autofac.ContainerBuilder();
var assembly = Assembly.GetExecutingAssembly();
var assemblyTypes = assembly.GetTypes();

foreach (var type in assemblyTypes)
{
    // Ignore interfaces
    if (type.IsInterface)
        continue;

    var typeInterfaces = type.GetInterfaces();

    // Class should implement IDependecy or ISingletonDependency
    if (!typeInterfaces.Any(i => i.IsAssignableFrom(typeof(IDependency))))
        continue;

    var registration = builder.RegisterType(type).AsImplementedInterfaces();

    if (typeInterfaces.Any(i => i.IsAssignableFrom(typeof(ISingletonDependency))))
        registration.SingleInstance();
    else
        registration.InstancePerRequest();
}

Where IDependency and ISingletonDependency are:

public interface IDependency {}

public interface ISingletonDependency : IDependency {}
No comments

Java AES Decryption


Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /www/wp-content/plugins/latex/latex.php on line 47

Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /www/wp-content/plugins/latex/latex.php on line 49

A while ago I needed to do some AES decryption in Java.In order to use 128-bit AES key and IV should be exactly 16 characters long \(\) where 8 is a size of UTF-8 character).

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;


public class AESDemo {

    private static final String key = "Ma5(;8F:;2u%X[YW";
    private static final String IV = "*Z^L_=-**d^jbR*=";
    
    private static final String decrypt = "";
    
    public static void main(String[] args) throws Exception {
        SecretKeySpec s = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
        IvParameterSpec iv = new IvParameterSpec(IV.getBytes("UTF-8"));
        
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
        cipher.init(Cipher.DECRYPT_MODE, s, iv);
        
        byte[] original = cipher.doFinal(hexStringToByteArray(decrypt));
        System.out.println(new String(original));
    }
    
    public static byte[] hexStringToByteArray(String s) {
        int len = s.length();
        byte[] data = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
                                 + Character.digit(s.charAt(i+1), 16));
        }
        return data;
    }
}
No comments

C++ - OpenBLAS Matrix Multiplication


Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /www/wp-content/plugins/latex/latex.php on line 47

Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /www/wp-content/plugins/latex/latex.php on line 49

Matrices are extremely popular in many fields of computer science but many operations are slow, especially the useful ones like matrix multiplication where the complexity reaches \(\). There are of course algorithms to speed things up, but there are much faster ways that can fully utilize computer's hardware.

Every operation when doing matrix multiplication is independent which means it can be parallelized through multiple CPU cores or even put on a GPU if you want the best you can get. But sometimes just CPU is enough to avoid expensive copies between CPU and GPU and to reach speed ups up to 10 times. This is where OpenBLAS comes in.

Continue reading

No comments

Internet Explorer - Touch Actions Not Working

During recent development of a JavaScript based Windows Store App I came across an interesting problem. On the development PC (it isn't touch ready) everything worked perfectly until the app was installed on the test tablet. After the famous line:

It works on my machine

said a couple of times and a long night of searching what might be the solution, I came across this neat CSS snippet:

html { 
    -ms-touch-action: none; /* -ms- was dropped with IE11 */
}

The cause

After an examination of CSS touch-event, Internet Explorer behaves by the standard so we really can't blame it for this one (just this one). Automatic value (auto) lets user agent (the browser) decide what actions are most appropriate for specific elements, therefore taking control of touch events for better browsing experience. By setting the value to none, the browser lets us do whatever we want with it.

zoom
No comments

C++ - The value of ESP was not properly saved across a function call

Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call.  This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention.

Another run time error that is almost impossible to quickly identify and debug. It usually occurs when libraries are involved and use different calling conventions e.g. _stdcall instead of _cdecl or other. But sometimes compiler misses a thing or two and compiles something that it shouldn't, after all, compiler only need to know how many bits and bytes to move and what do to with them to translate your program to an executable or a library. Sometimes it lets you do stupid things...

Continue reading

zoom
No comments

C++ - everything to know about Time and Timers

For years, since 1983 when C++ was first released, developers were stuck with C time library, packed in ctime header file. It contains a couple of functions and precision up to a second sometimes just isn't enough. For everything else clock() should be used or OS (Operating System) provided function, which really breaks code portability. In C++11 things got much better, with Chrono module.

Continue reading

zoom
No comments

C++ Lambda Expressions

C++ is currently going through some extensive updates started with the new C++11 standard back in 2011. One of the exciting and very powerful new features are anonymous functions also knows as lambdas. You can simply write them inline in your source code and pass them around as function pointers. An example of sorting vector of ints in a descending order:

vector<int> vec = { 10, 1, 5, 6, 7, 3, 5 };

sort(vec.begin(), vec.end(), [](int x, int y) { return x < y; });

Continue reading