• Stars
    star
    698
  • Rank 64,826 (Top 2 %)
  • Language
    JavaScript
  • License
    BSD 3-Clause "New...
  • Created almost 13 years ago
  • Updated over 2 years ago

Reviews

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

Repository Details

Fast and dependency-free cryptographic hashing library for node.js and browsers (supports MD5, SHA1, SHA256, SHA512, RIPEMD, HMAC)

jsHashes Build Status NPM version

jshashes is lightweight library implementing the most extended cryptographic hash function algorithms in pure JavaScript (ES5 compliant).

The goal is to provide an dependency-free, fast and reliable solution for hash algorithms for both client-side and server-side JavaScript environments. The code is fully compatible with the ECMAScript 5 specification and is used in production in browsers and node.js/io.js

If you are looking for a low-level performance library for the server-side, note that node.js/io.js provides its own native module: crypto

Supported hash algorithms

Additional functionalities

Environments

  • Browsers (ES3)
  • node.js/io.js (all versions)
  • Rhino
  • RingoJS

Usage

Each algorithm has its respective own instantiable object. Here you can see an example of how to create a new instance for each one:

// new MD5 instance
var MD5 = new Hashes.MD5
// new SHA1 instance
var SHA1 = new Hashes.SHA1
// new SHA256 instance
var SHA256 =  new Hashes.SHA256
// new SHA512 instace
var SHA512 = new Hashes.SHA512
// new RIPEMD-160 instace
var RMD160 = new Hashes.RMD160

An example of how to generate an hexadecimal-based hash encoding for each algorithm:

// sample string
var str = 'Sample text!'
// output to console
console.log('MD5: ' + MD5.hex(str))
console.log('SHA1: ' + SHA1.hex(str))
console.log('SHA256: ' + SHA256.hex(str))
console.log('SHA512: ' + SHA512.hex(str))
console.log('RIPEMD-160: ' + RMD160.hex(str))

Browsers

This is a simple implementation for a client-side environment:

<html>
<head>
<script type="text/javascript" src="src/hashes.js"></script>
<script type="text/javascript">
// sample string
var str = 'This is a sample text!'
// new MD5 instance and hexadecimal string encoding
var MD5 = new Hashes.MD5().hex(str)
// output into DOM
document.write('<p>MD5: <b>' + MD5 + '</b></p>')
</script>
</head>
<body>
</body>
</html>

node.js / io.js

// require the module
var Hashes = require('jshashes')
// sample string
var str = 'This is a sample text!'
// new SHA1 instance and base64 string encoding
var SHA1 = new Hashes.SHA1().b64(str)
// output to console
console.log('SHA1: ' + SHA1)

Command-line interface

You can use the simple command-line interface to generate hashes.

$ hashes sha1-hex This is a sample string
> b6a8501d8a70e74e1dc12a6082102622fdc719bb

# or with quotes
$ hashes sha1-hex "This is a sample string"
> b6a8501d8a70e74e1dc12a6082102622fdc719bb

For more information about the options supported, type:

$ hashes -h

Installation

Via npm

$ npm install jshashes

Via Bower:

$ bower install jshashes

Via Component:

$ component install h2non/jshashes

Or loading the script directly:

http://cdn.rawgit.com/h2non/jsHashes/master/hashes.js

Public methods

Each algorithm class provides the following public methods:

  • hex(string) - Hexadecimal hash encoding from string.
  • b64(string) - Base64 hash encoding from string.
  • any(string,encoding) - Custom hash algorithm values encoding.
  • hex_hmac(key,string) - Hexadecimal hash with HMAC salt key.
  • b64_hmac(key,string) - Base64 hash with HMAC salt key.
  • any_hmac(key,string,encoding) - Custom hash values encoding with HMAC salt key support.
  • vm_test() - Simple self-test to see is working. Returns this Object.
  • setUpperCase(boolean) - Enable/disable uppercase hexadecimal returned string. Returns this Object.
  • setPad(string) - Defines a custom base64 pad string. Default is '=' according with the RFC standard. Returns this Object.
  • setUTF8(boolean) - Enable/disable UTF-8 character encoding. Returns this Object.

