Artem's blog

Mainly .NET (C#, ASP.NET) and my projects

Hash code generator

Dear Readers,

The last time I have spent on the development of SKGL API, which is a library for those of you who want to secure, protect your hard-coded .NET application. I meanwhile, I have also spent sometime on a code, (basically half an hour), that produces hash code. It is not the best way of doing it, however, so it would be a pleasure to see another version of this code. Much appreciated!

        static string twentyfiveByteHash (string s)
        {
            int amountOfBlocks = s.Length / 5;
            string[] preHash = new string[amountOfBlocks];

            if (s.Length <= 5)
            {
                //if the input string is shorter than 5, no need of blocks!
                preHash[0] = GetStableHash(s).ToString ();
            }
            else if (s.Length > 5)
            {
                //if the input is more than 5, there is a need of dividing it into blocks.
                for (int i = 0; i < amountOfBlocks-1; i++)
                {
                    preHash[i] = GetStableHash (s.Substring(i*5,5)).ToString ();
                }
                preHash[preHash.Length-1] = GetStableHash (s.Substring((preHash.Length-1) * 5, s.Length - (preHash.Length - 1) * 5)).ToString ();

            }
            return String.Join ("",preHash);
        }

        static public int GetStableHash(string s)
        {
            /*
             * modification of code from:
             * http://stackoverflow.com/questions/548158/fixed-length-numeric-hash-code-from-variable-length-string-in-c-sharp
             *
             * modified by Artem Los
             *
             */
            const int MUST_BE_LESS_OR_EQUAL_TO = 100000000;
            uint hash = 0;

            foreach (byte b in System.Text.Encoding.Unicode.GetBytes(s))
            {
                hash += b;
                hash += (hash << 10);
                hash ^= (hash >> 6);
            }

            hash += (hash << 3);
            hash ^= (hash >> 11);
            hash += (hash << 15);

            int result = (int)(hash  % MUST_BE_LESS_OR_EQUAL_TO);
            int check = MUST_BE_LESS_OR_EQUAL_TO / result;

            if (check > 1)
            {
                result *= check;
            }

            return result;
        }

Secure DotNet Applications with SKGL

SKGL – Serial Key Generating Library is an API that contains a serial key generating algorithm that helps you secure your application. This version is in BETA mode, but can still be used.  (Example is included)

SKGL might be downloaded

HTML Editor online [dev net]

In some of my previous post, I wrote about a HMTL editor available online. There are some new features:
http://editor.clizware.net/ (the editor itself)
http://editor.clizware.net/main/ (the list of all available examples)

The right modulo

Recently, I’ve published an article here about the modular operator, and its defect. I’m not quite sure if the content is  hundred pro cents right, though. The previous post was about an encryption in Java Script, where I’ve used a certain function to get the modulo.

Number.prototype.mod = function(n) {
return ((this%n)+n)%n;
}

The prototype above is solving the entire problem. By writing a number, ex, “5.mod(3)” you will perform an modular operation 5 mod 3. The code itself is from http://javascript.about.com/od/problemsolving/a/modulobug.htm where you also might read more about that particular issue.

To see the code in work, please view this example: http://editor.clizware.net/?id=7

Tip of this weekend (25/2-12)

Another good method to encrypt ASCII based text. To view this example in work, http://editor.clizware.net/?id=7 (remember to press compile)

//Copyright (C) 2012 Artem Los, All rights reserved

function mod_c(text,key,enc_dec)
{
	//only for 128 ascii values
	if (key == "")
	{
		return "Error: no key given";
	}
	if (enc_dec === true)
	{
		var result = "";

		for (var i=0; i < text.length; i++)
		{
			result += String.fromCharCode( (text.charCodeAt(i) + key.charCodeAt(i % key.length)).mod(128)    );
		}
		return result;
	}
	else if(enc_dec === false)
	{
		var result = "";

		for (var i=0; i < text.length; i++)
		{
			result += String.fromCharCode( (text.charCodeAt(i) - key.charCodeAt(i % key.length)).mod(128)  );
		}
		return result;
	}
	else
	{
		return "Error: enc_dec is not given";
	}
}

Number.prototype.mod = function(n) {
return ((this%n)+n)%n;
}

Investigating mod

Almost all languages have operators. Example of these are, “+” – addition, “-” – subtraction, “*” – multiplication, “/” division. There are also more advanced operators such as “Mod”, “Exponent”, and “And”. Let us investigate the mod.

I have seen that there are much misunderstanding about mod. Some claims that it performs a modulo operation, some claims that it is the reminder. Actually it is a reminder, and not a modulo operation.

In this investigation, I chosen Visual Basic as the default language. So, let us try to use mod operation as a modulo operation.

'default mod, "reminder"
MsgBox(3 - 4 Mod 6) ' result is -1
MsgBox(-1 + 4 Mod 6) ' result is 3

I would like to add that there is no problem to get the same answer back, i.e., 3 -4 mod 6 is -1, and then by adding 4 and again mod, will result 3. However, that is not the modulo. To display the modulo, use the function below:

   Function mod_r(ByVal x As Integer, ByVal y As Integer) As Integer
        'the "real" modulo operation
        Return x - y * Int(x / y)
    End Function

In arrays, tables, indexes, where the index should be positive, we can use the function above.

        MsgBox(mod_r(3 - 4, 6)) ' result is 5
        MsgBox(mod_r(5 + 4, 6)) ' result is 3

Visual Studio 2012 is comming

 Visual Studio will soon be upgraded to a new version, 2012. It will be more integrated into the new Windows 8 operating system, with all designing features available in Windows. For designers, it will be more easier to only use XAML as a language for the UI. XAML is also integrated into other designing products of Microsoft, especially in Expression Blend. Visual Studio’s IDE is also changing a lot. Now, you will have the same designer features for all supported languages, for example, C#, C++, VB. XAML designer is a new designing tool that should make it easier to synchronize Visual Studio with Expression Blend.

Source:
http://channel9.msdn.com/Events/BUILD/BUILD2011/TOOL-504T

.NET Languages, etc.

Currently, I am reading about the CLR (common language runtime), and I will briefly describe it.

CLR ease the programming process for all languages that target CLR. Languages like Visual Basic, Visual C#, Visual F#, et cetera, targets CLR. Actually, these languages are only a subset of those possibilities that are available in CLR. That means that you might not be available to express your programming ideas through only one language, or, that others won’t be able to use your work, simply because of that. For example, if you’ve created a dynamic link library in the language C#, which is case-sensitive, where the functions can be so similar that one capital letter will separate them, i.e., “static void Hello”, “static void hello” or “static void HELLO” (c#-static. vb- shared member) it will work fine inside the C# environment. But outside, ex, when the function is to be called outside that environment, for example through Visual Basic, it may cause an exception. Note, that is also because of that the member is set to static/shared. If the member would be private or only accessible inside that assembly, everything would work fine. A great tip for all .NET developers is to always write in CLS (common language specification). Just include this peace of code in your assembly:

[assembly:CLSCompliant(true)]

That will always check your code and it will report when it does not follow the CLS.

Private information lock

When you are working for instance, on a project that should be hidden from other eyes, it is a good idea to secure it. All that can be done by TrueCrypt.

TrueCrypt – is an encrypting software that provides you with all functions that are needed to secure your secret information. There are several main features, that are pretty useful:

  • Creating a virtual disc (a file that is your disc; where you can put other files.)
  • Encrypting entire partition, storage device such as USB, or hard drive.
  • Encrypting a partition that contains Windows. (pre-boot)
  • Creating a hidden volume.

(there are more features as well!)

After some few experiments, I would say that the best way to secure your files is by “Creating a virtual disc“. However, it depends on the purpose as well. If your entire computer is to be protected, it is better to “Encrypt the Windows partition” and all other internal discs on the computer. By doing this kind of encryption, you will be prompted to enter a secret password each time the Windows boots. The good thing is, if you get your computer stolen, the information will remain inaccessible for all except you. Correctly speaking, it will remain accessible for anyone with the password. If you suddenly forget the main password, you can say “bye” to the information of yours.

By creating a virtual drive, your information (folder system with all files) will remain safe in one big file (the size is assigned when creating it). When you will encrypt your entire drive (Windows partition) it is good to take a backup of all your information, because it might be something that causes data lose (eventually a bug).

There are also an opportunity to hide a volume. It is also a good advanced feature that is build into TrueCrypt.”The principle is that a TrueCrypt volume is created within another TrueCrypt volume (within the free space on the volume). Even when the outer volume is mounted, it should be impossible to prove whether there is a hidden volume within it or not*, because free space on any TrueCrypt volume is always filled with random data when the volume is created** and no part of the (dismounted) hidden volume can be distinguished from random data. Note that TrueCrypt does not modify the file system (information about free space, etc.) within the outer volume in any way.“, (read more).

TrueCrypt is free for use, and can be downloaded here. If you like it, please donate to keep this program active! http://www.truecrypt.org/

Spotify is growing fast

Spotify is a music streaming service that is available in a big part of Europe and USA. The software itself consist of different versions: a free – “Open”, an “Unlimited”, and “Premium”. One of the big differences in-between these version is the advertising. The Open version contains advertising and a limited amount of listening hours. The Unlimited version is as Premium, but without certain mobile features. (Read more here…).

As Financial Times reported, the amount of premium users has grown from 15% to 20% in Mars 2011, which means ca 2.5 million premium users.

More about spotify at spotify.com
Source: http://blogs.ft.com/fttechhub/2012/01/spotify-hits-3m-subscribers/#axzz1kbJWfv67