3

I want to create an HOTP client using javascript similar to SpeakEasy

The above library is intended for server side javascript usage and it uses NodeJS.

I want to do the same thing on front end javascript in a browser but I haven't been able to use CryptoJS to achieve this behavior.

 var key = "abc"; var counter = "123"; // create an octet array from the counter var octet_array = new Array(8); var counter_temp = counter; for (var i = 0; i < 8; i++) { var i_from_right = 7 - i; // mask 255 over number to get last 8 octet_array[i_from_right] = counter_temp & 255; // shift 8 and get ready to loop over the next batch of 8 counter_temp = counter_temp >> 8; } // There is no such class called as Buffer on Browsers (its node js) var counter_buffer = new Buffer(octet_array); var hash = CryptoJS.HmacSHA1(key,counter_buffer); document.write("hex value "+ hash); document.write("hash value "+ CryptoJS.enc.Hex.stringify(hash)); 

I know this is possible on a native platform like java (android) or objective c (ios) Here is the corresponding implementation HOTP in Objective C but I doubt if it's possible to do on a web based front end.

Also, I highly doubt if such a thing is secure in browser because javascript is viewable from any browser. Any inputs suggestions would be useful. I am doing this for a POC. I am curious if anyone has used Hotp on web based platform.

5
  • Which hash do you want Commented Jun 29, 2015 at 19:32
  • @DrewPierce Ideally I would like to use sha512 Commented Jun 29, 2015 at 19:33
  • caligatio.github.io/jsSHA Commented Jun 29, 2015 at 19:35
  • @DrewPierce I have used it. However, the input to those functions are string. My input to hash function is not a string but binary data. So the results are not equivalent. Commented Jun 29, 2015 at 19:37
  • @DanielAranda my question is different than this. I want to know if doing this is feasible in terms of having hotp on a web based front end. Commented Jun 29, 2015 at 19:47

2 Answers 2

3

There is no such language that supports binary data strings in the code. You need to encode the binary data into some format such as Hex or Base64 and let CryptoJS decode it into it's own internal binary format which you then can pass to the various CryptoJS functions:

var wordArrayFromUtf = CryptoJS.enc.Utf8.parse("test"); var wordArrayFromHex = CryptoJS.enc.Hex.parse("74657374"); // "test" var wordArrayFromB64 = CryptoJS.enc.Base64.parse("dGVzdA=="); // "test" 

Other functions are:

wordArrayFromHex.toString(CryptoJS.enc.Utf8) // "test" CryptoJS.enc.Utf8.stringify(wordArrayFromB64) // "test" 

If you pass a string into a CrypoJS function (not these here), it will be assumed to be a Utf8-encoded string. If you don't want that, you need to decode it yourself.

Sign up to request clarification or add additional context in comments.

Comments

1

The code at http://caligatio.github.io/jsSHA/ works fine for SHA-512.

Drop the .js files, look in their test/test.html at line 515. It might look like a string to you but it is binary hex.

So their input is binary which is unmistaken. Don't get hung up on the fact it is sitting in a big string.

2 Comments

Btw I am providing this to show client side without regard to keys baked into .js which of course is foolish. For the topic of security figure out where SSL figures in. SSL would remove this whole question to begin with.
there is a good reason why you see this on node and not client side. It is a server side typical use, say php or nodeJs. For client side one would use servers public key of pki (think rsa). Only server can decrypt using pki private key. Or one uses SSL/TLS connections and doesn't re-invent the wheel.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.