MD5 ing is currently quite tedious

0 views
Skip to first unread message

Eitan Burcat

unread,
Sep 1, 2008, 6:00:01 PM9/1/08
to ubiquity-firefox
I'm trying to authenticate a call to Remember The Milk's API.
(MD5/Sha1 is a common issue with any authentication scheme these
days...).

MD5ing is currently quite tedious in a Ubiquity script.

In order to call an MD5 function, I need to either:
1) Find a library I can already access from within a ubi script.
Anyone knows something like this??
2) Copy an existing MD5 function into my script. This code is usually
quite long (couple hundreds lines of code), i.e here:
http://pajhome.org.uk/crypt/md5/md5src.html - and would be problematic
for people to skim and trust.
3) Add an MD5 function to Utils / CmdUtils? Sounds like CmdUtils will
soon become a junk-yard of such functions...
4) Call an API on the net for this - Bad idea. Very slow, inefficient,
and insecure. But can be coded with 3 lines of code which is cool :).
5) Is there a thought about adding packages and namespaces for such a
case?

Luca Tettamanti

unread,
Sep 1, 2008, 6:36:57 PM9/1/08
to ubiquity...@googlegroups.com
On Tue, Sep 2, 2008 at 12:00 AM, Eitan Burcat <bur...@gmail.com> wrote:
>
> I'm trying to authenticate a call to Remember The Milk's API.
> (MD5/Sha1 is a common issue with any authentication scheme these
> days...).
>
> MD5ing is currently quite tedious in a Ubiquity script.
[...]

NSS already has all the machinery you want (and much more); I'm not
sure you can access it directly via JS though...
It should be easy to write an OO interface (as in "IDL") for the most
common tasks and write a (portable!) implementation in native code.

Luca

Arvid Jakobsson

unread,
Sep 1, 2008, 6:53:13 PM9/1/08
to ubiquity...@googlegroups.com

Blair McBride

unread,
Sep 1, 2008, 7:08:55 PM9/1/08
to ubiquity...@googlegroups.com
I was *just* about to post that link to nsICryptoHash. The Herd command
for Ubiquity uses SHA1 hashing when it submits commands to the Herd. So
you could use that as an additional example:
https://labs.toolness.com/ubiquity-herd/command-feed/code/

If commands are going to be needing to use hashing functions, it may be
an idea for Ubiquity to provide an easier way to do so.

- Blair

Luca Tettamanti

unread,
Sep 2, 2008, 8:50:32 AM9/2/08
to ubiquity...@googlegroups.com
On Tue, Sep 2, 2008 at 1:08 AM, Blair McBride <unfo...@gmail.com> wrote:
> I was *just* about to post that link to nsICryptoHash.

Ah, I wasn't aware of this interface :)

> If commands are going to be needing to use hashing functions, it may be
> an idea for Ubiquity to provide an easier way to do so.

Something like this?

const Cc = Components.classes;
const Ci = Components.interfaces;

function Hash(algo) {
if (algo != 'md5' && algo != 'sha1' && algo != 'sha256' &&
algo != 'sha384' && algo != 'sha512')
throw new Error('Invalid hash algorithm: ' + algo);
this._ch = Cc["@mozilla.org/security/hash;1"].createInstance(Ci.nsICryptoHash);
this._algo = algo;
}

Hash.prototype = {
get _unicodeConverter() {
/* Singleton */
var converter = Cc['@mozilla.org/intl/scriptableunicodeconverter'].
createInstance(Ci.nsIScriptableUnicodeConverter);
converter.charset = 'UTF-8';
this.__defineGetter__('_unicodeConverter', function() converter);
return converter;
},

_toHexString: function(c) ('0' + c.toString(16)).slice(-2),

compute: function(str) {
// result is an out parameter,
// result.value will contain the array length
var result = {};
// data is an array of bytes
var data = this._unicodeConverter.convertToByteArray(str, result);
this._ch.initWithString(this._algo);
this._ch.update(data, data.length);
var hash = this._ch.finish(false);

// convert the binary hash data to a hex string.
var hexHash = [this._toHexString(hash.charCodeAt(i)) for (i in
hash)].join("");

return hexHash;
}
}

MD5 = function() {}
MD5.prototype = new Hash('md5');
SHA1 = function() {}
SHA1.prototype = new Hash('sha1');
SHA256 = function() {}
SHA256.prototype = new Hash('sha256');
SHA384 = function() {}
SHA384.prototype = new Hash('sha384');
SHA512 = function() {}
SHA512.prototype = new Hash('sha512');

L

Reply all
Reply to author
Forward
0 new messages