{"id":22780196,"url":"https://github.com/sitegui/pbkdf-sha256-asm","last_synced_at":"2025-03-30T14:16:04.295Z","repository":{"id":18939045,"uuid":"22158744","full_name":"sitegui/pbkdf-sha256-asm","owner":"sitegui","description":"SHA256, HMAC-SHA256 and PBKDF-SHA256 in JavaScript asm.js","archived":false,"fork":false,"pushed_at":"2014-09-07T07:52:20.000Z","size":892,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-05T15:53:59.087Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sitegui.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-07-23T15:56:18.000Z","updated_at":"2018-12-24T01:39:59.000Z","dependencies_parsed_at":"2022-08-02T15:00:16.633Z","dependency_job_id":null,"html_url":"https://github.com/sitegui/pbkdf-sha256-asm","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sitegui%2Fpbkdf-sha256-asm","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sitegui%2Fpbkdf-sha256-asm/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sitegui%2Fpbkdf-sha256-asm/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sitegui%2Fpbkdf-sha256-asm/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sitegui","download_url":"https://codeload.github.com/sitegui/pbkdf-sha256-asm/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246326783,"owners_count":20759439,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-12-11T20:12:30.226Z","updated_at":"2025-03-30T14:16:04.277Z","avatar_url":"https://github.com/sitegui.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# PBKDF-SHA256 in asm.js\n\nMy first attempt to compile C to JavaScript :)\n\nThis is my own implementation of three related crypto algorithms:\n\n* SHA-256, based on [FIPS 180-4](http://csrc.nist.gov/publications/fips/fips180-4/fips-180-4.pdf)\n* HMAC with H=SHA256, based on [FIPS 198-1](http://csrc.nist.gov/publications/fips/fips198-1/FIPS-198-1_final.pdf)\n* PBKDFv2 with H=SHA256, based on [NIST SP800-132](http://csrc.nist.gov/publications/nistpubs/800-132/nist-sp800-132.pdf)\n\n**NOTE**: this is a toy project and should not be taken too serious, i.e. don't use this implementation in critical production code. There are much more robust crypto libs out there!\n\n## Benchmark\nA small benchmark comparing this implementation to [CryptoJS](https://code.google.com/p/crypto-js/), a crypto lib written directly in JS.\n\nThe time that it took to execute 5000 rounds of PBKDF over input `message='password', salt='salt'` was measured and is represented in the graph bellow in rounds per second. That is, what should be the value of the rounds parameter if one want the function to return in 1 second.\n\n![Graph](https://raw.githubusercontent.com/sitegui/pbkdf-sha256-asm/master/asm.png)\n\nOk, I may be comparing apples to oranges, since CryptoJS is a much broader lib and is not limited to only SHA256. But even though, the result is pretty clear: JS almost as fast as C!\n\nJust for the record, this was my CryptoJS bench code:\n```javascript\nvar pbkdf = CryptoJS.algo.PBKDF2.create({\n\t\tkeySize: 256 / 32,\n\t\thasher: CryptoJS.algo.SHA256,\n\t\titerations: 5000\n\t}),\n\tpassword = CryptoJS.enc.Utf8.parse('password'),\n\tsalt = CryptoJS.enc.Utf8.parse('salt'),\n\tthen = Date.now(),\n\tkey = pbkdf.compute(password, salt).toString(),\n\tnow = Date.now(),\n\tdt = now - then\nif (key === '8fc2bcffbb4b1ac9b9de03588d390f3d9bf336c2c4422c90c158cc714225f629') {\n\tconsole.log('Took ' + dt + ' ms, ' + Math.round(5e6 / dt) + ' rounds/s')\n}\n```\n\n## Using\nInclude the file `js/pbkdf_min.js` and bind the functions with:\n```\nvar sha = Module.cwrap('sha_simple', 'string', ['string'])\nvar hmac = Module.cwrap('hmac_simple', 'string', ['string', 'string'])\nvar pbkdf = Module.cwrap('pbkdf_simple', 'string', ['string', 'string', 'number', 'number'])\n```\n\nThis will set three functions. All them return a 64-char hex-encoded string and receive UTF8 strings:\n\n* sha(message: string) -\u003e string\n* hmac(key: string, message: string) -\u003e string\n* pbkdf(password: string, salt: string, blockIndex: int, rounds: int) -\u003e string\n\nUsing is simple as `alert(sha('my-great-message'))`\n\n[See a simple example](http://sitegui.github.io/pbkdf-sha256-asm)\n\n### Hex input\nEach one of the 3 functions have another version that takes a hex-string (like `'fa40fa'`) as input:\n```\nvar shaHex = Module.cwrap('sha_simple_hex', 'string', ['string'])\nvar hmacHex = Module.cwrap('hmac_simple_hex', 'string', ['string', 'string'])\nvar pbkdfHex = Module.cwrap('pbkdf_simple_hex', 'string', ['string', 'string', 'number', 'number'])\n```\n\n## Compiling\nFirst you need to install [emscripten](https://github.com/kripken/emscripten/wiki). When done, just run one of the commands bellow in the repository path:\n\n### SHA256\n\temcc c/sha.c c/util.c c/memory.c -o js/sha.js\n\temcc c/sha.c c/util.c c/memory.c -DNDEBUG -O2 --memory-init-file 0 -o js/sha_min.js\n\n### HMAC-SHA256\nThese already include SHA256\n\n\temcc c/hmac.c c/sha.c c/util.c c/memory.c -o js/hmac.js\n\temcc c/hmac.c c/sha.c c/util.c c/memory.c -DNDEBUG -O2 --memory-init-file 0 -o js/hmac_min.js\n\n### PBKDF2-SHA256\nThese already include both HMAC and SHA256\n\n\temcc c/pbkdf.c c/hmac.c c/sha.c c/util.c c/memory.c -o js/pbkdf.js\n\temcc c/pbkdf.c c/hmac.c c/sha.c c/util.c c/memory.c -DNDEBUG -O2 --memory-init-file 0 -o js/pbkdf_min.js\n\n### Tests\n\temcc c/main.c c/test/hmac.c c/test/pbkdf.c c/test/sha.c c/test/util.c c/pbkdf.c c/hmac.c c/sha.c c/util.c c/memory.c -o js/test/test.html\n\temcc c/main.c c/test/hmac.c c/test/pbkdf.c c/test/sha.c c/test/util.c c/pbkdf.c c/hmac.c c/sha.c c/util.c c/memory.c -DNDEBUG -O2 --memory-init-file 0 -o js/test/test_min.html\nAnd then just open the .html page. It should log some 'OK!' messages in the console and also the benchmark result.\nThe `NDEBUG` flag turns off memory checks that I've used to make sure all allocated memory was freed correctly.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsitegui%2Fpbkdf-sha256-asm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsitegui%2Fpbkdf-sha256-asm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsitegui%2Fpbkdf-sha256-asm/lists"}