byte manipulation in Javascript = not fun

Google’s two-factor authentication is a really good idea, so I’m trying to port the Google Authenticator to webOS. This involves calculating hash values, etc. in Javascript, which is a giant pain because Javascript knows nothing but signed 32-bit integers and 64-bit floating point datatypes. So every time I look at the sample Java code and it uses a byte[], that means I have to manually convert to the -128..127 range. And the sample code does some things like converting 4 bytes to an int, which is easy in Java but a pain in Javascript.

Anyway, finally I had the bright idea to actually compile the Java code so I could compare the results, because I generated at least twenty different codes trying various permutations of things, none of which were right.

Another thing I learned! If you return an error string from a constructor, this does essentially nothing. You still get a constructed object. So, if your library is doing this, there’s a 99% chance that no one’s going to notice without a lot of pain in suffering.

Adding to my troubles: I am involved in some heavy yak-shaving at work.

6 thoughts on “byte manipulation in Javascript = not fun”

  1. RE errors in constructors – isn’t that the whole point behind exceptions? Because the only thing that a constructor can and should return is a pointer/object reference?

    Like

    1. Yeah, after I thought about it it made sense. It just seems a little weird that you can return something from a constructor that has absolutely no effect. I’m pretty sure if you tried to do the same in a compiled language you’d get a compile-time error saying “whatever you’re trying to do, it isn’t going to do anything”…

      Like

      1. For what it’s worth, here’s PHP’s take on the world:

         [stantonc:~] stantonc% php -v PHP 5.3.3 (cli) (built: Aug 22 2010 19:41:55) Copyright (c) 1997-2010 The PHP Group Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies [stantonc:~] stantonc% php <?php class Foo { public function __construct() { $this->variable1 = "foo"; return "bar"; } } print_r(new Foo); ^D Foo Object ( [variable1] => foo ) 

        Like

      2. Actually, if I do print instead of print_r, I do get an error. Catchable fatal error: Object of class Foo could not be converted to string in – on line 8 print_r is for dumping any kind of data structures, whereas print should just print strings. But the bigger issue remains – the return statement in the constructor is totally ignored and unreported.

        Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s