Skip to content

Commit

Permalink
📌 pin all dependencies to current version
Browse files Browse the repository at this point in the history
Ensures issues like #4 won't happen again
  • Loading branch information
oplik0 committed Jul 27, 2021
1 parent 480f6e1 commit 9dde68d
Show file tree
Hide file tree
Showing 10 changed files with 278 additions and 266 deletions.
3 changes: 3 additions & 0 deletions .releaserc.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
"releaseRules": {
"patch": {
"include": [":bento:", ":recycle:"]
},
"minor": {
"include": [":pushpin:"]
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"deno.enable": true
"deno.enable": true,
"deno.suggest.imports.hosts": {
"https://deno.land": false
}
}
53 changes: 27 additions & 26 deletions lib/_wasm/build.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,44 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
// adopted from https://deno.land/std/hash/_wasm/build.ts
import { encode as base64Encode } from "https://deno.land/std/encoding/base64.ts";
// adopted from https://deno.land/std@0.103.0/hash/_wasm/build.ts
import { encode as base64Encode } from "https://deno.land/std@0.103.0/encoding/base64.ts";

// 1. build wasm
async function buildWasm(path: string): Promise<void> {
const cmd = [
"wasm-pack",
"build",
"--target",
"web",
"--release",
"-d",
path,
];
const builder = Deno.run({ cmd });
const status = await builder.status();
const cmd = [
"wasm-pack",
"build",
"--target",
"web",
"--release",
"-d",
path,
];
const builder = Deno.run({ cmd });
const status = await builder.status();

if (!status.success) {
console.error(`Failed to build wasm: ${status.code}`);
Deno.exit(1);
}
if (!status.success) {
console.error(`Failed to build wasm: ${status.code}`);
Deno.exit(1);
}
}

// 2. encode wasm
async function encodeWasm(wasmPath: string): Promise<string> {
const wasm = await Deno.readFile(`${wasmPath}/scrypt_wasm_bg.wasm`);
return base64Encode(wasm);
const wasm = await Deno.readFile(`${wasmPath}/scrypt_wasm_bg.wasm`);
return base64Encode(wasm);
}

// 3. generate script
async function generate(wasm: string, output: string): Promise<void> {
const initScript = await Deno.readTextFile(`${output}/scrypt_wasm.js`);
const denoHashScript = "/* eslint-disable */\n" +
"//deno-fmt-ignore-file\n" +
`import * as base64 from "https://deno.land/std/encoding/base64.ts";` +
`export const source = base64.decode("${wasm}");` +
initScript;
const initScript = await Deno.readTextFile(`${output}/scrypt_wasm.js`);
const denoHashScript =
"/* eslint-disable */\n" +
"//deno-fmt-ignore-file\n" +
`import * as base64 from "https://deno.land/std@0.103.0/encoding/base64.ts";` +
`export const source = base64.decode("${wasm}");` +
initScript;

await Deno.writeFile("wasm.js", new TextEncoder().encode(denoHashScript));
await Deno.writeFile("wasm.js", new TextEncoder().encode(denoHashScript));
}

const OUTPUT_DIR = "./out";
Expand Down
78 changes: 44 additions & 34 deletions lib/_wasm/wasm.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions lib/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/**
* @todo document this module
*/
import { Sha256, HmacSha256 } from "https://deno.land/std/hash/sha256.ts";
import { Sha256, HmacSha256 } from "https://deno.land/std@0.103.0/hash/sha256.ts";
import {
encode,
decode,
} from "https://deno.land/std/encoding/base64.ts";
} from "https://deno.land/std@0.103.0/encoding/base64.ts";
// deno-fmt-ignore
export type logN =
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 |
Expand Down
2 changes: 1 addition & 1 deletion lib/helpers_test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
assertEquals,
} from "https://deno.land/std/testing/asserts.ts";
} from "https://deno.land/std@0.103.0/testing/asserts.ts";

import { decomposeFormat } from "./helpers.ts";

