I’ve been reading up on the OAuth spec. And for call signing one of the ways is by using HMAC-SHA1 as a hash function.
This is great and all - but since I need to call my own API from Javascript I needed a way to do this algorithm in Javascript. Well, off to Google I went.
After a few misses I found the script made by Paj which is located at http://pajhome.org.uk/crypt/md5/sha1src.html.
This is also where I got the MD5 algorithm when I needed one of those.
The next step was to add a Mootools wrapper for it, so I can do things like "some string".hmac_sha1("key");
For this I conjured up this little piece of code:
/*
* Mootools wrapper for the SHA1 functions.
* This adds String.sha1 and String.hmac_sha1
* Requires http://pajhome.org.uk/crypt/md5/sha1src.html
*/
String.extend({
sha1: function(output) {
if( output == 'base64' ) {
return b64_sha1(this);
} else if( output == 'hex' ) {
return hex_sha1(this);
} else {
return str_sha1(this);
}
},
hmac_sha1: function( key, output ) {
if( output == 'base64' ) {
return b64_hmac_sha1(key, this);
} else if( output == 'hex' ) {
return hex_hmac_sha1(key, this);
} else {
return str_hmac_sha1(key, this);
}
}
});
-fangel
March 16th, 2008 | JavaScript