From 21d9ac02ca0ab3d974faf4519eb41580f128c7e6 Mon Sep 17 00:00:00 2001 From: David Papp Date: Sun, 17 Mar 2024 12:15:17 +0100 Subject: [PATCH] feat: Add new hash functions and some small changes --- .github/workflows/publish.yaml | 19 +++ .github/workflows/test.yaml | 4 +- README.md | 17 ++- example/src/index.ts | 32 ++++- example/wrangler.toml | 2 +- package.json | 11 +- pnpm-lock.yaml | 239 ++++++++++++++++++++++++++++++++- src/index.ts | 32 ++++- tests/index.test.ts | 54 +++++++- tsconfig.json | 2 +- 10 files changed, 394 insertions(+), 18 deletions(-) create mode 100644 .github/workflows/publish.yaml diff --git a/.github/workflows/publish.yaml b/.github/workflows/publish.yaml new file mode 100644 index 0000000..a6dc78e --- /dev/null +++ b/.github/workflows/publish.yaml @@ -0,0 +1,19 @@ +name: npm-publish +on: + workflow_dispatch: +jobs: + build: + runs-on: ubuntu-latest + permissions: + contents: read + id-token: write + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-node@v3 + with: + node-version: '18.x' + registry-url: 'https://registry.npmjs.org' + - run: npm ci + - run: npm publish --provenance --access public + env: + NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} \ No newline at end of file diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 34f6d4d..2beee14 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -1,4 +1,4 @@ -name: Sync service prod +name: Test and Build on: push: @@ -25,4 +25,4 @@ jobs: cache: 'pnpm' - run: pnpm install - run: pnpm run build - - run: NODE_ENV=test pnpm test + - run: pnpm test diff --git a/README.md b/README.md index f40a76a..7058bab 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,10 @@ -# Hash function for Cloudflare Workers +# Hash functions for Cloudflare Workers + +[![NPM](https://nodei.co/npm/cf-workers-hash.png?stars&downloads)](https://nodei.co/npm/cf-workers-hash/) + +![GitHub CI](https://github.com/pigri/cf-workers-hash/actions/workflows/test.yaml/badge.svg) + +![NPM Publish](https://github.com/pigri/cf-workers-hash/actions/workflows/publish.yaml/badge.svg) ### What is this? This is a simple hash function library for Cloudflare Workers. It supports the following hash functions: @@ -7,11 +13,11 @@ This is a simple hash function library for Cloudflare Workers. It supports the f - SHA-384 - SHA-512 - MD5 -- BASE64 +- BASE64 / BASE64-DECODE - XXHASH32 - XXHASH64 - BCRYPT / BCRYPT-COMPARE -- WHIRLPOOL +- WHIRLPOOL / WHIRLPOOL-DECODE - SHA3-224 - SHA3-256 - SHA3-384 @@ -20,6 +26,9 @@ This is a simple hash function library for Cloudflare Workers. It supports the f - KECCAK256 - KECCAK384 - KECCAK512 +- MD6 +- CRC32 +- CRC32C ### How to install? ``` @@ -31,7 +40,7 @@ npm install cf-workers-hash Check the `example` directory. ``` -import { sha1, sha256, sha384, sha512, base64, md5, xxhash64, xxhash32, bcrypt, bcryptCompare, whirlpool, sha3_512, sha3_224, sha3_256, sha3_384, keccak512, keccak384, keccak256, keccak224 } from 'cf-workers-hash'; +import { sha1, sha256, sha384, sha512, base64, base64Decode, md5, xxhash64, xxhash32, bcrypt, bcryptCompare, whirlpool, whirlpoolDecode, sha3_512, sha3_224, sha3_256, sha3_384, keccak512, keccak384, keccak256, keccak224, md6, crc32, crc32c } from 'cf-workers-hash'; await sha1('test'); ``` diff --git a/example/src/index.ts b/example/src/index.ts index 0322630..4b6c616 100644 --- a/example/src/index.ts +++ b/example/src/index.ts @@ -1,4 +1,26 @@ -import { sha1, sha256, sha384, sha512, base64, md5, xxhash64, xxhash32, bcrypt, whirlpool, sha3_512, sha3_224, sha3_256, sha3_384, keccak512, keccak384, keccak256, keccak224 } from '../../dist'; +import { + sha1, + sha256, + sha384, + sha512, + base64, + md5, + xxhash64, + xxhash32, + bcrypt, + whirlpool, + sha3_512, + sha3_224, + sha3_256, + sha3_384, + keccak512, + keccak384, + keccak256, + keccak224, + md6, + crc32, + crc32c +} from '../../dist'; export default { async fetch(_: Request) { @@ -21,6 +43,9 @@ export default { const hashKeccak384 = await keccak384(testString); const hashKeccak256 = await keccak256(testString); const hashKeccak224 = await keccak224(testString); + const hashMd6 = await md6(testString); + const hashCrc32 = await crc32(testString); + const hashCrc32c = await crc32c(testString); const data = { testString, @@ -41,7 +66,10 @@ export default { hashKeccak512, hashKeccak384, hashKeccak256, - hashKeccak224 + hashKeccak224, + hashMd6, + hashCrc32, + hashCrc32c }; return Response.json(data); diff --git a/example/wrangler.toml b/example/wrangler.toml index 025be93..5ad93a2 100644 --- a/example/wrangler.toml +++ b/example/wrangler.toml @@ -1,7 +1,7 @@ name = "cf-worker-hash-example" main = "src/index.ts" compatibility_date = "2024-03-04" -compatibility_flags = [ "nodejs_compat", "nodejs_als"] +node_compat = true workers_dev = true #logpush = true diff --git a/package.json b/package.json index b8c1781..a988264 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "cf-workers-hash", - "version": "1.0.1", + "version": "1.0.2", "description": "A simple hash function for Cloudflare Workers", "main": "./dist/index.js", "module": "./dist/index.mjs", @@ -22,10 +22,14 @@ "cloudflare", "hash", "crypto", - "cf" + "cf", + "workers", + "cf-workers", + "cloudflare-workers" ], "devDependencies": { "@cloudflare/workers-types": "4.20240314.0", + "@swc/core": "1.4.8", "@types/bcryptjs": "2.4.6", "@types/jest": "29.5.12", "@types/node": "20.11.28", @@ -52,9 +56,12 @@ }, "homepage": "https://github.com/pigri/cf-workers-hash#readme", "dependencies": { + "@aws-crypto/crc32": "5.2.0", + "@aws-crypto/crc32c": "5.2.0", "bcryptjs": "2.4.3", "js-md5": "0.8.3", "js-sha3": "0.9.3", + "md6-hash": "1.0.0", "whirlpool-hash": "1.1.6", "xxhashjs": "0.2.2" } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index c0523d0..f6675d2 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -8,15 +8,30 @@ importers: .: dependencies: + '@aws-crypto/crc32': + specifier: ^5.2.0 + version: 5.2.0 + '@aws-crypto/crc32c': + specifier: ^5.2.0 + version: 5.2.0 bcryptjs: specifier: 2.4.3 version: 2.4.3 + crc-32: + specifier: 1.2.2 + version: 1.2.2 + js-crc: + specifier: ^0.2.0 + version: 0.2.0 js-md5: specifier: 0.8.3 version: 0.8.3 js-sha3: specifier: 0.9.3 version: 0.9.3 + md6-hash: + specifier: 1.0.0 + version: 1.0.0 whirlpool-hash: specifier: 1.1.6 version: 1.1.6 @@ -27,6 +42,9 @@ importers: '@cloudflare/workers-types': specifier: 4.20240314.0 version: 4.20240314.0 + '@swc/core': + specifier: 1.4.8 + version: 1.4.8 '@types/bcryptjs': specifier: 2.4.6 version: 2.4.6 @@ -62,7 +80,7 @@ importers: version: 29.1.2(@babel/core@7.24.0)(esbuild@0.17.19)(jest@29.7.0)(typescript@5.4.2) tsup: specifier: 8.0.2 - version: 8.0.2(typescript@5.4.2) + version: 8.0.2(@swc/core@1.4.8)(typescript@5.4.2) typescript: specifier: 5.4.2 version: 5.4.2 @@ -122,6 +140,39 @@ packages: '@jridgewell/trace-mapping': 0.3.25 dev: true + /@aws-crypto/crc32@5.2.0: + resolution: {integrity: sha512-nLbCWqQNgUiwwtFsen1AdzAtvuLRsQS8rYgMuxCrdKf9kOssamGLuPwyTY9wyYblNr9+1XM8v6zoDTPPSIeANg==} + engines: {node: '>=16.0.0'} + dependencies: + '@aws-crypto/util': 5.2.0 + '@aws-sdk/types': 3.535.0 + tslib: 2.6.2 + dev: false + + /@aws-crypto/crc32c@5.2.0: + resolution: {integrity: sha512-+iWb8qaHLYKrNvGRbiYRHSdKRWhto5XlZUEBwDjYNf+ly5SVYG6zEoYIdxvf5R3zyeP16w4PLBn3rH1xc74Rag==} + dependencies: + '@aws-crypto/util': 5.2.0 + '@aws-sdk/types': 3.535.0 + tslib: 2.6.2 + dev: false + + /@aws-crypto/util@5.2.0: + resolution: {integrity: sha512-4RkU9EsI6ZpBve5fseQlGNUWKMa1RLPQ1dnjnQoe07ldfIzcsGb5hC5W0Dm7u423KWzawlrpbjXBrXCEv9zazQ==} + dependencies: + '@aws-sdk/types': 3.535.0 + '@smithy/util-utf8': 2.3.0 + tslib: 2.6.2 + dev: false + + /@aws-sdk/types@3.535.0: + resolution: {integrity: sha512-aY4MYfduNj+sRR37U7XxYR8wemfbKP6lx00ze2M2uubn7mZotuVrWYAafbMSXrdEMSToE5JDhr28vArSOoLcSg==} + engines: {node: '>=14.0.0'} + dependencies: + '@smithy/types': 2.12.0 + tslib: 2.6.2 + dev: false + /@babel/code-frame@7.23.5: resolution: {integrity: sha512-CgH3s1a96LipHCmSUmYFPwY7MNx8C3avkq7i4Wl3cfa662ldtUe4VM1TPXX70pfmrlWTb6jLqTYrZyT2ZTJBgA==} engines: {node: '>=6.9.0'} @@ -1594,6 +1645,159 @@ packages: '@sinonjs/commons': 3.0.1 dev: true + /@smithy/is-array-buffer@2.2.0: + resolution: {integrity: sha512-GGP3O9QFD24uGeAXYUjwSTXARoqpZykHadOmA8G5vfJPK0/DC67qa//0qvqrJzL1xc8WQWX7/yc7fwudjPHPhA==} + engines: {node: '>=14.0.0'} + dependencies: + tslib: 2.6.2 + dev: false + + /@smithy/types@2.12.0: + resolution: {integrity: sha512-QwYgloJ0sVNBeBuBs65cIkTbfzV/Q6ZNPCJ99EICFEdJYG50nGIY/uYXp+TbsdJReIuPr0a0kXmCvren3MbRRw==} + engines: {node: '>=14.0.0'} + dependencies: + tslib: 2.6.2 + dev: false + + /@smithy/util-buffer-from@2.2.0: + resolution: {integrity: sha512-IJdWBbTcMQ6DA0gdNhh/BwrLkDR+ADW5Kr1aZmd4k3DIF6ezMV4R2NIAmT08wQJ3yUK82thHWmC/TnK/wpMMIA==} + engines: {node: '>=14.0.0'} + dependencies: + '@smithy/is-array-buffer': 2.2.0 + tslib: 2.6.2 + dev: false + + /@smithy/util-utf8@2.3.0: + resolution: {integrity: sha512-R8Rdn8Hy72KKcebgLiv8jQcQkXoLMOGGv5uI1/k0l+snqkOzQ1R0ChUBCxWMlBsFMekWjq0wRudIweFs7sKT5A==} + engines: {node: '>=14.0.0'} + dependencies: + '@smithy/util-buffer-from': 2.2.0 + tslib: 2.6.2 + dev: false + + /@swc/core-darwin-arm64@1.4.8: + resolution: {integrity: sha512-hhQCffRTgzpTIbngSnC30vV6IJVTI9FFBF954WEsshsecVoCGFiMwazBbrkLG+RwXENTrMhgeREEFh6R3KRgKQ==} + engines: {node: '>=10'} + cpu: [arm64] + os: [darwin] + requiresBuild: true + dev: true + optional: true + + /@swc/core-darwin-x64@1.4.8: + resolution: {integrity: sha512-P3ZBw8Jr8rKhY/J8d+6WqWriqngGTgHwtFeJ8MIakQJTbdYbFgXSZxcvDiERg3psbGeFXaUaPI0GO6BXv9k/OQ==} + engines: {node: '>=10'} + cpu: [x64] + os: [darwin] + requiresBuild: true + dev: true + optional: true + + /@swc/core-linux-arm-gnueabihf@1.4.8: + resolution: {integrity: sha512-PP9JIJt19bUWhAGcQW6qMwTjZOcMyzkvZa0/LWSlDm0ORYVLmDXUoeQbGD3e0Zju9UiZxyulnpjEN0ZihJgPTA==} + engines: {node: '>=10'} + cpu: [arm] + os: [linux] + requiresBuild: true + dev: true + optional: true + + /@swc/core-linux-arm64-gnu@1.4.8: + resolution: {integrity: sha512-HvEWnwKHkoVUr5iftWirTApFJ13hGzhAY2CMw4lz9lur2m+zhPviRRED0FCI6T95Knpv7+8eUOr98Z7ctrG6DQ==} + engines: {node: '>=10'} + cpu: [arm64] + os: [linux] + requiresBuild: true + dev: true + optional: true + + /@swc/core-linux-arm64-musl@1.4.8: + resolution: {integrity: sha512-kY8+qa7k/dEeBq9p0Hrta18QnJPpsiJvDQSLNaTIFpdM3aEM9zbkshWz8gaX5VVGUEALowCBUWqmzO4VaqM+2w==} + engines: {node: '>=10'} + cpu: [arm64] + os: [linux] + requiresBuild: true + dev: true + optional: true + + /@swc/core-linux-x64-gnu@1.4.8: + resolution: {integrity: sha512-0WWyIw432wpO/zeGblwq4f2YWam4pn8Z/Ig4KzHMgthR/KmiLU3f0Z7eo45eVmq5vcU7Os1zi/Zb65OOt09q/w==} + engines: {node: '>=10'} + cpu: [x64] + os: [linux] + requiresBuild: true + dev: true + optional: true + + /@swc/core-linux-x64-musl@1.4.8: + resolution: {integrity: sha512-p4yxvVS05rBNCrBaSTa20KK88vOwtg8ifTW7ec/yoab0bD5EwzzB8KbDmLLxE6uziFa0sdjF0dfRDwSZPex37Q==} + engines: {node: '>=10'} + cpu: [x64] + os: [linux] + requiresBuild: true + dev: true + optional: true + + /@swc/core-win32-arm64-msvc@1.4.8: + resolution: {integrity: sha512-jKuXihxAaqUnbFfvPxtmxjdJfs87F1GdBf33il+VUmSyWCP4BE6vW+/ReDAe8sRNsKyrZ3UH1vI5q1n64csBUA==} + engines: {node: '>=10'} + cpu: [arm64] + os: [win32] + requiresBuild: true + dev: true + optional: true + + /@swc/core-win32-ia32-msvc@1.4.8: + resolution: {integrity: sha512-O0wT4AGHrX8aBeH6c2ADMHgagAJc5Kf6W48U5moyYDAkkVnKvtSc4kGhjWhe1Yl0sI0cpYh2In2FxvYsb44eWw==} + engines: {node: '>=10'} + cpu: [ia32] + os: [win32] + requiresBuild: true + dev: true + optional: true + + /@swc/core-win32-x64-msvc@1.4.8: + resolution: {integrity: sha512-C2AYc3A2o+ECciqsJWRgIpp83Vk5EaRzHe7ed/xOWzVd0MsWR+fweEsyOjlmzHfpUxJSi46Ak3/BIZJlhZbXbg==} + engines: {node: '>=10'} + cpu: [x64] + os: [win32] + requiresBuild: true + dev: true + optional: true + + /@swc/core@1.4.8: + resolution: {integrity: sha512-uY2RSJcFPgNOEg12RQZL197LZX+MunGiKxsbxmh22VfVxrOYGRvh4mPANFlrD1yb38CgmW1wI6YgIi8LkIwmWg==} + engines: {node: '>=10'} + requiresBuild: true + peerDependencies: + '@swc/helpers': ^0.5.0 + peerDependenciesMeta: + '@swc/helpers': + optional: true + dependencies: + '@swc/counter': 0.1.3 + '@swc/types': 0.1.5 + optionalDependencies: + '@swc/core-darwin-arm64': 1.4.8 + '@swc/core-darwin-x64': 1.4.8 + '@swc/core-linux-arm-gnueabihf': 1.4.8 + '@swc/core-linux-arm64-gnu': 1.4.8 + '@swc/core-linux-arm64-musl': 1.4.8 + '@swc/core-linux-x64-gnu': 1.4.8 + '@swc/core-linux-x64-musl': 1.4.8 + '@swc/core-win32-arm64-msvc': 1.4.8 + '@swc/core-win32-ia32-msvc': 1.4.8 + '@swc/core-win32-x64-msvc': 1.4.8 + dev: true + + /@swc/counter@0.1.3: + resolution: {integrity: sha512-e2BR4lsJkkRlKZ/qCHPw9ZaSxc0MVUd7gtbtaB7aMvHeJVYe8sOB8DBZkP2DtISHGSku9sCK6T6cnY0CtXrOCQ==} + dev: true + + /@swc/types@0.1.5: + resolution: {integrity: sha512-myfUej5naTBWnqOCc/MdVOLVjXUXtIA+NpDrDBKJtLLg2shUjBu3cZmB/85RyitKc55+lUUyl7oRfLOvkr2hsw==} + dev: true + /@types/babel__core@7.20.5: resolution: {integrity: sha512-qoQprZvz5wQFJwMDqeseRXWv3rqMvhgpbXFfVyWhbx9X47POIA6i/+dXefEmZKoAgOaTdaIgNSMqMIU61yRyzA==} dependencies: @@ -2100,6 +2304,12 @@ packages: engines: {node: '>= 0.6'} dev: true + /crc-32@1.2.2: + resolution: {integrity: sha512-ROmzCKrTnOwybPcJApAA6WBWij23HVfGVNKqqrZpuyZOHqK2CwHSvpGuyt/UNNvaIjEd8X5IFGp4Mh+Ie1IHJQ==} + engines: {node: '>=0.8'} + hasBin: true + dev: false + /create-jest@29.7.0(@types/node@20.11.28): resolution: {integrity: sha512-Adz2bdH0Vq3F53KEMJOoftQFutWCukm6J24wbPWRO4k1kMY7gS7ds/uoJkNuV8wDCtWWnuwGcJwpWcih+zEW1Q==} engines: {node: ^14.15.0 || ^16.10.0 || >=18.0.0} @@ -3345,6 +3555,10 @@ packages: engines: {node: '>=10'} dev: true + /js-crc@0.2.0: + resolution: {integrity: sha512-8DdCSAOACpF8WDAjyDFBC2rj8OS4HUP9mNZBDfl8jCiPCnJG+2bkuycalxwZh6heFy6PrMvoWTp47lp6gzT65A==} + dev: false + /js-md5@0.8.3: resolution: {integrity: sha512-qR0HB5uP6wCuRMrWPTrkMaev7MJZwJuuw4fnwAzRgP4J4/F8RwtodOKpGp4XpqsLBFzzgqIO42efFAyz2Et6KQ==} dev: false @@ -3510,6 +3724,13 @@ packages: tmpl: 1.0.5 dev: true + /md6-hash@1.0.0: + resolution: {integrity: sha512-sc0IZ6yKaEvSD42cePg1cGi5tTOhMRRoDr+lqRQqnfHxrY+Pk6mTF+4JkCGEh6lbXDYZ5CdHFo/xFro3/sUhuA==} + engines: {node: '>=8'} + dependencies: + ow: 0.15.1 + dev: false + /merge-stream@2.0.0: resolution: {integrity: sha512-abv/qOcuPfk3URPfDzmZU1LKmuw8kT+0nIHvKrKgFrwifol/doWcdA4ZqsWQ8ENrFKkd67Mfpo/LovbIUsbt3w==} dev: true @@ -3689,6 +3910,13 @@ packages: type-check: 0.4.0 dev: true + /ow@0.15.1: + resolution: {integrity: sha512-rwiuvCnk3Ug9T4s5oKzw3QXQSiTXlTUiQgHmZ9Ozw/37YzeX8LycosVKOtO3v5+fuARGmCgz9rVhaBJeGV+2bQ==} + engines: {node: '>=8'} + dependencies: + type-fest: 0.8.1 + dev: false + /p-limit@2.3.0: resolution: {integrity: sha512-//88mFWSJx8lxCzwdAABTJL2MyWB12+eIY7MDL2SqLmAkeKU9qxRvWuSyTjm3FUmpBEMuFfckAIqEaVGUDxb6w==} engines: {node: '>=6'} @@ -4297,9 +4525,8 @@ packages: /tslib@2.6.2: resolution: {integrity: sha512-AEYxH93jGFPn/a2iVAwW87VuUIkR1FVUKB77NwMF7nBTDkDrrT/Hpt/IrCJ0QXhW27jTBDcf5ZY7w6RiqTMw2Q==} - dev: true - /tsup@8.0.2(typescript@5.4.2): + /tsup@8.0.2(@swc/core@1.4.8)(typescript@5.4.2): resolution: {integrity: sha512-NY8xtQXdH7hDUAZwcQdY/Vzlw9johQsaqf7iwZ6g1DOUlFYQ5/AtVAjTvihhEyeRlGo4dLRVHtrRaL35M1daqQ==} engines: {node: '>=18'} hasBin: true @@ -4318,6 +4545,7 @@ packages: typescript: optional: true dependencies: + '@swc/core': 1.4.8 bundle-require: 4.0.2(esbuild@0.19.12) cac: 6.7.14 chokidar: 3.6.0 @@ -4360,6 +4588,11 @@ packages: engines: {node: '>=10'} dev: true + /type-fest@0.8.1: + resolution: {integrity: sha512-4dbzIzqvjtgiM5rw1k5rEHtBANKmdudhGyBEajN01fEyhaAIhsoKNy6y7+IN93IfpFtwY9iqi7kD+xwKhQsNJA==} + engines: {node: '>=8'} + dev: false + /typescript@5.4.2: resolution: {integrity: sha512-+2/g0Fds1ERlP6JsakQQDXjZdZMM+rqpamFZJEKh4kwTIn3iDkgKtby0CeNd5ATNZ4Ry1ax15TMx0W2V+miizQ==} engines: {node: '>=14.17'} diff --git a/src/index.ts b/src/index.ts index 2ffba31..d32af3a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,8 +1,13 @@ +/* @typescript-eslint/no-unused-functions */ + import * as bcryptjs from 'bcryptjs'; import * as md5js from 'js-md5'; import * as xxh from 'xxhashjs'; import * as whirlpooljs from 'whirlpool-hash'; import * as sha3 from 'js-sha3'; +import md6Hash from 'md6-hash'; +import { Crc32 } from '@aws-crypto/crc32'; +import { Crc32c } from '@aws-crypto/crc32c'; function cryptoHasher(hashType: string) { return async (input: string): Promise => { @@ -22,6 +27,10 @@ export function base64(input: string): Promise { return Promise.resolve(btoa(input)); } +export function base64Decode(input: string): Promise { + return Promise.resolve(atob(input)); +} + export const sha1 = cryptoHasher('SHA-1'); export const sha256 = cryptoHasher('SHA-256'); export const sha384 = cryptoHasher('SHA-384'); @@ -59,7 +68,28 @@ export async function bcryptCompare(input: string, hash: string): Promise { const hasher = new whirlpooljs.Whirlpool(); const hash = hasher.getHash(input) - return whirlpooljs.encoders.toHex(hash.toString()).toUpperCase(); + return base64(whirlpooljs.encoders.toHex(hash.toString()).toUpperCase()); +} + +export async function whirlpoolDecode(input: string): Promise { + const hasher = new whirlpooljs.Whirlpool(); + return whirlpooljs.encoders.fromBase64(input); +} + +export async function md6(input: string, size: number = 256, key: string = "", levels: number = 64): Promise { + return md6Hash(input,size, key, levels) +} + +export async function crc32(input: string): Promise { + const encoder = new TextEncoder(); + const data = encoder.encode(input); + return Promise.resolve((new Crc32()).update(data).digest()); +} + +export async function crc32c(input: string): Promise { + const encoder = new TextEncoder(); + const data = encoder.encode(input); + return Promise.resolve((new Crc32c()).update(data).digest()); } export const sha3_512 = (input: string): Promise => Promise.resolve(sha3.sha3_512(input)); diff --git a/tests/index.test.ts b/tests/index.test.ts index 113a409..5df9de2 100644 --- a/tests/index.test.ts +++ b/tests/index.test.ts @@ -1,4 +1,30 @@ -import { sha1, sha256, sha384, sha512, base64, md5, xxhash64, xxhash32, bcrypt, bcryptCompare, whirlpool, sha3_512, sha3_224, sha3_256, sha3_384, keccak512, keccak384, keccak256, keccak224 } from '../dist'; +import { + sha1, + sha256, + sha384, + sha512, + base64, + md5, + xxhash64, + xxhash32, + bcrypt, + bcryptCompare, + whirlpool, + sha3_512, + sha3_224, + sha3_256, + sha3_384, + keccak512, + keccak384, + keccak256, + keccak224, + whirlpoolDecode, + base64Decode, + md6, + crc32, + crc32c +} from '../dist'; + test('hasher SHA-1', async () => { const result = await sha1('test'); expect(result).toBe('a94a8fe5ccb19ba61c4c0873d391e987982fbbd3'); @@ -34,6 +60,11 @@ test('hasher base64', async () => { expect(result).toBe('dGVzdA=='); }); +test('hasher base64 decode', async () => { + const result = await base64Decode('dGVzdA=='); + expect(result).toBe('test'); +}); + test('hasher md5', async () => { const result = await md5('test'); expect(result).toBe('098f6bcd4621d373cade4e832627b4f6'); @@ -63,6 +94,11 @@ test('hasher bcrypt compare false', async () => { test('hasher whirlpool', async () => { const result = await whirlpool('test'); + expect(result).toBe('QjkxM0Q1QkJCOEU0NjFDMkM1OTYxQ0JFMEVEQ0RBREZEMjlGMDY4MjI1Q0VCMzdEQTZERUZDRjg5ODQ5MzY4RjhDNkMyRUI2QTRDNEFDNzU3NzVEMDMyQTBFQ0ZERkU4NTUwNTczMDYyQjY1M0ZFOTJGQzdCOEZCM0I3QkU4RDY='); +}); + +test('hasher whirlpool decode', async () => { + const result = await whirlpoolDecode('QjkxM0Q1QkJCOEU0NjFDMkM1OTYxQ0JFMEVEQ0RBREZEMjlGMDY4MjI1Q0VCMzdEQTZERUZDRjg5ODQ5MzY4RjhDNkMyRUI2QTRDNEFDNzU3NzVEMDMyQTBFQ0ZERkU4NTUwNTczMDYyQjY1M0ZFOTJGQzdCOEZCM0I3QkU4RDY='); expect(result).toBe('B913D5BBB8E461C2C5961CBE0EDCDADFD29F068225CEB37DA6DEFCF89849368F8C6C2EB6A4C4AC75775D032A0ECFDFE8550573062B653FE92FC7B8FB3B7BE8D6'); }); @@ -86,7 +122,6 @@ test('hasher sha3_512', async () => { expect(result).toBe('9ece086e9bac491fac5c1d1046ca11d737b92a2b2ebd93f005d7b710110c0a678288166e7fbe796883a4f2e9b3ca9f484f521d0ce464345cc1aec96779149c14'); }); - test('hasher keccak512', async () => { const result = await keccak512('test'); expect(result).toBe('1e2e9fc2002b002d75198b7503210c05a1baac4560916a3c6d93bcce3a50d7f00fd395bf1647b9abb8d1afcc9c76c289b0c9383ba386a956da4b38934417789e'); @@ -106,3 +141,18 @@ test('hasher keccak224', async () => { const result = await keccak224('test'); expect(result).toBe('3be30a9ff64f34a5861116c5198987ad780165f8366e67aff4760b5e'); }); + +test('hasher md6', async () => { + const result = await md6('test'); + expect(result).toBe('93c8a7d0ff132f325138a82b2baa98c12a7c9ac982feb6c5b310a1ca713615bd'); +}); + +test('hasher crc32', async () => { + const result = await crc32('test'); + expect(result).toBe(3632233996); +}); + +test('hasher crc32c', async () => { + const result = await crc32c('test'); + expect(result).toBe(2258662080); +}); \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json index d05fc05..34e57bb 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,6 +1,6 @@ { "compilerOptions": { - "target": "esnext", + "target": "es6", "module": "commonjs", "moduleResolution": "node", "outDir": "./lib",