Code Monkey home page Code Monkey logo

ezcrypto-js's Introduction

EzCrypto.js – Easy to use Crypto for JavaScript

This is a nice and usable DSL that wraps some JavaScript encryption libraries from some awesome folks:

I didn’t write any of the encryption libraries, I just wrapped them in a nice and easy to understand DSL that is inspired by the EzCrypto Ruby gem.

WARNING: JavaScript crytography is still more or less the wild west. See this article for a pretty decent explanation of what you are getting yourself into. Use at your own risk.

Usage

Loading the libraries

When you load ezcrypto.js it will include the JavaScript encryption libraries from vendor/. In the future this might turn into one big CommonJS style module.

  
    <script src="ezcrypto.js"></script>
  

Generating a key and encrypting data using that key

The most secure type of key is the randomly generated key:

  
    <script src="ezcrypto.js"></script>
    
    // generate an RSA keypair
    var key = ezcrypto.generateKey();
    => {"private": "3521df…", "public": "95d8cf…"}
    
    // encrypt a message with your key
    // under the hood, this AES encrypts your message using your RSA key
    // because AES works better with larger chunks of data than RSA
    var encryptedData = ezcrypto.encrypt('secrets', key);
    => {"key": "8170f7…", "message": "72308a…"}
    
    // decrypt the encrypted data with your key
    ezcrypto.decrypt(encryptedData, key));
    => 'secrets'
  

Generating a key with a password and encrypting data using that password

You can also use a password to encrypt data, in case you don’t want to store keys and instead want to remember passwords. This is obviously less secure since rememberable passwords are much much shorter than RSA keys. The generateKey method simply creates JavaScript Objects.

      
    // generate an RSA keypair and immediately encrypt the users password using the RSA key.
    var key = ezcrypto.generateKey('my_awesome_password');
    => {"private": "3521df…", "public": "95d8cf…", "encryptedPassword": "823jbv…"}
    
    // encrypting and decrypting messages works the same
    var encryptedData = ezcrypto.encrypt('secrets', key);
    => {"key": "2j9svs…", "message": "393bb3…"}
    
    ezcrypto.decrypt(encryptedData, key));
    => 'secrets'
  

Open test.html in a browser and check console output for a demo.

License

The MIT License

Copyright © 2010 Max Ogden and Tyler Gillies

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the “Software”), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

ezcrypto-js's People

Contributors

hackervera avatar max-mapper avatar

Watchers

 avatar

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.