diff --git a/.eslintrc b/.eslintrc new file mode 100644 index 0000000..33bd34c --- /dev/null +++ b/.eslintrc @@ -0,0 +1,76 @@ +{ + "root": true, + + "extends": "@ljharb", + + "rules": { + "func-style": "off", + "no-magic-numbers": "off", + }, + + "overrides": [ + { + "files": "bin.js", + "extends": "@ljharb/eslint-config/node/0.4", + "rules": { + "func-style": "off", + }, + }, + { + "files": [ + "hash.js", + "sha.js", + "sha1.js", + "sha224.js", + "sha256.js", + "sha384.js", + "sha512.js", + "test/vectors.js", + ], + "rules": { + "no-underscore-dangle": "off", + }, + }, + { + "files": [ + "sha.js", + "sha1.js", + "sha224.js", + ], + "rules": { + "max-params": "off", + }, + }, + { + "files": [ + "sha256.js", + "sha512.js", + ], + "rules": { + "max-statements": "off", + }, + }, + { + "files": [ + "sha512.js", + ], + "rules": { + "new-cap": "warn", + "max-lines": "off", + "max-lines-per-function": "off", + }, + }, + { + "files": "hash.js", + "globals": { + "Uint8Array": false, + }, + }, + { + "files": "test/test.js", + "globals": { + "Uint16Array": false, + }, + }, + ], +} diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 0000000..4cd0974 --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1,12 @@ +# These are supported funding model platforms + +github: [ljharb] +patreon: # Replace with a single Patreon username +open_collective: # Replace with a single Open Collective username +ko_fi: # Replace with a single Ko-fi username +tidelift: npm/sha.js +community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry +liberapay: # Replace with a single Liberapay username +issuehunt: # Replace with a single IssueHunt username +otechie: # Replace with a single Otechie username +custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2'] diff --git a/.github/workflows/node-aught.yml b/.github/workflows/node-aught.yml new file mode 100644 index 0000000..50f9e36 --- /dev/null +++ b/.github/workflows/node-aught.yml @@ -0,0 +1,11 @@ +name: 'Tests: node.js < 10' + +on: [pull_request, push] + +jobs: + tests: + uses: ljharb/actions/.github/workflows/node.yml@main + with: + range: '>= 0.10 < 10' + type: minors + command: npm run tests-only diff --git a/.github/workflows/node-pretest.yml b/.github/workflows/node-pretest.yml new file mode 100644 index 0000000..765edf7 --- /dev/null +++ b/.github/workflows/node-pretest.yml @@ -0,0 +1,7 @@ +name: 'Tests: pretest/posttest' + +on: [pull_request, push] + +jobs: + tests: + uses: ljharb/actions/.github/workflows/pretest.yml@main diff --git a/.github/workflows/node-tens.yml b/.github/workflows/node-tens.yml new file mode 100644 index 0000000..7ac9c5b --- /dev/null +++ b/.github/workflows/node-tens.yml @@ -0,0 +1,14 @@ +name: 'Tests: node.js 10 - 20' + +on: [pull_request, push] + +permissions: + contents: read + +jobs: + tests: + uses: ljharb/actions/.github/workflows/node.yml@main + with: + range: '>= 10 < 20' + type: minors + command: npm run tests-only diff --git a/.github/workflows/node-twenties.yml b/.github/workflows/node-twenties.yml new file mode 100644 index 0000000..209e157 --- /dev/null +++ b/.github/workflows/node-twenties.yml @@ -0,0 +1,14 @@ +name: 'Tests: node.js >= 20' + +on: [pull_request, push] + +permissions: + contents: read + +jobs: + tests: + uses: ljharb/actions/.github/workflows/node.yml@main + with: + range: '>= 20' + type: minors + command: npm run tests-only diff --git a/.github/workflows/rebase.yml b/.github/workflows/rebase.yml new file mode 100644 index 0000000..b9e1712 --- /dev/null +++ b/.github/workflows/rebase.yml @@ -0,0 +1,9 @@ +name: Automatic Rebase + +on: [pull_request_target] + +jobs: + _: + uses: ljharb/actions/.github/workflows/rebase.yml@main + secrets: + token: ${{ secrets.GITHUB_TOKEN }} diff --git a/.github/workflows/require-allow-edits.yml b/.github/workflows/require-allow-edits.yml new file mode 100644 index 0000000..7b842f8 --- /dev/null +++ b/.github/workflows/require-allow-edits.yml @@ -0,0 +1,12 @@ +name: Require “Allow Edits” + +on: [pull_request_target] + +jobs: + _: + name: "Require “Allow Edits”" + + runs-on: ubuntu-latest + + steps: + - uses: ljharb/require-allow-edits@main diff --git a/.gitignore b/.gitignore index 93f1361..ca586cd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,9 @@ node_modules npm-debug.log + +# Only apps should have lockfiles +npm-shrinkwrap.json +package-lock.json +yarn.lock + +.npmignore diff --git a/.npmignore b/.npmignore index 2bfa6a4..5a51f76 100644 --- a/.npmignore +++ b/.npmignore @@ -1 +1,15 @@ -tests/ +# Rules from: /Users/ljharb/Dropbox/git/browserify/sha.js.git/.gitignore +node_modules +npm-debug.log + +# Only apps should have lockfiles +npm-shrinkwrap.json +package-lock.json +yarn.lock + +.npmignore + + +# npmignore - autogenerated +.github/workflows +.github diff --git a/.npmrc b/.npmrc new file mode 100644 index 0000000..eacea13 --- /dev/null +++ b/.npmrc @@ -0,0 +1,3 @@ +package-lock=false +allow-same-version=true +message=v%s diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 0b606eb..0000000 --- a/.travis.yml +++ /dev/null @@ -1,17 +0,0 @@ -sudo: false -os: - - linux -language: node_js -node_js: - - "4" - - "5" - - "6" - - "7" -env: - matrix: - - TEST_SUITE=unit -matrix: - include: - - node_js: "7" - env: TEST_SUITE=lint -script: npm run $TEST_SUITE diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..6fe1d94 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,423 @@ +# Changelog + +All notable changes to this project will be documented in this file. + +The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) +and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). + +## [v2.4.12](https://github.com/browserify/sha.js/compare/v2.4.11...v2.4.12) - 2025-07-01 + +### Commits + +- [eslint] switch to eslint [`7acadfb`](https://github.com/browserify/sha.js/commit/7acadfbd3abb558880212b20669fcb09e1aa1c58) +- [meta] add `auto-changelog` [`b46e711`](https://github.com/browserify/sha.js/commit/b46e7116ebeaa82f34bbf2d7494fff7ef46eab3e) +- [eslint] fix package.json indentation [`df9d521`](https://github.com/browserify/sha.js/commit/df9d521e16ddf55dc877c43c05706d43c057fad4) +- [Tests] migrate from travis to GHA [`c43c64a`](https://github.com/browserify/sha.js/commit/c43c64adc6d3607d470538df72338fc02e63bc24) +- [Fix] support multi-byte wide typed arrays [`f2a258e`](https://github.com/browserify/sha.js/commit/f2a258e9f2d0fcd113bfbaa49706e1ac0d979ba5) +- [meta] reorder package.json [`d8d77c0`](https://github.com/browserify/sha.js/commit/d8d77c0a729c99593e304047f9d4335b498fd9ed) +- [meta] add `npmignore` [`35aec35`](https://github.com/browserify/sha.js/commit/35aec35c667b606b2495be3e4186bbe977b9e087) +- [Tests] avoid console logs [`73e33ae`](https://github.com/browserify/sha.js/commit/73e33ae0ca6bca232627cac7473028e1d218f67e) +- [Tests] fix tests run in batch [`2629130`](https://github.com/browserify/sha.js/commit/262913006e94616c8cd245ef6bd61bc4410b29e3) +- [Tests] drop node requirement to 0.10 [`00c7f23`](https://github.com/browserify/sha.js/commit/00c7f234aa3bdbd427ffeb929bacbb05334eb3e9) +- [Dev Deps] update `buffer`, `hash-test-vectors`, `standard`, `tape`, `typedarray` [`92b5de5`](https://github.com/browserify/sha.js/commit/92b5de5f67472d9f18413d38ad5b9aba29ff4c22) +- [Tests] drop node requirement to v3 [`9b5eca8`](https://github.com/browserify/sha.js/commit/9b5eca80fd9bb21cf05bdf43ce42661f1bbafeaa) +- [meta] set engines to `>= 4` [`807084c`](https://github.com/browserify/sha.js/commit/807084c5c0f943459e89838252cafbd175b549b7) +- Only apps should have lockfiles [`c72789c`](https://github.com/browserify/sha.js/commit/c72789c7a129cf453d44008ba27a88b90ac7989b) +- [Deps] update `inherits`, `safe-buffer` [`5428cfc`](https://github.com/browserify/sha.js/commit/5428cfc6f7177ad1a41c837b9387308848db96de) +- [Dev Deps] update `@ljharb/eslint-config` [`2dbe0aa`](https://github.com/browserify/sha.js/commit/2dbe0aab419e90add5032c70c9663b8fc562adb8) +- update README to reflect LICENSE [`8938256`](https://github.com/browserify/sha.js/commit/8938256dbb2241a7c749e4a399dbaff48cbe8e95) +- [Dev Deps] add missing peer dep [`d528896`](https://github.com/browserify/sha.js/commit/d52889688ce524e63570f35e448635a29e6dd791) +- [Dev Deps] remove unused `buffer` dep [`94ca724`](https://github.com/browserify/sha.js/commit/94ca7247f467ef045f41d534708bf7c700e03828) + +## [v2.4.11](https://github.com/browserify/sha.js/compare/v2.4.10...v2.4.11) - 2018-03-20 + +### Merged + +- Project is bound by MIT AND BSD-3-Clause licenses. [`#55`](https://github.com/browserify/sha.js/pull/55) + +## [v2.4.10](https://github.com/browserify/sha.js/compare/v2.4.9...v2.4.10) - 2018-01-22 + +### Merged + +- Modified greater than uint32 bits data test [`#53`](https://github.com/browserify/sha.js/pull/53) +- convert lowBits to unsigned in hash.js [`#51`](https://github.com/browserify/sha.js/pull/51) + +### Commits + +- Simplify bigData allocation [`107141a`](https://github.com/browserify/sha.js/commit/107141ac2c4ca61538e4ad9622cd0c2e21d38095) +- Modified large file test [`9d037bd`](https://github.com/browserify/sha.js/commit/9d037bd51e84d0d77aa56bb94ed2af2b436d9d66) + +## [v2.4.9](https://github.com/browserify/sha.js/compare/v2.4.8...v2.4.9) - 2017-09-25 + +### Merged + +- Buffer: use alloc/allocUnsafe/from instead new [`#50`](https://github.com/browserify/sha.js/pull/50) +- Change "new shajs.SHA256()" to lowercase to make it actually work. [`#48`](https://github.com/browserify/sha.js/pull/48) +- drop Node <4 [`#46`](https://github.com/browserify/sha.js/pull/46) +- hash: _update never returns anything [`#45`](https://github.com/browserify/sha.js/pull/45) +- README: remove typed array comments, re-format support algorithms [`#40`](https://github.com/browserify/sha.js/pull/40) +- Fix digesting of large data (more than MAX_UINT32 bits) [`#43`](https://github.com/browserify/sha.js/pull/43) +- use buffer module [`#41`](https://github.com/browserify/sha.js/pull/41) + +### Commits + +- tests: compare hex, not byte-by-byte (easier debugging) [`5d5a8d8`](https://github.com/browserify/sha.js/commit/5d5a8d882b614060b774e195821a43051f3345b7) +- hash: remove repeated remainder calculation [`158bc83`](https://github.com/browserify/sha.js/commit/158bc835fbffbbd80f93c97ba0db8e7da7db9c0e) +- tests: use safe-buffer constructors [`1ac913b`](https://github.com/browserify/sha.js/commit/1ac913b8e043d495c899a1c52258e8e4e970ee95) +- hash: increase readability of block-by-block hashing [`e9ff865`](https://github.com/browserify/sha.js/commit/e9ff865980615cb8ee2730c6868e7f6781af3c5b) +- use safe-buffer [`22adba6`](https://github.com/browserify/sha.js/commit/22adba6c745ca703cce356faa988dfe1d84eefa4) +- Add test for large data [`e963695`](https://github.com/browserify/sha.js/commit/e9636950b88c8e2a0b012c19f4957229d409b04f) +- tests: formatting [`678c338`](https://github.com/browserify/sha.js/commit/678c3380273516094e32eb79c50e4bce18da6346) +- Fix digesting of large data [`aee24f1`](https://github.com/browserify/sha.js/commit/aee24f1e0d7fefca68633e6c1be52670fb65a1a5) +- hash: update never returns anything [`d308cb0`](https://github.com/browserify/sha.js/commit/d308cb0004a0f3e0fcb7a27ea52868384c654c95) +- hash: rm unnecessary _s state [`388d45e`](https://github.com/browserify/sha.js/commit/388d45ec3a040e7f6ffeecd077498baf32e270e9) +- npmignore: ignore test/ [`03702a8`](https://github.com/browserify/sha.js/commit/03702a8032fe2bc0b033860bec37c09c1d4af44b) +- package: bump standard [`8551e53`](https://github.com/browserify/sha.js/commit/8551e53f389cbe3728b21cd62e2df917b4dad9d6) + +## [v2.4.8](https://github.com/browserify/sha.js/compare/v2.4.7...v2.4.8) - 2016-11-11 + +### Commits + +- travis: add 6 [`62a582c`](https://github.com/browserify/sha.js/commit/62a582ccebffa04f7b281c680095ffd7a7107e12) + +## [v2.4.7](https://github.com/browserify/sha.js/compare/v2.4.6...v2.4.7) - 2016-11-10 + +### Commits + +- re-add bin.js [`30546ca`](https://github.com/browserify/sha.js/commit/30546ca68e683e7fcb4d6c372e48c6b9fda35b4c) + +## [v2.4.6](https://github.com/browserify/sha.js/compare/v2.4.5...v2.4.6) - 2016-11-10 + +### Merged + +- use hash-base [`#36`](https://github.com/browserify/sha.js/pull/36) +- travis: add node 6 [`#38`](https://github.com/browserify/sha.js/pull/38) +- 2.4.5 [`#35`](https://github.com/browserify/sha.js/pull/35) + +### Commits + +- update implementations [`aba27f9`](https://github.com/browserify/sha.js/commit/aba27f9132de39dca4089a120b6c66e097fcd865) +- update tests [`8522be9`](https://github.com/browserify/sha.js/commit/8522be9bc5abc34adf3cf4b17c132471c0f4f80a) +- remove bin.js [`f7c86a7`](https://github.com/browserify/sha.js/commit/f7c86a70d6a70dd807cce06f811d3cbf9cbebff0) +- update README.md [`8eec0fb`](https://github.com/browserify/sha.js/commit/8eec0fbf2025cdf9c5d2f8877d3fe3e276dbda88) +- move shaX to lib directory [`cf2ab1d`](https://github.com/browserify/sha.js/commit/cf2ab1dc9bdd434dfd3afd043f0956894c931ad2) +- travis: add 6 [`891c962`](https://github.com/browserify/sha.js/commit/891c96228dd4cb9777fbae169e8ee8f2c3dc022c) + +## [v2.4.5](https://github.com/browserify/sha.js/compare/v2.4.4...v2.4.5) - 2016-02-26 + +### Merged + +- Improve performace [`#34`](https://github.com/browserify/sha.js/pull/34) +- Add node v4 and v5 to travis config [`#33`](https://github.com/browserify/sha.js/pull/33) + +### Commits + +- Update package.json [`2b250d6`](https://github.com/browserify/sha.js/commit/2b250d6358efed8c9476805ccb86e20c63e721a6) + +## [v2.4.4](https://github.com/browserify/sha.js/compare/v2.4.3...v2.4.4) - 2015-09-19 + +### Merged + +- inline Sigma functions [`#32`](https://github.com/browserify/sha.js/pull/32) + +## [v2.4.3](https://github.com/browserify/sha.js/compare/v2.4.2...v2.4.3) - 2015-09-15 + +### Merged + +- Remove testling [`#31`](https://github.com/browserify/sha.js/pull/31) + +### Fixed + +- Adds npm badge (resolves #28) [`#28`](https://github.com/browserify/sha.js/issues/28) + +### Commits + +- fix standard issues [`52659f7`](https://github.com/browserify/sha.js/commit/52659f73bdc9ce1147da010cb303f3f008428498) +- README: update badge paths [`66a0b4c`](https://github.com/browserify/sha.js/commit/66a0b4c50b3499e2db37c4d1b0545d6536bc2f3b) +- Update README.md [`ca03356`](https://github.com/browserify/sha.js/commit/ca03356cbf74ea5b57df4d4f5ccc1e5557a75966) + +## [v2.4.2](https://github.com/browserify/sha.js/compare/v2.4.1...v2.4.2) - 2015-06-05 + +### Merged + +- Use standard [`#26`](https://github.com/browserify/sha.js/pull/26) + +### Commits + +- sha*: adhere to standard [`74f5fc4`](https://github.com/browserify/sha.js/commit/74f5fc4741447385f5691cb3140cf716a1288312) +- tests: adhere to standard [`e6851ca`](https://github.com/browserify/sha.js/commit/e6851ca9bb0843fa90d8eee7d3f7b3f9d1bbe4fb) +- bin: adhere to standard [`d1a23ab`](https://github.com/browserify/sha.js/commit/d1a23ab987eed4a6b940161693a805fa67c6e16d) +- vectors: adhere to standard [`5657c76`](https://github.com/browserify/sha.js/commit/5657c76f23e92d268010e0fd5880d5c52014eab2) +- hexpp: adhere to stnadard [`2aa2707`](https://github.com/browserify/sha.js/commit/2aa27074799df136b6b49cd0cdb272fe39c742a9) +- tests: remove unused generateCount function [`4a0b095`](https://github.com/browserify/sha.js/commit/4a0b0958e287070efe0bc3d9addff9401e4bbf18) +- adds standard [`0041dbb`](https://github.com/browserify/sha.js/commit/0041dbbd440c0f2e279c6a965485d1d841db3e9e) +- index: adhere to standard [`1839fb7`](https://github.com/browserify/sha.js/commit/1839fb715518fb18077e2b02449ad3627efc3ecb) +- hash: adhere to standard [`1334d89`](https://github.com/browserify/sha.js/commit/1334d89fe96a5854c2871c6352bc6d1a7f843145) +- package: use standard 4.0.0 [`ace4747`](https://github.com/browserify/sha.js/commit/ace474780c743368934c63df64cf12f556bb86a6) +- example is sha256 not sha1 [`8eb102b`](https://github.com/browserify/sha.js/commit/8eb102b6c3faa4a87c134644d019e8c8806a5801) + +## [v2.4.1](https://github.com/browserify/sha.js/compare/v2.4.0...v2.4.1) - 2015-05-19 + +### Merged + +- Update README.md [`#22`](https://github.com/browserify/sha.js/pull/22) + +## [v2.4.0](https://github.com/browserify/sha.js/compare/v2.3.6...v2.4.0) - 2015-04-05 + +### Commits + +- sha0: add implementation [`ca6950d`](https://github.com/browserify/sha.js/commit/ca6950d53c064aa5d767e7166ea29dd0cb61a1b6) +- document legacyness of sha1 and sha0 [`4563da6`](https://github.com/browserify/sha.js/commit/4563da67ee0e86e4eea55b844c6ec73f677e094a) +- README: not just SHA1 anymore [`2a67456`](https://github.com/browserify/sha.js/commit/2a67456d5ab2c6197f8314a83ddb4a9dc59ebfd1) + +## [v2.3.6](https://github.com/browserify/sha.js/compare/v2.3.5...v2.3.6) - 2015-01-14 + +### Commits + +- transfer to crypto-browserify org [`40f1aa9`](https://github.com/browserify/sha.js/commit/40f1aa960c0e7ddc4dc933013d63df41e6362737) + +## [v2.3.5](https://github.com/browserify/sha.js/compare/v2.3.4...v2.3.5) - 2015-01-14 + +### Commits + +- sha512: same branch extraction as #18 [`f985426`](https://github.com/browserify/sha.js/commit/f9854264d841f7138f65eec36010f54043868fdd) +- sha256: extract branches out [`e5486fd`](https://github.com/browserify/sha.js/commit/e5486fde95542a1f79c568f010810b8c7b9889c4) + +## [v2.3.4](https://github.com/browserify/sha.js/compare/v2.3.3...v2.3.4) - 2015-01-13 + +### Commits + +- sha1: use a closure over separate loops [`26a75ec`](https://github.com/browserify/sha.js/commit/26a75eca5f841850a05384d8a3a95e22ee8d9617) + +## [v2.3.3](https://github.com/browserify/sha.js/compare/v2.3.2...v2.3.3) - 2015-01-13 + +### Commits + +- sha1: unroll conditionals [`f830142`](https://github.com/browserify/sha.js/commit/f8301422051bd82cdb490881904b4bd418d5049d) +- sha1: use a closure over seperate loops [`bf46619`](https://github.com/browserify/sha.js/commit/bf46619c437f14c8aec95049fc054a13ee42c779) +- sha1: inline _ft, _kt functions [`3b32ff2`](https://github.com/browserify/sha.js/commit/3b32ff2b18642d57c069152d0dba2dfdc8f6c3ac) + +## [v2.3.2](https://github.com/browserify/sha.js/compare/v2.3.1...v2.3.2) - 2015-01-12 + +### Commits + +- improve sha* code structuring consistency [`d35623d`](https://github.com/browserify/sha.js/commit/d35623d4eddad7bc6f74c6c4f20e59cfc582215d) +- sha*: avoid unnecessary var declaration separation [`d985016`](https://github.com/browserify/sha.js/commit/d9850165dd29f662ff5c9490f9922a2bc65fec73) +- sha1: format sha1_kt similar to sha1_ft for clarity [`c18e7eb`](https://github.com/browserify/sha.js/commit/c18e7eb5ea14c7f5bb99f2664b04e132feeff296) +- adds .gitignore for node_modules [`9dc2814`](https://github.com/browserify/sha.js/commit/9dc2814271d9f119f8b30367d823e72b4f98e1ed) + +## [v2.3.1](https://github.com/browserify/sha.js/compare/v2.3.0...v2.3.1) - 2015-01-12 + +### Commits + +- Use inherits module instead of util [`aef9b82`](https://github.com/browserify/sha.js/commit/aef9b82c629f6ebaf346dab15c2a9bd30fb05aa6) + +## [v2.3.0](https://github.com/browserify/sha.js/compare/v2.2.7...v2.3.0) - 2014-11-18 + +### Commits + +- clean up factories [`996be1c`](https://github.com/browserify/sha.js/commit/996be1cb6a62a479dfb4758c5c431c6381c226cd) +- sha224 and 384 [`56694e5`](https://github.com/browserify/sha.js/commit/56694e5db70844f11a6a4082a549339d3b24ea95) +- add prepublish safety script [`84bde3c`](https://github.com/browserify/sha.js/commit/84bde3cb011f2370034c135727d602522d34e078) + +## [v2.2.7](https://github.com/browserify/sha.js/compare/v2.2.6...v2.2.7) - 2014-11-06 + +### Commits + +- use hash-test-vectors module [`526e246`](https://github.com/browserify/sha.js/commit/526e246cd58f108b410eeae3982756acd06c659c) + +## [v2.2.6](https://github.com/browserify/sha.js/compare/v2.2.5...v2.2.6) - 2014-09-18 + +### Commits + +- don't use global module [`8734884`](https://github.com/browserify/sha.js/commit/87348845d238ba6d8609f22a0972a438d4fc6ab1) +- safely check for IntArray32 existance [`e2376fd`](https://github.com/browserify/sha.js/commit/e2376fd5824fa89ad571e0ea666367511d3a02b5) + +## [v2.2.5](https://github.com/browserify/sha.js/compare/v2.2.4...v2.2.5) - 2014-09-16 + +### Commits + +- move buffer and typedarray into devdeps [`68797f9`](https://github.com/browserify/sha.js/commit/68797f971f55bcf53014e13d9b83adcc0e113ea0) + +## [v2.2.4](https://github.com/browserify/sha.js/compare/v2.2.3...v2.2.4) - 2014-09-16 + +### Commits + +- merge [`7d8b28f`](https://github.com/browserify/sha.js/commit/7d8b28f7627c82ea289e9396d1b93139264e4e1f) +- Fall back to normal array if no typed arrays [`8ca8dfc`](https://github.com/browserify/sha.js/commit/8ca8dfc025e5b2de4a126235b2f3eb4a1046b2d6) +- Don't use console.error [`6e0bd2d`](https://github.com/browserify/sha.js/commit/6e0bd2d8f3db4c267fbaebcbd1b542bc03b1e356) + +## [v2.2.3](https://github.com/browserify/sha.js/compare/v2.2.2...v2.2.3) - 2014-09-16 + +### Commits + +- fix test [`b4e83fa`](https://github.com/browserify/sha.js/commit/b4e83fa8ef732e90c399fcde5f55f8417d623524) + +## [v2.2.2](https://github.com/browserify/sha.js/compare/v2.2.1...v2.2.2) - 2014-09-16 + +### Merged + +- Copyright to contributors [`#10`](https://github.com/browserify/sha.js/pull/10) + +### Commits + +- LICENSE: update to include all contributors [`ac05b4d`](https://github.com/browserify/sha.js/commit/ac05b4d8bfca0c67edd8f20808d61b1aea980ebd) + +## [v2.2.1](https://github.com/browserify/sha.js/compare/v2.2.0...v2.2.1) - 2014-09-16 + +### Commits + +- document implemented hashes [`d123901`](https://github.com/browserify/sha.js/commit/d123901fe28148dce55637ed7942cd4953c9f448) + +## [v2.2.0](https://github.com/browserify/sha.js/compare/v2.1.8...v2.2.0) - 2014-09-16 + +### Commits + +- sha512: add implementation [`3e19416`](https://github.com/browserify/sha.js/commit/3e1941651b20741579c4adfcf69aa0bd607ef932) +- fixtures: remove unused md4 data [`13e43c5`](https://github.com/browserify/sha.js/commit/13e43c59a7109d31147f45a6619af6f8bfa11923) +- get tests working correctly [`01e393f`](https://github.com/browserify/sha.js/commit/01e393fbc4253ce82cf1f57f10c76b087a4b7787) +- remove utils.js [`418d59d`](https://github.com/browserify/sha.js/commit/418d59d40315a50972244ab4103c6d2a59dd86a2) +- fixtures: cleanup of vectors generation [`40f50cc`](https://github.com/browserify/sha.js/commit/40f50ccc29db3f689a04ebfecac2753c895398be) +- sha: jshint cleanup [`a04fae0`](https://github.com/browserify/sha.js/commit/a04fae03acdfb3bbfc7fbf15d928244364b5083a) +- hash: adhere to NIST paper properly [`fb2e39f`](https://github.com/browserify/sha.js/commit/fb2e39f86ce80948b697ac7d0d5f9b7f99c0672c) +- hash: increase verbosity [`b431a1a`](https://github.com/browserify/sha.js/commit/b431a1a24d5d37f856592aabe4593196d60b3b7f) +- hash: use update() argument instead [`0703b9d`](https://github.com/browserify/sha.js/commit/0703b9d38e816d71794a6604060a30e26072b6b7) +- sha: remove unused POOL [`0299989`](https://github.com/browserify/sha.js/commit/02999896280b859d5526820716c737ecbc46d0f6) +- README: add newline before testling badge [`a184d68`](https://github.com/browserify/sha.js/commit/a184d680dae744e7a6adfb73839c3feb6bd5f840) +- LICENSE: update to include all contributors [`edf48c3`](https://github.com/browserify/sha.js/commit/edf48c3b12638cafd509d51b84c39d63f6f00d3b) +- index: remove unused export [`b4de630`](https://github.com/browserify/sha.js/commit/b4de630c9e2092072d0baa5a0a5e93d00ce43c44) + +## [v2.1.8](https://github.com/browserify/sha.js/compare/v2.1.7...v2.1.8) - 2014-08-31 + +### Merged + +- check if DataView exist before using instanceof check [`#6`](https://github.com/browserify/sha.js/pull/6) + +## [v2.1.7](https://github.com/browserify/sha.js/compare/v2.1.6...v2.1.7) - 2014-07-24 + +### Commits + +- check for streaming updates [`4fc22d2`](https://github.com/browserify/sha.js/commit/4fc22d239c87d62155292ed7ccef2e36819bd7a6) +- also test with 3 partial updates [`37981e0`](https://github.com/browserify/sha.js/commit/37981e0b751e4cbb0631472aa19f5facb220cc31) +- Fix streaming updates (limit writing so it doesn't go over block size) [`50b8ddb`](https://github.com/browserify/sha.js/commit/50b8ddb4a5ec8fdaef7c51a01540b237e14a9b5e) + +## [v2.1.6](https://github.com/browserify/sha.js/compare/v2.1.5...v2.1.6) - 2014-07-19 + +### Merged + +- Fixes disparity between 'SHA1' working on node but failing in browser [`#3`](https://github.com/browserify/sha.js/pull/3) + +## [v2.1.5](https://github.com/browserify/sha.js/compare/v2.1.4...v2.1.5) - 2014-06-07 + +### Commits + +- use buffer/ [`23ee33f`](https://github.com/browserify/sha.js/commit/23ee33f8d9ebd5226f6f2fcc6dfcbfddda18af17) + +## v2.1.4 - 2014-06-07 + +### Commits + +- add tests from NIST [`422aa1f`](https://github.com/browserify/sha.js/commit/422aa1fccbd4efc5d2a72fbe7404971c45348c16) +- code to prepare nist-vectors.json [`e799a6f`](https://github.com/browserify/sha.js/commit/e799a6f8b15a9a3796dce295d9485defc7246568) +- inject Buffer dep, so can test with different implementations [`3d89958`](https://github.com/browserify/sha.js/commit/3d8995821e8da1cbf85114589e15843b371b9285) +- initial [`c1cabff`](https://github.com/browserify/sha.js/commit/c1cabff65dc811bd9c7e7530aab90db3b1080f04) +- expose createHash, like node's crypto [`41a1c53`](https://github.com/browserify/sha.js/commit/41a1c531c7947f1bc16ff33ba8dc9335a1a932fd) +- update stuff, still one problem with finalizing some lengths... [`d91aabb`](https://github.com/browserify/sha.js/commit/d91aabb27b0320708aa19943626029510aab3cbb) +- inject Buffer dep into hash [`21df559`](https://github.com/browserify/sha.js/commit/21df55938c274a75d5d04dfd7a4c9abe41d0ce7c) +- refactor tests [`fa6f893`](https://github.com/browserify/sha.js/commit/fa6f893ea0459caa71cd603f244e35f11a617b3b) +- this is quite a bit faster [`84379b3`](https://github.com/browserify/sha.js/commit/84379b3651daca535cbc9aba987563c26d84816f) +- implement sha256! [`70a6101`](https://github.com/browserify/sha.js/commit/70a6101ba6c6a4ae2bf6b89a9feae41c2bcd9559) +- tidy [`dce6d28`](https://github.com/browserify/sha.js/commit/dce6d28d15672d8c95cfc855b8071398b8aaca67) +- move of string stuff, use dataview [`55c7003`](https://github.com/browserify/sha.js/commit/55c7003b99880e54bbd709138028f66c94f51c64) +- update to buffer incrementally [`8cbcade`](https://github.com/browserify/sha.js/commit/8cbcade0875305d3005b97b94b7cdaa61cb93f34) +- refactor, to use buffers in tests [`8e7119b`](https://github.com/browserify/sha.js/commit/8e7119b5c079f51e5c24bfa2d5c3e25536e8b677) +- this is a little faster, but not much... [`55dfc90`](https://github.com/browserify/sha.js/commit/55dfc909269e7b21fb748a5bb594ba3b92a3e03e) +- refactor util functions out [`283f192`](https://github.com/browserify/sha.js/commit/283f1923bdf0bfe48167f240062071cdd2340f78) +- more encodings [`e5071ca`](https://github.com/browserify/sha.js/commit/e5071ca79c80b4d10a2fac860026cdaa291aee27) +- more tests [`655a7be`](https://github.com/browserify/sha.js/commit/655a7be9914298ad63ea77ba4c85dc34ed5f7f9b) +- deal with endianness [`1331b1f`](https://github.com/browserify/sha.js/commit/1331b1f4a1a449c0314cdaec9b0c363a8d8357dd) +- remove custom encoding stuff - just use buffer [`b464d5b`](https://github.com/browserify/sha.js/commit/b464d5bf5cd34de2cb294108179e6967bf3aef28) +- add more encodings to write [`19ce345`](https://github.com/browserify/sha.js/commit/19ce345a06206dda644f21c602aa326597998822) +- separate basic stuff into Hash function [`fe59f0c`](https://github.com/browserify/sha.js/commit/fe59f0cb949fa9e4b5f162f54ebe7b3312b1fc0f) +- experiment using node buffers [`27f6767`](https://github.com/browserify/sha.js/commit/27f676750b9e6e042d2d2ea11ef2c4196f3b417c) +- Several Memory Related Performance Improvements [`9b9badc`](https://github.com/browserify/sha.js/commit/9b9badccae5585d0a1f563ce171635404f97108d) +- tidy [`51c40fa`](https://github.com/browserify/sha.js/commit/51c40fa0c632c5114b574199063f8366621eaaa8) +- use toggle to compare with forge, but inlining makes this the same perf, although removing safe_add improved perf a lot [`15f80b9`](https://github.com/browserify/sha.js/commit/15f80b9e7d1677962f4f0f930e13e0d90de2ef13) +- remove unused utils [`a331a15`](https://github.com/browserify/sha.js/commit/a331a1513efa3bc2cdebc955fa3832ff6306d34f) +- tests for Hash [`417c298`](https://github.com/browserify/sha.js/commit/417c29858090b53b74d91f91b8f8e830dc0384a8) +- for some reason, this is MUCH faster! [`91649a6`](https://github.com/browserify/sha.js/commit/91649a61d6fe8ab2b1f785c1833efa89feeff3b0) +- leaking globals [`7e94cf7`](https://github.com/browserify/sha.js/commit/7e94cf7758ebf70f4c876518495445e971e2eff0) +- delete fakebuffer.js [`e42d66c`](https://github.com/browserify/sha.js/commit/e42d66cf5190a8a0c45e19231ddcd2ef983f1380) +- use bigendian [`f633b94`](https://github.com/browserify/sha.js/commit/f633b94aef9504607b14a2c2805b1e491f3e61b8) +- fix digest [`fdee30b`](https://github.com/browserify/sha.js/commit/fdee30be69cc3fa58a25ae3e32d629251e0a005d) +- tidy [`6f03926`](https://github.com/browserify/sha.js/commit/6f0392697e73f03c8900ccebd4a7d706c77bdea9) +- test incremental update [`d11e6f6`](https://github.com/browserify/sha.js/commit/d11e6f69f4be8d2d1c89f8492d4de2cf2b42027b) +- fake buffer, based on DataView [`71a31b6`](https://github.com/browserify/sha.js/commit/71a31b642d3212076dbd9e54cf6b6072b03c61ad) +- command to hash a large file [`618f16d`](https://github.com/browserify/sha.js/commit/618f16de80ee4b499ab6a493e88845f161dbb0dc) +- { on end of line [`8c1a1a7`](https://github.com/browserify/sha.js/commit/8c1a1a743e740df2e04fa4a9d97a9aea5831529d) +- hammer in a piton, incase I fall off this cliff [`0a211b2`](https://github.com/browserify/sha.js/commit/0a211b2b0abda6252f59f6f3892df1411670c72f) +- basic tests for encoding [`dece220`](https://github.com/browserify/sha.js/commit/dece220424b1d2776a88b35f28d46001e13b10d7) +- tests for hex encoding [`f860f65`](https://github.com/browserify/sha.js/commit/f860f65c173ff92b07643ae55a76516de0b1dded) +- fix fakebuffer [`c421953`](https://github.com/browserify/sha.js/commit/c421953c135fbb1fa3b46df0c3710bc39e50ac4d) +- remove encoding utils [`b0a9d4b`](https://github.com/browserify/sha.js/commit/b0a9d4bc153bd8ffd23d85e5c6a51ac8d7f81d51) +- tidy [`72b825b`](https://github.com/browserify/sha.js/commit/72b825b5a071a37de7bf00f2701bc0af66618ec2) +- tests for fakebuffer [`391fc9f`](https://github.com/browserify/sha.js/commit/391fc9f84988ebc32e8e14687568d8a2418fa34f) +- avoid unnecessary overwrite, 5% improvement [`d061547`](https://github.com/browserify/sha.js/commit/d0615475d9d23ab97814f55fab2e7c6db7adb9bb) +- use dataview [`04b9dee`](https://github.com/browserify/sha.js/commit/04b9deefaf8b1646bc9191f6d955e50c36441da0) +- update vector test to cover sha256 [`aa0d4fa`](https://github.com/browserify/sha.js/commit/aa0d4faef9f05aedf388bf9ca0e5012c4fc14326) +- readme [`6a9992a`](https://github.com/browserify/sha.js/commit/6a9992a77747286d059f0335d5358a013cd0096b) +- toHex supports strings and buffers [`9e17355`](https://github.com/browserify/sha.js/commit/9e173551bba96b7a6d395311948a4887e1461e5e) +- remove redundant tests [`9c701f4`](https://github.com/browserify/sha.js/commit/9c701f4b390b25fd154f85f6b8a5b187542bc463) +- testling [`3515f2f`](https://github.com/browserify/sha.js/commit/3515f2f8c958e3b598a1b6be87ba33cd70ae1591) +- support hex encoding [`b1488b5`](https://github.com/browserify/sha.js/commit/b1488b5dd416c4525e6bc817d7b87da27b275c94) +- remove logging [`ce7d53a`](https://github.com/browserify/sha.js/commit/ce7d53af357062def163f895aa5386e3f6b7e605) +- the working buffer can use system default endianness [`3da2747`](https://github.com/browserify/sha.js/commit/3da27472f74929d223c9ca47c59c219748989981) +- use dataview [`bdba2ec`](https://github.com/browserify/sha.js/commit/bdba2ecbd5877d4e2db012eaaa489467a08fb143) +- support binary encoding [`7b0cae7`](https://github.com/browserify/sha.js/commit/7b0cae71407ec363dc0150c1676fcd0a96a7ea48) +- refactor tests, for createHash [`f424197`](https://github.com/browserify/sha.js/commit/f4241979e140b8317abeb84773136e92d8811ca6) +- Int32 is a little faster than Uint32 [`c61542e`](https://github.com/browserify/sha.js/commit/c61542e06a4c06245aca3860aa36d97e250f08b9) +- simplify bit manipulations [`7e2fc4c`](https://github.com/browserify/sha.js/commit/7e2fc4c06350b35c83d7336c4c36b0acef274373) +- tidy [`e34e8b5`](https://github.com/browserify/sha.js/commit/e34e8b540202f4f548a70d44600287f9e2bd6e82) +- load browserify.js to force native-buffer-browserify [`fd5e58a`](https://github.com/browserify/sha.js/commit/fd5e58a4caa4223fe8f69614d137f219cc11d640) +- tidyup [`12e401b`](https://github.com/browserify/sha.js/commit/12e401b47e7f62ac47edea70efa2db4a044df015) +- this tiny change make it 11% faster on 174mb file! [`f58c321`](https://github.com/browserify/sha.js/commit/f58c3212e40b9c65dc81dcf4765052d4181cf96d) +- support multiple encodings [`36506c6`](https://github.com/browserify/sha.js/commit/36506c6ded807f9a23014b4eb2385ec8ae5d681b) +- tidy [`2c664aa`](https://github.com/browserify/sha.js/commit/2c664aadf9008111c8f19d30f0b8125d0133b79c) +- update hash tests - for some reason, t.deepEqual doesn't work well on buffers? [`8e8e854`](https://github.com/browserify/sha.js/commit/8e8e8547612af4cb628400be0933edeb856f28a5) +- rename to Sha1 [`6620d1a`](https://github.com/browserify/sha.js/commit/6620d1a9a8598e7cf2799e72a786f5bafbaef7a2) +- tidy [`2313658`](https://github.com/browserify/sha.js/commit/2313658f0330b3b851ba55ada7f9ab8a11734802) +- use bops for encoding/decoding [`48d1eb9`](https://github.com/browserify/sha.js/commit/48d1eb9eeee33c2bf55aaae4112ffdf236e20fa2) +- handle large updates all at once, to pass NIST tests [`f2adc77`](https://github.com/browserify/sha.js/commit/f2adc77e49c74d34d8d6715f8b172f3d898bbe32) +- use bops [`5167411`](https://github.com/browserify/sha.js/commit/51674113e9dc35a54d1ec564c51374ad869252b9) +- use fakebuffer instead of buffer [`a6398fe`](https://github.com/browserify/sha.js/commit/a6398fe0582e88aa837a6058494ff7a9c783bd23) +- remove final, and force to Uint8Array [`c42eb76`](https://github.com/browserify/sha.js/commit/c42eb76a6afb318202fd63aff4e6de2145fe4d51) +- todo [`52ef73e`](https://github.com/browserify/sha.js/commit/52ef73e7db22937a2f47ad69e4c4e75fb03215a1) +- remove debugging stuff [`afeb954`](https://github.com/browserify/sha.js/commit/afeb95445f1aff601dba5436ef6024c818d5ed06) +- use bops@0.1.1 [`ccb7eaf`](https://github.com/browserify/sha.js/commit/ccb7eaf1f6e65d590fb4d56797f342e1139a643b) +- convert to string [`abe5373`](https://github.com/browserify/sha.js/commit/abe5373aaf0742cf10dc82b879c0848e22a611fe) +- work around tape/ff [`b95d57c`](https://github.com/browserify/sha.js/commit/b95d57c596a455b3a7b9ad8de95e4374a7d84cf8) +- remove bops [`4d9fb4d`](https://github.com/browserify/sha.js/commit/4d9fb4d8fd8332d69b055e9d1dd1ba5aad5ebb8c) +- this made no difference [`0a0ee38`](https://github.com/browserify/sha.js/commit/0a0ee38c5fead881b3bdb96c9b47a59b62451b13) +- drop support for legacy versions [`e7c530f`](https://github.com/browserify/sha.js/commit/e7c530f19a33aac5768e50dffc8fc93df4070af3) +- a few more test cases [`48ce51b`](https://github.com/browserify/sha.js/commit/48ce51b50b62bb53bd6effdfe42be232f31ffa52) +- use buffer methods [`6a572d2`](https://github.com/browserify/sha.js/commit/6a572d2a27e20c990f8a32d7c39606a0571f9fe7) +- getter for buffer length [`56c1e35`](https://github.com/browserify/sha.js/commit/56c1e35583aa53b1aaaff68d43a4bf4a0c206eea) +- more debuging [`f1c9d10`](https://github.com/browserify/sha.js/commit/f1c9d104d188fbc5ff447a8dc7976400646d3ffb) +- HAHA IT WORKS [`ee95185`](https://github.com/browserify/sha.js/commit/ee9518599957a64d46d8d45aeaae649ec85641c9) +- test coverage for binary encoding [`96e417c`](https://github.com/browserify/sha.js/commit/96e417cd12df156f6f0d3608bdd62ef9d661c41d) +- set debug mode to show time elapsed [`36d4639`](https://github.com/browserify/sha.js/commit/36d46393bde62248e5949b1954b61a96e7220c1c) +- interpret utf-8 as utf8 [`53bd808`](https://github.com/browserify/sha.js/commit/53bd8080ed929d408856b762a43076b1cad19584) +- use browserify edge case to get browser version of core module in node [`657c0a9`](https://github.com/browserify/sha.js/commit/657c0a94c914dee1a473b21f1f3c736a5e715a45) +- native-buffer-browserify -> buffer [`c6a2777`](https://github.com/browserify/sha.js/commit/c6a2777e3ec85de16215f79171fbf2b561556b17) +- do not run test/test.js in the browser, it depends on node.js [`d1d4ac8`](https://github.com/browserify/sha.js/commit/d1d4ac8d27ef65c3fb1dac6b7ef612fabceeea26) +- compute correct length for binary string [`c616d74`](https://github.com/browserify/sha.js/commit/c616d7435493c67d399954d8b5673691b5405e83) +- tidy [`d176073`](https://github.com/browserify/sha.js/commit/d176073dbba34810b86e656dac3a268b438abafe) +- this is twice as fast! turns out creating DataViews is quite slow! /cc @feross [`3ba9a1f`](https://github.com/browserify/sha.js/commit/3ba9a1fe2c33cc4e88ff953ba5c5544d4be311d1) +- use _blockLength property [`fdf1030`](https://github.com/browserify/sha.js/commit/fdf10309e4334347e8f668d6ec6d25ea16856f8e) +- allow subclass to give hash by _hash method [`d47673b`](https://github.com/browserify/sha.js/commit/d47673bc4b2b66f46f0eec342f5e424f5147c48d) +- use my toHex [`76ffe66`](https://github.com/browserify/sha.js/commit/76ffe66ba119e9e8fc88d6a5b5dcd5ffabf2a9da) +- didin't work [`254a4e8`](https://github.com/browserify/sha.js/commit/254a4e8c3e4fe79c8ce4cacd23a91426e9d8f32f) +- always run all tests [`18f39f8`](https://github.com/browserify/sha.js/commit/18f39f8e96ddebd86d32604b55c388e0c72306e1) +- remove hexpp [`e7f3030`](https://github.com/browserify/sha.js/commit/e7f30308c64bfc5f00a9b8ccf4c17ae65a3ddc55) +- make installable as a command [`f6842dd`](https://github.com/browserify/sha.js/commit/f6842dde37f0b37042e513fc63d214989417fff4) +- 0.11 is not working... [`a6aacc6`](https://github.com/browserify/sha.js/commit/a6aacc66f417ee17a4363cc1d20350bfa7a682cc) diff --git a/README.md b/README.md index 1cc3db5..2a6ad74 100644 --- a/README.md +++ b/README.md @@ -41,4 +41,4 @@ It does update incrementally, so you can hash things larger than RAM, as it uses This work is derived from Paul Johnston's [A JavaScript implementation of the Secure Hash Algorithm](http://pajhome.org.uk/crypt/md5/sha1.html). -## LICENSE [MIT](LICENSE) +## LICENSE [MIT AND BSD-3-Clause](LICENSE) diff --git a/bin.js b/bin.js index 5a7ac83..596f292 100755 --- a/bin.js +++ b/bin.js @@ -1,41 +1,44 @@ #! /usr/bin/env node -var createHash = require('./browserify') -var argv = process.argv.slice(2) +'use strict'; -function pipe (algorithm, s) { - var start = Date.now() - var hash = createHash(algorithm || 'sha1') +var createHash = require('./browserify'); +var argv = process.argv.slice(2); - s.on('data', function (data) { - hash.update(data) - }) +function pipe(algorithm, s) { + var start = Date.now(); + var hash = createHash(algorithm || 'sha1'); - s.on('end', function () { - if (process.env.DEBUG) { - return console.log(hash.digest('hex'), Date.now() - start) - } + s.on('data', function (data) { + hash.update(data); + }); - console.log(hash.digest('hex')) - }) + s.on('end', function () { + if (process.env.DEBUG) { + console.log(hash.digest('hex'), Date.now() - start); + } else { + console.log(hash.digest('hex')); + } + }); } -function usage () { - console.error('sha.js [algorithm=sha1] [filename] # hash filename with algorithm') - console.error('input | sha.js [algorithm=sha1] # hash stdin with algorithm') - console.error('sha.js --help # display this message') +function usage() { + console.error('sha.js [algorithm=sha1] [filename] # hash filename with algorithm'); + console.error('input | sha.js [algorithm=sha1] # hash stdin with algorithm'); + console.error('sha.js --help # display this message'); } if (!process.stdin.isTTY) { - pipe(argv[0], process.stdin) + pipe(argv[0], process.stdin); } else if (argv.length) { - if (/--help|-h/.test(argv[0])) { - usage() - } else { - var filename = argv.pop() - var algorithm = argv.pop() - pipe(algorithm, require('fs').createReadStream(filename)) - } + if ((/--help|-h/).test(argv[0])) { + usage(); + } else { + var filename = argv.pop(); + var algorithm = argv.pop(); + // eslint-disable-next-line global-require + pipe(algorithm, require('fs').createReadStream(filename)); + } } else { - usage() + usage(); } diff --git a/hash.js b/hash.js index 013537a..397b87a 100644 --- a/hash.js +++ b/hash.js @@ -1,81 +1,84 @@ -var Buffer = require('safe-buffer').Buffer +'use strict'; + +var Buffer = require('safe-buffer').Buffer; +var toBuffer = require('to-buffer'); // prototype class for hash functions -function Hash (blockSize, finalSize) { - this._block = Buffer.alloc(blockSize) - this._finalSize = finalSize - this._blockSize = blockSize - this._len = 0 +function Hash(blockSize, finalSize) { + this._block = Buffer.alloc(blockSize); + this._finalSize = finalSize; + this._blockSize = blockSize; + this._len = 0; } Hash.prototype.update = function (data, enc) { - if (typeof data === 'string') { - enc = enc || 'utf8' - data = Buffer.from(data, enc) - } - - var block = this._block - var blockSize = this._blockSize - var length = data.length - var accum = this._len - - for (var offset = 0; offset < length;) { - var assigned = accum % blockSize - var remainder = Math.min(length - offset, blockSize - assigned) - - for (var i = 0; i < remainder; i++) { - block[assigned + i] = data[offset + i] - } - - accum += remainder - offset += remainder - - if ((accum % blockSize) === 0) { - this._update(block) - } - } - - this._len += length - return this -} + /* eslint no-param-reassign: 0 */ + data = toBuffer(data, enc || 'utf8'); + + var block = this._block; + var blockSize = this._blockSize; + var length = data.length; + var accum = this._len; + + for (var offset = 0; offset < length;) { + var assigned = accum % blockSize; + var remainder = Math.min(length - offset, blockSize - assigned); + + for (var i = 0; i < remainder; i++) { + block[assigned + i] = data[offset + i]; + } + + accum += remainder; + offset += remainder; + + if ((accum % blockSize) === 0) { + this._update(block); + } + } + + this._len += length; + return this; +}; Hash.prototype.digest = function (enc) { - var rem = this._len % this._blockSize + var rem = this._len % this._blockSize; - this._block[rem] = 0x80 + this._block[rem] = 0x80; - // zero (rem + 1) trailing bits, where (rem + 1) is the smallest - // non-negative solution to the equation (length + 1 + (rem + 1)) === finalSize mod blockSize - this._block.fill(0, rem + 1) + /* + * zero (rem + 1) trailing bits, where (rem + 1) is the smallest + * non-negative solution to the equation (length + 1 + (rem + 1)) === finalSize mod blockSize + */ + this._block.fill(0, rem + 1); - if (rem >= this._finalSize) { - this._update(this._block) - this._block.fill(0) - } + if (rem >= this._finalSize) { + this._update(this._block); + this._block.fill(0); + } - var bits = this._len * 8 + var bits = this._len * 8; - // uint32 - if (bits <= 0xffffffff) { - this._block.writeUInt32BE(bits, this._blockSize - 4) + // uint32 + if (bits <= 0xffffffff) { + this._block.writeUInt32BE(bits, this._blockSize - 4); - // uint64 - } else { - var lowBits = (bits & 0xffffffff) >>> 0 - var highBits = (bits - lowBits) / 0x100000000 + // uint64 + } else { + var lowBits = (bits & 0xffffffff) >>> 0; + var highBits = (bits - lowBits) / 0x100000000; - this._block.writeUInt32BE(highBits, this._blockSize - 8) - this._block.writeUInt32BE(lowBits, this._blockSize - 4) - } + this._block.writeUInt32BE(highBits, this._blockSize - 8); + this._block.writeUInt32BE(lowBits, this._blockSize - 4); + } - this._update(this._block) - var hash = this._hash() + this._update(this._block); + var hash = this._hash(); - return enc ? hash.toString(enc) : hash -} + return enc ? hash.toString(enc) : hash; +}; Hash.prototype._update = function () { - throw new Error('_update must be implemented by subclass') -} + throw new Error('_update must be implemented by subclass'); +}; -module.exports = Hash +module.exports = Hash; diff --git a/index.js b/index.js index 87cdf49..912e597 100644 --- a/index.js +++ b/index.js @@ -1,15 +1,19 @@ -var exports = module.exports = function SHA (algorithm) { - algorithm = algorithm.toLowerCase() +'use strict'; - var Algorithm = exports[algorithm] - if (!Algorithm) throw new Error(algorithm + ' is not supported (we accept pull requests)') +module.exports = function SHA(algorithm) { + var alg = algorithm.toLowerCase(); - return new Algorithm() -} + var Algorithm = module.exports[alg]; + if (!Algorithm) { + throw new Error(alg + ' is not supported (we accept pull requests)'); + } -exports.sha = require('./sha') -exports.sha1 = require('./sha1') -exports.sha224 = require('./sha224') -exports.sha256 = require('./sha256') -exports.sha384 = require('./sha384') -exports.sha512 = require('./sha512') + return new Algorithm(); +}; + +module.exports.sha = require('./sha'); +module.exports.sha1 = require('./sha1'); +module.exports.sha224 = require('./sha224'); +module.exports.sha256 = require('./sha256'); +module.exports.sha384 = require('./sha384'); +module.exports.sha512 = require('./sha512'); diff --git a/package.json b/package.json index bfe633b..16c91b5 100644 --- a/package.json +++ b/package.json @@ -1,30 +1,58 @@ { - "name": "sha.js", - "description": "Streamable SHA hashes in pure javascript", - "version": "2.4.11", - "homepage": "https://github.com/crypto-browserify/sha.js", - "repository": { - "type": "git", - "url": "git://github.com/crypto-browserify/sha.js.git" - }, - "dependencies": { - "inherits": "^2.0.1", - "safe-buffer": "^5.0.1" - }, - "devDependencies": { - "buffer": "~2.3.2", - "hash-test-vectors": "^1.3.1", - "standard": "^10.0.2", - "tape": "~2.3.2", - "typedarray": "0.0.6" - }, - "bin": "./bin.js", - "scripts": { - "prepublish": "npm ls && npm run unit", - "lint": "standard", - "test": "npm run lint && npm run unit", - "unit": "set -e; for t in test/*.js; do node $t; done;" - }, - "author": "Dominic Tarr (dominictarr.com)", - "license": "(MIT AND BSD-3-Clause)" + "name": "sha.js", + "description": "Streamable SHA hashes in pure javascript", + "version": "2.4.12", + "repository": { + "type": "git", + "url": "git://github.com/crypto-browserify/sha.js.git" + }, + "bin": "./bin.js", + "scripts": { + "prepack": "npmignore --auto --commentLines=autogenerated", + "lint": "eslint --ext=js,mjs .", + "pretest": "npm run lint", + "test": "npm run tests-only", + "tests-only": "tape 'test/**/*.js'", + "posttest": "npx npm@'>= 10.2' audit --production", + "version": "auto-changelog && git add CHANGELOG.md", + "postversion": "auto-changelog && git add CHANGELOG.md && git commit --no-edit --amend && git tag -f \"v$(node -e \"console.log(require('./package.json').version)\")\"" + }, + "license": "(MIT AND BSD-3-Clause)", + "author": "Dominic Tarr (dominictarr.com)", + "funding": { + "url": "https://github.com/sponsors/ljharb" + }, + "homepage": "https://github.com/crypto-browserify/sha.js", + "dependencies": { + "inherits": "^2.0.4", + "safe-buffer": "^5.2.1", + "to-buffer": "^1.2.0" + }, + "devDependencies": { + "@ljharb/eslint-config": "^21.2.0", + "auto-changelog": "^2.5.0", + "encoding": "^0.1.13", + "eslint": "=8.8.0", + "hash-test-vectors": "^1.3.2", + "npmignore": "^0.3.1", + "tape": "^5.9.0", + "typedarray": "^0.0.7" + }, + "auto-changelog": { + "output": "CHANGELOG.md", + "template": "keepachangelog", + "unreleased": false, + "commitLimit": false, + "backfillLimit": false, + "hideCredit": true + }, + "publishConfig": { + "ignore": [ + ".github/workflows", + ".github" + ] + }, + "engines": { + "node": ">= 0.10" + } } diff --git a/sha.js b/sha.js index 50c4fa8..fdfe4c1 100644 --- a/sha.js +++ b/sha.js @@ -1,3 +1,5 @@ +'use strict'; + /* * A JavaScript implementation of the Secure Hash Algorithm, SHA-0, as defined * in FIPS PUB 180-1 @@ -6,89 +8,97 @@ * operation was added. */ -var inherits = require('inherits') -var Hash = require('./hash') -var Buffer = require('safe-buffer').Buffer +var inherits = require('inherits'); +var Hash = require('./hash'); +var Buffer = require('safe-buffer').Buffer; var K = [ - 0x5a827999, 0x6ed9eba1, 0x8f1bbcdc | 0, 0xca62c1d6 | 0 -] + 0x5a827999, 0x6ed9eba1, 0x8f1bbcdc | 0, 0xca62c1d6 | 0 +]; -var W = new Array(80) +var W = new Array(80); -function Sha () { - this.init() - this._w = W +function Sha() { + this.init(); + this._w = W; - Hash.call(this, 64, 56) + Hash.call(this, 64, 56); } -inherits(Sha, Hash) +inherits(Sha, Hash); Sha.prototype.init = function () { - this._a = 0x67452301 - this._b = 0xefcdab89 - this._c = 0x98badcfe - this._d = 0x10325476 - this._e = 0xc3d2e1f0 + this._a = 0x67452301; + this._b = 0xefcdab89; + this._c = 0x98badcfe; + this._d = 0x10325476; + this._e = 0xc3d2e1f0; - return this -} + return this; +}; -function rotl5 (num) { - return (num << 5) | (num >>> 27) +function rotl5(num) { + return (num << 5) | (num >>> 27); } -function rotl30 (num) { - return (num << 30) | (num >>> 2) +function rotl30(num) { + return (num << 30) | (num >>> 2); } -function ft (s, b, c, d) { - if (s === 0) return (b & c) | ((~b) & d) - if (s === 2) return (b & c) | (b & d) | (c & d) - return b ^ c ^ d +function ft(s, b, c, d) { + if (s === 0) { + return (b & c) | (~b & d); + } + if (s === 2) { + return (b & c) | (b & d) | (c & d); + } + return b ^ c ^ d; } Sha.prototype._update = function (M) { - var W = this._w - - var a = this._a | 0 - var b = this._b | 0 - var c = this._c | 0 - var d = this._d | 0 - var e = this._e | 0 - - for (var i = 0; i < 16; ++i) W[i] = M.readInt32BE(i * 4) - for (; i < 80; ++i) W[i] = W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16] - - for (var j = 0; j < 80; ++j) { - var s = ~~(j / 20) - var t = (rotl5(a) + ft(s, b, c, d) + e + W[j] + K[s]) | 0 - - e = d - d = c - c = rotl30(b) - b = a - a = t - } - - this._a = (a + this._a) | 0 - this._b = (b + this._b) | 0 - this._c = (c + this._c) | 0 - this._d = (d + this._d) | 0 - this._e = (e + this._e) | 0 -} + var w = this._w; + + var a = this._a | 0; + var b = this._b | 0; + var c = this._c | 0; + var d = this._d | 0; + var e = this._e | 0; + + for (var i = 0; i < 16; ++i) { + w[i] = M.readInt32BE(i * 4); + } + for (; i < 80; ++i) { + w[i] = w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16]; + } + + for (var j = 0; j < 80; ++j) { + var s = ~~(j / 20); + var t = (rotl5(a) + ft(s, b, c, d) + e + w[j] + K[s]) | 0; + + e = d; + d = c; + c = rotl30(b); + b = a; + a = t; + } + + this._a = (a + this._a) | 0; + this._b = (b + this._b) | 0; + this._c = (c + this._c) | 0; + this._d = (d + this._d) | 0; + this._e = (e + this._e) | 0; +}; Sha.prototype._hash = function () { - var H = Buffer.allocUnsafe(20) + var H = Buffer.allocUnsafe(20); - H.writeInt32BE(this._a | 0, 0) - H.writeInt32BE(this._b | 0, 4) - H.writeInt32BE(this._c | 0, 8) - H.writeInt32BE(this._d | 0, 12) - H.writeInt32BE(this._e | 0, 16) + H.writeInt32BE(this._a | 0, 0); + H.writeInt32BE(this._b | 0, 4); + H.writeInt32BE(this._c | 0, 8); + H.writeInt32BE(this._d | 0, 12); + H.writeInt32BE(this._e | 0, 16); - return H -} + return H; +}; -module.exports = Sha +module.exports = Sha; diff --git a/sha1.js b/sha1.js index cabd747..0891453 100644 --- a/sha1.js +++ b/sha1.js @@ -1,3 +1,5 @@ +'use strict'; + /* * A JavaScript implementation of the Secure Hash Algorithm, SHA-1, as defined * in FIPS PUB 180-1 @@ -7,93 +9,101 @@ * See http://pajhome.org.uk/crypt/md5 for details. */ -var inherits = require('inherits') -var Hash = require('./hash') -var Buffer = require('safe-buffer').Buffer +var inherits = require('inherits'); +var Hash = require('./hash'); +var Buffer = require('safe-buffer').Buffer; var K = [ - 0x5a827999, 0x6ed9eba1, 0x8f1bbcdc | 0, 0xca62c1d6 | 0 -] + 0x5a827999, 0x6ed9eba1, 0x8f1bbcdc | 0, 0xca62c1d6 | 0 +]; -var W = new Array(80) +var W = new Array(80); -function Sha1 () { - this.init() - this._w = W +function Sha1() { + this.init(); + this._w = W; - Hash.call(this, 64, 56) + Hash.call(this, 64, 56); } -inherits(Sha1, Hash) +inherits(Sha1, Hash); Sha1.prototype.init = function () { - this._a = 0x67452301 - this._b = 0xefcdab89 - this._c = 0x98badcfe - this._d = 0x10325476 - this._e = 0xc3d2e1f0 + this._a = 0x67452301; + this._b = 0xefcdab89; + this._c = 0x98badcfe; + this._d = 0x10325476; + this._e = 0xc3d2e1f0; - return this -} + return this; +}; -function rotl1 (num) { - return (num << 1) | (num >>> 31) +function rotl1(num) { + return (num << 1) | (num >>> 31); } -function rotl5 (num) { - return (num << 5) | (num >>> 27) +function rotl5(num) { + return (num << 5) | (num >>> 27); } -function rotl30 (num) { - return (num << 30) | (num >>> 2) +function rotl30(num) { + return (num << 30) | (num >>> 2); } -function ft (s, b, c, d) { - if (s === 0) return (b & c) | ((~b) & d) - if (s === 2) return (b & c) | (b & d) | (c & d) - return b ^ c ^ d +function ft(s, b, c, d) { + if (s === 0) { + return (b & c) | (~b & d); + } + if (s === 2) { + return (b & c) | (b & d) | (c & d); + } + return b ^ c ^ d; } Sha1.prototype._update = function (M) { - var W = this._w - - var a = this._a | 0 - var b = this._b | 0 - var c = this._c | 0 - var d = this._d | 0 - var e = this._e | 0 - - for (var i = 0; i < 16; ++i) W[i] = M.readInt32BE(i * 4) - for (; i < 80; ++i) W[i] = rotl1(W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16]) - - for (var j = 0; j < 80; ++j) { - var s = ~~(j / 20) - var t = (rotl5(a) + ft(s, b, c, d) + e + W[j] + K[s]) | 0 - - e = d - d = c - c = rotl30(b) - b = a - a = t - } - - this._a = (a + this._a) | 0 - this._b = (b + this._b) | 0 - this._c = (c + this._c) | 0 - this._d = (d + this._d) | 0 - this._e = (e + this._e) | 0 -} + var w = this._w; + + var a = this._a | 0; + var b = this._b | 0; + var c = this._c | 0; + var d = this._d | 0; + var e = this._e | 0; + + for (var i = 0; i < 16; ++i) { + w[i] = M.readInt32BE(i * 4); + } + for (; i < 80; ++i) { + w[i] = rotl1(w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16]); + } + + for (var j = 0; j < 80; ++j) { + var s = ~~(j / 20); + var t = (rotl5(a) + ft(s, b, c, d) + e + w[j] + K[s]) | 0; + + e = d; + d = c; + c = rotl30(b); + b = a; + a = t; + } + + this._a = (a + this._a) | 0; + this._b = (b + this._b) | 0; + this._c = (c + this._c) | 0; + this._d = (d + this._d) | 0; + this._e = (e + this._e) | 0; +}; Sha1.prototype._hash = function () { - var H = Buffer.allocUnsafe(20) + var H = Buffer.allocUnsafe(20); - H.writeInt32BE(this._a | 0, 0) - H.writeInt32BE(this._b | 0, 4) - H.writeInt32BE(this._c | 0, 8) - H.writeInt32BE(this._d | 0, 12) - H.writeInt32BE(this._e | 0, 16) + H.writeInt32BE(this._a | 0, 0); + H.writeInt32BE(this._b | 0, 4); + H.writeInt32BE(this._c | 0, 8); + H.writeInt32BE(this._d | 0, 12); + H.writeInt32BE(this._e | 0, 16); - return H -} + return H; +}; -module.exports = Sha1 +module.exports = Sha1; diff --git a/sha224.js b/sha224.js index 35541e5..9cd0702 100644 --- a/sha224.js +++ b/sha224.js @@ -1,3 +1,5 @@ +'use strict'; + /** * A JavaScript implementation of the Secure Hash Algorithm, SHA-256, as defined * in FIPS 180-2 @@ -6,48 +8,48 @@ * */ -var inherits = require('inherits') -var Sha256 = require('./sha256') -var Hash = require('./hash') -var Buffer = require('safe-buffer').Buffer +var inherits = require('inherits'); +var Sha256 = require('./sha256'); +var Hash = require('./hash'); +var Buffer = require('safe-buffer').Buffer; -var W = new Array(64) +var W = new Array(64); -function Sha224 () { - this.init() +function Sha224() { + this.init(); - this._w = W // new Array(64) + this._w = W; // new Array(64) - Hash.call(this, 64, 56) + Hash.call(this, 64, 56); } -inherits(Sha224, Sha256) +inherits(Sha224, Sha256); Sha224.prototype.init = function () { - this._a = 0xc1059ed8 - this._b = 0x367cd507 - this._c = 0x3070dd17 - this._d = 0xf70e5939 - this._e = 0xffc00b31 - this._f = 0x68581511 - this._g = 0x64f98fa7 - this._h = 0xbefa4fa4 - - return this -} + this._a = 0xc1059ed8; + this._b = 0x367cd507; + this._c = 0x3070dd17; + this._d = 0xf70e5939; + this._e = 0xffc00b31; + this._f = 0x68581511; + this._g = 0x64f98fa7; + this._h = 0xbefa4fa4; + + return this; +}; Sha224.prototype._hash = function () { - var H = Buffer.allocUnsafe(28) + var H = Buffer.allocUnsafe(28); - H.writeInt32BE(this._a, 0) - H.writeInt32BE(this._b, 4) - H.writeInt32BE(this._c, 8) - H.writeInt32BE(this._d, 12) - H.writeInt32BE(this._e, 16) - H.writeInt32BE(this._f, 20) - H.writeInt32BE(this._g, 24) + H.writeInt32BE(this._a, 0); + H.writeInt32BE(this._b, 4); + H.writeInt32BE(this._c, 8); + H.writeInt32BE(this._d, 12); + H.writeInt32BE(this._e, 16); + H.writeInt32BE(this._f, 20); + H.writeInt32BE(this._g, 24); - return H -} + return H; +}; -module.exports = Sha224 +module.exports = Sha224; diff --git a/sha256.js b/sha256.js index 342e48a..6741589 100644 --- a/sha256.js +++ b/sha256.js @@ -1,3 +1,5 @@ +'use strict'; + /** * A JavaScript implementation of the Secure Hash Algorithm, SHA-256, as defined * in FIPS 180-2 @@ -6,130 +8,182 @@ * */ -var inherits = require('inherits') -var Hash = require('./hash') -var Buffer = require('safe-buffer').Buffer +var inherits = require('inherits'); +var Hash = require('./hash'); +var Buffer = require('safe-buffer').Buffer; var K = [ - 0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5, - 0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5, - 0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3, - 0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174, - 0xE49B69C1, 0xEFBE4786, 0x0FC19DC6, 0x240CA1CC, - 0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA, - 0x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7, - 0xC6E00BF3, 0xD5A79147, 0x06CA6351, 0x14292967, - 0x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13, - 0x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85, - 0xA2BFE8A1, 0xA81A664B, 0xC24B8B70, 0xC76C51A3, - 0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070, - 0x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5, - 0x391C0CB3, 0x4ED8AA4A, 0x5B9CCA4F, 0x682E6FF3, - 0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208, - 0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2 -] - -var W = new Array(64) - -function Sha256 () { - this.init() - - this._w = W // new Array(64) - - Hash.call(this, 64, 56) + 0x428A2F98, + 0x71374491, + 0xB5C0FBCF, + 0xE9B5DBA5, + 0x3956C25B, + 0x59F111F1, + 0x923F82A4, + 0xAB1C5ED5, + 0xD807AA98, + 0x12835B01, + 0x243185BE, + 0x550C7DC3, + 0x72BE5D74, + 0x80DEB1FE, + 0x9BDC06A7, + 0xC19BF174, + 0xE49B69C1, + 0xEFBE4786, + 0x0FC19DC6, + 0x240CA1CC, + 0x2DE92C6F, + 0x4A7484AA, + 0x5CB0A9DC, + 0x76F988DA, + 0x983E5152, + 0xA831C66D, + 0xB00327C8, + 0xBF597FC7, + 0xC6E00BF3, + 0xD5A79147, + 0x06CA6351, + 0x14292967, + 0x27B70A85, + 0x2E1B2138, + 0x4D2C6DFC, + 0x53380D13, + 0x650A7354, + 0x766A0ABB, + 0x81C2C92E, + 0x92722C85, + 0xA2BFE8A1, + 0xA81A664B, + 0xC24B8B70, + 0xC76C51A3, + 0xD192E819, + 0xD6990624, + 0xF40E3585, + 0x106AA070, + 0x19A4C116, + 0x1E376C08, + 0x2748774C, + 0x34B0BCB5, + 0x391C0CB3, + 0x4ED8AA4A, + 0x5B9CCA4F, + 0x682E6FF3, + 0x748F82EE, + 0x78A5636F, + 0x84C87814, + 0x8CC70208, + 0x90BEFFFA, + 0xA4506CEB, + 0xBEF9A3F7, + 0xC67178F2 +]; + +var W = new Array(64); + +function Sha256() { + this.init(); + + this._w = W; // new Array(64) + + Hash.call(this, 64, 56); } -inherits(Sha256, Hash) +inherits(Sha256, Hash); Sha256.prototype.init = function () { - this._a = 0x6a09e667 - this._b = 0xbb67ae85 - this._c = 0x3c6ef372 - this._d = 0xa54ff53a - this._e = 0x510e527f - this._f = 0x9b05688c - this._g = 0x1f83d9ab - this._h = 0x5be0cd19 - - return this -} - -function ch (x, y, z) { - return z ^ (x & (y ^ z)) + this._a = 0x6a09e667; + this._b = 0xbb67ae85; + this._c = 0x3c6ef372; + this._d = 0xa54ff53a; + this._e = 0x510e527f; + this._f = 0x9b05688c; + this._g = 0x1f83d9ab; + this._h = 0x5be0cd19; + + return this; +}; + +function ch(x, y, z) { + return z ^ (x & (y ^ z)); } -function maj (x, y, z) { - return (x & y) | (z & (x | y)) +function maj(x, y, z) { + return (x & y) | (z & (x | y)); } -function sigma0 (x) { - return (x >>> 2 | x << 30) ^ (x >>> 13 | x << 19) ^ (x >>> 22 | x << 10) +function sigma0(x) { + return ((x >>> 2) | (x << 30)) ^ ((x >>> 13) | (x << 19)) ^ ((x >>> 22) | (x << 10)); } -function sigma1 (x) { - return (x >>> 6 | x << 26) ^ (x >>> 11 | x << 21) ^ (x >>> 25 | x << 7) +function sigma1(x) { + return ((x >>> 6) | (x << 26)) ^ ((x >>> 11) | (x << 21)) ^ ((x >>> 25) | (x << 7)); } -function gamma0 (x) { - return (x >>> 7 | x << 25) ^ (x >>> 18 | x << 14) ^ (x >>> 3) +function gamma0(x) { + return ((x >>> 7) | (x << 25)) ^ ((x >>> 18) | (x << 14)) ^ (x >>> 3); } -function gamma1 (x) { - return (x >>> 17 | x << 15) ^ (x >>> 19 | x << 13) ^ (x >>> 10) +function gamma1(x) { + return ((x >>> 17) | (x << 15)) ^ ((x >>> 19) | (x << 13)) ^ (x >>> 10); } Sha256.prototype._update = function (M) { - var W = this._w - - var a = this._a | 0 - var b = this._b | 0 - var c = this._c | 0 - var d = this._d | 0 - var e = this._e | 0 - var f = this._f | 0 - var g = this._g | 0 - var h = this._h | 0 - - for (var i = 0; i < 16; ++i) W[i] = M.readInt32BE(i * 4) - for (; i < 64; ++i) W[i] = (gamma1(W[i - 2]) + W[i - 7] + gamma0(W[i - 15]) + W[i - 16]) | 0 - - for (var j = 0; j < 64; ++j) { - var T1 = (h + sigma1(e) + ch(e, f, g) + K[j] + W[j]) | 0 - var T2 = (sigma0(a) + maj(a, b, c)) | 0 - - h = g - g = f - f = e - e = (d + T1) | 0 - d = c - c = b - b = a - a = (T1 + T2) | 0 - } - - this._a = (a + this._a) | 0 - this._b = (b + this._b) | 0 - this._c = (c + this._c) | 0 - this._d = (d + this._d) | 0 - this._e = (e + this._e) | 0 - this._f = (f + this._f) | 0 - this._g = (g + this._g) | 0 - this._h = (h + this._h) | 0 -} + var w = this._w; + + var a = this._a | 0; + var b = this._b | 0; + var c = this._c | 0; + var d = this._d | 0; + var e = this._e | 0; + var f = this._f | 0; + var g = this._g | 0; + var h = this._h | 0; + + for (var i = 0; i < 16; ++i) { + w[i] = M.readInt32BE(i * 4); + } + for (; i < 64; ++i) { + w[i] = (gamma1(w[i - 2]) + w[i - 7] + gamma0(w[i - 15]) + w[i - 16]) | 0; + } + + for (var j = 0; j < 64; ++j) { + var T1 = (h + sigma1(e) + ch(e, f, g) + K[j] + w[j]) | 0; + var T2 = (sigma0(a) + maj(a, b, c)) | 0; + + h = g; + g = f; + f = e; + e = (d + T1) | 0; + d = c; + c = b; + b = a; + a = (T1 + T2) | 0; + } + + this._a = (a + this._a) | 0; + this._b = (b + this._b) | 0; + this._c = (c + this._c) | 0; + this._d = (d + this._d) | 0; + this._e = (e + this._e) | 0; + this._f = (f + this._f) | 0; + this._g = (g + this._g) | 0; + this._h = (h + this._h) | 0; +}; Sha256.prototype._hash = function () { - var H = Buffer.allocUnsafe(32) - - H.writeInt32BE(this._a, 0) - H.writeInt32BE(this._b, 4) - H.writeInt32BE(this._c, 8) - H.writeInt32BE(this._d, 12) - H.writeInt32BE(this._e, 16) - H.writeInt32BE(this._f, 20) - H.writeInt32BE(this._g, 24) - H.writeInt32BE(this._h, 28) - - return H -} + var H = Buffer.allocUnsafe(32); + + H.writeInt32BE(this._a, 0); + H.writeInt32BE(this._b, 4); + H.writeInt32BE(this._c, 8); + H.writeInt32BE(this._d, 12); + H.writeInt32BE(this._e, 16); + H.writeInt32BE(this._f, 20); + H.writeInt32BE(this._g, 24); + H.writeInt32BE(this._h, 28); + + return H; +}; -module.exports = Sha256 +module.exports = Sha256; diff --git a/sha384.js b/sha384.js index afc85e5..ad260dd 100644 --- a/sha384.js +++ b/sha384.js @@ -1,57 +1,59 @@ -var inherits = require('inherits') -var SHA512 = require('./sha512') -var Hash = require('./hash') -var Buffer = require('safe-buffer').Buffer +'use strict'; -var W = new Array(160) +var inherits = require('inherits'); +var SHA512 = require('./sha512'); +var Hash = require('./hash'); +var Buffer = require('safe-buffer').Buffer; -function Sha384 () { - this.init() - this._w = W +var W = new Array(160); - Hash.call(this, 128, 112) +function Sha384() { + this.init(); + this._w = W; + + Hash.call(this, 128, 112); } -inherits(Sha384, SHA512) +inherits(Sha384, SHA512); Sha384.prototype.init = function () { - this._ah = 0xcbbb9d5d - this._bh = 0x629a292a - this._ch = 0x9159015a - this._dh = 0x152fecd8 - this._eh = 0x67332667 - this._fh = 0x8eb44a87 - this._gh = 0xdb0c2e0d - this._hh = 0x47b5481d - - this._al = 0xc1059ed8 - this._bl = 0x367cd507 - this._cl = 0x3070dd17 - this._dl = 0xf70e5939 - this._el = 0xffc00b31 - this._fl = 0x68581511 - this._gl = 0x64f98fa7 - this._hl = 0xbefa4fa4 - - return this -} + this._ah = 0xcbbb9d5d; + this._bh = 0x629a292a; + this._ch = 0x9159015a; + this._dh = 0x152fecd8; + this._eh = 0x67332667; + this._fh = 0x8eb44a87; + this._gh = 0xdb0c2e0d; + this._hh = 0x47b5481d; + + this._al = 0xc1059ed8; + this._bl = 0x367cd507; + this._cl = 0x3070dd17; + this._dl = 0xf70e5939; + this._el = 0xffc00b31; + this._fl = 0x68581511; + this._gl = 0x64f98fa7; + this._hl = 0xbefa4fa4; + + return this; +}; Sha384.prototype._hash = function () { - var H = Buffer.allocUnsafe(48) + var H = Buffer.allocUnsafe(48); - function writeInt64BE (h, l, offset) { - H.writeInt32BE(h, offset) - H.writeInt32BE(l, offset + 4) - } + function writeInt64BE(h, l, offset) { + H.writeInt32BE(h, offset); + H.writeInt32BE(l, offset + 4); + } - writeInt64BE(this._ah, this._al, 0) - writeInt64BE(this._bh, this._bl, 8) - writeInt64BE(this._ch, this._cl, 16) - writeInt64BE(this._dh, this._dl, 24) - writeInt64BE(this._eh, this._el, 32) - writeInt64BE(this._fh, this._fl, 40) + writeInt64BE(this._ah, this._al, 0); + writeInt64BE(this._bh, this._bl, 8); + writeInt64BE(this._ch, this._cl, 16); + writeInt64BE(this._dh, this._dl, 24); + writeInt64BE(this._eh, this._el, 32); + writeInt64BE(this._fh, this._fl, 40); - return H -} + return H; +}; -module.exports = Sha384 +module.exports = Sha384; diff --git a/sha512.js b/sha512.js index fb28f2f..9328a76 100644 --- a/sha512.js +++ b/sha512.js @@ -1,260 +1,382 @@ -var inherits = require('inherits') -var Hash = require('./hash') -var Buffer = require('safe-buffer').Buffer +'use strict'; + +var inherits = require('inherits'); +var Hash = require('./hash'); +var Buffer = require('safe-buffer').Buffer; var K = [ - 0x428a2f98, 0xd728ae22, 0x71374491, 0x23ef65cd, - 0xb5c0fbcf, 0xec4d3b2f, 0xe9b5dba5, 0x8189dbbc, - 0x3956c25b, 0xf348b538, 0x59f111f1, 0xb605d019, - 0x923f82a4, 0xaf194f9b, 0xab1c5ed5, 0xda6d8118, - 0xd807aa98, 0xa3030242, 0x12835b01, 0x45706fbe, - 0x243185be, 0x4ee4b28c, 0x550c7dc3, 0xd5ffb4e2, - 0x72be5d74, 0xf27b896f, 0x80deb1fe, 0x3b1696b1, - 0x9bdc06a7, 0x25c71235, 0xc19bf174, 0xcf692694, - 0xe49b69c1, 0x9ef14ad2, 0xefbe4786, 0x384f25e3, - 0x0fc19dc6, 0x8b8cd5b5, 0x240ca1cc, 0x77ac9c65, - 0x2de92c6f, 0x592b0275, 0x4a7484aa, 0x6ea6e483, - 0x5cb0a9dc, 0xbd41fbd4, 0x76f988da, 0x831153b5, - 0x983e5152, 0xee66dfab, 0xa831c66d, 0x2db43210, - 0xb00327c8, 0x98fb213f, 0xbf597fc7, 0xbeef0ee4, - 0xc6e00bf3, 0x3da88fc2, 0xd5a79147, 0x930aa725, - 0x06ca6351, 0xe003826f, 0x14292967, 0x0a0e6e70, - 0x27b70a85, 0x46d22ffc, 0x2e1b2138, 0x5c26c926, - 0x4d2c6dfc, 0x5ac42aed, 0x53380d13, 0x9d95b3df, - 0x650a7354, 0x8baf63de, 0x766a0abb, 0x3c77b2a8, - 0x81c2c92e, 0x47edaee6, 0x92722c85, 0x1482353b, - 0xa2bfe8a1, 0x4cf10364, 0xa81a664b, 0xbc423001, - 0xc24b8b70, 0xd0f89791, 0xc76c51a3, 0x0654be30, - 0xd192e819, 0xd6ef5218, 0xd6990624, 0x5565a910, - 0xf40e3585, 0x5771202a, 0x106aa070, 0x32bbd1b8, - 0x19a4c116, 0xb8d2d0c8, 0x1e376c08, 0x5141ab53, - 0x2748774c, 0xdf8eeb99, 0x34b0bcb5, 0xe19b48a8, - 0x391c0cb3, 0xc5c95a63, 0x4ed8aa4a, 0xe3418acb, - 0x5b9cca4f, 0x7763e373, 0x682e6ff3, 0xd6b2b8a3, - 0x748f82ee, 0x5defb2fc, 0x78a5636f, 0x43172f60, - 0x84c87814, 0xa1f0ab72, 0x8cc70208, 0x1a6439ec, - 0x90befffa, 0x23631e28, 0xa4506ceb, 0xde82bde9, - 0xbef9a3f7, 0xb2c67915, 0xc67178f2, 0xe372532b, - 0xca273ece, 0xea26619c, 0xd186b8c7, 0x21c0c207, - 0xeada7dd6, 0xcde0eb1e, 0xf57d4f7f, 0xee6ed178, - 0x06f067aa, 0x72176fba, 0x0a637dc5, 0xa2c898a6, - 0x113f9804, 0xbef90dae, 0x1b710b35, 0x131c471b, - 0x28db77f5, 0x23047d84, 0x32caab7b, 0x40c72493, - 0x3c9ebe0a, 0x15c9bebc, 0x431d67c4, 0x9c100d4c, - 0x4cc5d4be, 0xcb3e42b6, 0x597f299c, 0xfc657e2a, - 0x5fcb6fab, 0x3ad6faec, 0x6c44198c, 0x4a475817 -] - -var W = new Array(160) - -function Sha512 () { - this.init() - this._w = W - - Hash.call(this, 128, 112) + 0x428a2f98, + 0xd728ae22, + 0x71374491, + 0x23ef65cd, + 0xb5c0fbcf, + 0xec4d3b2f, + 0xe9b5dba5, + 0x8189dbbc, + 0x3956c25b, + 0xf348b538, + 0x59f111f1, + 0xb605d019, + 0x923f82a4, + 0xaf194f9b, + 0xab1c5ed5, + 0xda6d8118, + 0xd807aa98, + 0xa3030242, + 0x12835b01, + 0x45706fbe, + 0x243185be, + 0x4ee4b28c, + 0x550c7dc3, + 0xd5ffb4e2, + 0x72be5d74, + 0xf27b896f, + 0x80deb1fe, + 0x3b1696b1, + 0x9bdc06a7, + 0x25c71235, + 0xc19bf174, + 0xcf692694, + 0xe49b69c1, + 0x9ef14ad2, + 0xefbe4786, + 0x384f25e3, + 0x0fc19dc6, + 0x8b8cd5b5, + 0x240ca1cc, + 0x77ac9c65, + 0x2de92c6f, + 0x592b0275, + 0x4a7484aa, + 0x6ea6e483, + 0x5cb0a9dc, + 0xbd41fbd4, + 0x76f988da, + 0x831153b5, + 0x983e5152, + 0xee66dfab, + 0xa831c66d, + 0x2db43210, + 0xb00327c8, + 0x98fb213f, + 0xbf597fc7, + 0xbeef0ee4, + 0xc6e00bf3, + 0x3da88fc2, + 0xd5a79147, + 0x930aa725, + 0x06ca6351, + 0xe003826f, + 0x14292967, + 0x0a0e6e70, + 0x27b70a85, + 0x46d22ffc, + 0x2e1b2138, + 0x5c26c926, + 0x4d2c6dfc, + 0x5ac42aed, + 0x53380d13, + 0x9d95b3df, + 0x650a7354, + 0x8baf63de, + 0x766a0abb, + 0x3c77b2a8, + 0x81c2c92e, + 0x47edaee6, + 0x92722c85, + 0x1482353b, + 0xa2bfe8a1, + 0x4cf10364, + 0xa81a664b, + 0xbc423001, + 0xc24b8b70, + 0xd0f89791, + 0xc76c51a3, + 0x0654be30, + 0xd192e819, + 0xd6ef5218, + 0xd6990624, + 0x5565a910, + 0xf40e3585, + 0x5771202a, + 0x106aa070, + 0x32bbd1b8, + 0x19a4c116, + 0xb8d2d0c8, + 0x1e376c08, + 0x5141ab53, + 0x2748774c, + 0xdf8eeb99, + 0x34b0bcb5, + 0xe19b48a8, + 0x391c0cb3, + 0xc5c95a63, + 0x4ed8aa4a, + 0xe3418acb, + 0x5b9cca4f, + 0x7763e373, + 0x682e6ff3, + 0xd6b2b8a3, + 0x748f82ee, + 0x5defb2fc, + 0x78a5636f, + 0x43172f60, + 0x84c87814, + 0xa1f0ab72, + 0x8cc70208, + 0x1a6439ec, + 0x90befffa, + 0x23631e28, + 0xa4506ceb, + 0xde82bde9, + 0xbef9a3f7, + 0xb2c67915, + 0xc67178f2, + 0xe372532b, + 0xca273ece, + 0xea26619c, + 0xd186b8c7, + 0x21c0c207, + 0xeada7dd6, + 0xcde0eb1e, + 0xf57d4f7f, + 0xee6ed178, + 0x06f067aa, + 0x72176fba, + 0x0a637dc5, + 0xa2c898a6, + 0x113f9804, + 0xbef90dae, + 0x1b710b35, + 0x131c471b, + 0x28db77f5, + 0x23047d84, + 0x32caab7b, + 0x40c72493, + 0x3c9ebe0a, + 0x15c9bebc, + 0x431d67c4, + 0x9c100d4c, + 0x4cc5d4be, + 0xcb3e42b6, + 0x597f299c, + 0xfc657e2a, + 0x5fcb6fab, + 0x3ad6faec, + 0x6c44198c, + 0x4a475817 +]; + +var W = new Array(160); + +function Sha512() { + this.init(); + this._w = W; + + Hash.call(this, 128, 112); } -inherits(Sha512, Hash) +inherits(Sha512, Hash); Sha512.prototype.init = function () { - this._ah = 0x6a09e667 - this._bh = 0xbb67ae85 - this._ch = 0x3c6ef372 - this._dh = 0xa54ff53a - this._eh = 0x510e527f - this._fh = 0x9b05688c - this._gh = 0x1f83d9ab - this._hh = 0x5be0cd19 - - this._al = 0xf3bcc908 - this._bl = 0x84caa73b - this._cl = 0xfe94f82b - this._dl = 0x5f1d36f1 - this._el = 0xade682d1 - this._fl = 0x2b3e6c1f - this._gl = 0xfb41bd6b - this._hl = 0x137e2179 - - return this -} - -function Ch (x, y, z) { - return z ^ (x & (y ^ z)) + this._ah = 0x6a09e667; + this._bh = 0xbb67ae85; + this._ch = 0x3c6ef372; + this._dh = 0xa54ff53a; + this._eh = 0x510e527f; + this._fh = 0x9b05688c; + this._gh = 0x1f83d9ab; + this._hh = 0x5be0cd19; + + this._al = 0xf3bcc908; + this._bl = 0x84caa73b; + this._cl = 0xfe94f82b; + this._dl = 0x5f1d36f1; + this._el = 0xade682d1; + this._fl = 0x2b3e6c1f; + this._gl = 0xfb41bd6b; + this._hl = 0x137e2179; + + return this; +}; + +function Ch(x, y, z) { + return z ^ (x & (y ^ z)); } -function maj (x, y, z) { - return (x & y) | (z & (x | y)) +function maj(x, y, z) { + return (x & y) | (z & (x | y)); } -function sigma0 (x, xl) { - return (x >>> 28 | xl << 4) ^ (xl >>> 2 | x << 30) ^ (xl >>> 7 | x << 25) +function sigma0(x, xl) { + return ((x >>> 28) | (xl << 4)) ^ ((xl >>> 2) | (x << 30)) ^ ((xl >>> 7) | (x << 25)); } -function sigma1 (x, xl) { - return (x >>> 14 | xl << 18) ^ (x >>> 18 | xl << 14) ^ (xl >>> 9 | x << 23) +function sigma1(x, xl) { + return ((x >>> 14) | (xl << 18)) ^ ((x >>> 18) | (xl << 14)) ^ ((xl >>> 9) | (x << 23)); } -function Gamma0 (x, xl) { - return (x >>> 1 | xl << 31) ^ (x >>> 8 | xl << 24) ^ (x >>> 7) +function Gamma0(x, xl) { + return ((x >>> 1) | (xl << 31)) ^ ((x >>> 8) | (xl << 24)) ^ (x >>> 7); } -function Gamma0l (x, xl) { - return (x >>> 1 | xl << 31) ^ (x >>> 8 | xl << 24) ^ (x >>> 7 | xl << 25) +function Gamma0l(x, xl) { + return ((x >>> 1) | (xl << 31)) ^ ((x >>> 8) | (xl << 24)) ^ ((x >>> 7) | (xl << 25)); } -function Gamma1 (x, xl) { - return (x >>> 19 | xl << 13) ^ (xl >>> 29 | x << 3) ^ (x >>> 6) +function Gamma1(x, xl) { + return ((x >>> 19) | (xl << 13)) ^ ((xl >>> 29) | (x << 3)) ^ (x >>> 6); } -function Gamma1l (x, xl) { - return (x >>> 19 | xl << 13) ^ (xl >>> 29 | x << 3) ^ (x >>> 6 | xl << 26) +function Gamma1l(x, xl) { + return ((x >>> 19) | (xl << 13)) ^ ((xl >>> 29) | (x << 3)) ^ ((x >>> 6) | (xl << 26)); } -function getCarry (a, b) { - return (a >>> 0) < (b >>> 0) ? 1 : 0 +function getCarry(a, b) { + return (a >>> 0) < (b >>> 0) ? 1 : 0; } Sha512.prototype._update = function (M) { - var W = this._w - - var ah = this._ah | 0 - var bh = this._bh | 0 - var ch = this._ch | 0 - var dh = this._dh | 0 - var eh = this._eh | 0 - var fh = this._fh | 0 - var gh = this._gh | 0 - var hh = this._hh | 0 - - var al = this._al | 0 - var bl = this._bl | 0 - var cl = this._cl | 0 - var dl = this._dl | 0 - var el = this._el | 0 - var fl = this._fl | 0 - var gl = this._gl | 0 - var hl = this._hl | 0 - - for (var i = 0; i < 32; i += 2) { - W[i] = M.readInt32BE(i * 4) - W[i + 1] = M.readInt32BE(i * 4 + 4) - } - for (; i < 160; i += 2) { - var xh = W[i - 15 * 2] - var xl = W[i - 15 * 2 + 1] - var gamma0 = Gamma0(xh, xl) - var gamma0l = Gamma0l(xl, xh) - - xh = W[i - 2 * 2] - xl = W[i - 2 * 2 + 1] - var gamma1 = Gamma1(xh, xl) - var gamma1l = Gamma1l(xl, xh) - - // W[i] = gamma0 + W[i - 7] + gamma1 + W[i - 16] - var Wi7h = W[i - 7 * 2] - var Wi7l = W[i - 7 * 2 + 1] - - var Wi16h = W[i - 16 * 2] - var Wi16l = W[i - 16 * 2 + 1] - - var Wil = (gamma0l + Wi7l) | 0 - var Wih = (gamma0 + Wi7h + getCarry(Wil, gamma0l)) | 0 - Wil = (Wil + gamma1l) | 0 - Wih = (Wih + gamma1 + getCarry(Wil, gamma1l)) | 0 - Wil = (Wil + Wi16l) | 0 - Wih = (Wih + Wi16h + getCarry(Wil, Wi16l)) | 0 - - W[i] = Wih - W[i + 1] = Wil - } - - for (var j = 0; j < 160; j += 2) { - Wih = W[j] - Wil = W[j + 1] - - var majh = maj(ah, bh, ch) - var majl = maj(al, bl, cl) - - var sigma0h = sigma0(ah, al) - var sigma0l = sigma0(al, ah) - var sigma1h = sigma1(eh, el) - var sigma1l = sigma1(el, eh) - - // t1 = h + sigma1 + ch + K[j] + W[j] - var Kih = K[j] - var Kil = K[j + 1] - - var chh = Ch(eh, fh, gh) - var chl = Ch(el, fl, gl) - - var t1l = (hl + sigma1l) | 0 - var t1h = (hh + sigma1h + getCarry(t1l, hl)) | 0 - t1l = (t1l + chl) | 0 - t1h = (t1h + chh + getCarry(t1l, chl)) | 0 - t1l = (t1l + Kil) | 0 - t1h = (t1h + Kih + getCarry(t1l, Kil)) | 0 - t1l = (t1l + Wil) | 0 - t1h = (t1h + Wih + getCarry(t1l, Wil)) | 0 - - // t2 = sigma0 + maj - var t2l = (sigma0l + majl) | 0 - var t2h = (sigma0h + majh + getCarry(t2l, sigma0l)) | 0 - - hh = gh - hl = gl - gh = fh - gl = fl - fh = eh - fl = el - el = (dl + t1l) | 0 - eh = (dh + t1h + getCarry(el, dl)) | 0 - dh = ch - dl = cl - ch = bh - cl = bl - bh = ah - bl = al - al = (t1l + t2l) | 0 - ah = (t1h + t2h + getCarry(al, t1l)) | 0 - } - - this._al = (this._al + al) | 0 - this._bl = (this._bl + bl) | 0 - this._cl = (this._cl + cl) | 0 - this._dl = (this._dl + dl) | 0 - this._el = (this._el + el) | 0 - this._fl = (this._fl + fl) | 0 - this._gl = (this._gl + gl) | 0 - this._hl = (this._hl + hl) | 0 - - this._ah = (this._ah + ah + getCarry(this._al, al)) | 0 - this._bh = (this._bh + bh + getCarry(this._bl, bl)) | 0 - this._ch = (this._ch + ch + getCarry(this._cl, cl)) | 0 - this._dh = (this._dh + dh + getCarry(this._dl, dl)) | 0 - this._eh = (this._eh + eh + getCarry(this._el, el)) | 0 - this._fh = (this._fh + fh + getCarry(this._fl, fl)) | 0 - this._gh = (this._gh + gh + getCarry(this._gl, gl)) | 0 - this._hh = (this._hh + hh + getCarry(this._hl, hl)) | 0 -} + var w = this._w; + + var ah = this._ah | 0; + var bh = this._bh | 0; + var ch = this._ch | 0; + var dh = this._dh | 0; + var eh = this._eh | 0; + var fh = this._fh | 0; + var gh = this._gh | 0; + var hh = this._hh | 0; + + var al = this._al | 0; + var bl = this._bl | 0; + var cl = this._cl | 0; + var dl = this._dl | 0; + var el = this._el | 0; + var fl = this._fl | 0; + var gl = this._gl | 0; + var hl = this._hl | 0; + + for (var i = 0; i < 32; i += 2) { + w[i] = M.readInt32BE(i * 4); + w[i + 1] = M.readInt32BE((i * 4) + 4); + } + for (; i < 160; i += 2) { + var xh = w[i - (15 * 2)]; + var xl = w[i - (15 * 2) + 1]; + var gamma0 = Gamma0(xh, xl); + var gamma0l = Gamma0l(xl, xh); + + xh = w[i - (2 * 2)]; + xl = w[i - (2 * 2) + 1]; + var gamma1 = Gamma1(xh, xl); + var gamma1l = Gamma1l(xl, xh); + + // w[i] = gamma0 + w[i - 7] + gamma1 + w[i - 16] + var Wi7h = w[i - (7 * 2)]; + var Wi7l = w[i - (7 * 2) + 1]; + + var Wi16h = w[i - (16 * 2)]; + var Wi16l = w[i - (16 * 2) + 1]; + + var Wil = (gamma0l + Wi7l) | 0; + var Wih = (gamma0 + Wi7h + getCarry(Wil, gamma0l)) | 0; + Wil = (Wil + gamma1l) | 0; + Wih = (Wih + gamma1 + getCarry(Wil, gamma1l)) | 0; + Wil = (Wil + Wi16l) | 0; + Wih = (Wih + Wi16h + getCarry(Wil, Wi16l)) | 0; + + w[i] = Wih; + w[i + 1] = Wil; + } + + for (var j = 0; j < 160; j += 2) { + Wih = w[j]; + Wil = w[j + 1]; + + var majh = maj(ah, bh, ch); + var majl = maj(al, bl, cl); + + var sigma0h = sigma0(ah, al); + var sigma0l = sigma0(al, ah); + var sigma1h = sigma1(eh, el); + var sigma1l = sigma1(el, eh); + + // t1 = h + sigma1 + ch + K[j] + w[j] + var Kih = K[j]; + var Kil = K[j + 1]; + + var chh = Ch(eh, fh, gh); + var chl = Ch(el, fl, gl); + + var t1l = (hl + sigma1l) | 0; + var t1h = (hh + sigma1h + getCarry(t1l, hl)) | 0; + t1l = (t1l + chl) | 0; + t1h = (t1h + chh + getCarry(t1l, chl)) | 0; + t1l = (t1l + Kil) | 0; + t1h = (t1h + Kih + getCarry(t1l, Kil)) | 0; + t1l = (t1l + Wil) | 0; + t1h = (t1h + Wih + getCarry(t1l, Wil)) | 0; + + // t2 = sigma0 + maj + var t2l = (sigma0l + majl) | 0; + var t2h = (sigma0h + majh + getCarry(t2l, sigma0l)) | 0; + + hh = gh; + hl = gl; + gh = fh; + gl = fl; + fh = eh; + fl = el; + el = (dl + t1l) | 0; + eh = (dh + t1h + getCarry(el, dl)) | 0; + dh = ch; + dl = cl; + ch = bh; + cl = bl; + bh = ah; + bl = al; + al = (t1l + t2l) | 0; + ah = (t1h + t2h + getCarry(al, t1l)) | 0; + } + + this._al = (this._al + al) | 0; + this._bl = (this._bl + bl) | 0; + this._cl = (this._cl + cl) | 0; + this._dl = (this._dl + dl) | 0; + this._el = (this._el + el) | 0; + this._fl = (this._fl + fl) | 0; + this._gl = (this._gl + gl) | 0; + this._hl = (this._hl + hl) | 0; + + this._ah = (this._ah + ah + getCarry(this._al, al)) | 0; + this._bh = (this._bh + bh + getCarry(this._bl, bl)) | 0; + this._ch = (this._ch + ch + getCarry(this._cl, cl)) | 0; + this._dh = (this._dh + dh + getCarry(this._dl, dl)) | 0; + this._eh = (this._eh + eh + getCarry(this._el, el)) | 0; + this._fh = (this._fh + fh + getCarry(this._fl, fl)) | 0; + this._gh = (this._gh + gh + getCarry(this._gl, gl)) | 0; + this._hh = (this._hh + hh + getCarry(this._hl, hl)) | 0; +}; Sha512.prototype._hash = function () { - var H = Buffer.allocUnsafe(64) - - function writeInt64BE (h, l, offset) { - H.writeInt32BE(h, offset) - H.writeInt32BE(l, offset + 4) - } - - writeInt64BE(this._ah, this._al, 0) - writeInt64BE(this._bh, this._bl, 8) - writeInt64BE(this._ch, this._cl, 16) - writeInt64BE(this._dh, this._dl, 24) - writeInt64BE(this._eh, this._el, 32) - writeInt64BE(this._fh, this._fl, 40) - writeInt64BE(this._gh, this._gl, 48) - writeInt64BE(this._hh, this._hl, 56) - - return H -} - -module.exports = Sha512 + var H = Buffer.allocUnsafe(64); + + function writeInt64BE(h, l, offset) { + H.writeInt32BE(h, offset); + H.writeInt32BE(l, offset + 4); + } + + writeInt64BE(this._ah, this._al, 0); + writeInt64BE(this._bh, this._bl, 8); + writeInt64BE(this._ch, this._cl, 16); + writeInt64BE(this._dh, this._dl, 24); + writeInt64BE(this._eh, this._el, 32); + writeInt64BE(this._fh, this._fl, 40); + writeInt64BE(this._gh, this._gl, 48); + writeInt64BE(this._hh, this._hl, 56); + + return H; +}; + +module.exports = Sha512; diff --git a/test/hash.js b/test/hash.js index 5fa000d..fd84453 100644 --- a/test/hash.js +++ b/test/hash.js @@ -1,75 +1,80 @@ -var tape = require('tape') -var Hash = require('../hash') -var hex = '0A1B2C3D4E5F6G7H' +'use strict'; -function equal (t, a, b) { - t.equal(a.length, b.length) - t.equal(a.toString('hex'), b.toString('hex')) +var tape = require('tape'); +var Buffer = require('safe-buffer').Buffer; + +var Hash = require('../hash'); + +var hex = '0A1B2C3D4E5F6G7H'; + +function equal(t, a, b) { + t.equal(a.length, b.length); + t.equal(a.toString('hex'), b.toString('hex')); } -var hexBuf = Buffer.from('0A1B2C3D4E5F6G7H', 'utf8') +var hexBuf = Buffer.from('0A1B2C3D4E5F6G7H', 'utf8'); var count16 = { - strings: ['0A1B2C3D4E5F6G7H'], - buffers: [ - hexBuf, - Buffer.from('80000000000000000000000000000080', 'hex') - ] -} + strings: ['0A1B2C3D4E5F6G7H'], + buffers: [ + hexBuf, + Buffer.from('80000000000000000000000000000080', 'hex') + ] +}; var empty = { - strings: [''], - buffers: [ - Buffer.from('80000000000000000000000000000000', 'hex') - ] -} + strings: [''], + buffers: [ + Buffer.from('80000000000000000000000000000000', 'hex') + ] +}; var multi = { - strings: ['abcd', 'efhijk', 'lmnopq'], - buffers: [ - Buffer.from('abcdefhijklmnopq', 'ascii'), - Buffer.from('80000000000000000000000000000080', 'hex') - ] -} + strings: ['abcd', 'efhijk', 'lmnopq'], + buffers: [ + Buffer.from('abcdefhijklmnopq', 'ascii'), + Buffer.from('80000000000000000000000000000080', 'hex') + ] +}; var long = { - strings: [hex + hex], - buffers: [ - hexBuf, - hexBuf, - Buffer.from('80000000000000000000000000000100', 'hex') - ] -} + strings: [hex + hex], + buffers: [ + hexBuf, + hexBuf, + Buffer.from('80000000000000000000000000000100', 'hex') + ] +}; -function makeTest (name, data) { - tape(name, function (t) { - var h = new Hash(16, 8) - var hash = Buffer.alloc(20) - var n = 2 - var expected = data.buffers.slice() - // t.plan(expected.length + 1) +function makeTest(name, data) { + tape(name, function (t) { + var h = new Hash(16, 8); + var hash = Buffer.alloc(20); + var n = 2; + var expected = data.buffers.slice(); + // t.plan(expected.length + 1) - h._update = function (block) { - var e = expected.shift() - equal(t, block, e) + h._update = function (block) { + var e = expected.shift(); + equal(t, block, e); - if (n < 0) { - throw new Error('expecting only 2 calls to _update') - } - } - h._hash = function () { - return hash - } + if (n < 0) { + throw new Error('expecting only 2 calls to _update'); + } + }; + h._hash = function () { + return hash; + }; - data.strings.forEach(function (string) { - h.update(string, 'ascii') - }) + data.strings.forEach(function (string) { + h.update(string, 'ascii'); + }); - equal(t, h.digest(), hash) - t.end() - }) + equal(t, h.digest(), hash); + t.end(); + }); } -makeTest('Hash#update 1 in 1', count16) -makeTest('empty Hash#update', empty) -makeTest('Hash#update 1 in 3', multi) -makeTest('Hash#update 2 in 1', long) +makeTest('Hash#update 1 in 1', count16); +makeTest('empty Hash#update', empty); +makeTest('Hash#update 1 in 3', multi); +makeTest('Hash#update 2 in 1', long); diff --git a/test/test.js b/test/test.js index dac8580..f173252 100644 --- a/test/test.js +++ b/test/test.js @@ -1,100 +1,138 @@ -var crypto = require('crypto') -var tape = require('tape') -var Sha1 = require('../').sha1 +'use strict'; + +var crypto = require('crypto'); +var tape = require('tape'); +var Buffer = require('safe-buffer').Buffer; + +var Sha1 = require('../').sha1; + +var nodeSupportsUint16 = false; +try { + crypto.createHash('sha1').update(new Uint16Array()); + nodeSupportsUint16 = true; +} catch (err) {} var inputs = [ - ['', 'ascii'], - ['abc', 'ascii'], - ['123', 'ascii'], - ['123456789abcdef123456789abcdef123456789abcdef123456789abcdef', 'ascii'], - ['123456789abcdef123456789abcdef123456789abcdef123456789abc', 'ascii'], - ['123456789abcdef123456789abcdef123456789abcdef123456789ab', 'ascii'], - ['0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcde', 'ascii'], - ['0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef', 'ascii'], - ['foobarbaz', 'ascii'] -] + ['', 'ascii'], + ['abc', 'ascii'], + ['123', 'ascii'], + ['123456789abcdef123456789abcdef123456789abcdef123456789abcdef', 'ascii'], + ['123456789abcdef123456789abcdef123456789abcdef123456789abc', 'ascii'], + ['123456789abcdef123456789abcdef123456789abcdef123456789ab', 'ascii'], + ['0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcde', 'ascii'], + ['0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef', 'ascii'], + ['foobarbaz', 'ascii'], + [Buffer.from('buffer')], + nodeSupportsUint16 ? [new Uint16Array([1, 2, 3])] : null +].filter(Boolean); tape("hash is the same as node's crypto", function (t) { - inputs.forEach(function (v) { - var a = new Sha1().update(v[0], v[1]).digest('hex') - var e = crypto.createHash('sha1').update(v[0], v[1]).digest('hex') - console.log(a, '==', e) - t.equal(a, e) - }) + inputs.forEach(function (v) { + var a = new Sha1().update(v[0], v[1]).digest('hex'); + var e = crypto.createHash('sha1').update(v[0], v[1]).digest('hex'); + t.equal(a, e, a + ' == ' + e); + }); - t.end() -}) + t.end(); +}); tape('call update multiple times', function (t) { - inputs.forEach(function (v) { - var hash = new Sha1() - var _hash = crypto.createHash('sha1') - - for (var i = 0; i < v[0].length; i = (i + 1) * 2) { - var s = v[0].substring(i, (i + 1) * 2) - hash.update(s, v[1]) - _hash.update(s, v[1]) - } - - var a = hash.digest('hex') - var e = _hash.digest('hex') - console.log(a, '==', e) - t.equal(a, e) - }) - t.end() -}) + inputs.forEach(function (v) { + var hash = new Sha1(); + var sha1hash = crypto.createHash('sha1'); + + for (var i = 0; i < v[0].length; i = (i + 1) * 2) { + var s = v[0].slice(i, (i + 1) * 2); + hash.update(s, v[1]); + sha1hash.update(s, v[1]); + } + + var a = hash.digest('hex'); + var e = sha1hash.digest('hex'); + t.equal(a, e, a + ' == ' + e); + }); + t.end(); +}); tape('call update twice', function (t) { - var _hash = crypto.createHash('sha1') - var hash = new Sha1() + var sha1hash = crypto.createHash('sha1'); + var hash = new Sha1(); - _hash.update('foo', 'ascii') - hash.update('foo', 'ascii') + sha1hash.update('foo', 'ascii'); + hash.update('foo', 'ascii'); - _hash.update('bar', 'ascii') - hash.update('bar', 'ascii') + sha1hash.update('bar', 'ascii'); + hash.update('bar', 'ascii'); - _hash.update('baz', 'ascii') - hash.update('baz', 'ascii') + sha1hash.update('baz', 'ascii'); + hash.update('baz', 'ascii'); - var a = hash.digest('hex') - var e = _hash.digest('hex') + var a = hash.digest('hex'); + var e = sha1hash.digest('hex'); - t.equal(a, e) - t.end() -}) + t.equal(a, e); + t.end(); +}); tape('hex encoding', function (t) { - inputs.forEach(function (v) { - var hash = new Sha1() - var _hash = crypto.createHash('sha1') - - for (var i = 0; i < v[0].length; i = (i + 1) * 2) { - var s = v[0].substring(i, (i + 1) * 2) - hash.update(Buffer.from(s, 'ascii').toString('hex'), 'hex') - _hash.update(Buffer.from(s, 'ascii').toString('hex'), 'hex') - } - var a = hash.digest('hex') - var e = _hash.digest('hex') - - console.log(a, '==', e) - t.equal(a, e) - }) - - t.end() -}) + inputs.forEach(function (v) { + var hash = new Sha1(); + var sha1hash = crypto.createHash('sha1'); + + for (var i = 0; i < v[0].length; i = (i + 1) * 2) { + var s = v[0].slice(i, (i + 1) * 2); + hash.update(Buffer.from(s, 'ascii').toString('hex'), 'hex'); + sha1hash.update(Buffer.from(s, 'ascii').toString('hex'), 'hex'); + } + var a = hash.digest('hex'); + var e = sha1hash.digest('hex'); + + t.equal(a, e, a + ' == ' + e); + }); + + t.end(); +}); + +tape('throws on invalid input', function (t) { + var invalid = [ + {}, // non-arrayish + { length: 20 }, // undefined values + [NaN], // non-numbers + [[]], // non-numbers + [1, 1.5], // non-integers + [1, 256], // out of bounds + [-1, 0] // out of bounds + ]; + + invalid.forEach(function (input) { + var hash = new Sha1(); + + t['throws'](function () { + hash.update(input); + hash.digest('hex'); + }); + }); + + t.end(); +}); tape('call digest for more than MAX_UINT32 bits of data', function (t) { - var _hash = crypto.createHash('sha1') - var hash = new Sha1() - var bigData = Buffer.alloc(0x1ffffffff / 8) - - hash.update(bigData) - _hash.update(bigData) - - var a = hash.digest('hex') - var e = _hash.digest('hex') - - t.equal(a, e) - t.end() -}) + var sha1hash = crypto.createHash('sha1'); + var hash = new Sha1(); + var bigData; + try { + bigData = Buffer.alloc(0x1ffffffff / 8); + } catch (err) { + // node < 3 has a lower buffer size limit than node 3+. node 0.10 requires the `/8`, 0.12 - 2 are fine with `-8` + bigData = Buffer.alloc(0x3fffffff / 8); + } + + hash.update(bigData); + sha1hash.update(bigData); + + var a = hash.digest('hex'); + var e = sha1hash.digest('hex'); + + t.equal(a, e, a + ' == ' + e); + t.end(); +}); diff --git a/test/vectors.js b/test/vectors.js index 48a646e..25874f0 100644 --- a/test/vectors.js +++ b/test/vectors.js @@ -1,72 +1,72 @@ -var tape = require('tape') -var vectors = require('hash-test-vectors') +'use strict'; + +var tape = require('tape'); +var vectors = require('hash-test-vectors'); // var from = require('bops/typedarray/from') -var Buffer = require('safe-buffer').Buffer +var Buffer = require('safe-buffer').Buffer; -var createHash = require('../') +var createHash = require('../'); -function makeTest (alg, i, verbose) { - var v = vectors[i] +function makeTest(alg, i, verbose) { + var v = vectors[i]; - tape(alg + ': NIST vector ' + i, function (t) { - if (verbose) { - console.log(v) - console.log('VECTOR', i) - console.log('INPUT', v.input) - console.log(Buffer.from(v.input, 'base64').toString('hex')) - } + tape(alg + ': NIST vector ' + i, function (t) { + if (verbose) { + t.comment(v); + t.comment('VECTOR', i); + t.comment('INPUT', v.input); + t.comment(Buffer.from(v.input, 'base64').toString('hex')); + } - var buf = Buffer.from(v.input, 'base64') - t.equal(createHash(alg).update(buf).digest('hex'), v[alg]) + var buf = Buffer.from(v.input, 'base64'); + t.equal(createHash(alg).update(buf).digest('hex'), v[alg]); - i = ~~(buf.length / 2) - var buf1 = buf.slice(0, i) - var buf2 = buf.slice(i, buf.length) + // eslint-disable-next-line no-param-reassign + i = ~~(buf.length / 2); + var buf1 = buf.slice(0, i); + var buf2 = buf.slice(i, buf.length); - console.log(buf1.length, buf2.length, buf.length) - console.log(createHash(alg)._block.length) + t.comment(buf1.length + ', ' + buf2.length + ', ' + buf.length); + t.comment(createHash(alg)._block.length); - t.equal( - createHash(alg) - .update(buf1) - .update(buf2) - .digest('hex'), - v[alg] - ) + t.equal( + createHash(alg) + .update(buf1) + .update(buf2) + .digest('hex'), + v[alg] + ); - var j, buf3 + var j, buf3; - i = ~~(buf.length / 3) - j = ~~(buf.length * 2 / 3) - buf1 = buf.slice(0, i) - buf2 = buf.slice(i, j) - buf3 = buf.slice(j, buf.length) + // eslint-disable-next-line no-param-reassign + i = ~~(buf.length / 3); + j = ~~(buf.length * 2 / 3); + buf1 = buf.slice(0, i); + buf2 = buf.slice(i, j); + buf3 = buf.slice(j, buf.length); - t.equal( - createHash(alg) - .update(buf1) - .update(buf2) - .update(buf3) - .digest('hex'), - v[alg] - ) + t.equal( + createHash(alg) + .update(buf1) + .update(buf2) + .update(buf3) + .digest('hex'), + v[alg] + ); - setTimeout(function () { - // avoid "too much recursion" errors in tape in firefox - t.end() - }) - }) + setTimeout(function () { + // avoid "too much recursion" errors in tape in firefox + t.end(); + }); + }); } -if (process.argv[2]) { - makeTest(process.argv[2], parseInt(process.argv[3], 10), true) -} else { - vectors.forEach(function (v, i) { - makeTest('sha', i) - makeTest('sha1', i) - makeTest('sha224', i) - makeTest('sha256', i) - makeTest('sha384', i) - makeTest('sha512', i) - }) -} +vectors.forEach(function (v, i) { + makeTest('sha', i); + makeTest('sha1', i); + makeTest('sha224', i); + makeTest('sha256', i); + makeTest('sha384', i); + makeTest('sha512', i); +});