Coder Social home page Coder Social logo

filecrypt's Introduction

FileCrypt

npm

A webcrypto wrapper for files

Install:

npm i --save-dev filecrypt

Usage:

import FileCrypt from 'filecrypt';

Generating an encryption key:

FileCrypt.generateKey()
.then(key => {
    // key is a CryptoKey object
    console.log(key);
});

Generating an encryption key from a password:

FileCrypt.importPassword(password)
.then(key => {
    // key is a CryptoKey object
    console.log(key);
});

Wrapping and unwrapping a CryptoKey for storage:

FileCrypt.wrapKey(keyToWrap, wrappingKey)
.then(data => {
    const {key, iv} = data;
    // key is an ArrayBuffer representing the wrapped key, iv is the iv used
    console.log(key);
});
FileCrypt.unwrapKey(wrappedKey, unwrappingKey, iv)
.then(key => {
    // key is a CryptoKey
    console.log(key);
});

Importing and exporting a CryptoKey for insecure storage:

FileCrypt.exportKey(key)
.then(buf => {
    // buf is an ArrayBuffer
    console.log(buf);
});
FileCrypt.importKey(buf)
.then(key => {
    // key is a CryptoKey
    console.log(key);
});

Encrypting and decrypting:

input.addEventListener('change', function(e) {
	var file = e.target.files.item(0);
	FileCrypt.encrypt(key, file) // key is a CryptoKey
	.then(res => {
	    let {result, iv} = res;
	    console.log(iv.buffer); // the ArrayBuffer containing the iv used to encrypt
	    console.log(result); // the ArrayBuffer containing the encrypted data
	});
});

FileCrypt.decrypt(key, iv, buffer)
.then(data => {
    // data is an ArrayBuffer containing the decrypted data
    console.log(data);
});

Saving the iv together with the encrypted file:

var merged = FileCrypt.mergeIvAndData(iv.buffer, result);
// merged is iv buffer prepended to result buffer

let {iv, data} = FileCrypt.splitIvAndData(merged);
console.log(iv);
console.log(data);
// iv and data are ArrayBuffers

Extra utilities:

// ArrayBuffer to base64 string
FileCrypt.ab2str(arrayBuffer);

// base64 string to ArrayBuffer
FileCrypt.str2ab(b64str);

// ArrayBuffer to File
FileCrypt.ab2file(arrayBuffer);

// File to ArrayBuffer
FileCrypt.file2ab(file)
.then(buffer => console.log(buffer));

filecrypt's People

Contributors

tklg avatar

Watchers

James Cloos avatar

filecrypt's Issues

Can I use your accordion?

Hi @villa7 ๐Ÿ‘‹ ๐Ÿ˜…

I normally don't do this kind of thing, but I couldn't find any other way to contact you. I saw your accordion on codepen and I thought it looked really nice. I wonder if you would give me permission to use it in a talk that I'm giving in July. I'll be talking about component patterns and want to use an accordion as a base example for things. I would probably just have a gif of your accordion at the start of my talk to give context for everyone. Let me know if that's ok with you.

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.