{"action":"created","issue":{"url":"https://api.github.com/repos/jedisct1/libsodium/issues/233","labels_url":"https://api.github.com/repos/jedisct1/libsodium/issues/233/labels{/name}","comments_url":"https://api.github.com/repos/jedisct1/libsodium/issues/233/comments","events_url":"https://api.github.com/repos/jedisct1/libsodium/issues/233/events","html_url":"https://github.com/jedisct1/libsodium/pull/233","id":54016152,"number":233,"title":"Modular JS wrapper","user":{"login":"BatikhSouri","id":3635680,"avatar_url":"https://avatars.githubusercontent.com/u/3635680?v=3","gravatar_id":"","url":"https://api.github.com/users/BatikhSouri","html_url":"https://github.com/BatikhSouri","followers_url":"https://api.github.com/users/BatikhSouri/followers","following_url":"https://api.github.com/users/BatikhSouri/following{/other_user}","gists_url":"https://api.github.com/users/BatikhSouri/gists{/gist_id}","starred_url":"https://api.github.com/users/BatikhSouri/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/BatikhSouri/subscriptions","organizations_url":"https://api.github.com/users/BatikhSouri/orgs","repos_url":"https://api.github.com/users/BatikhSouri/repos","events_url":"https://api.github.com/users/BatikhSouri/events{/privacy}","received_events_url":"https://api.github.com/users/BatikhSouri/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-01-12T02:48:35Z","updated_at":"2015-01-12T13:22:44Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/jedisct1/libsodium/pulls/233","html_url":"https://github.com/jedisct1/libsodium/pull/233","diff_url":"https://github.com/jedisct1/libsodium/pull/233.diff","patch_url":"https://github.com/jedisct1/libsodium/pull/233.patch"},"body":"Hey @jedisct1,\r\n\r\nI'm proposing a first version functional version of the JavaScript wrapper we talked about a while ago. To sum up, here is what you can find in this pull request:\r\n\r\n* I've completed the JS wrapping for a rather [wide range](https://github.com/LockateMe/libsodium/tree/master/dist-build/js-build/symbols) of methods of the library.\r\n* I've updated [emscripen.sh](https://github.com/LockateMe/libsodium/blob/master/dist-build/emscripten.sh) to let it build the JS wrapper after emscripten and its testing are finished.\r\n* I've built a [web page](https://github.com/LockateMe/libsodium/blob/master/test/js/index.html) that lets you test the following set of algorithms:\r\n  * Curve25519\r\n  * Ed25519\r\n  * Ed25519->Curve25519 transition\r\n  * CryptoBox\r\n  * CryptoSecretBox\r\n  * Scrypt\r\n  * Hash (SHA256 & SHA512)\r\n  * HMAC (SHA256, SHA512, and the default crypto_auth with SHA512/256)\r\n* For all the algorithms listed above, the test procedure also runs on [the test vectors](https://github.com/LockateMe/libsodium/blob/master/test/js/test.js) (harvested from the C version)\r\n\r\nI hope it suits your expectations and that it will not be too much of a hassle to merge. (A priori, aside the changes in `emscripten.sh`, I don't see where it can go wrong).\r\n\r\nI tried to make it easier to use and keep it multi-platform (as it's a fundamental goal of libsodium). Tell me if there is a problem.\r\n\r\nPS: For proper attribution, I'd like to add that I probably wouldn't have been able to build this stuff without looking at @salemware input on #178 and @CryptoNoob's version in #211 (FYI it opened my eyes to the fact that C's `long long` parameters are \"trans-typed\" as 2 Numbers; since JS numbers cannot hold 64 bit integers)."},"comment":{"url":"https://api.github.com/repos/jedisct1/libsodium/issues/comments/69568416","html_url":"https://github.com/jedisct1/libsodium/pull/233#issuecomment-69568416","issue_url":"https://api.github.com/repos/jedisct1/libsodium/issues/233","id":69568416,"user":{"login":"jedisct1","id":124872,"avatar_url":"https://avatars.githubusercontent.com/u/124872?v=3","gravatar_id":"","url":"https://api.github.com/users/jedisct1","html_url":"https://github.com/jedisct1","followers_url":"https://api.github.com/users/jedisct1/followers","following_url":"https://api.github.com/users/jedisct1/following{/other_user}","gists_url":"https://api.github.com/users/jedisct1/gists{/gist_id}","starred_url":"https://api.github.com/users/jedisct1/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jedisct1/subscriptions","organizations_url":"https://api.github.com/users/jedisct1/orgs","repos_url":"https://api.github.com/users/jedisct1/repos","events_url":"https://api.github.com/users/jedisct1/events{/privacy}","received_events_url":"https://api.github.com/users/jedisct1/received_events","type":"User","site_admin":false},"created_at":"2015-01-12T13:22:44Z","updated_at":"2015-01-12T13:22:44Z","body":"Awesome work, Batikh! :+1: \r\n\r\nA rebased version is available in the `libsodium.js` branch in my repository.\r\n\r\nWe need to make sure that the library compiles with Emscripten. Such a target is also interesting to spot bugs that native code doesn't easily trigger.\r\n\r\nBut I am not sure that higher-level wrappers (which are required, or else using the library in Javascript is painful) should be part of the same repository.\r\nI'd rather suggest a distinct repository, with the libsodium repository loaded as a submodule. The Javascript repository can even contain precompiled javascript files that people can copy and use directly.\r\n\r\nSome remarks about the code:\r\n\r\n- `injectBytes()` is allocating memory on the heap. Output buffers are free()d when converted, but other buffers are not, leading to memory leaks until `free_all()` is manually called. Users can call `free_all()` after each call to libsodium, but that a bit annoying, and it gets worse when an exception is thrown. I'm not sure what the best solution to address this is, but maybe the wrappers should always free everything they allocate using `malloc`.\r\n- `uint` cannot be `0`. Maybe it should. For example the key length in `crypto_generichash` can be zero.\r\n- `leftPadding` in `injectBytes()` is not required. libsodium doesn't require left padding, and that was really only designed to be used with pointers in C.\r\n- `to_hex`/`from_hex`: maybe use the libsodium functions (`sodium_bin2hex` and `sodium_hex2bin`) instead. `bin2hex` is constant time and `hex2bin` is more flexible.\r\n- Maybe the `encode_utf8` and `encode_latin1` function names could describe a bit more what the functions do. How about a single `string_to_uint8array` function instead?"}}