XML for Secured Message Passing

Securely passing XML Documents between two parties requires that the receiver can find out:

  • who sent the message
  • whether the message has been tampered with

and the sender can guarantee that:

  • enough information is available so the receiver can verify who the sender is
  • the receiver can find out whether the message has been tampered with

These requirements can be satisfied if the document is digitally signed. However, using XML as a message format brings additional complications that must be handled. Consider the following two documents:

They represent the same information but in different way:

  • the attribute value is double quoted in the first document while it is single quoted in the second one
  • the empty element uses different syntax

This is a simple example. Think about all the other things that can differ. For example:

  • the order of the attributes
  • the document encoding
  • arbitrary whitespaces
  • new lines
  • how new lines are encoded

All these differences will cause documents having the same data result in different signatures.


XML Canonicalization to the Rescue

To avoid these situations, the XML document must be canonicalized so it is always represented in the same way no matter what the original document structure is. The canonicalization is done by following the rules defined in https://www.w3.org/TR/xml-c14n/. The following list is a sample of the rules just to show what they look like:

  • XML declaration is omitted
  • All characters are encoded to UTF-8
  • Empty element must be converted to start-end tag pair
  • Attribute values use double quotes
  • CDATA sections are replaced by its content only


To demonstrate XML canonicalization the Apache Santuario library will be used.

The following code uses the Canonicalizer class to convert the XML documents to have the same structure.

As mentioned early this is a simple example. The XML document can be much richer containing comments, namespaces, entity references etc. The canonicalization of these cannot be handled by just one mapping. That’s why there are different algorithms that can be specified when a Canonicalizer is instantiated. You can see the full documentation here or in the specification 

If you are curious what is the canonicalized form of your XMLs just give them a try following the example above.

LVM2: Extend file system

LVM2 refers to the userspace toolset that provide logical volume management facilities on Linux. It is reasonably backwards-compatible with the original LVM toolset.

Resizing volumes with the LVM2 toolset is easy. First you have to adjust your physical partition table using fdisk. Let’s say you have a 60GB physical disk and a 50GB physical partition on it.

You want to resize the partition to take the whole disk. To do that you delete the partition and create it again with the new geometry.

The highlighted rows show the commands issued to fdisk. After the partition table is rewritten reboot the system.

The new partition table should now look like this:

Now that the new partition is setup you can assign the new space to a PV (physical volume). You can check the physical volumes with the pvs command.

In this case you have only one PV (physical volume) of size 50GB. Since you know your partition is 60GB you can very easily resize the PV to take all space with the pvresize command.

As you can see now the PV is of size 60GB and there is 10GB of free space. You can now use the free space to resize an LV (logical volume) using the lvextend command.

The -r option tells lvextend to resize the underlying filesystem (e.g. ext4 or btrfs) along with the LV. The -l option tells lvextend to set the LV size in units of logical extents. In this case the argument to -l is +100%FREE which means that 100% of the free space will be used. In other words all free space will be added to the system/root LV. After lvextend returns the file system should be resized and operational without system reboot.

Java To JavaScript Cheat Sheet

Coming from the Java language and landing in the JavaScript realm can be kind of confusing experience. All you see is functions and thinking in objects can be hard if you are not familiar with the language idioms.

This post is an attempt to help overcome the initial struggle and can be used as a cheat sheets for Java developers who want to start writing in JavaScript.

Let’s start with something simple – how to create objects.

Object Creation

Java sample that declares a Book class and creates an instance of it

The Java code should be clear so no need to dwell on it. However let’s see the JavaScript code for the same thing.

JavaScript samples for creating and using a Book object

The easiest way is to define an Object literal (a.k.a inline object)

As you can see JavaScript does not have any notion of classes – you define objects instead.

What about creating several object that have the same properties and methods? One way is to place object literals in the code however this can be a bit frustrating. To avoid code duplication and accidental typos a function that creates such objects can be defined

There is a shortcut to this approach and it is to use constructor function:

The constructor function is the closest version of a class in JavaScript. It basically defines properties and methods.

Note that in this case the new operator is used to invoke the function. Also by convention the name of a constructor function starts with a capital letter.

There is still another way to create an object – by invoking the Object() constructor.

You can use this constructor to create generic object and attach properties and methods to it later.

Default values for properties and methods can be passed to the Object constructor:


Java sample – Render Rectangle and Square

Again the Java code should be clear so let’s see the JavaScript part.

JavaScript samples showing the dynamic nature of prototypal inheritance

Since JavaScript has no notion of classes the inheritance is implemented by using a prototype object. Basically each object has a prototype object that serves as a template and extenders inherit properties and methods from it. It might sound confusing at first but after running the following examples it makes sense.

Let’s start by defining a Rectangle object.

It has two properties. If this object is designed to be extendable all methods that can be inherited must be defined in the prototype object.

(Remember that each object has a prototype object)

The code above adds the render method to the prototype object thus any object that inherits Rectangle can override it.

Now let’s define another object – Square, that will extend Rectangle

As you can see it has only one argument and calls Rectangle ‘s constructor passing it.

Now If we call render()

We’ll see the following error:

To fix this we have to copy the prototype object from the parent object

Calling render() again will result in:

Now we can override the behaviour of renderfor Square objects:

And here is the full code:

Can you guess the output?


Java sample – private Rectangle properties

The Java code defines a Rectangle whose properties are not exposed and can be changed only through the resize method.

Let’s see how this can be achieved in JavaScript

JavaScript counterpart – function scoped variables

The code looks a bit stranger than the previous snippets so let’s start from the top. The expression

defines anonymous function and executes it. If the last () is omitted then only a function will be declared but not executed (if no variable points to it then the function cannot be called at later time).

The next important thing is this idiom:

This is an anonymous function that returns an empty object. Having in mind that the variables are scoped in a function this means that everything that is declared in the function and before the return statement will be visible to the inlined object and not visible to the callers of the function. Example:

This will log 10 in the console output. And this will log undefined

Hopefully the code we started with is more clear now. The next question is – What about having multiple objects with scoped state?

JavaScript sample – multiple objects with scoped state

As a last note – this approach is not restricted to variables only. Functions declared outside the return statement won’t be visible as well.

Git cheats

git --everything-is-local

Git is a great and very powerful tool. There are probably over a hundred command line options that allow you to take advantage of its features. Here are few interesting things you can do easily.

Restore a deleted file

To restore a file that has been deleted several commits ago you have to find the commit that deleted the file. Then checkout the version of the file that was before the delete commit. Suppose you have the following in your repository.

As you can see originally there was a file named main.c but at some point it got deleted by: 2626b9b. To restore the content of main.c you can find the last commit that changed it.

And checkout the previous version (denoted by the caret character: ^).

Of course you checkout the previous version because the last commit that changed the file is the one that deleted it.

Rename files and directories

Git is very smart when it comes to renaming files. You don’t actually have to tell git that you’re renaming a file, it will figure it out on its own. Here’s an example, first we rename the file with whatever tool we like.

At this point git knows that you deleted a file that was in the repository (Main.java) and created a new file that is still untracked (Launcher.java).

Then you can just add these changes to the staging area and git will know that you renamed the file because the content is the same.

Actually it will detect that you renamed a file even if you made changes to the content. In this case I also changed the name of the class to keep it a valid Java file.

As you can see git calculates a similarity index of 68% and correctly concludes this is a rename operation.

Revert commits

Sometimes you’ll need to revert the changes introduced by one or more commits. There are generally two approaches to do that. One is to change the existing git history and the other to create new commits. Changing history can lead to issues if the commits are shared (when other people have them in their repositories). So probably the easiest and safest way is to use the revert command to create new commits with the opposite changes. Lets look at the following history.

Commit 329d44c adds a main method.

You can create a new commit that has exactly the opposite changes.

Now the history contains a new commit that reverted the changes 329d44c introduced.