Expand Down
53 changes: 27 additions & 26 deletions lib/scrypt.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import init, { source, scrypt as scryptWASM } from "./_wasm/wasm.js";
import { encode as base64encode } from "https://deno.land/std/encoding/base64.ts";
import { encode as hexencode } from "https://deno.land/std/encoding/hex.ts";
import { encode as base64encode } from "https://deno.land/std@0.103.0/encoding/base64.ts";
import { encode as hexencode } from "https://deno.land/std@0.103.0/encoding/hex.ts";
const encoder: TextEncoder = new TextEncoder();
const decoder: TextDecoder = new TextDecoder("utf-8");
await init(source);
export type encoding = ("utf-8" | "base64" | "hex");
export type encoding = "utf-8" | "base64" | "hex";
/**
* Scrypt implementation using web assembly
* @param {string|Uint8Array} password - string to hash
* @param {string|Uint8Array} salt -
* @param {string|Uint8Array} salt -
* @param {number} N - CPU/Memory cost parameter. Must be a power of 2 smaller than 2^(128*r/8)
* @param {number} r - block size
* @param {number} p - parallelism factor
Expand All @@ -17,26 +17,27 @@ export type encoding = ("utf-8" | "base64" | "hex");
* @returns {Promise<string|Uint8Array>} - the resulting hash encoded according to outputEncoding
*/
export async function scrypt(
password: (string | Uint8Array),
salt: (string | Uint8Array),
N: number,
r: number,
p: number,
dklen?: number,
outputEncoding?: encoding,
): Promise<(Uint8Array | string)> {
dklen = dklen ?? 64;
password = typeof password === "string" ? encoder.encode(password) : password;
salt = typeof salt === "string" ? encoder.encode(salt) : salt;
const result: Uint8Array = scryptWASM(password, salt, N, r, p, dklen);
switch (outputEncoding) {
case "base64":
return base64encode(result);
case "hex":
return hexencode(result);
case "utf-8":
return decoder.decode(result);
default:
return result;
}
password: string | Uint8Array,
salt: string | Uint8Array,
N: number,
r: number,
p: number,
dklen?: number,
outputEncoding?: encoding
): Promise<Uint8Array | string> {
dklen = dklen ?? 64;
password =
typeof password === "string" ? encoder.encode(password) : password;
salt = typeof salt === "string" ? encoder.encode(salt) : salt;
const result: Uint8Array = scryptWASM(password, salt, N, r, p, dklen);
switch (outputEncoding) {
case "base64":
return base64encode(result);
case "hex":
return hexencode(result);
case "utf-8":
return decoder.decode(result);
default:
return result;
}
}
144 changes: 69 additions & 75 deletions lib/scrypt_bench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,102 +2,96 @@
* Just a few simple benchmarks
* @todo document the benchmarks better
*/
import { runBenchmarks, bench } from "https://deno.land/std/testing/bench.ts";
import {
runBenchmarks,
bench,
} from "https://deno.land/std@0.103.0/testing/bench.ts";
import { scrypt } from "./scrypt.ts";
const extremeSalt: string =
`IkjpewCbNm4A7cuY1VL9KC3y92blEthtLf9Cet6uEoTxz5cyF660da2zeci42fYwVCVwsKogETMCXupSZZAUh6a80ZnnBTk17B3UTCSVQPpYfL8GktJ2BDokE7ox2hV8OwwUT1hFvCuJqwHZpRvZw1RNCO6HfukPdgMHhq9rWLTXXUNwrIjlmkeydKGFJz2mS1xFcvLQtle4olJVK0SXXXYHAigBfpYxxSC2acvoxuacWcXhzSSRZAMysU2J7zDfXdxnYoqz50rvmvi36g7t2WDSAdzZ44JpxVcc3bYD7xYI3UgfVMPOfeblzwJi455QIurHzDuXEUNS0tZX1kWwZ0XcNSCwGzPs7WSVHxHc0KVUNhwSz16wDYFK4pYeA29ThXgFiFICSLVshiRrCfuzRthW7IZtRa9efcf4nFJsVBk51jpHY0b8CLhARrQU92mlBULwmJKe8DgST3Vn9rva98E9jk4y7NfSb4i9g74OjuFQ8yRO3BHksBZoVtBl4wUppM2hsLt72LZKA0ZsaWW7dG9a1bgWUkBBRG5OwzARenDqQIA2Gp5V4JsXuUUYNDylCelkLUVfS7hB1AZHtnIgwVqTaGDxl7nNZGKpAx6MrOd40laTUhrtZo4prwFZROHPNVJGQk2PQDgwqxX5SWoBTK8cCCzrbGBfHq9r8BwBvSVdeQ7bgjUW2j7NCapHHZ6filzxZaVsLsEITGZNcK0t5DdSnaDLRxyOn21ncKVIyZfOdlvpytvqpQaH5RWu4G50IPkEevue8KenXpGLP0pmEseBf6eX02rlN9arqZ4HJWmD7RbAChs7OJwfKlNIawb0V3G3N0eJeXiRsYOk10GIb91pyZRLSr2AJDtiWCcMuOWZfgLVHIrUVftfh9iXmRk2RAT1sigivbNtdqcF2cVvbTVMUCe7MIPRt4dGqwOQqdReGjPy9p1CNfKfJBIgW0xhYsOGMkcUqSurHxPl4wTOnMBx8vEZQsqJCZomENA1`;
const extremePassword: string =
`TFImeWrtF2kOIvDjG4P0ybmMrNOq0bQ0aERcC69iHflECWrwuSMO4JPD3Ng5HwNXZrCpHyEwviW8zly3WLsQ6zJ60lnfwhVRdkEQCsFiH4NvGl0tCAuty9Rruf47WHeE3GK7qAJwhcXHx3FCJgWN8KHdoy3vn2zUKJlhhjSFGANJdVYQGSaQTmtoJdhcemmYT5hprkALp7Q9vMwCk9hDvV5vB0evXfxqG0dFV3MPJmywwWAUJEi5MyM2Pio7fL50M5ohPWFmUllpa6G5pVBhi26GtOy6sM3GDGHmnohavtsMvTeRcMX1ds4HWA9U3vH7urQ3XGkCUzulB6WxuxHn8Z3fRz3BL6MZI0EReep2qUVaqJn8onzsI6da6pU6iDtRbufWxi0q8XN1S3BCtFGjzaTU12nvfg5js53PiSw1KUnZj2thKxWtnKcpwzbXdTuuZ9GVhZHIMcOXXrDR0rj539ZLAVyJmqwDOMjTsqPN7BY522PcJHoTElSRNRAsAsFx2m7h9brhcZXOgV1PZohJsdQS7RWhAl9EYBkgF8WCgGw9DXidVduIIHDlEd7mAVJfo9HYX85kFcwrLEpuPiFxfNhubeDpeBu2FAbAo6DNHFlqXUUnyKvMbzptcgisSr2V1pwykB6uLVrwx3AceRnyqg5flldmfsSKw0AFZ4PagGMJuFDMGrV29Vmqhv61SRL9in0ngZx0gJ2vKv26qS3jGN72UUsbkysuGNz6ul0D5jIapvIcCTncIiXSY24pPctxFsawcXvSNw4jEKccsHCTZF0gri6iFS7JqqQd87FNowbrug6sIWSwiWHYGN1VfSwuE5plQHVvNCHNZnMBBIoaMWh45lhtlfCWdUwVpjjK5dAUcOtKftJ2hcl4mIlxs7Fy8ASWhYvWAbpp3fRgmAeTRYAFEwMohN9b03iXyDSNFIeZtQoaL7HYFVWoXV4BfBVlR3CvNIwp6OPBAFFSDlSn9CZU06UziY1tSwqBzkCD`;
const extremeSalt: string = `IkjpewCbNm4A7cuY1VL9KC3y92blE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0tCAuty9Rruf47WHeE3GK7qAJwhcXHx3FCJgWN8KHdoy3vn2zUKJlhhjSFGANJdVYQGSaQTmtoJdhcemmYT5hprkALp7Q9vMwCk9hDvV5vB0evXfxqG0dFV3MPJmywwWAUJEi5MyM2Pio7fL50M5ohPWFmUllpa6G5pVBhi26GtOy6sM3GDGHmnohavtsMvTeRcMX1ds4HWA9U3vH7urQ3XGkCUzulB6WxuxHn8Z3fRz3BL6MZI0EReep2qUVaqJn8onzsI6da6pU6iDtRbufWxi0q8XN1S3BCtFGjzaTU12nvfg5js53PiSw1KUnZj2thKxWtnKcpwzbXdTuuZ9GVhZHIMcOXXrDR0rj539ZLAVyJmqwDOMjTsqPN7BY522PcJHoTElSRNRAsAsFx2m7h9brhcZXOgV1PZohJsdQS7RWhAl9EYBkgF8WCgGw9DXidVduIIHDlEd7mAVJfo9HYX85kFcwrLEpuPiFxfNhubeDpeBu2FAbAo6DNHFlqXUUnyKvMbzptcgisSr2V1pwykB6uLVrwx3AceRnyqg5flldmfsSKw0AFZ4PagGMJuFDMGrV29Vmqhv61SRL9in0ngZx0gJ2vKv26qS3jGN72UUsbkysuGNz6ul0D5jIapvIcCTncIiXSY24pPctxFsawcXvSNw4jEKccsHCTZF0gri6iFS7JqqQd87FNowbrug6sIWSwiWHYGN1VfSwuE5plQHVvNCHNZnMBBIoaMWh45lhtlfCWdUwVpjjK5dAUcOtKftJ2hcl4mIlxs7Fy8ASWhYvWAbpp3fRgmAeTRYAFEwMohN9b03iXyDSNFIeZtQoaL7HYFVWoXV4BfBVlR3CvNIwp6OPBAFFSDlSn9CZU06UziY1tSwqBzkCD`;

