JSPM

  • Created
  • Published
  • Downloads 2833215
  • Score
    100M100P100Q208387F
  • License MIT

All the cryptographic primitives used in Ethereum

Package Exports

  • ethereum-cryptography
  • ethereum-cryptography/keccak.js
  • ethereum-cryptography/pure/keccak.js
  • ethereum-cryptography/pure/scrypt.js
  • ethereum-cryptography/scrypt.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (ethereum-cryptography) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

ethereum-cryptography

npm version Travis CI license

This npm package contains all the cryptographic primitives normally used when developing Javascript/TypeScript applications and tools for Ethereum.

This package contains pure-js implementations of these primitives:

  • keccak
  • scrypt
  • sha256
  • ripemd160
  • secp256k1

Installation

Via npm:

$ npm install ethereum-cryptography

Via yarn:

$ yarn add ethereum-cryptography

Usage

There's a submodule available for each cryprographic primitive.

No index.js/main is provided, as that would lead to huge bundles when using this package for the web.

keccak submodule

The keccack submodule has four functions that receive a Buffer with the message to hash, and return a Buffer with the hash. These are keccak224, keccak256, keccak384, and keccak512.

Function types

function keccak224(msg: Buffer): Buffer;

function keccak256(msg: Buffer): Buffer;

function keccak384(msg: Buffer): Buffer;

function keccak512(msg: Buffer): Buffer;

Example usage

const { keccak256 } = require("ethereum-cryptography/keccak");

console.log(keccak256(Buffer.from("Hello, world!", "ascii")).toString("hex"));

scrypt submodule

The scrypt submodule has two functions implementing the scrypt hash algorithm in synchronous and asynchronous ways. This algorithm is very slow, and using the synchronous version in the browser is not recommended, as it will block its main thread and hang your ui.

Password encoding

Encoding passwords is a frequent source of errors. Please read these notes before using this submodule.

Function types

function scrypt(password: Buffer, salt: Buffer, n: number, p: number, r: number, dklen: number): Buffer;

function scryptAsync(password: Buffer, salt: Buffer, n: number, p: number, r: number, dklen: number): Promise<Buffer>;

Example usage

const { scrypt } = require("ethereum-cryptography/scrypt");

console.log(
  scrypt(
    Buffer.from("ascii password", "ascii"),
    Buffer.from("AAAA", "hex"),
    16,
    1,
    1,
    64
  ).toString("hex")
);

sha256 submodule

The sha256 submodule contains a single functions implementing the sha256 hash algorithm.

Function types

function sha256(msg: Buffer): Buffer;

Example usage

const { sha256 } = require("ethereum-cryptography/sha256");

console.log(sha256(Buffer.from("message", "ascii")).toString("hex"));

ripemd160 submodule

The ripemd160 submodule contains a single functions implementing the ripemd160 hash algorithm.

Function types

function ripemd160(msg: Buffer): Buffer;

Example usage

const { ripemd160 } = require("ethereum-cryptography/ripemd160");

console.log(ripemd160(Buffer.from("message", "ascii")).toString("hex"));

secp256k1 submodule

The secp256k1 submodule has the same API than the native module secp256k1 from cryptocoinjs version 3.x, but it's backed by elliptic.

Function types

Consult secp256k1's documentation.

Example usage

const { sign } = require("ethereum-cryptography/secp256k1");

const msgHash = Buffer.from(
  "82ff40c0a986c6a5cfad4ddf4c3aa6996f1a7837f9c398e17e5de5cbd5a12b28",
  "hex"
);

const privateKey = Buffer.from(
  "3c9229289a6125f7fdf1885a77bb12c37a8d3b4962d936f7e3084dece32a3ca1",
  "hex"
);

console.log(sign(msgHash, privateKey).toString("hex"));

Browser usage

This package works with all the major Javascript bundlers. It is tested with webpack, Rollup, Parcel, and Browserify.

For using it with Rollup you need to use these plugins: rollup-plugin-node-builtins, rollup-plugin-node-globals, and rollup-plugin-json.

Opt-in native implementations (Node.js only)

If you are using this package in Node, you can install ethereum-cryptography-native to opt-in to use native implementations of some of the cryptographic primitives provided by this package.

No extra work is needed for this to work. This package will detect that ethereum-cryptography-native is installed, and use it.

While installing ethereum-cryptography-native will generally improve the performance of your application, we recommend leaving the decision of installing it to your users. It has multiple native dependencies that need to be compiled, and this can be problematic in some environments.

License

ethereum-cryptography is released under the MIT License.