Click here to Skip to main content
5,786,882 members and growing! (20,881 online)
Email Password   helpLost your password?
Web Development » Ajax and Atlas » Libraries     Advanced License: The Code Project Open License (CPOL)

Extensions for the Microsoft AJAX Framework

By Ruben Buniatyan

Provides cryptographic services including secure encoding and decoding of data, as well as hashing and random number generation, and new methods for JavaScript native objects.
Javascript, XML, Windows, .NET, Visual Studio, WebForms, Ajax, ASP.NET, Dev

Posted: 8 Jul 2007
Updated: 9 Nov 2007
Views: 12,456
Bookmarked: 17 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
6 votes for this Article.
Popularity: 3.25 Rating: 4.18 out of 5
0 votes, 0.0%
1
0 votes, 0.0%
2
1 vote, 16.7%
3
3 votes, 50.0%
4
2 votes, 33.3%
5

Introduction

Here, I present a JavaScript library that extends the Microsoft AJAX Framework with new classes. Undoubtedly, the Microsoft AJAX Framework is great. However, it doesn't provide or emulate all the classes and functionality that the .NET Framework does. That's why I decided to extend the Microsoft AJAX Framework with some classes and methods that the .NET BCL provides, and which can be useful in a JavaScript environment.

Currently, the library includes three files: Sys.Core.js, Sys.Text.js, and Sys.Crypto.js.

  • Similar to .NET 3.5 that provides additional classes through the new System.Core.dll, Sys.Core.js provides additional methods for JavaScript native objects and some new classes like Sys.Convert.
  • Sys.Text.js contains classes representing ASCII, Unicode, UTF-7, UTF-8, and UTF-32 character encoding. These classes are helpful for data encoding or decoding and in Cryptography services.
  • Sys.Crypto.js provides cryptographic services including secure encoding and decoding of data, as well as hashing and random number generation, like the System.Security.Cryptography namespace in the .NET Framework.

In this article, I will describe how to use the cryptographic services this library provides.

Let's compare the most current and popular implementation of MD5 - Paul Johnston's implementation in JavaScript, with the one we have in this library. First, Johnston's implementation requires string as an input. What does this mean? This means, you cannot use any encoding you want. For example, if you hash some non-ASCII string with Johnston's implementation and compare it with the hash computed with .NET's widely-known FormsAuthentication.HashPasswordForStoringInConfigFile method, you'll see they do not match. Why? Because, the HashPasswordForStoringInConfigFile method uses UTF-8 that Johnston's implementation is unable to provide. A cryptographic algorithm should not care about strings and encodings. It should work only with bytes, like .NET works. Next is the performance. The Sys.Crypto.MD5CryptoServiceProvider class works about 6 - 8 times faster than Johnston's one (much here depends on the browser).

Let's see how to use the class mentioned above.

Using the code

Using the MD5 algorithm:

var buff = Sys.Text.Encoding.UTF8.getBytes("abc");
var md5 = Sys.Crypto.MD5.create();
var hash = md5.computeHash(buff);

window.alert(Sys.Convert.toBase64String(hash));

Compared with C# code:

byte[] buff = System.Text.Encoding.UTF8.GetBytes("abc");
MD5 md5 = System.Security.Cryptography.MD5.Create();
byte[] hash = md5.ComputeHash(buff);

Console.WriteLine(System.Convert.ToBase64String(hash));

The Sys.Crypto namespace provides classes for the following algorithms: MD5, SHA1, HMAC, Rijndael (AES). Let's see how to use them.

SHA1 algorithm:

var buff = Sys.Text.Encoding.UTF7.getBytes("abc");
var sha1= Sys.Crypto.SHA1.create();
var hash = sha1.computeHash(buff);

window.alert(Sys.Convert.toBase64String(hash));

HMAC algorithm:

var hmac = new Sys.Crypto.HMAC("SHA1");
// currently supported SHA1 and MD5

var key = Sys.Text.Encoding.BigEndianUnicode.getBytes("Key to mix");

hmac.set_key(key);
// if key is not provided, a random genereted key will be used


var buffer = Sys.Text.Encoding.BigEndianUnicode.getBytes("Hello World!");
var hash = hmac.computeHash(buffer);

window.alert(Sys.Convert.toBase64String(hash));

AES algorithm:

var aes = new Sys.Crypto.Aes.create();

// encrypting

var aesEnc = aes.createEncryptor();
var buffer = Sys.Text.Encoding.ASCII.getBytes("Hello World!");
var encrypted = aesEnc.transform(buffer);

window.alert(Sys.Convert.toBase64String(encrypted));

// decrypting

var aesDec = aes.createDecryptor();
var decrypted = aesDec.transform(encrypted);

window.alert(Sys.Text.Encoding.ASCII.getString(decrypted));

The Sys.Text namespace classes, now, are fixed according to Microsoft KB940521 (security bulletin MS07-040), except the UTF7Encoding class which will be fixed soon.

Here, I introduce the Sys.Crypto namespace in a nutshell. For complete documentation of this namespace and its base and abstract classes (not mentioned here), see the attached files. Actually, currently, there is no any documentation for the classes in Sys.Core.js. It'll be ready soon.

In the near future, I plan to release XML and Drawing classes.

Any feedback, suggestions, performance improvements, or critics will be welcome and appreciated.

Aslo available at CodePlex.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Ruben Buniatyan



Occupation: Web Developer
Location: Armenia Armenia

Other popular Ajax and Atlas articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 11 of 11 (Total in Forum: 11) (Refresh)FirstPrevNext
RantAES padding bugmemberScottless_guy15:50 10 Nov '08  
GeneralRe: AES padding bug [modified]memberRuben Buniatyan2:01 11 Nov '08  
QuestionHow does this really work?memberSmboInc7:06 17 Jul '07  
AnswerRe: How does this really work?memberRuben Buniatyan23:53 17 Jul '07  
GeneralGood workmemberSimone Busoli2:34 11 Jul '07  
GeneralRe: Good workmemberRuben Buniatyan3:49 11 Jul '07  
GeneralRe: Good workmemberChristopher Pietschmann, MCSD, MCAD12:52 7 Nov '07  
GeneralRe: Good workmemberRuben Buniatyan5:16 8 Nov '07  
GeneralExtending the "Sys" namespace considered badmemberhexy22:55 8 Jul '07  
GeneralRe: Extending the "Sys" namespace considered badmemberRuben Buniatyan4:01 11 Jul '07  
GeneralRe: Extending the "Sys" namespace considered badmemberSecrets21:43 16 Jul '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 9 Nov 2007
Editor: Smitha Vijayan
Copyright 2007 by Ruben Buniatyan
Everything else Copyright © CodeProject, 1999-2009
Web15 | Advertise on the Code Project