GithubHelp home page GithubHelp logo

chiefbiiko / deno-libhydrogen Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 0.0 195 KB

deno plugin 2 libhydrogen - broken due 2 changes in the deno plugin api - prs welcome

License: Other

TypeScript 63.06% Rust 36.94%

deno-libhydrogen's Introduction

deno-libhydrogen

deno+libhydrogen logo

deno plugin 2 libhydrogen

ci

import

import * as hydro from "https://denopkg.com/chiefbiiko/[email protected]/mod.ts";

or just import what you need

import { hash } from "https://denopkg.com/chiefbiiko/[email protected]/mod.ts";

auto-loading prebuilts

during import the module searches a shared library for your os within the directory ./.deno_plugins - configurable via environment variable DENO_PLUGINS. if the plugin can not be located a suitable prebuilt will be fetched from github releases.

api

following rust-libhydrogen's api amap

diffs

  • utils.pad(buf, blocksize) and utils.unpad(buf, blocksize) do not modify buf in place - rather return a modified copy of buf

namespaces

random

export namespace random {
  export const SEEDBYTES: number = 32;

  export class Seed {
    public readonly bufferview: Uint8Array;
    constructor(raw_seed?: Uint8Array);
    public static gen(): Seed;
  }

  export function buf(out_len: number): Uint8Array;
  export function buf_deterministic(out_len: number, seed: Seed): Uint8Array;
  export function buf_deterministic_into(out: Uint8Array, seed: Seed): void;
  export function buf_into(out: Uint8Array): void;
  export function ratchet(): void;
  export function reseed(): void;
  export function u32(): number;
  export function uniform(upper_bound: number): number;
}

example

const r: number = random.uniform(100);

const buf: Uint8Array = random.buf(r + 1);

hash

export namespace hash {
  export const BYTES: number = 32;
  export const BYTES_MAX: number = 65535;
  export const BYTES_MIN: number = 16;
  export const CONTEXTBYTES: number = 8;
  export const KEYBYTES: number = 32;

  export class Context {
    public readonly bufferview: Uint8Array;
    constructor(raw_context: string | Uint8Array);
    public static create(raw_context: string | Uint8Array): Context;
  }

  export class Key {
    public readonly bufferview: Uint8Array;
    constructor(raw_key?: Uint8Array);
    public static gen(): Key;
  }

  export interface DefaultHasher {
    update(input: Uint8Array): DefaultHasher;
    finish(out_len: number): Uint8Array;
    finish_into(out: Uint8Array): void;
  }

  export function init(context: Context, key?: Key): DefaultHasher;
  export function hash(out_len: number, input: Uint8Array, context: Context, key?: Key): Uint8Array;
  export function hash_into(out: Uint8Array, input: Uint8Array, context: Context, key?: Key): void;
}

example

const msg: Uint8Array = Uint8Array.from([65, 67, 65, 66]);
const context: hash.Context = hash.Context.create("examples");

const digest: Uint8Array = hash.hash(hash.BYTES, msg, context);

kdf

export namespace kdf {
  export const BYTES_MAX: number = 65535;
  export const BYTES_MIN: number = 16;
  export const CONTEXTBYTES: number = 8;
  export const KEYBYTES: number = 32;

  export class Context {
    public readonly bufferview: Uint8Array;
    constructor(raw_context: string | Uint8Array);
    public static create(raw_context: string | Uint8Array): Context;
  }

  export class Key {
    public readonly bufferview: Uint8Array;
    constructor(raw_key?: Uint8Array);
    public static gen(): Key;
  }

  export function derive_from_key(subkey_len: number, subkey_id: bigint, context: Context, key: Key): Uint8Array;
}

example

const context: kdf.Context = kdf.Context.create("examples");
const master_key: kdf.Key = kdf.Key.gen();

const subkey: Uint8Array = kdf.derive_from_key(32, 1n, context, master_key);

secretbox

export namespace secretbox {
  export const CONTEXTBYTES: number = 8;
  export const HEADERBYTES: number = 36;
  export const KEYBYTES: number = 32;
  export const PROBEBYTES: number = 16;

  export class Context {
    public readonly bufferview: Uint8Array;
    constructor(raw_context: string | Uint8Array);
    public static create(raw_context: string | Uint8Array): Context;
  }

  export class Key {
    public readonly bufferview: Uint8Array;
    constructor(raw_key?: Uint8Array);
    public static gen(): Key;
  }

  export class Probe {
    public readonly bufferview: Uint8Array;
    constructor(input: Uint8Array, context: Context, key: Key);
    public static create(input: Uint8Array, context: Context, key: Key): Probe;
    public verify(input: Uint8Array, context: Context, key: Key): void;
  }

  export function decrypt(input: Uint8Array, msg_id: bigint, context: Context, key: Key): Uint8Array;
  export function encrypt(input: Uint8Array, msg_id: bigint, context: Context, key: Key): Uint8Array;
}

note

secretbox.decrypt throws if the ciphertext/tag is invalid

example

const context: secretbox.Context = secretbox.Context.create("examples");
const key: secretbox.Key = secretbox.Key.gen();
const msg: Uint8Array = Uint8Array.from([65, 67, 65, 66]);

const ciphertext: Uint8Array = secretbox.encrypt(msg, 0n, context, key);
const plaintext: Uint8Array = secretbox.decrypt(ciphertext, 0n, context, key);

sign

export namespace sign {
  export const BYTES: number = 64;
  export const CONTEXTBYTES: number = 8;
  export const PUBLICKEYBYTES: number = 32;
  export const SECRETKEYBYTES: number = 64;
  export const SEEDBYTES: number = 32;