Hash encoding formats supported

  • Hexadecimal (most extended)
  • Base64
  • Custom hash values any() method

Benchmark

Node.js 0.6.18 running on a VPS Intel I7 930 with 512 MB of RAM (see server/benchmark.js)

Simple benchmark test generating 10000 hashes for each algorithm.
String: "A0gTtNtKh3RaduBfIo59ZdfTc5pTdOQrkxdZ5EeVOIZh1cXxqPyexKZBg6VlE1KzIz6pd6r1LLIpT5B8THRfcGvbJElwhWBi9ZAE"

* MD5
** Done in: 205 milliseconds
* SHA1
** Done in: 277 milliseconds
* SHA256
** Done in: 525 milliseconds
* SHA512
** Done in: 593 milliseconds
* RMD160
** Done in: 383 milliseconds

See client/benchmark.html for client-side.

Notes

  • Don't support checksum hash for files on the server-side, only strings-based inputs are supported.
  • It has not been planned to include support for more hash algorithms.
  • The goal is to provide the same JavaScript code in both server and client side, so it isn't planned to improve it in other ways.
  • Only Node.js server-side was tested, so with minimal changes, you can setup jsHashes in other server-side JS environment.

Changelog

  • 1.0.7
    • Merge #37: fix terminator statement token.
  • 1.0.6
    • Fix #34: options pad typo.
  • 1.0.4
    • Fix CLI script call error when use it from Bash
    • Added CLI usage example
  • 1.0.3
    • Important bugfixes to UTF-8 encoding (broken in 1.0.2) and the RIPEMD-160 hash (broken in 1.0.1). (gh #6)
    • New test suite for hashes, CRC32, and hmac; run with 'npm test' in node.
    • Fixed global variable leaks. (gh #13)
    • CRC32 will now always return positive values. (gh #11)
    • Added package version property to the exposed Hashes Object
    • Updated CLI script utility supporting all algorithms (see bin/hashes)
    • Fixed UTF-8 encoding/decoding error (if input parameter is undefined or invalid)
  • 1.0.2
    • Performance improvements and minimal refactor (length property caching, literal notation)
    • Available from Bower package manager
  • 1.0.1
    • Refactoring (hoisting, coercion, removed redundant functions, scoping, restructure...)
    • Performance improves
    • JSLint validation (except bitwise operators)
    • Now the library can be used like a AMD CommonJS module
    • Updated documentation
    • New folders structure
    • Added closure compiled and minimized library version
    • Available from Jam package manager
  • 0.1.5b
    • Added index.js for easy call the module in Node.js
    • Updated documentation
  • 0.1.4b
    • Now declaring objects using Literal Notation.
    • Solved syntax errors on minimized version (jshashes.min.js)
    • Added benchmark test and sample
  • 0.1.3b
    • Starting non-redundancy code refactorization
    • Added Helpers Object with some global functions
    • Added native support for Base64 provided as class
    • Added CRC-32 calculation support
    • Added URL encode/decode helpers functions
  • 0.1.2b
    • SHA1 error fixed.
    • General code changes (renaming classes, private methods, new methods...).
    • Changing library namespace to 'Hashes'.
    • Starting code documentation.
    • Added new examples of how to use.
  • 0.1.1b
    • Minimal library improvements.
    • There has been added some samples, like how to use it and support for NPM package.
  • 0.1.0b
    • First release: the code is stable, but the library is still beta and must be improved and documented.

TODO

  • Performance benchmarking

Authors

Library author

Original algorithm authors

Other contributors

License

jsHashes is released under New BSD license. See LICENSE file.

Issues

Feel free to report any issue you experiment via Github https://github.com/h2non/jsHashes/issues.

More Repositories

1

imaginary

Fast, simple, scalable, Docker-ready HTTP microservice for high-level image processing
Go
5,543
star
2

toxy

Hackable HTTP proxy for resiliency testing and simulated network conditions
JavaScript
2,733
star
3

bimg

Go package for fast high-level image processing powered by libvips C library
Go
2,683
star
4

filetype

Fast, dependency-free Go package to infer binary file types based on the magic numbers header signature
Go
2,076
star
5

gock

HTTP traffic mocking and testing made easy in Go เผผส˜ฬšู„อœส˜ฬšเผฝ
Go
2,067
star
6

gentleman

Plugin-driven, extensible HTTP client toolkit for Go
Go
1,072
star
7

videoshow

Simple node.js utility to create video slideshows from images with optional audio and visual effects using ffmpeg
JavaScript
873
star
8

baloo

Expressive end-to-end HTTP API testing made easy in Go
Go
776
star
9

filetype.py

Small, dependency-free, fast Python package to infer binary file types checking the magic numbers signature
Python
642
star
10

nar

node.js application archive - create self-contained binary like executable applications that are ready to ship and run
LiveScript
428
star
11

rocky

Full-featured, middleware-oriented, programmatic HTTP and WebSocket proxy for node.js (deprecated)
JavaScript
371
star
12

pook

HTTP traffic mocking and testing made easy in Python
Python
329
star
13

paco

Small utility library for coroutine-driven asynchronous generic programming in Python
Python
202
star
14

semver.c

Semantic version in ANSI C
C
185
star
15

riprova

Versatile async-friendly retry package with multiple backoff strategies
Python
116
star
16

node-imaginary

Minimalist node.js command-line & programmatic API client for imaginary
JavaScript
106
star
17

youtube-video-api

Simplified programmatic and command-line interface for YouTube Video API. Built for node.js
JavaScript
67
star
18

siringa

Minimalist dependency injection library for Python that embraces type annotations syntax
Hy
54
star
19

audioconcat

Tiny node.js module to concat multiple audio files using ffmpeg
JavaScript
45
star
20

requireg

Resolve and require local & global modules in node.js like a boss
JavaScript
45
star
21

thread.js

Frictionless library to deal with multithread programming in the browser
JavaScript
41
star
22

grunt-stubby

Grunt task to setup a stub/mock server based on JSON/YAML/JS configuration files
JavaScript
26
star
23

hu

Small, generic functional helper library for node.js and browsers
wisp
20
star
24

gentleman-consul

Gentleman's plugin for Consul server discovery and optional configurable retry/backoff policies
Go
19
star
25

nightmare-google-oauth2

Nightmare plugin to retrieve a Google APIs OAuth2 token. Useful for server-to-server automation
JavaScript
19
star
26

balboa

Simple HTTP forward proxy
JavaScript
16
star
27

domlight

Spotlight DOM elements easily
JavaScript
16
star
28

resizr

Dead simple HTTP service written in Go for fast and easy image resizing from remote URLs
Go
16
star
29

google-oauth2-token

No headaches. Automation wins. Get a fresh OAuth2 token for Google APIs in just one command
JavaScript
16
star
30

angular-thread

AngularJS primitive bindings for thread.js
JavaScript
15
star
31

resilient-consul

Resilient.js middleware to use Consul for service discovery and balancing
JavaScript
14
star
32

gentleman-mock

HTTP mocking for gentleman's clients made easy. Uses gock under the hood
Go
14
star
33

rocky-consul

Rocky middleware for service discovery and dynamic traffic balancing using Consul
JavaScript
14
star
34

angular-resilient

Use $http as a resilient, failover and client-side balanced HTTP client
JavaScript
13
star
35

go-is-svg

Check if a given buffer is a valid SVG image in Go (golang)
Go
12
star
36

node-os-shim

Native OS module API shim for older node.js versions
JavaScript
12
star
37

stream-interceptor

Intercept, modify and/or ignore chunks data and events in any readable stream
JavaScript
11
star
38

gentleman-retry

gentleman's plugin providing retry policy capabilities in your HTTP clients
Go
11
star
39

bbscraper

Simple phpBB forum thread web scraper written in Python
Python
11
star
40

Sencha-WebSocket

WebSocket client abstraction library for Sencha JavaScript frameworks (DEPRECATED)
JavaScript
10
star
41

grunt-beautiful-docs

Generate beautiful markdown-based documentation using Grunt
CoffeeScript
9
star
42

findup

Find the first file matching in a current working directory or the nearest ancestor directory up to root using Go
Go
9
star
43

resolve-tree

Recursively resolve node.js modules and its dependencies looking into node_modules trees.
JavaScript
8
star
44

http-forward

Simple one-line proxy forward for incoming HTTP requests. Built for node.js/io.js
JavaScript
7
star
45

injecty

Micro library for dependency injection and inversion of control containers in node and browsers
wisp
6
star
46

sharedworkers-angular-poc

A simple proof of concept using Shared WebWorkers + Two-way data binding in AngularJS
CSS
6
star
47

toxy-ip

toxy rule to easily filter by IP addresses (supports CIDR, subnet, IP ranges...)
JavaScript
6
star
48

fw

Tiny library for asynchronous control-flow in node and browsers
wisp
6
star
49

pipefy

Transform a function into a pipeable writable stream in node/io.js
JavaScript
5
star
50

node-bintray

CLI and programmatic interface for Bintray built for node.js
CoffeeScript
5
star
51

generator-api-service

A Yeoman generator to build an opinionated but community-driven convention-focused, general purpose, resource-oriented HTTP service in node.js
JavaScript
4
star
52

htgen

Tiny hypertext markup code generator for node and browsers
LiveScript
4
star
53

node-authrc

authrc implementation for node
CoffeeScript
4
star
54

gulp-nar

Create and extract nar archives from Gulp
JavaScript
4
star
55

ng-exam

Are you a great AngularJS developer? (wip)
JavaScript
4
star
56

oml

Minimalist template engine built-on-top of the Oli language for node and browsers
JavaScript
4
star
57

buildspec

Declarative, featured, unopiniotated, CI-agnostic build configuration file
4
star
58

OPEW

[NOT MAINTAINED] OPEW is a powerful, complete, independent and extensible open source distribution stack for GNU/Linux (64 bits) based OS. Its goal is to provides a rich portable ready-to-run development environment focused on modern and robust programming languages in oder to satisfy the common needs for the web development.
Shell
4
star
59

promitto

Tiny promise library mostly compatible with Promise/A+ spec and ES6
wisp
3
star
60

heroku-buildpack-binary-download

Download and execute POSIX compatible binaries in Heroku apps
Shell
3
star
61

grunt-nar

Create and extract nar archives from Grunt
CoffeeScript
2
star
62

ASIR

ASIR course exercises for fun and profit
Shell
2
star
63

consul-cluster-test

Consul cluster test suite
Python
2
star
64

butler

A small, elegant and friendly library to deal elegantly with Service Workers (experimental)
JavaScript
2
star
65

node-cloudimage

Minimalist node/io.js CLI & programmatic stream-based interface for Cloudimage.io
JavaScript
2
star
66

mimeware

Simple node.js/io.js HTTP middleware to infer the proper MIME content type response header
JavaScript
2
star
67

bock

Browser next-generation HTTP traffic mock and proxy interceptor using Service Worker (experimental)
JavaScript
2
star
68

OPEW-bash-installer

Bash-based installer builder utility for the OPEW project
Shell
2
star
69

findup.rs

Find the first file matching in a current working directory or the nearest ancestor directory up to root
Rust
2
star
70

http-version

HTTP API version matching strategies as middleware for connect/express/rocky
JavaScript
2
star
71

nar-installer

Simple Bash script to easily install nar executable packages. Analog to npm install --global
Shell
2
star
72

http-api-versioning

Analysis of multiple HTTP API versioning design strategies
1
star
73

midware-pool

Tiny module to create a pool of connect-style domain-agnostic middleware layers in browsers and node.js
JavaScript
1
star
74

apachelog

Simple Go net/http compatible middleware for Apache style logging
Go
1
star
75

rocky-cli

Command-line interface for rocky. Supports TOML configuration file to easily set up your proxy
JavaScript
1
star
76

carcasse

Build structured, modular and object-oriented JavaScript projects
JavaScript
1
star
77

go-memstats

Human-friendly debugger to print in stdout the memory and GC stats
Go
1
star