• Stars
    star
    164
  • Rank 230,032 (Top 5 %)
  • Language
    JavaScript
  • Created over 11 years ago
  • Updated almost 8 years ago

Reviews

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

Repository Details

Emscripten-compiled Javascript version of scrypt, a high-quality password-based key derivation function.

js-scrypt: Pure-Javascript Emscripten-compiled scrypt routine

Emscripten-compiled scrypt (version 1.2.0), a Password-Based Key Derivation Function from Colin Percival.

For general background on what scrypt is, and why it's useful, see these slides (PDF) and Colin Percival's page on scrypt.

This library is intended only for use in the browser; for node.js, there are plenty of existing options.

This library was written in order to interoperate with js-nacl, a cryptographic toolkit library.

Change history

v1.2.0: Changed emscripten generation options: the .js.mem file is no longer required. The interface to scrypt_module_factory remains the same.

v1.1.0: Based on scrypt v1.2.0. INCOMPATIBLE API CHANGES since 0.5.0.

The interface to scrypt_module_factory has changed.

Previously, scrypt_module_factory expected one (optional) parameter, requested_total_memory, and returned the new scrypt module directly.

Now, Emscripten uses an asynchronous loading technique that requires use of a callback. scrypt_module_factory now expects a callback which will be called with the scrypt module when it is ready to be called. The module factory returns no useful value: it is up to the user-supplied callback to transmit the scrypt module on to where it is needed.

v0.5.0: Based on scrypt v1.1.6.

Building the library

The git checkout includes a pre-compiled version of the library, so you won't need Emscripten unless you want to change something about the underlying library itself or how it is compiled.

Essentially, the source checkout contains everything you will need to use the library in the browser.

Using the library

In the browser, include the browser/scrypt.js script, and invoke scrypt_module_factory to produce a usable scrypt module:

<script src="browser/scrypt.js"></script>
<script> scrypt_module_factory(function (scrypt) {
  ...
  alert(scrypt.to_hex(scrypt.random_bytes(16)));
  ...
}); </script>

The scrypt_module_factory function takes an optional second argument, a dictionary specifying optional configuration values. At present, it supports only one configuration option: the total memory available for use by scrypt(), to be placed in a key named requested_total_memory. If supplied, requested_total_memory must be a power of two. It defaults to 33,554,432 (225) bytes; 32 MB.

The amount of memory needed is directly proportional to the work factor N. A good rule of thumb is:

