Extract isArgon2idOptions
This commit is contained in:
parent
ef7d171a64
commit
a1b86ade81
@ -8,6 +8,7 @@ export {
|
||||
xchacha20NonceLength,
|
||||
Argon2id,
|
||||
Argon2idOptions,
|
||||
isArgon2idOptions,
|
||||
Ed25519,
|
||||
Ed25519Keypair,
|
||||
} from "./libsodium";
|
||||
|
||||
@ -3,6 +3,7 @@
|
||||
//
|
||||
// libsodium.js API: https://gist.github.com/webmaster128/b2dbe6d54d36dd168c9fabf441b9b09c
|
||||
|
||||
import { isNonNullObject } from "@cosmjs/utils";
|
||||
import sodium from "libsodium-wrappers";
|
||||
|
||||
export interface Argon2idOptions {
|
||||
@ -24,6 +25,14 @@ export interface Argon2idOptions {
|
||||
readonly memLimitKib: number;
|
||||
}
|
||||
|
||||
export function isArgon2idOptions(thing: unknown): thing is Argon2idOptions {
|
||||
if (!isNonNullObject(thing)) return false;
|
||||
if (typeof (thing as Argon2idOptions).outputLength !== "number") return false;
|
||||
if (typeof (thing as Argon2idOptions).opsLimit !== "number") return false;
|
||||
if (typeof (thing as Argon2idOptions).memLimitKib !== "number") return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
export class Argon2id {
|
||||
public static async execute(
|
||||
password: string,
|
||||
|
||||
1
packages/crypto/types/index.d.ts
vendored
1
packages/crypto/types/index.d.ts
vendored
@ -8,6 +8,7 @@ export {
|
||||
xchacha20NonceLength,
|
||||
Argon2id,
|
||||
Argon2idOptions,
|
||||
isArgon2idOptions,
|
||||
Ed25519,
|
||||
Ed25519Keypair,
|
||||
} from "./libsodium";
|
||||
|
||||
1
packages/crypto/types/libsodium.d.ts
vendored
1
packages/crypto/types/libsodium.d.ts
vendored
@ -16,6 +16,7 @@ export interface Argon2idOptions {
|
||||
*/
|
||||
readonly memLimitKib: number;
|
||||
}
|
||||
export declare function isArgon2idOptions(thing: unknown): thing is Argon2idOptions;
|
||||
export declare class Argon2id {
|
||||
static execute(password: string, salt: Uint8Array, options: Argon2idOptions): Promise<Uint8Array>;
|
||||
}
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
import {
|
||||
Argon2id,
|
||||
Argon2idOptions,
|
||||
isArgon2idOptions,
|
||||
Random,
|
||||
Sha256,
|
||||
Sha512,
|
||||
@ -9,7 +9,6 @@ import {
|
||||
Xchacha20poly1305Ietf,
|
||||
} from "@cosmjs/crypto";
|
||||
import { toAscii } from "@cosmjs/encoding";
|
||||
import { assert } from "@cosmjs/utils";
|
||||
|
||||
import { StdSignature } from "./types";
|
||||
|
||||
@ -82,11 +81,8 @@ export interface KdfConfiguration {
|
||||
export async function executeKdf(password: string, configuration: KdfConfiguration): Promise<Uint8Array> {
|
||||
switch (configuration.algorithm) {
|
||||
case "argon2id": {
|
||||
const { outputLength, opsLimit, memLimitKib } = configuration.params;
|
||||
assert(typeof outputLength === "number");
|
||||
assert(typeof opsLimit === "number");
|
||||
assert(typeof memLimitKib === "number");
|
||||
const options: Argon2idOptions = { outputLength, opsLimit, memLimitKib };
|
||||
const options = configuration.params;
|
||||
if (!isArgon2idOptions(options)) throw new Error("Invalid format of argon2id params");
|
||||
return Argon2id.execute(password, cosmjsSalt, options);
|
||||
}
|
||||
default:
|
||||
|
||||
Loading…
Reference in New Issue
Block a user