|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionHere, 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.
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 Let's see how to use the class mentioned above. Using the codeUsing 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 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 Here, I introduce the 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.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||