For any work factor N = 2^x, your requested memory should be 2^(x+11) bytes.
(Just add 11 to the exponent, if it's a power of 2).
Describing your memory size requirements as a function of N: 
    mem(N) = 2^(log2(N) + 11)
wich can be simplified to:
    mem(N) = 2048 * N

NOTE: these values were found through experimenting. Chrome (Linux, V. 52.0.2743.82 (64-bit)) will not allow more than 1GB and Firefox (Linux, V. 50) crashed my desktop at 2GB (with 8GB RAM installed).

(Practical) example: Hashing with the recommended N = 220 = 1048576 for file encryption, would require 231 = 2147483648 bytes (2GB) of memory. If N = 214 is consicered sufficient, only 225 = 33554432 bytes (32MB) are required.

To enlarge the work factor without running into memory issues, this paper from the RFC suggests increasing p instead of N:

a large value of p can be used to increase the computational cost of scrypt without increasing the memory usage

For general use, assuming the variable N holds the (highest) work factor and is larger than 214 (16384), it is recommended to initialize scrypt like this:

scrypt_module_factory(on_ready, { 
    requested_total_memory: N * 2048
});

The memory assigned to the produced scrypt module will not be released until the module is garbage collected.

Strings vs. Binary Data

The library enforces a strict distinction between strings and binary data. Binary data is represented using instances of Uint8Array.

scrypt.to_hex(Uint8Array) → String

Returns a lower-case hexadecimal representation of the given binary data.

scrypt.encode_utf8(String) → Uint8Array

Returns the binary equivalent of the argument, encoded using UTF-8.

scrypt.encode_latin1(String) → Uint8Array

Returns the binary equivalent of the argument, encoded using Latin1 (an 8-bit clean encoding). If any of the character codes in the argument string are greater than 255, an exception is thrown.

scrypt.decode_utf8(Uint8Array) → String

Decodes the binary data in the argument using the UTF-8 encoding, producing the corresponding string.

scrypt.decode_latin1(Uint8Array) → String

Decodes the binary data in the argument using the Latin1 8-bit clean encoding, producing the corresponding string.

Using crypto_scrypt

To generate L bytes of derived key material from a password passwd and a salt salt,

  • choose N, which must be a power of two, which will set the overall difficulty of the computation. The scrypt paper uses 214=16384 for interactive logins, and 220=1048576 for file encryption, but running in the browser is slow so Your Mileage Will Almost Certainly Vary.

    Be aware that N > 214 will require more memory than is available by default. Refer to Using the library for an in-depth tutorial on how to reserve the required ammount of memory.

  • choose r and p. Good values are r=8 and p=1. See the scrypt paper for details on these parameters.

Choose wisely! Picking good values for N, r and p is important for making your keys sufficiently hard to brute-force.

Ensure your password and salt are both represented as Uint8Array instances, perhaps by calling scrypt.encode_utf8 or similar.

Then,

var keyBytes = scrypt.crypto_scrypt(password, salt, N, r, p, L);

and keyBytes will contain L bytes of key material.

For example,

scrypt.crypto_scrypt(scrypt.encode_utf8("pleaseletmein"),
                     scrypt.encode_utf8("SodiumChloride"),
                     16384, 8, 1, 64)

produces 64 bytes of key material,

7023bdcb3afd7348461c06cd81fd38eb
fda8fbba904f8e3ea9b543f6545da1f2
d5432955613f0fcf62d49705242a9af9
e61e85dc0d651e40dfcf017b45575887

as a Uint8Array.

License

js-scrypt is written by Tony Garnock-Jones [email protected] and is licensed under the 2-clause BSD license:

Copyright © 2013–2016, Tony Garnock-Jones
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

js-scrypt relies on scrypt itself, which is written by Colin Percival and licensed as follows:

Copyright 2009 Colin Percival
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

More Repositories

1

js-nacl

Pure-Javascript High-level API to Emscripten-compiled libsodium routines.
JavaScript
491
star
2

rabbithub

Experimental RabbitMQ PubSubHubBub interface
Erlang
228
star
3

reversehttp

A dynamic, ReST-style means of enrolment and participation in an HTTP network; a dynamically-configurable "Remote CGI" service. Joining the World Wide Web as an HTTP server has been an ad-hoc, manual process. By using the protocol defined here, programs can provide services to the Web just as easily as they request services from the Web.
Java
220
star
4

syndicate

syn·di·cate: a language for interactive programs
Racket
151
star
5

erlang-rfc4627

Erlang RFC4627 (JSON) codec and JSON-RPC server implementation.
Erlang
121
star
6

pi-nothing

i386, x86_64, ARMv7 assembler/linker; Nothing-like mid-level language; Linear-scan register allocator; Operating system for Raspberry Pi
Racket
99
star
7

erlang-serial

Erlang serial-port support; based on serial-1.0 by Johan Bevemyr
C
89
star
8

rabbiter

Microblogging using RabbitMQ and ejabberd
Erlang
75
star
9

marketplace

Repository has moved: https://git.syndicate-lang.org/syndicate-lang/marketplace-2014
73
star
10

rmacs

An EMACS written in Racket. Runs in ANSI-compatible terminals.
Racket
72
star
11

racket-reloadable

Support for code-reloading for long-running racket programs (such as web-servers).
Racket
60
star
12

python-rfc3339

Implementation of the majority of RFC 3339 for python.
Python
55
star
13

udp-exchange

Extends RabbitMQ Server with support for a new experimental exchange type, `x-udp`.
Erlang
50
star
14

synchrotron

A Javascript JSON DVCS underpinning a Smalltalk-like image holding code & data in a single HTML file. A bit like Tiddlywiki. Work in progress. Also, little modules for diff, diff3, and merge of text etc.
HTML
42
star
15

script-exchange

RabbitMQ "Script Exchange" plugin
Erlang
41
star
16

racket-rfc6455

RFC 6455 WebSockets support for Racket.
Racket
40
star
17

racket-something

Indentation-based Racket Syntax
Racket
37
star
18

erlang-smtp

Erlang SMTP and POP3 server code.
Erlang
35
star
19

kali-scheme

Kali is Kelsey and Rees's distributed Scheme implementation based on Scheme48
Scheme
34
star
20

presence-exchange

An experimental RabbitMQ "Presence" exchange: notifies bound queues when other bindings appear and disappear
Java
32
star
21

racket-explorer

Utility for interactive exploration of complex data structures.
Racket
32
star
22

3-move

a multi-user networked online text-based programmable virtual environment
C
31
star
23

racket-bitsyntax

Erlang-style binaries/bitstrings for Racket
Racket
31
star
24

erlang-scrypt

Erlang port driver for Colin Percival's "scrypt" function.
C
25
star
25

erlang-ircd

A pluggable IRC daemon application/library for Erlang.
Erlang
23
star
26

js-vau

Kernel-like interpreter
JavaScript
22
star
27

camstream

Camstream uses AMQP to route live, streaming video from one or more webcams to one or more displays.
Java
22
star
28

racket-monad

Monads for Racket (!)
Racket
21
star
29

revctrl.org

An extract, as complete as I can make it, of content from the revctrl.org wiki
Python
21
star
30

racket-ansi

ANSI and VT10x escape sequences for Racket.
Racket
18
star
31

rabbitmq-xmpp

RabbitMQ XMPP gateway
Erlang
16
star
32

erlang-nacl

Erlang binding to NaCl in the form of libsodium.
C
15
star
33

racket-packet-socket

Access to raw Ethernet frames from Racket
Racket
14
star
34

racket-operational-transformation

Implements Operational Transformation (OT) for Racket.
Racket
14
star
35

squeaker

Like Docker, but for Squeak. You know, for kids.
Python
14
star
36

racket-nat-traversal

Racket implementation of NAT traversal utilities for opening ports on home routers.
Racket
13
star
37

ocaml-networking

Network server programming with OCaml
OCaml
12
star
38

squeak-actors

Erlang-style Actors for Squeak. http://tonyg.github.io/squeak-actors/
CSS
12
star
39

racket-reloadable-example

A simple website written with the Racket webserver, supporting runtime code-reloading.
Racket
12
star
40

racket-stomp

An implementation of the STOMP 1.1 protocol (client) for Racket
Racket
12
star
41

smlnj-networking

Network server programming with SML/NJ and CML
Standard ML
11
star
42

minimart

Repository has moved: https://git.syndicate-lang.org/syndicate-lang/minimart-2014
11
star
43

stoj

A join-calculus-based programming language for exploring description, simulation, and visualization of biochemical systems modelled as concurrent, rate-limited processes.
Java
10
star
44

prex

Prex is an open source, royalty-free, real-time operating system for embedded systems.
C
9
star
45

rust-scheme

I am teaching myself Rust by failing a lot.
Rust
9
star
46

newmoon

A naïve continuation-passing-style-transform based compiler for a language asymptotically approaching R5RS Scheme.
Scheme
9
star
47

delta-t-udp

Experiments in constructing a Delta-t-like protocol atop UDP.
8
star
48

python-supercollider

Communicate with the Supercollider server using python instead of the language and GUI
Python
8
star
49

hop

Repository has moved: https://git.syndicate-lang.org/syndicate-lang/hop-2012
7
star
50

racl

Racket bindings for nacl.cr.yp.to
Racket
7
star
51

streaming-bt

Experiments in N-way queue replication
Erlang
7
star
52

racket-unix-signals

Sending and handling Unix signals from Racket
Racket
7
star
53

racketmq

Repository has moved: https://git.syndicate-lang.org/syndicate-lang/racketmq-2017
7
star
54

erlang-xmpp-component

XMPP Component Protocol (XEP-0114) Library for Erlang
Erlang
7
star
55

js-marketplace

Repository has moved: https://git.syndicate-lang.org/syndicate-lang/js-marketplace-2014
7
star
56

racket-auxiliary-macro-context

Syntactic extensibility outside expressions.
Racket
6
star
57

webcam-snapshot-osx

Java Quicktime code for grabbing frames off a webcam on OS X
Java
6
star
58

texpict.rkt

Embed LaTeX snippets in picts for e.g. slideshow usage
Racket
6
star
59

erlang-openmoko

Erlang Openmoko userland replacement for gsmd/frameworkd/dialer/sms/addressbook etc.
Erlang
6
star
60

marketplace-dns

Repository has moved: https://git.syndicate-lang.org/syndicate-lang/marketplace-dns-2014
6
star
61

multicast-sync-experiments

Experiments in using multicasting to synchronise participants in a distributed topic
Erlang
5
star
62

xcb-shim

Intermediate form of XCB protocol specification data structures, to make it easier to build native bindings rather than wrapping libxcb
Python
5
star
63

passthru

TCP proxy tool for getting a (hex) dump of everything that passes by. Useful for protocol analysis and debugging.
C
5
star
64

erlang-jukebox

An Erlang Jukebox server
Python
5
star
65

racket-effects

Delimited-continuation-based effects for Racket
Racket
5
star
66

rais

C
5
star
67

scheme-httpd

A simple HTTP daemon, written in R5RSish Scheme
Scheme
5
star
68

stomp

A ruby gem for sending and receiving messages from a Stomp protocol compliant message queue.
Ruby
5
star
69

racket-abnf

ABNF parser implementation #lang for Racket.
Racket
5
star
70

minimart-netstack

Repository has moved: https://git.syndicate-lang.org/syndicate-lang/minimart-netstack-2014
5
star
71

racket-font

First-principles font rendering for Racket
Racket
5
star
72

marketplace-ssh

Repository has moved: https://git.syndicate-lang.org/syndicate-lang/marketplace-ssh-2014
5
star
73

pyle2

A python WikiClone
Python
4
star
74

racket-dbus

Native D-Bus Client For Racket
Racket
4
star
75

barebones-android

Scavenging a kernel, initramfs, and recovery tooling from a Cyanogenmod installation, and using them to make a horrible Frankenstein's monster.
C
4
star
76

rabbitmq-universe

Scripts for building packages for RabbitMQ server and plugins
Python
4
star
77

racket-timers

Timer event utilities that integrate well with (sync).
Racket
4
star
78

gst-cairo-widgets

A widget library for GNU Smalltalk built on SDL and Cairo
Smalltalk
4
star
79

racket-opencv-videocapture

Simple FFI binding to opencv video capture routines from Racket
Racket
4
star
80

mixfix

A mixfix parsing system after Danielsson and Norell 2008
Racket
4
star
81

racket-content-defined-chunking

Content-Defined Chunking, after rsync / Rabin fingerprinting / FastCDC / Ronomon's algorithm.
Racket
4
star
82

cl-match

NOTE: SUPERSEDED BY OPTIMA, https://github.com/m2ym/optima. Bug fixes for Daniel S. Bensen's CL-MATCH common-lisp pattern matching library
Common Lisp
4
star
83

json-shapes

Schema validation and other goodies for JSON data
JavaScript
4
star
84

racket-google

Google APIs (Drive, Plus, ...) for Racket.
Racket
4
star
85

racket-camstream

A Racket camdisplay implementation compatible with the original.
Racket
3
star
86

racket-send-exp

Terse syntax for object-oriented message sending
Racket
3
star
87

ohm-ecmascript-extension-example

Example demonstrating syntactic extension of ES5 using Ohm.
JavaScript
3
star
88

racket-struct-defaults

Default values in struct ctors and patterns.
Racket
3
star
89

racket-pkg-server

Racket package catalog server setup scripts and ops documentation
Shell
3
star
90

racket-inverted-index

Playing around with inverted indexing for Racket
Racket
3
star
91

racket-ohm

An Ohm-style parsing package.
Racket
3
star
92

racket-salty-crypto

libsodium and libb2 bindings for Racket, plus an implementation of the Noise Protocol Framework
Racket
3
star
93

pgg

The PGG system is Peter Thiemann's partial evaluation system for the programming language Scheme.
Scheme
3
star
94

racket-critbit

Critbit trees in Racket
Racket
3
star
95

racket-portaudio

FFI bindings to PortAudio, http://portaudio.com/
Racket
3
star
96

racket-opus

FFI bindings to Opus, http://www.opus-codec.org/
Racket
3
star
97

js-voice

Audio-processing libraries for Javascript suitable for simple VOIPish applications
JavaScript
3
star
98

racket-scrypt

Racket binding to Colin Percival's "scrypt" function.
Racket
3
star
99

gyre

A simple CMS, usable as a blog or wiki.
Python
3
star
100

racket-pretty-printing-combinators

Pretty-printing combinators for Racket.
Racket
2
star