?

Log in

No account? Create an account
byte manipulation in Javascript = not fun - Greg [entries|archive|friends|userinfo]
Greg

[ website | gregstoll.com ]
[ userinfo | livejournal userinfo ]
[ archive | journal archive ]

Links
[Links:| * Homepage * Mobile apps (Windows Phone, Win8, Android, webOS) * Pictures * LJBackup * Same-sex marriage map * iTunesAnalysis * Where's lunch? ]

byte manipulation in Javascript = not fun [Feb. 24th, 2011|09:22 am]
Greg
[Tags|, , , ]
[Current Mood |irritatedirritated]

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.
LinkReply

Comments:
From: spchampion
2011-02-24 04:30 pm (UTC)
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?
(Reply) (Thread)
[User Picture]From: gregstoll
2011-02-24 04:33 pm (UTC)
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"...
(Reply) (Parent) (Thread)
From: spchampion
2011-02-24 10:01 pm (UTC)
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
)
(Reply) (Parent) (Thread)
[User Picture]From: gregstoll
2011-02-24 10:16 pm (UTC)
Hmm, so the same thing...maybe I'm just wrong :-)
(Reply) (Parent) (Thread)
From: spchampion
2011-02-25 08:20 pm (UTC)
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.
(Reply) (Parent) (Thread)
[User Picture]From: omega697
2011-02-24 07:35 pm (UTC)
+1 for 2-factor!
(Reply) (Thread)