fast-sha256

SHA-256, HMAC and PBKDF2 implementation with typed arrays for modern browsers and Node.js

fast-sha256-js

SHA-256 implementation for JavaScript/TypeScript with typed arrays that works in modern browsers and Node.js. Implements the hash function, HMAC, and PBKDF2.

Public domain. No warranty.

Installation

You can install fast-sha256-js via NPM:

$ npm install fast-sha256

or download source code.

Usage

Functions accept and return Uint8Arrays. To convert strings, use external library (for example, nacl.util).

sha256(message)

Returns a SHA-256 hash of the message.

sha256.hmac(key, message)

Returns an HMAC-SHA-256 of the message for the key.

sha256.pbkdf2(password, salt, rounds, dkLen)

Returns a key of length dkLen derived using PBKDF2-HMAC-SHA256 from the given password, salt, and the number of rounds.

sha256.hkdf(key, salt, info?, length?)

Returns a key of the given length derived using HKDF as described in RFC 5869.

There are also classes Hashand HMAC:

new sha256.Hash()

Constructor for hash instance. Should be used with new. Available methods: update(), digest(), reset(), etc.

new sha256.HMAC(key)

Constructor for HMAC instance. Should be used with new. Available methods: update(), digest(), reset(), etc.

See comments in src/sha256.tsfor details.

Usage with TypeScript

import sha256, { Hash, HMAC } from "fast-sha256";

sha256(data) // default export is hash

const h = new HMAC(key); // also Hash and HMAC classes
const mac = h.update(data).digest();

// alternatively:

import * as sha256 from "fast-sha256";

sha256.pbkdf2(password, salt, iterations, dkLen); // returns derived key
sha256.hash(data)

const hasher = new sha256.Hash();
hasher.update(data1);
hasher.update(data2);
const result = hasher.digest();

Testing and building

Install development dependencies:

$ npm install

Build JavaScript, minified version, and typings:

$ npm run build

Run tests:

$ npm test

Run tests on a different source file:

$ SHA256_SRC=sha256.min.js npm test

Run benchmark:

$ npm run bench

(or in a browser, open tests/bench.html).

Lint:

$ npm run lint

Notes

While this implementation is pretty fast compared to previous generation implementations, if you need an even faster one, check out asmCrypto.

Repository

https://github.com/dchest/fast-sha256-js


上一篇:yuntan-gateway
下一篇:zeo-blockchain

相关推荐

  • 移动端触摸、点击事件优化(fastclick源码学习)

    最近在做一些微信移动端的页面,在此记录关于移动端触摸和点击事件的学习优化过程,主要内容围绕fastclick展开。 fastclick github(https://github.com/ftlabs...

    2 年前
  • 移动端使用swiper+iscroll+fastClick:安卓触摸swiper会触发点击事件。

    这两天做H5页面,使用swiperiscrollfastClick,并没有用swiper提供的tap和click事件,自己在元素上bind,因为回调函数是统一处理,就没用swiper,后面发现即使是使...

    1 年前
  • 浅谈FastClick 填坑及源码解析

    最近产品妹子提出了一个体验issue —— 用 iOS 在手Q阅读书友交流区发表书评时,光标点击总是不好定位到正确的位置: (https://files.javascriptcn.com/filei...

    2 年前
  • 推荐一个在线接口Mock工具fastmock

    fastmock可以让你在没有后端程序的情况下能真实地在线模拟ajax请求,你可以用fatmock实现项目初期纯前端的效果演示,也可以用fastmock实现开发中的数据模拟从而实现前后端分离。

    1 年前
  • 如何青鸟'的util.tofastproperties功能使一个物体'的属性“快”?

    Qantas 94 Heavy(https://stackoverflow.com/users/2074608/qantas94heavy)提出了一个问题:How does Bluebird's ut...

    2 年前
  • very-fast-args

    A very fast way to convert arguments into array. Very fast way of converting "arguments" into array...

    1 年前
  • to-fast-properties

    Force V8 to use fast properties for an object tofastproperties Build Status(https://travisci.org/s...

    1 年前
  • supervisor 的使用和进阶 (fastcgi 的管理)

    本文主要介绍 supervisor 对 fastcgi 进程的管理 fastcgi 进程的管理 在php 中,phpfpm 有主进程来管理和维护子进程的数量。但是并不是所有的服务都有类似的主进程来做子...

    2 个月前
  • superfast

    A singlepage, isomorphic web app framework. Superfast Build Status(https://travisci.org/tylerjohns...

    10 个月前
  • sha256

    Compute SHA256 of bytes or strings. sha256 JavaScript component to compute the SHA256 of strings ...

    1 年前

官方社区

扫码加入 JavaScript 社区