• Stars
    star
    274
  • Rank 150,274 (Top 3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 11 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

High-performance pure-javascript SHA1 implementation suitable for large binary data, reaching up to half the native speed.

Rusha

A high-performance pure-javascript SHA1 implementation suitable for large binary data.

npm npm Build Status

Installing

NPM

Rusha is available via npm:

npm install rusha

Bower

Rusha is available via bower:

bower install rusha

Usage

It is highly recommended to run CPU-intensive tasks in a Web Worker. To do so, just follow the instructions on Using the Rusha Worker.

If you have a good reason not to use Web Workers, follow the instructions on Using the Rusha Hash API instead.

Using the Rusha Worker

Spawning workers

You can create a new worker in two ways. The preferred way is using Rusha.createWorker(), which spawns a webworker containing the hashing logic, and returns back a Worker object:

const worker = Rusha.createWorker();

If for some reason this does not work for you, you can also just point the Worker constructor at rusha.js or rusha.min.js, like so:

const worker = new Worker("dist/rusha.min.js");

Note: In order to make the latter work, Rusha will by default subscribe to incoming messages when it finds itself inside a worker context. This can lead to problems when you would like to use Rusha as a library inside a web worker, but still have control over the messaging. To disable this behaviour, you can call Rusha.disableWorkerBehaviour() from within the worker.

Communicating with the worker

You can send your instance of the web worker messages in the format {id: jobid, data: dataobject}. The worker then sends back a message in the format {id: jobid, hash: hash}, were jobid is the id of the job previously received and hash is the hash of the data-object you passed, be it a Blob, Array, Buffer, ArrayBuffer or String

Using the Rusha Hash API

The Rusha Hash API is inspired by the Node.js Hash API.

Examples

Simple usage
const hexHash = Rusha.createHash().update('I am Rusha').digest('hex'); 
Incremental usage
const hash = Rusha.createHash(); 
hash.update('I am');
hash.update(' Rusha');
const hexHash = rusha.digest('hex');

Reference

You instantiate a new Hash object by calling Rusha.createHash().

Methods
  • update(data): Update the hash state with the given data, which can be a binary String, Buffer, Array or ArrayBuffer.
  • digest([encoding]): Calculates the digest of all of the data passed to be hashed. The encoding can be 'hex' or undefined. If encoding is provided a string will be returned; otherwise an ArrayBuffer is returned.

Note: Due to its synchronous nature, Hash#update does not accept data of type Blob. If you need to work with Blobs, you can either use the Rusha Worker, or use FileReader#readAsArrayBuffer to read the contents of the Blob, and then invoke Hash#update with the ArrayBuffer that was returned.

Properties
  • state (getter and setter): Allows getting and setting the internal hashing state.

Using the Rusha Object (DEPRECATED)

The Rusha Object API is deprecated, and is only documented here for older code bases that might still be using it.

You should be using the Hash API instead, which is documented above.

Examples

Normal usage
const rusha = new Rusha();
const hexHash = rusha.digest('I am Rusha'); 
Incremental usage
const rusha = new Rusha();
rusha.resetState();
rusha.append('I am');
rusha.append(' Rusha');
const hexHash = rusha.end();

Reference

Your instantiate a new Rusha object by doing new Rusha(). When created, it provides the following methods:

  • digest(d): Create a hex digest from data of the three kinds mentioned below, or throw and error if the type is unsupported.
  • digestFromString(s): Create a hex digest from a binary String. A binary string is expected to only contain characters whose charCode < 256.
  • digestFromBuffer(b): Create a hex digest from a Buffer or Array. Both are expected to only contain elements < 256.
  • digestFromArrayBuffer(a): Create a hex digest from an ArrayBuffer object.
  • rawDigest(d): Behaves just like #digest(d), except that it returns the digest as an Int32Array of size 5.
  • resetState(): Resets the internal state of the computation.
  • append(d): Appends a binary String, Buffer, Array, ArrayBuffer or Blob.
  • setState(state): Sets the internal computation state. See: getState().
  • setState(): Returns an object representing the internal computation state. You can pass this state to setState(). This feature is useful to resume an incremental sha.
  • end(): Finishes the computation of the sha, returning a hex digest.
  • rawEnd(): Behaves just like #end(), except that it returns the digest as an Int32Array of size 5.

Development

  • Download npm dependencies with npm install
  • Make changes to the files in src/
  • Build with npm run build
  • Run tests with npm test

Benchmarks

Tested were my Rusha implementation, the sha1.js implementation by P. A. Johnston, Tim Caswell's Cifre and the Node.JS native implementation.

If you want to check the performance for yourself in your own browser, I compiled a JSPerf Page.

A normalized estimation based on the best results for each implementation, smaller is better: rough performance graph

Results per Implementation and Platform: performance chart

All tests were performed on a MacBook Air 1.7 GHz Intel Core i5 and 4 GB 1333 MHz DDR3.

More Repositories

1

rust-aws-lambda

Support for running Rust programs on AWS Lambda
Rust
319
star
2

rust-crc32fast

Fast, SIMD-accelerated CRC32 (IEEE) checksum computation in Rust
Rust
265
star
3

rust-tokio-retry

Extensible, asynchronous retry behaviours for futures/tokio
Rust
127
star
4

rust-plumbum

Conduit-like data processing library for Rust
Rust
55
star
5

rust-gob

serialization and deserialization for the gob binary format
Rust
29
star
6

rust-copperline

Pure-Rust Command Line Editing Library
Rust
27
star
7

rpdf

Rust
22
star
8

rust-gearhash

Fast, SIMD-accelerated hash function for content-defined chunking
Rust
21
star
9

hwsl2-haskell

Hashing with SL2 for Haskell
Haskell
14
star
10

rust-serde-schema

Rust
12
star
11

muject

10 sloc, β…› kb, no-bullshit dependency injection micro-library
JavaScript
12
star
12

rust-futures-backoff

Extensible, asynchronous retry behaviours
Rust
9
star
13

rust-semaphore

Atomic counting semaphore
Rust
7
star
14

rust-cfn

Type-safe representation for AWS CloudFormation templates
Rust
7
star
15

git-build

Build a git repository with a Dockerfile.
Go
6
star
16

clam-js

Control a ClamAV daemon over TCP or Unix Domain Sockets.
JavaScript
6
star
17

hwsl2-scala

Hashing with SL2 for Scala
Scala
6
star
18

hwsl2-core

Hashing with SL2
C
6
star
19

haskell-lambda-term-zipper

Functional Pearl: Environmentally Aware Zippers
Haskell
6
star
20

node-aff

Asynchronous effect system for TypeScript
TypeScript
5
star
21

rust-try-future

Convenient short-hand for returning early from `futures`-based functions
Rust
5
star
22

node-arbitrator

Generative property testing for JavaScript
JavaScript
4
star
23

rust-follow-redirects

Extension for hyper to follow HTTP redirects
Rust
4
star
24

haskell-ndjson-conduit

Conduit-based parsing and serialization for newline delimited JSON
Haskell
4
star
25

hwsl2-node

Hashing with SL2 for Node.js
C++
4
star
26

haskell-aws-dynamodb-conduit

Conduit-based interface for AWS DynamoDB
Haskell
3
star
27

node-clam-engine

Native bindings for the clamav engine
C++
3
star
28

rustretto

Rust
3
star
29

node-lcg

Seedable and Splittable (LCG) Random Number Generator
TypeScript
3
star
30

vim-colors-rusty

Rust-inspired color scheme for vim
Vim Script
3
star
31

rust-coyoneda

Functor composition via the Co-Yoneda Lemma
Rust
3
star
32

haskell-result

Encode success or at least one error
Haskell
2
star
33

rust-simdop

SIMD operations for Rust, with traits and pixiedust
Rust
2
star
34

statistics-hypergeometric-genvar

Random variate generation for hypergeometric distributions
Haskell
2
star
35

rust-functor

Rust
2
star
36

rust-comonoid

Comonoid trait for Rust.
Rust
2
star
37

node-kung-fu

Functional Standard Library with TypeScript support
TypeScript
2
star
38

haskell-free-concurrent

Free monads suitable for concurrent computation
Haskell
2
star
39

udp-stats-redis-adapter

Small performant frontend to redis, receiving minimal statistics data via udp messaging.
C
2
star
40

rust-tagged

PhantomData's partner in crime!
Rust
1
star
41

node-buffer-indexof-fast

Fast .indexOf for the Buffer data type
C++
1
star
42

backtape

A doctape client for the truly paranoid.
JavaScript
1
star
43

haskell-json-togo

Incremental conduit-based parsing of JSON documents.
Haskell
1
star
44

random-hypergeometric

Random variate generation from hypergeometric distributions
Haskell
1
star
45

node-resumable-hash

Serializable and deserializable hashing implementations
C++
1
star
46

rust-operational

Operational Monad for Rust
Rust
1
star
47

talks

1
star
48

rust-ot

Operational Transformations in Rust
Rust
1
star
49

haskell-attoparsec-trans

Interleaved effects for attoparsec parsers
Haskell
1
star
50

haskell-zipkin

Haskell
1
star
51

rijckit

Handwritten, low-level lexer for c-like languages. Still in early development.
C
1
star
52

rust-backtrace-parser

A zero-copy parser for Rust backtraces
Rust
1
star
53

node-redis-function

Run transpiled Javascript functions on the Redis scripting engine.
JavaScript
1
star