• Stars
    star
    140
  • Rank 253,416 (Top 6 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 11 years ago
  • Updated over 1 year ago

Reviews

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

Repository Details

SHA3 for JavaScript - The Keccak family of hash algorithms

SHA-3 for JavaScript

Travis CI npm version npm downloads dependencies devDependencies license

A pure JavaScript implementation of the Keccak family of cryptographic hashing algorithms, most notably including Keccak and SHA3.

πŸ’‘ Legacy Note: In previous versions of this library, the SHA3Hash object provided a Keccak hash, not what we currently know as a SHA-3 hash. For backwards-compatibility, this object is still exported. However, users are encouraged to switch to using the SHA3 or Keccak objects instead, which provide the SHA-3 and Keccak hashing algorithms, respectively.

Installation

Via npm:

$ npm install sha3

Via yarn:

$ yarn add sha3

Usage

You can use this library from Node.js, from web browsers, and/or using ES6 imports.

Node.js (CommonJS style)

// Standard FIPS 202 SHA-3 implementation
const { SHA3 } = require('sha3');

// The Keccak hash function is also available
const { Keccak } = require('sha3');

// The SHAKE extendable output function (XOF) is also available
const { SHAKE } = require('sha3');

ES6

// Standard FIPS 202 SHA-3 implementation
import { SHA3 } from 'sha3';

// The Keccak hash function is also available
import { Keccak } from 'sha3';

// The SHAKE extendable output function (XOF) is also available
import { SHAKE } from 'sha3';

What's in the box

FIPS-compatible interfaces for the following algorithms:

  • SHA3: The SHA3 algorithm.
  • Keccak: The Keccak algorithm.
  • SHAKE: The SHAKE XOF algorithm.

πŸ’‘ Legacy Note: Savvy inspectors may notice that SHA3Hash is also provided. Prior to v2.0.0, this library only implemented an early version of the SHA3 algorithm. Since then, SHA3 has diverged from Keccak and is using a different padding scheme, but for compatibility, this alias is sticking around for a bit longer.

Examples

Generating a SHA3-512 hash

import { SHA3 } from 'sha3';

const hash = new SHA3(512);

hash.update('foo');
hash.digest('hex');

Generating a Keccak-256 hash

import { Keccak } from 'sha3';

const hash = new Keccak(256);

hash.update('foo');
hash.digest('hex');

Generating a SHAKE128 hash with 2048 bytes

import { SHAKE } from 'sha3';

const hash = new SHAKE(128);

hash.update('foo');
hash.digest({ buffer: Buffer.alloc(2048), format: 'hex' });

API Reference

All hash implementations provided by this library conform to the following API specification.

#constructor([size=512])

The constructor for each hash (e.g: Keccak, SHA3), expects the following parameters:

  • size (Number): Optional. The size of the hash to create, in bits. If provided, this must be one of 224, 256, 384, or 512. Defaults to 512.
Example
// Construct a new Keccak hash of size 256
const hash = new Keccak(256);

#update(data, [encoding='utf8'])

Updates the hash content with the given data. Returns the hash object itself.

  • data (Buffer|string): Required. The data to read into the hash.
  • encoding (string): Optional. The encoding of the given data, if of type string. Defaults to 'utf8'.

πŸ’‘ See Buffers and Character Encodings for a list of allowed encodings.

Example
const hash = new Keccak(256);

hash.update('hello');

hash.update('we can also chain these').update('together');

#digest([encoding='binary'])

Digests the hash and returns the result. After calling this function, the hash may continue to receive input.

  • encoding (string): Optional. The encoding to use for the returned digest. Defaults to 'binary'.

If an encoding is provided and is a value other than 'binary', then this function returns a string. Otherwise, it returns a Buffer.

πŸ’‘ See Buffers and Character Encodings for a list of allowed encodings.

Example
const hash = new Keccak(256);

hash.update('hello');

hash.digest('hex');
// => hash of 'hello' as a hex-encoded string

#digest([options={}])

Digests the hash and returns the result. After calling this function, the hash may continue to receive input.

Options include:

  • buffer (Buffer): Optional. A pre-allocated buffer to fill with output bytes. This is how XOF algorithms like SHAKE can be used to obtain an arbitrary number of hash bytes.
  • format (string): Optional. The encoding to use for the returned digest. Defaults to 'binary'. If buffer is also provided, this value will passed directly into Buffer#toString() on the given buffer.
  • padding (byte): Optional. Override the padding used to pad the input bytes to the algorithm's block size. Typically this should be omitted, but may be required if building additional cryptographic algorithms on top of this library.

If a format is provided and is a value other than 'binary', then this function returns a string. Otherwise, it returns a Buffer.

Example
const hash = new Keccak(256);

hash.update('hello');

hash.digest({ buffer: Buffer.alloc(32), format: 'hex' });
// => hash of 'hello' as a hex-encoded string

#reset()

Resets a hash to its initial state.

  • All input buffers are cleared from memory.
  • The hash object can safely be reused to compute another hash.
Example
const hash = new Keccak(256);

hash.update('hello');
hash.digest();
// => hash of 'hello'

hash.reset();

hash.update('world');
hash.digest();
// => hash of 'world'

Testing

Run yarn test for the full test suite.

Disclaimer

Cryptographic hashes provide integrity, but do not provide authenticity or confidentiality. Hash functions are one part of the cryptographic ecosystem, alongside other primitives like ciphers and MACs. If considering this library for the purpose of protecting passwords, you may actually be looking for a key derivation function, which can provide much better security guarantees for this use case.

Special Thanks

The following resources were invaluable to this implementation and deserve special thanks for work well done:

  • Keccak pseudocode: The Keccak team's excellent pseudo-code and technical descriptions.

  • mjosaarinen/tiny_sha3: Markku-Juhani O. Saarinen's compact, legible, and hackable implementation.

  • Phusion: For the initial release and maintenance of this project, and gracious hand-off to Twuni for continued development and maintenance.

More Repositories

1

baseimage-docker

A minimal Ubuntu base image modified for Docker-friendliness
Shell
8,856
star
2

passenger

A fast and robust web server and application server for Ruby, Python and Node.js
C++
4,964
star
3

passenger-docker

Docker base images for Ruby, Python, Node.js and Meteor web apps
Shell
2,751
star
4

traveling-ruby

Self-contained Ruby binaries that can run on any Linux distribution and any macOS machine.
Shell
2,082
star
5

juvia

A commenting server similar to Disqus and IntenseDebate.
Ruby
1,029
star
6

holy-build-box

System for building cross-distribution Linux binaries
Shell
545
star
7

open-vagrant-boxes

Docker-compatible Vagrant base boxes
Shell
518
star
8

passenger-ruby-heroku-demo

Demonstrates running a Ruby app on Heroku with Phusion Passenger
Ruby
280
star
9

nginx

Git mirror of the Nginx SVN repository, automatically updated 2 times a day.
C
111
star
10

digest-sha3-ruby

SHA-3 (Keccak) extension for Ruby
C
67
star
11

passenger-ruby-websocket-demo

Demonstration of WebSockets on Phusion Passenger
CSS
63
star
12

phusion-server-tools

Set of server administration tools that we use at Phusion.
Ruby
60
star
13

passenger_library

Phusion Passenger documentation
HTML
47
star
14

passenger-nodejs-websocket-demo

Phusion Passenger: Node.js WebSocket demo
CSS
36
star
15

passenger-ruby-server-sent-events-demo

Phusion Passenger: HTML5 Server Side Events demo (Ruby version)
Ruby
33
star
16

apachai-hopachai

Travis-like continuous integration (CI) system built on Docker
Ruby
27
star
17

eurovat

European Union VAT number utilities
Ruby
25
star
18

nginx-modsecurity-ubuntu

Ubuntu package for modsecurity-nginx
Makefile
23
star
19

frontapp

Ruby client to work with Frontapp API
Ruby
22
star
20

action-cable-demo

A project to demonstrate ActionCable
Ruby
20
star
21

passenger_apt_automation

Tools for automatically building a Debian APT repository for Phusion Passenger
C
20
star
22

passenger-python-flask-demo

Passenger: Flask example app
HTML
19
star
23

passenger_rpm_automation

Phusion Passenger RPM packaging automation
HTML
19
star
24

passenger-ruby-sinatra-demo

Passenger: Sinatra example app
HTML
17
star
25

passenger_autobuilder

Phusion Passenger binary building automation infrastructure. Superceded by https://github.com/phusion/passenger_binary_build_automation
Shell
16
star
26

passenger-ruby-rails-demo

Passenger: Ruby on Rails example app
Ruby
12
star
27

passenger_status_service

[DEPRECATED] Make Passenger status reports work on Heroku
Ruby
11
star
28

netdata-ubuntu-16.04

Ubuntu 16.04 packages for netdata
JavaScript
9
star
29

support_central

Manage multiple user support channels with ease
Ruby
9
star
30

unicorn

Ruby application server. Includes Phusion extensions. Automatically synchronized with Eric Wong's repo every 6 hours (see the eric_master branch).
Ruby
7
star
31

SheetsAPI

A simple API for writing to Google Sheets
Ruby
5
star
32

traveling-ruby-gems-demo

Traveling Ruby tutorial 2: gem dependencies
Ruby
5
star
33

cxxcodebuilder

Generate C/C++ code with a Builder-style DSL
Ruby
5
star
34

phusion-mvc

A frontend MVC framework with Polymer
JavaScript
4
star
35

onepush

Simple web app server setup and deployment
Ruby
4
star
36

traveling-ruby-hello-demo

Traveling Ruby tutorial 1: hello world
Ruby
3
star
37

ruby-reaper

A Ruby script that can run as PID1 and reaps orphan and zombie processes
Ruby
3
star
38

passenger_binary_build_automation

System for building portable binaries for Phusion Passenger
Shell
3
star
39

frontapp-spam-filter

Implements a spam filter for Front through webhooks and a third party service
Ruby
3
star
40

passenger-ruby-faye-websocket-demo

Demonstrates WebSockets on Phusion Passenger through the faye-websocket gem
CSS
3
star
41

homebrew-passenger-enterprise

Passenger Enterprise Homebrew formula
Ruby
3
star
42

brow-route.js

A simple hash based router for modern web applications
CoffeeScript
2
star
43

phusion-docker-classic-linux

Docker images for 32-bit Ubuntu 10.04 and CentOS 5
Shell
2
star
44

passenger-docker-redhat

Docker images for running Ruby, Python, Node.js and Meteor web apps with Passenger
Python
2
star
45

rails-cve-2012-5664-test

Demo app showing how the Rails CVE-2013-5664 vulnerability works.
Ruby
2
star
46

union_station_hooks_rails

Union Station Ruby on Rails hooks
Ruby
2
star
47

traveling-ruby-native-extensions-demo

Traveling Ruby tutorial 2: native extensions
Ruby
2
star
48

traveling-ruby-windows-demo

Traveling Ruby tutorial 4: creating packages for Windows
Ruby
1
star
49

phusion-spa

A Single Page App view switching system for Polymer
HTML
1
star
50

laposta

Ruby client for Laposta API
Ruby
1
star
51

passenger-ci-test

A mock Passenger repository for testing our CI scripts
C++
1
star
52

actioncable-slow-client

A project to test ActionCable with slow clients
Ruby
1
star
53

bottleneck_benchmark

Rails project for benchmarking application servers for applications with CPU or IO bottlenecks.
Ruby
1
star
54

passenger-nodejs-connect-demo

Passenger + Node.js/io.js + Connect.js example app
JavaScript
1
star
55

rails_wizard_support

Support library for writing wizard models in Rails.
Ruby
1
star
56

zangetsu

A NoSQL database written in Node.js
JavaScript
1
star
57

supportbee_sla_enforcer

SLA enforcement script for Supportbee
Ruby
1
star
58

passenger_homebrew_automation

Phusion Passenger Homebrew formula release automation
Ruby
1
star