antelle / argon2-browser

Argon2 library compiled for browser runtime
https://antelle.net/argon2-browser
MIT License
377 stars 81 forks source link

TypeScript type definitions #69

Closed MrXyfir closed 3 years ago

MrXyfir commented 3 years ago

I wrote up a type definition file for this module to use in my project until I realized I wouldn't be able to use this package due to lacking WASM support in my environment. Oh well. Maybe someone will find it helpful or it can be integrated directly into this codebase!

declare module 'argon2-browser' {
  export enum Argon2Type {
    Argon2id = 2,
    Argon2i = 1,
    Argon2d = 0,
  }

  export interface Argon2Error {
    message?: string;
    code: number;
  }

  export interface Argon2Response {
    hashHex: string;
    encoded: string;
    hash: Uint8Array;
  }

  export interface Argon2HashOptions {
    pass: string;
    salt: string;
    /** Desired parallelism (however it won't be computed in parallel) */
    parallelism?: number;
    /** Desired hash length */
    hashLen?: number;
    /** Optional secret data */
    secret?: Uint8Array;
    /** The number of iterations */
    time?: number;
    type?: Argon2Type;
    /** Used memory, in KiB */
    mem?: number;
    /** Optional associated data */
    ad?: Uint8Array;
  }

  export function hash(opt: Argon2HashOptions): Promise<Argon2Response>;

  export interface Argon2VerifyOptions {
    encoded: string;
    /** Optional secret data */
    secret?: Uint8Array;
    type?: Argon2Type;
    pass: string;
    /** Optional associated data */
    ad?: Uint8Array;
  }

  export function verify(opt: Argon2VerifyOptions): Promise<Argon2Response>;

  export default {
    verify,
    hash,
  };
}

It might be as simple as saving this to argon2-browser.d.ts and then adding "types": "./argon2-browser.d.ts" in package.json although I'm not sure if the code above might need some restructuring. Maybe someone who actually needs this can figure it out, otherwise anyone who uses TypeScript can copy-paste this into their own project and it should work!

MrXyfir commented 3 years ago

Just realized this already exists: https://www.npmjs.com/package/@types/argon2-browser

It's just one of those days...