Calculate Modular Exponentiation (PowerMod) in Javascript (a^p % n)

Computing PowerMod is required for implementing public-key cryptography as it is used to encrypt and decrypt data. Calculating it by regular arithmetic (i.e. calculating pth power of a) doesn’t work with large numbers used for encryption. That’s where PowerMod helps, it calculates the result by keeping the numbers significantly small and within range of the integer data type.

There are different algorithms for calculating PowerMod. Following is based on the pseudocode from Wikipedia, it is called ‘Right-to-left binary method’.

// calculates   base^exponent % modulus
function powerMod(base, exponent, modulus) {
    if (modulus === 1) return 0;
    var result = 1;
    base = base % modulus;
    while (exponent > 0) {
        if (exponent % 2 === 1)  //odd number
            result = (result * base) % modulus;
        exponent = exponent >> 1; //divide by 2
        base = (base * base) % modulus;
    return result;

Learn more at:

CodeSchool courses on JavaScript Frameworks

CodeSchool is a great site for introductory programming courses. With all guides and practice exercises available right within the browser, it is a hassle-free way to start learning. The design of the site is also very pleasing which enhances the overall experience.

Recently, CodeSchool offered a free weekend pass to all their content. I took the opportunity to brush up my skills on the new version of Angular and also quickly went through the React course.


Course Link: Accelerating through Angular 2



Course Link: Powering Up with React


Convert C# code to JavaScript

There are some tools which convert C# code to JavaScript. Obviously these will only be useful if your code is not tightly coupled with any C# library as the library won’t be available in JavaScript. These conversion tools may come handy for model classes or any other logic which is, for the most part, independent of .Net framework classes.

The main focus of these tools is not code conversion, rather they primarily try to do for C# what GWT does for Java. They allow you to write code in C#, access the DOM and JavaScript libraries to develop web applications. When the code is compiled, the output is not .Net Application but a web application written in JavaScript and HTML.


Conversion Tool Description
duocode duocode is a new tool on the block, currently in beta. It is going to be a commercial software but free to use as of now. duocode uses Microsoft Roslyn and support debugging C# code your compiled JavaScript is running in the browser. It supports pretty comprehensive set of C# feature including Await/Async, C# 6.0 language features, resource files etc.


SharpKit SharpKit is commercial (free for open source & small projects) as opposed to the alternatives listed below but does an excellent job of converting C# to JavaScript which is clean and readable. Moreover, it supports all features of C# language.You can see SharpKit live in action here. Give it a C# class or a snippet, it will convert it to JavaScript right there.Sharpkit compares itself with JSC, Script# and GWT at

Google Code:

JSIL JSIL is a compiler that transforms .NET applications and libraries from their native executable format (CIL bytecode) into standards-compliant, cross-browser JavaScript.There are quite a few demos on JSIL website, one of them allows you to convert C# code to JavaScript right in the browser (though the generated code looks a bit obscure).


JSC JSC is an Open Source cross-compiler where you can create web applications for Java, JavaScript, Flash ActionScript, PHP and Google App Engine using Microsoft .NET in C#, VB or F#. And also integrates into the .NET frameworks, Silverlight and ASP.NET.

Wiki & Tutorials:

Script# Script# is a compiler that generate the JavaScript instead of MSIL (Microsoft Intermediate Language) from C# source code like GWT (Google Web Toolkit).Script# supports a subset of language features, for instance enum and struct are not supported.

GitHub Repository:

Saltarelle Saltarelle is an open source C# to JavaScript compiler. Saltarelle is not an entire framework for web application development (such as GWT), rather it consists of a compiler and a small runtime library that only contains the things necessary for the language features to work. It is, however, designed with interoperability in mind so you can use any existing JavaScript framework with it. It comes with support for jQuery, and other libraries can be used either by authoring an import library (which is just a class / set of classes with special attributes on their members) or by using they ‘dynamic’ feature of C# 4.

Git Repository:

For a comprehensive list of tools that convert C# and other languages to JavaScript, visit

How to embed chrome browser in .net application

Chrome browser can be embedded in native application taking a hybrid approach to development where application is partly developed in native technology and partly in web. This has several advantages:

  • Salvage the existing investment in HTML/Javascript projects.
  • The part of application written in web technology is platform independent. This is very important due to variety of devices that are prevalent today.
  • Overcome the limitation of web technology and leverage the full power of the hardware through native code.

We need to know about three projects:

Project Type Description
Chromium Web Browser Chromium is the open source browser project which is behind Google Chrome.
Web Browser Control Based on Chromium project, an open source embed-able Web Browser Control is developed called ChromiumEmbedded. ChromiumEmbedded supports programming interface for C++.
Xilium.CefGlue .Net Wrapper Quite a few wrappers are available to make ChromiumEmbedded accessible in other languages. The most updated and active wrapper for .Net is Xilium.CefGlue.

Follow the below steps to embed Chromium Web Browser control in your .Net application.

  1. Download the source code for Xilium from here (Click on the Branches tab and download the Master branch).
  2. Check for the current ChromiumEmbedded (CEF) build supported by Xilium here. As of now, the ChromiumEmbedded supported build is CEF 3.1547.1448. Download the relevant pre build binaries from here.
  3. Unzip the two downloaded files. Copy all DLLs from the Release folder of ChromuimEmbedded and place it in Debug folder of CefGlue.Demo.WinForms project in Xilium source code.
  4. Copy the locales folder under Resources from ChromuimEmbedded and place it in the Debug folder of CefGlue.Demo.WinForms project in Xilium source code.
  5. Open the Xilium source code in Visual Studio using Xilium.CefGlue.sln. Set CefGlue.Demo.WinForms as the startup project.
  6. Build and run the solution and you will get the demo .Net application running with embedded chromium browser.

Completed ‘HTML5 Game Development’ course from Udacity

I have been taking Massively Open Online Courses at Coursera, but this is the first one at Udacity. The two platforms take different approaches… Coursera is more like a virtual classroom while Udacity feels like a very well designed interactive tutorial. Udacity is self-paced with very short video clips interspersed with questions and quick programming assignments. On the other hand, courses at Coursera start at specific dates and the assignments have deadlines. All students of a batch enroll in the class at the same time and discuss same topics on forums as the course progresses. This gives a more real university classroom like structure to Coursera.

The ‘HTML5 Game Development’ course teaches basic skills to get started with 2D game development. It does that by going through techniques and tools that were used to develop GRITS. GRITS can be downloaded from here. The overall quality of the course could be improved, particularly the grader bugs in some of the assignments were frustrating. Still I had a good time, mainly because Udacity as a platform is great and going by the posts in discussion forums, rest of the computer science courses at Udacity have excellent content. I would be hooked on Udacity for some time 🙂

Following are the tools used or recommended during the course:

Creating Sprite Sheets
Map Editor (Tiled)
Physics Engine
Audio Editor

HTML5 Game Development - Udacity-page-001

Passed Microsoft Exam 70-480: Programming in HTML5 with JavaScript and CSS3

There is huge interest in HTML 5 among the web development community these days. Once again there is hope that we can ‘write once, and run anywhere’ with the latest version of HTML. The ability to run your code anywhere is very significant considering the plethora of devices (from smart phone to TVs) that we use to access internet these days. HTML 5 is supposed to eliminate the need for browser plugins and to cut into the share of native applications.

I haven’t got to use HTML 5 yet in any of the projects but I have been closely following the development in this regard. Today I took the Microsoft exam for HTML 5 after going through couple of resources for preparation.

I enjoyed the format of the exam as it require you to complete code snippets, drag and drop code lines etc. in addition to the usual multiple choice questions.