bench({
name: "small scrypt",
runs: 10,
async func(b): Promise<void> {
b.start();
await scrypt("password", "salt", 1024, 8, 1, 64);
b.stop();
},
name: "small scrypt",
runs: 10,
async func(b): Promise<void> {
b.start();
await scrypt("password", "salt", 1024, 8, 1, 64);
b.stop();
},
});
bench({
name: "small scrypt (longer password)",
runs: 10,
async func(b): Promise<void> {
b.start();
await scrypt(
"long password to test that",
"salt",
1024,
8,
1,
64,
);
b.stop();
},
name: "small scrypt (longer password)",
runs: 10,
async func(b): Promise<void> {
b.start();
await scrypt("long password to test that", "salt", 1024, 8, 1, 64);
b.stop();
},
});
bench({
name: "small scrypt (longer salt)",
runs: 100,
async func(b): Promise<void> {
b.start();
await scrypt("password", "long salt to test that", 1024, 8, 1, 64);
b.stop();
},
name: "small scrypt (longer salt)",
runs: 100,
async func(b): Promise<void> {
b.start();
await scrypt("password", "long salt to test that", 1024, 8, 1, 64);
b.stop();
},
});
bench({
name: "small scrypt (longer password and salt)",
runs: 100,
async func(b): Promise<void> {
b.start();
await scrypt(
"long password to test that",
"long salt to test that",
1024,
8,
1,
64,
);
b.stop();
},
name: "small scrypt (longer password and salt)",
runs: 100,
async func(b): Promise<void> {
b.start();
await scrypt(
"long password to test that",
"long salt to test that",
1024,
8,
1,
64
);
b.stop();
},
});

