Giter VIP home page Giter VIP logo

keyczarjs's Introduction

Keyczar JS

A partial Javascript implementation of Google Keyczar. It is a wrapper around the Forge Javascript crypto library. Released under the Apache 2.0 license, like the official Keyczar library.

Quick Start

  1. Run npm install in the keyczarjs directory to download Forge using NPM.
  2. Run ./runtests.sh to run all the unit tests.
  3. Open browser_test.html for an example of Keyczar JS in a web browser.
  4. (OPTIONAL): Run make to run the Closure compiler to type check all JavaScript (you will probably need to editg the Makefile to provide the location of the Closure Compiler .jar file)

Example use (NodeJS)

var keyczar = require('./keyczar');

// Create a new keyset and serialize it
var keyset = keyczar.create(keyczar.TYPE_AES);
var keysetSerialized = keyset.toJson();

// Load the keyset and use it
var plaintext = 'hello message';
keyset = keyczar.fromJson(keysetSerialized);
var encrypted = keyset.encrypt(plaintext);
var decrypted = keyset.decrypt(encrypted);
console.log('plaintext:', plaintext);
console.log('encrypted:', encrypted);
console.log('decrypted:', decrypted);

// Create an asymmetric key
var private = keyczar.create(keyczar.TYPE_RSA_PRIVATE);
var public = private.exportPublicKey();
var privateSerialized = private.toJson();

// encrypt some data in a "session" to avoid asymmetric length limits
var session = keyczar.createSessionCrypter(public);
encrypted = session.encrypt(plaintext);
var sessionMaterial = session.sessionMaterial;

// take the private key and the session material to decrypt the data
private = keyczar.fromJson(privateSerialized);
session = keyczar.createSessionCrypter(private, sessionMaterial);
decrypted = session.decrypt(encrypted);
console.log('plaintext:', plaintext);
console.log('sessionMaterial:', sessionMaterial);
console.log('encrypted:', encrypted);
console.log('decrypted:', decrypted);

// convenience method to pack session material together with the message
encrypted = keyczar.encryptWithSession(public, plaintext);
decrypted = keyczar.decryptWithSession(private, encrypted);
console.log('plaintext:', plaintext);
console.log('encrypted:', encrypted);
console.log('decrypted:', decrypted);

Differences from the original Keyczar implementation

  • Input is treated as a Javascript string (Unicode). It is encoded as UTF-8 before encryption, and decoded back to a Javascript Unicode string after decryption. This can cause exceptions to be thrown if decrypting binary data that is not valid UTF-8. In this case, use encryptBinary()/decryptBinary().

  • Key sets are read and written as JSON strings. The structure is the same as Keyczar's directories, just as a JSON object.

Password-Protected Keys

KeyczarJS supports reading and writing keys that are encrypted by a password. The format is compatible with the C++ implementation, which is based on OpenSSL's password-based encryption.

To make it difficult to accidentally "leak" an unencrypted key, toJson() does not work for password protected keys. Instead, you should use toJsonEncrypted(). In rare cases where you must access the serialized key, you can use exportDecryptedJson().

Adding KeyczarJS to your project

Each script in this package is usable both by NodeJS (require()) and in a browser. In the browser, all exported functions are in the global keyczar namespace. In a browser, you must load the following script files:

  • From Forge: aes.js sha1.js sha256.js md.js util.js prng.js random.js jsbn.js pbkdf2.js hmac.js asn1.js oids.js pkcs1.js rsa.js pki.js
  • From Keyczar JS: keyczar_util.js keyczar.js

Additions to Java Keyczar

To use Keyczar JS with Java Keyczar, we wrote some additional support classes. Ideally we would like to push some changes upstream:

  • Creating a new keyset without writing it to disk, and adding a key to it. Right now, this involves passing around a KeyczarReader, creating a GenericKeyczar to add keys, writing it out, then re-reading it to create a Crypter.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.