Skip to content

Commit

Permalink
chore: bump std dependencies and format with dprint
Browse files Browse the repository at this point in the history
  • Loading branch information
oplik0 committed Jun 13, 2022
1 parent a057803 commit aaa26ed
Show file tree
Hide file tree
Showing 11 changed files with 556 additions and 246 deletions.
2 changes: 1 addition & 1 deletion .releaserc.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@
"@semantic-release/github",
"@semantic-release/npm"
]
}
}
14 changes: 7 additions & 7 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"deno.enable": true,
"deno.suggest.imports.hosts": {
"https://deno.land": false
},
"deno.lint": true,
"deno.unstable": false
}
"deno.enable": true,
"deno.suggest.imports.hosts": {
"https://deno.land": false
},
"deno.lint": true,
"deno.unstable": false
}
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

This is a wasm-based (using rust-crypto) implementation of scrypt key derivation function that doesn't require any privileges.


[![Deno CI](https://github.com/denorg/scrypt/workflows/Deno%20CI/badge.svg)](https://github.com/denorg/scrypt/actions)
[![GitHub](https://img.shields.io/github/license/denorg/scrypt)](https://github.com/denorg/scrypt/blob/master/LICENSE)
[![Contributors](https://img.shields.io/github/contributors/denorg/scrypt)](https://github.com/denorg/scrypt/graphs/contributors)
Expand All @@ -11,7 +10,6 @@ This is a wasm-based (using rust-crypto) implementation of scrypt key derivation
[![TypeScript](https://img.shields.io/badge/types-TypeScript-blue)](https://github.com/denorg/scrypt)
[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)


## ⭐ Getting started

Import the `hash` and/or `verify` functions and use them:
Expand Down
2 changes: 1 addition & 1 deletion cli.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { hash, verify, genSalt } from "./mod.ts";
import { genSalt, hash, verify } from "./mod.ts";
/**
* @todo add a proper argument parser ([args](https://deno.land/x/args) perhaps?)
*/
Expand Down
83 changes: 69 additions & 14 deletions lib/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,73 @@
/**
* @todo document this module
*/
import { Sha256, HmacSha256 } from "https://deno.land/std@0.127.0/hash/sha256.ts";
import {
encode,
decode,
} from "https://deno.land/std@0.127.0/encoding/base64.ts";
import { decode, encode } from "https://deno.land/std@0.143.0/encoding/base64.ts";
import { HmacSha256, Sha256 } from "https://deno.land/std@0.143.0/hash/sha256.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 |
19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 |
34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 |
49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63;
export type logN =
| 1
| 2
| 3
| 4
| 5
| 6
| 7
| 8
| 9
| 10
| 11
| 12
| 13
| 14
| 15
| 16
| 17
| 18
| 19
| 20
| 21
| 22
| 23
| 24
| 25
| 26
| 27
| 28
| 29
| 30
| 31
| 32
| 33
| 34
| 35
| 36
| 37
| 38
| 39
| 40
| 41
| 42
| 43
| 44
| 45
| 46
| 47
| 48
| 49
| 50
| 51
| 52
| 53
| 54
| 55
| 56
| 57
| 58
| 59
| 60
| 61
| 62
| 63;
export interface ScryptParameters {
logN?: logN;
r?: number;
Expand All @@ -20,7 +76,7 @@ export interface ScryptParameters {
dklen?: number;
N?: number;
}
export type scryptFormat = ("scrypt" | "phc" | "raw");
export type scryptFormat = "scrypt" | "phc" | "raw";

export async function formatScrypt(
rawHash: string,
Expand Down Expand Up @@ -71,7 +127,7 @@ async function decomposeScrypt(
* @param {logN} logN - log2 of cost factor
* @param {number} r - block size
* @param {number} p - parallelism factor
* @param {string|Uint8Array} salt - salt used when hashing
* @param {string|Uint8Array} salt - salt used when hashing
*/
export async function formatPHC(
rawHash: string,
Expand All @@ -86,8 +142,7 @@ export async function formatPHC(
return `\$scrypt\$ln=${logN},r=${r},p=${p}\$${salt}\$${rawHash}`;
}
async function decomposePHC(formattedHash: string): Promise<ScryptParameters> {
const regex =
/\$scrypt\$ln=(?<logN>\d+),r=(?<r>\d+),p=(?<p>\d+)\$(?<salt>[a-zA-Z0-9\-\_\+\/\=]*)\$/;
const regex = /\$scrypt\$ln=(?<logN>\d+),r=(?<r>\d+),p=(?<p>\d+)\$(?<salt>[a-zA-Z0-9\-\_\+\/\=]*)\$/;
const parameters: ScryptParameters = formattedHash.match(regex)
?.groups as ScryptParameters;
parameters.salt = new Uint8Array(decode(parameters.salt as string));
Expand Down
49 changes: 40 additions & 9 deletions lib/helpers_test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import {
assertEquals,
} from "https://deno.land/std@0.127.0/testing/asserts.ts";
import { assertEquals } from "https://deno.land/std@0.143.0/testing/asserts.ts";

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

Expand All @@ -9,11 +7,44 @@ Deno.test("decompose scrypt with format detection", async (): Promise<void> => {
"c2NyeXB0AAwAAAAIAAAAAcQ0zwp7QNLklxCn14vB75AYWDIrrT9I/7F9+lVGBfKN/1TH2hs/HboSy1ptzN0YzMmobAXD3CqJJLRLaTK7nOHbjNTWA20LuUmGwEoJtonW",
);
// deno-fmt-ignore
const expectedParams = {logN:12, r:8, p:1, salt:new Uint8Array([
196, 52, 207, 10, 123, 64, 210, 228,
151, 16, 167, 215, 139, 193, 239, 144,
24, 88, 50, 43, 173, 63, 72, 255,
177, 125, 250, 85, 70, 5, 242, 141
])}
const expectedParams = {
logN: 12,
r: 8,
p: 1,
salt: new Uint8Array([
196,
52,
207,
10,
123,
64,
210,
228,
151,
16,
167,
215,
139,
193,
239,
144,
24,
88,
50,
43,
173,
63,
72,
255,
177,
125,
250,
85,
70,
5,
242,
141,
]),
};
assertEquals(params, expectedParams);
});
49 changes: 24 additions & 25 deletions lib/scrypt.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import init, { source, scrypt as scryptWASM } from "./_wasm/wasm.js";
import { encode as base64encode } from "https://deno.land/std@0.127.0/encoding/base64.ts";
import { encode as hexencode } from "https://deno.land/std@0.127.0/encoding/hex.ts";
import { encode as base64encode } from "https://deno.land/std@0.143.0/encoding/base64.ts";
import { encode as hexencode } from "https://deno.land/std@0.143.0/encoding/hex.ts";
import init, { scrypt as scryptWASM, source } from "./_wasm/wasm.js";
const encoder: TextEncoder = new TextEncoder();
const decoder: TextDecoder = new TextDecoder("utf-8");
await init(source);
Expand All @@ -17,27 +17,26 @@ 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
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;
}
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;
}
}
Loading

0 comments on commit aaa26ed

Please sign in to comment.