Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DRAFT: calculate encoded length in js #39

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ set -eu
-sNO_FILESYSTEM \
-sMALLOC=emmalloc \
-sALLOW_MEMORY_GROWTH \
-sEXPORTED_FUNCTIONS=_malloc,_free,_argon2_hash,_argon2_verify,_argon2_error_message,_argon2_encodedlen \
-sEXPORTED_FUNCTIONS=_malloc,_free,_argon2_hash,_argon2_verify,_argon2_error_message \
-DARGON2_NO_THREADS \
--cache=../.emscripten \
-Iinclude \
Expand Down
48 changes: 34 additions & 14 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ export interface Argon2TryVerifyError {

export type Argon2TryVerifyResult = Argon2TryVerifySuccess | Argon2TryVerifyError;

export const typeStringLength = (type: Argon2Type): number => {
switch (type) {
case Argon2Type.Argon2d:
case Argon2Type.Argon2i:
return 7;
case Argon2Type.Argon2id:
return 8;
}
};

export const typeFromEncoded = (encoded: string): Argon2Type | undefined => {
if (!encoded?.length) return;
if (encoded.startsWith("$argon2d$")) return Argon2Type.Argon2d;
Expand Down Expand Up @@ -145,6 +155,23 @@ export const generateSalt = (length: number): Uint8Array => {
// eslint-disable-next-line @typescript-eslint/no-empty-function
const noop = () => {};

const numberLength = (value: number) => Math.ceil(Math.log10(value + 1));

const base64Length = (len: number) => Math.trunc((len * 4 + 3 - 1) / 3);

const encodedConstantLength = "$$v=$m=,t=,p=$$".length;

const encodedLength = (opts: Argon2HashOptions, saltLength: number) =>
encodedConstantLength +
typeStringLength(opts.type) +
numberLength(opts.timeCost) +
numberLength(opts.memoryCost) +
numberLength(opts.parallelism) +
base64Length(saltLength) +
base64Length(opts.hashLength) +
numberLength(opts.version) +
1;

class Argon2 {
readonly #exports: Argon2Exports;
readonly #encoder = new TextEncoder();
Expand Down Expand Up @@ -214,25 +241,18 @@ class Argon2 {
...options
};

const salt = options?.salt ?? generateSalt(16);

const error = validateHashOptions(opts);
if (error) return { success: false, error };

const salt = opts.salt ?? generateSalt(16);

const encodedLength = this.#exports.argon2_encodedlen(
opts.timeCost,
opts.memoryCost,
opts.parallelism,
salt.length,
opts.hashLength,
opts.type
);
const $encodedLength = encodedLength(opts, salt.length);

using passwordPtr = this.#copyStringToHeap(password);
using saltPtr = this.#copyToHeap(salt);

using hashPtr = this.#malloc(opts.hashLength + 1);
using encodedPtr = this.#malloc(encodedLength);
using hashPtr = this.#malloc(opts.hashLength);
using encodedPtr = this.#malloc($encodedLength);

const result = this.#exports.argon2_hash(
opts.timeCost,
Expand All @@ -245,15 +265,15 @@ class Argon2 {
hashPtr.ptr,
opts.hashLength,
encodedPtr.ptr,
encodedLength,
$encodedLength,
opts.type,
opts.version
);

if (result !== 0) return { success: false, error: this.#errorMessage(result) };

const hash = this.#copyFromHeap(hashPtr.ptr, opts.hashLength);
const encoded = this.#fromCString(encodedPtr.ptr, encodedLength - 1);
const encoded = this.#fromCString(encodedPtr.ptr, $encodedLength - 1);

return { success: true, data: { encoded, hash } };
};
Expand Down