  export class Context {
    public readonly bufferview: Uint8Array;
    constructor(raw_context: string | Uint8Array);
    public static create(raw_context: string | Uint8Array): Context;
  }

  export class KeyPair {
    public readonly public_key: PublicKey;
    public readonly secret_key: SecretKey;
    constructor(raw_public_key: Uint8Array, raw_secret_key: Uint8Array);
    public static gen(): KeyPair;
  }

  export class PublicKey {
    public readonly bufferview: Uint8Array;
    constructor(raw_public_key: Uint8Array);
  }

  export class SecretKey {
    public readonly bufferview: Uint8Array;
    constructor(raw_secret_key: Uint8Array);
  }

  export class Signature {
    public readonly bufferview: Uint8Array;
    constructor(raw_signature: Uint8Array);
  }

  export interface Sign {
    update(input: Uint8Array): Sign;
    finish_create(secret_key: SecretKey): Signature;
    finish_verify(signature: Signature, public_key: PublicKey): void;
  }

  export function init(context: Context): Sign;
  export function create(input: Uint8Array, context: Context, secret_key: SecretKey): Signature;
  export function verify(signature: Signature, input: Uint8Array, context: Context, public_key: PublicKey): void;
}

note

sign.verify and Sign#finish_verify throw if the signature is invalid

example

const msg: Uint8Array = Uint8Array.from([65, 67, 65, 66]);

const context: sign.Context = sign.Context.create("example\0");
const keypair: sign.KeyPair = sign.KeyPair.gen();

const sig: sign.Signature = sign.create(msg, context, keypair.secret_key);

sign.verify(sig, msg, context, keypair.public_key);

kx

submodule bindings pending

pwhash

submodule bindings pending

utils

export namespace utils {
  export function bin2hex(buf: Uint8Array): string;
  export function compare(a: Uint8Array, b: Uint8Array): number;
  export function equal(a: Uint8Array, b: Uint8Array): boolean;
  export function hex2bin(hex: string, ignore: string = ""): Uint8Array;
  export function increment(buf: Uint8Array): void;
  export function memzero(buf: Uint8Array): void;
  export function pad(buf: Uint8Array, blocksize: number): Uint8Array;
  export function unpad(buf: Uint8Array, blocksize: number): Uint8Array;
}

example

const bin: Uint8Array = utils.hex2bin("abab");

utils.increment(bin);

const hex: string = utils.bin2hex(bin); // -> "acab"

errors

export namespace errors {
  export class HydroError extends Error {
    constructor();
  }
}

note

the errors.HydroError constructor does not have a message parameter

version

export namespace version {
  export function major(): number;
  export function minor(): number;
  export function string(): string;
}

example

const major: number = version.major();
const minor: number = version.minor();
const v: string = version.string();

security considerations

  • module throws only errors.HydroError - its instances have a fixed uninformative bogus message - to avoid leakage

    • still there is err.stack - make sure not to expose it to the outside of your application
  • deno-libhydrogen clears any internally allocated memory after use, both on deno and rust side, again - to avoid leakage

    • make sure to clear any secret memory owned by the parent application once you no longer need it, fx: utils.memzero(keypair.secret_key.bufferview)

license

MIT

deno-libhydrogen's People

Contributors

chiefbiiko avatar

Stargazers

 avatar

Watchers

 avatar  avatar

deno-libhydrogen's Issues

deno 1.2.0

error: TS2349 [ERROR]: This expression is not callable.
  Type '{ get(key: string): string | undefined; set(key: string, value: string): void; delete(key: string): void; toObject(): { [index: string]: string; }; }' has no call signatures.
const PLUGINS: string = Deno.env().DENO_PLUGINS?.trim()?.replace(/\/$/, "") ||
                             ~~~
    at https://raw.githubusercontent.com/chiefbiiko/deno-libhydrogen/v0.2.1/lib.ts:5:30

TS2367 [ERROR]: This condition will always return 'false' since the types '"darwin" | "linux" | "windows"' and '"win"' have no overlap.
const PLUGIN_NAME: string = (Deno.build.os === "win" ? "" : "lib") +
                             ~~~~~~~~~~~~~~~~~~~~~~~
    at https://raw.githubusercontent.com/chiefbiiko/deno-libhydrogen/v0.2.1/lib.ts:8:30

TS2367 [ERROR]: This condition will always return 'false' since the types '"darwin" | "linux" | "windows"' and '"win"' have no overlap.
  (Deno.build.os === "win"
   ~~~~~~~~~~~~~~~~~~~~~~~
    at https://raw.githubusercontent.com/chiefbiiko/deno-libhydrogen/v0.2.1/lib.ts:10:4

TS2367 [ERROR]: This condition will always return 'false' since the types '"darwin" | "linux" | "windows"' and '"mac"' have no overlap.
    : Deno.build.os === "mac" ? ".dylib" : ".so");
      ~~~~~~~~~~~~~~~~~~~~~~~
    at https://raw.githubusercontent.com/chiefbiiko/deno-libhydrogen/v0.2.1/lib.ts:12:7

TS2694 [ERROR]: Namespace 'Deno' has no exported member 'Plugin'.
export const plugin: Deno.Plugin = Deno.openPlugin(LOCAL);
                          ~~~~~~
    at https://raw.githubusercontent.com/chiefbiiko/deno-libhydrogen/v0.2.1/lib.ts:50:27

Found 5 errors.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.