bench({
name: "small scrypt (extremely long salt)",
runs: 100,
async func(b): Promise<void> {
b.start();
await scrypt("password", extremeSalt, 1024, 8, 1, 64);
b.stop();
},
name: "small scrypt (extremely long salt)",
runs: 100,
async func(b): Promise<void> {
b.start();
await scrypt("password", extremeSalt, 1024, 8, 1, 64);
b.stop();
},
});

bench({
name: "small scrypt (extremely long password)",
runs: 100,
async func(b): Promise<void> {
b.start();
await scrypt(extremePassword, "NaCl", 1024, 8, 1, 64);
b.stop();
},
name: "small scrypt (extremely long password)",
runs: 100,
async func(b): Promise<void> {
b.start();
await scrypt(extremePassword, "NaCl", 1024, 8, 1, 64);
b.stop();
},
});

bench({
name: "small scrypt (extremely long password and salt)",
runs: 100,
async func(b): Promise<void> {
b.start();
await scrypt(extremePassword, extremeSalt, 1024, 8, 1, 64);
b.stop();
},
name: "small scrypt (extremely long password and salt)",
runs: 100,
async func(b): Promise<void> {
b.start();
await scrypt(extremePassword, extremeSalt, 1024, 8, 1, 64);
b.stop();
},
});

bench({
name: "standard scrypt",
runs: 20,
async func(b): Promise<void> {
b.start();
await scrypt("password", "NaCl", 16384, 8, 1, 64);
b.stop();
},
name: "standard scrypt",
runs: 20,
async func(b): Promise<void> {
b.start();
await scrypt("password", "NaCl", 16384, 8, 1, 64);
b.stop();
},
});

runBenchmarks();
Loading

0 comments on commit 9dde68d

Please sign in to comment.