Skip to content

Commit

Permalink
chore: unit tests pass, switch to node test runner
Browse files Browse the repository at this point in the history
  • Loading branch information
broofa committed Jun 17, 2024
1 parent c2da22f commit 1ff5491
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 24 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
"@babel/preset-env": "7.24.6",
"@commitlint/cli": "19.3.0",
"@commitlint/config-conventional": "19.2.2",
"@types/jest": "29.5.12",
"@types/random-seed": "0.3.5",
"@wdio/browserstack-service": "7.16.10",
"@wdio/cli": "7.16.10",
Expand Down Expand Up @@ -117,7 +116,7 @@
"test:node": "npm-run-all --parallel examples:node:**",
"test:pack": "./scripts/testpack.sh",
"test:watch": "node --test --watch dist/esm/test",
"test": "BABEL_ENV=commonjsNode node --throw-deprecation node_modules/.bin/jest dist/esm/test"
"test": "node --test dist/esm/test"
},
"repository": {
"type": "git",
Expand Down
40 changes: 18 additions & 22 deletions src/md5-browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,24 @@
*/
function md5(bytes: Uint8Array) {
const words = uint8ToUint32(bytes);

const md5Bytes = wordsToMd5(words, bytes.length * 8);
return md5ToHexEncodedArray(md5Bytes);
return uint32ToUint8(md5Bytes);
}

/*
* Convert an array of little-endian words to an array of bytes
*/
function md5ToHexEncodedArray(input: Uint32Array) {
const byteLength = input.length * 4;
const bytes = new Uint8Array(byteLength);
const hexTab = '0123456789abcdef';

// TODO: Can we just create a Uint8Array on top of input.arrayBuffer?
for (let i = 0; i < byteLength; i++) {
const x = (input[i >> 2] >>> i % 4) & 0xff;

const hex = parseInt(hexTab.charAt((x >>> 4) & 0x0f) + hexTab.charAt(x & 0x0f), 16);

bytes[i] = hex;
function uint32ToUint8(input: Uint32Array) {
// Note: On little endian platforms we could simply return `new
// Uint8Array(input.buffer)` here, but that that won't work on big-endian
// systems. (That said, there's code below that appears to already assume
// little-endian, so maybe this is a moot point? Either way, keeping the
// existing code for now to be safe.)
const bytes = new Uint8Array(input.length * 4);
for (let i = 0; i < input.length * 4; i++) {
bytes[i] = (input[i >> 2] >>> ((i % 4) * 8)) & 0xff;
}

return bytes;
}

Expand All @@ -56,8 +53,11 @@ function getOutputLength(inputLength8: number) {
*/
function wordsToMd5(x: Uint32Array, len: number) {
/* append padding */
x[len >> 5] |= 0x80 << len % 32;
x[getOutputLength(len) - 1] = len;
const xpad = new Uint32Array(getOutputLength(len)).fill(0);
xpad.set(x);
xpad[len >> 5] |= 0x80 << len % 32;
xpad[xpad.length - 1] = len;
x = xpad;

let a = 1732584193;
let b = -271733879;
Expand Down Expand Up @@ -155,14 +155,10 @@ function uint8ToUint32(input: Uint8Array) {
return new Uint32Array();
}

const inputBitLength = input.length * 8;

const output = new Uint32Array(getOutputLength(inputBitLength)).fill(0);

const output = new Uint32Array(getOutputLength(input.length * 8)).fill(0);
for (let i = 0; i < input.length; i++) {
output[i >> 2] |= (input[i] & 0xff) << (i & 0x3);
output[i >> 2] |= (input[i] & 0xff) << ((i % 4) * 8);
}

return output;
}

Expand Down

0 comments on commit 1ff5491

Please sign in to comment.