Skip to content

Commit

Permalink
Merge pull request #663 from golemfactory/feature/JST-549/deprecate-f…
Browse files Browse the repository at this point in the history
…oreach-and-map

Refactor hashcat examples to use promise.any
  • Loading branch information
grisha87 authored Nov 16, 2023
2 parents effec9a + 24c88b9 commit 8726925
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 60 deletions.
48 changes: 23 additions & 25 deletions examples/docs-examples/tutorials/running-parallel-tasks/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,37 +19,35 @@ async function main(args) {
const step = Math.floor(keyspace / args.numberOfProviders + 1);
const range = [...Array(Math.floor(keyspace / step) + 1).keys()].map((i) => i * step);

const futureResults = range.map(async (skip = 0) => {
return executor.run(async (ctx) => {
const results = await ctx
const findPasswordInRange = async (skip) => {
const password = await executor.run(async (ctx) => {
const [, potfileResult] = await ctx
.beginBatch()
.run(
`hashcat -a 3 -m 400 '${args.hash}' '${args.mask}' --skip=${skip} --limit=${Math.min(
keyspace,
skip + step,
)} -o pass.potfile`,
`hashcat -a 3 -m 400 '${args.hash}' '${args.mask}' --skip=${skip} --limit=${
skip + step
} -o pass.potfile || true`,
)
.run("cat pass.potfile")
.end()
.catch((err) => console.error(err));
if (!results?.[1]?.stdout) return false;
return results?.[1]?.stdout.toString().split(":")[1];
.run("cat pass.potfile || true")
.end();
if (!potfileResult.stdout) return false;
// potfile format is: hash:password
return potfileResult.stdout.toString().trim().split(":")[1];
});
});

const results = await Promise.all(futureResults);

let password = "";
for (const result of results) {
if (result) {
password = result;
break;
if (!password) {
throw new Error(`Cannot find password in range ${skip} - ${skip + step}`);
}
return password;
};

try {
const password = await Promise.any(range.map(findPasswordInRange));
console.log(`Password found: ${password}`);
} catch (err) {
console.log(`Password not found`);
} finally {
await executor.end();
}

if (!password) console.log("No password found");
else console.log(`Password found: ${password}`);
await executor.end();
}

program
Expand Down
38 changes: 20 additions & 18 deletions examples/yacat/yacat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,33 +23,35 @@ async function main(args) {
const range = [...Array(Math.floor(keyspace / step)).keys()].map((i) => i * step);
console.log(`Keyspace size computed. Keyspace size = ${keyspace}. Tasks to compute = ${range.length}`);

const futureResults = range.map((skip) =>
executor.run(async (ctx) => {
const results = await ctx
const findPasswordInRange = async (skip: number) => {
const password = await executor.run(async (ctx) => {
const [, potfileResult] = await ctx
.beginBatch()
.run(
`hashcat -a 3 -m 400 '${args.hash}' '${args.mask}' --skip=${skip} --limit=${
skip! + step
skip + step
} -o pass.potfile || true`,
)
.run("cat pass.potfile || true")
.end();
if (!results?.[1]?.stdout) return false;
return results?.[1]?.stdout.toString().trim().split(":")[1];
}),
);
const results = await Promise.all(futureResults);

let password = "";
for await (const result of results) {
if (result) {
password = result;
break;
if (!potfileResult.stdout) return false;
// potfile format is: hash:password
return potfileResult.stdout.toString().trim().split(":")[1];
});
if (!password) {
throw new Error(`Cannot find password in range ${skip} - ${skip + step}`);
}
return password;
};

try {
const password = await Promise.any(range.map(findPasswordInRange));
console.log(`Password found: ${password}`);
} catch (err) {
console.log(`Password not found`);
} finally {
await executor.end();
}
if (!password) console.log("No password found");
else console.log(`Password found: ${password}`);
await executor.end();
}

program
Expand Down
33 changes: 16 additions & 17 deletions tests/e2e/yacat.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,28 +34,27 @@ describe("Password cracking", function () {
if (!keyspace) return;
const step = Math.floor(keyspace / 3);
const ranges = range(0, keyspace, step);
const futureResults = ranges.map((skip) =>
executor.run(async (ctx) => {
const results = await ctx

const findPasswordInRange = async (skip: number) => {
const password = await executor.run(async (ctx) => {
const [, potfileResult] = await ctx
.beginBatch()
.run(
`hashcat -a 3 -m 400 '${hash}' '${mask}' --skip=${skip} --limit=${skip! + step} -o pass.potfile -D 1,2`,
`hashcat -a 3 -m 400 '${hash}' '${mask}' --skip=${skip} --limit=${skip + step} -o pass.potfile || true`,
)
.run("cat pass.potfile")
.run("cat pass.potfile || true")
.end();
if (!results?.[1]?.stdout) return false;
return results?.[1]?.stdout.toString().split(":")?.[1]?.trim();
}),
);
const results = await Promise.all(futureResults);
let password = "";
for (const result of results) {
if (result) {
password = result;
break;
if (!potfileResult.stdout) return false;
// potfile format is: hash:password
return potfileResult.stdout.toString().trim().split(":")[1];
});
if (!password) {
throw new Error(`Cannot find password in range ${skip} - ${skip + step}`);
}
}
expect(password).toEqual("yo");
return password;
};

await expect(Promise.any(ranges.map(findPasswordInRange))).resolves.toEqual("yo");
await executor.end();
},
1000 * 60 * 5,
Expand Down

0 comments on commit 8726925

Please sign in to comment.