There are too many standards, frameworks, APIs, applications designed for the same purpose but use different approaches to achieve the goal. In this post I'm gonna put together an implementation and a client-side reference.
Microsoft has released a great AJAX client-side javascript library. Unfortunately it lacks the implementation and doesn't duplicate the server-side API. Ruben Buniatyan took the first step to extend it with a Sys.Crypto and Sys.Text namespaces. I think this guy did a great job in defining basic interfaces and their implementation. I would like to take this chance and contribute some code of mine to it.
SHA256. I ported Angel Martin's and Paul Johnson's original code into Sys.Crypto namespace. Use it just the way you wound use MD5 class from the Ruben's extension:
var sha256 = Sys.Crypto.SHA256.create();
var digest = sha256.computeHash(Sys.Text.Encoding.UTF8.getBytes("message digest"))));
My implementation is available for download here.
PBKDF2. Password-Based Key Derivation Function 2. Original code by Pervez Anandam. I created an implementation derived from Sys.Crypto.HashAlgorithm, but there's a twist. The execution of the code may take significant time which may result is browser hanging if executed synchronously. To avoid this situation the execution is takes place asynchronously so computeHash function returns at once.
function completedCallback(hash, context) {
display_message2("My derived key is: " + utilsByteArrayToHex(hash));
}
function progressCallback(percentage, context) {
display_message2("Computed " + percentage + "%");
}
var f = Sys.Crypto.PBKDF2.create();
f.set_hashSize(256);
f.set_salt(Sys.Text.Encoding.UTF8.getBytes("my salt"));
f.set_interationCount(1000);
f.set_completedCallback(completedCallback);
f.set_progressCallback(progressCallback);
f.computeHash(Sys.Text.Encoding.UTF8.getBytes("Oleg Ignat"));
The code is available for download here.
I would like to enphasis that you need to download Ruben's extension in order to use my code. If you happen to find bugs don't hesitate to let me know.