GithubHelp home page GithubHelp logo

sergeyshpak / datapeps-sdk-js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from wallix/datapeps-sdk-js

0.0 2.0 0.0 314 KB

DataPeps SDK in TypeScript/JavaScript

Home Page: https://datapeps.com/docs/sdk/js/index.html

TypeScript 13.54% JavaScript 86.46%

datapeps-sdk-js's Introduction

To start working with DataPeps, you need to add DataPeps SDK to your project.

Using a package manager

The simplest way to get started, is to add DataPeps SDK, using your favourite package manager.

npm

Go to your project directory and run:

npm install datapeps-sdk

yarn

Go to your project directory and run:

yarn add datapeps-sdk

Instantiation of DataPeps SDK

Just add the following line to your code to import the DataPeps SDK:

import * as DataPeps from 'datapeps-sdk';

Using the SDK with Node.js

When using DataPeps SDK with Node.js (on backend or if testing with Mocha), you need to add the following packages:

The components above are supported by browsers, but when calling DataPeps API on backend, you need to add them to the project manually along with a couple of lines to configure their usage. We'll show how to do it further.

Hello, DataPeps!

Let's have a glimpse at how exactly DataPeps simplifies cryptography for a developer.

Say there are two friends, Alice and Bob. Alice wants to share a nice photo of a cheetah with Bob; however, Alice is quite rather concerned about data security (good for her!), and she wants to ensure, that only Bob can eventually see the picture. DataPeps to the rescue!

First, we will show you Alice's part:

async function aliceSend(sdk, fileName) {
    let session = await sdk.login("[email protected]", "pass")
    let resource = await session.Resource.create("img/jpg", "", ["[email protected]"])
    let photo = await readFile(fileName)
    let encryptedPhoto = resource.encrypt(photo)
    await writeFile("encrypted_" + fileName, encryptedPhoto)
    return [resource.id, "encrypted_" + fileName]
}

and Bob's part:

async function bobReceive(
        sdk,
        resourceId,
        encryptedFileName) {
    let session = await sdk.login("[email protected]", "pass")
    let resource = await session.Resource.get(resourceId)
    let encryptedData = await readFile(encryptedFileName)
    let decryptedData = resource.decrypt(encryptedData)
    await writeFile("decrypted_" + encryptedFileName, decryptedData)
}

Short and awesome! Now, let's discuss it a bit.

Suppose, Alice and Bob are already registered with DataPeps (we cover the case when they are not a bit further). First of all, Alice needs to establish a session with DataPeps server:

let session = await sdk.login("[email protected]", "pass")

Now, as Alice wants only Bob to have access to the picture, she needs to encrypt it for Bob (and only Bob). For this Alice creates "a resource":

let resource = await session.Resource.create("img/jpg", "", ["[email protected]"])

Last, Alice encrypts the photo and saves the result:

let photo = await readFile(fileName)
let encryptedPhoto = resource.encrypt(photo)
await writeFile("encrypted_" + fileName, encryptedPhoto)

Alice sends Bob the encrypted file and the resource's ID:

return [resource.id, "encrypted_" + fileName]

When Bob wants to decrypt the encrypted file from Alice, he establishes the session (the same way Alice did) and fetches the resource, created by Alice from the DataPeps server, using the resource id:

let session = await sdk.login("[email protected]", "pass")
let resource = await session.Resource.get(resourceId)

Bob decrypts the file and saves the result:

let encryptedData = await readFile(encryptedFileName)
let decryptedData = resource.decrypt(encryptedData)
await writeFile("decrypted_" + encryptedFileName, decryptedData)

Adding dependencies for Node.js

We will use Node.js to run the resulting code, so we will need to satisfy all the relevant dependencies:

declare var global: any  
global["TextEncoder"] = require('text-encoding').TextEncoder                                                                                                                                                                                                            
global["TextDecoder"] = require('text-encoding').TextDecoder                                                                                                                                                                                               
global["XMLHttpRequest"] = require('xhr2')                                                                                                                                                                                                                                                          
global["WebSocket"] = require('ws')                                                                              
global["btoa"] = require('btoa')
global["atob"] = require('atob')

"Hello, DataPeps" code

Here is the complete example, with definitions of readFile and writeFile functions:

Show code
declare var global: any  
global["TextEncoder"] = require('text-encoding').TextEncoder                                                                                                                                                                                                            
global["TextDecoder"] = require('text-encoding').TextDecoder                                                                                                                                                                                               
global["XMLHttpRequest"] = require('xhr2')                                                                                                                                                                                                                                                          
global["WebSocket"] = require('ws')                                                                              
global["btoa"] = require('btoa')
global["atob"] = require('atob')

import * as Peps from 'pepssdk'
import * as Fs from 'fs'

function readFile(filePath: string) {
    return new Promise<Buffer>((resolve, reject) => {
        let data = Fs.readFile(filePath, (e, data) => {
            if (e) {
                return reject(e)
            }
            return resolve(data)
        })
    })
}

function writeFile(filePath, data) {
    return new Promise<void>((resolve, reject) => {
        Fs.writeFile(filePath, data, (e) => {
            if (e) {
                return reject(e)
            }
            return resolve()
        })
    })
}

async function aliceSend(, fileName) {
    let session = await sdk.login("[email protected]", "pass")
    let resource = await session.Resource.create("img/jpg", "", ["[email protected]"])
    let photo = await readFile(fileName)
    let encryptedPhoto = resource.encrypt(photo)
    await writeFile("encrypted_" + fileName, encryptedPhoto)
    return [resource.id, "encrypted_" + fileName]
}

async function bobReceive(
        sdk,
        resourceId,
        encryptedFileName) {
    let session = await sdk.login("[email protected]", "pass")
    let resource = await session.Resource.get(resourceId)
    let encryptedData = await readFile(encryptedFileName)
    let decryptedData = resource.decrypt(encryptedData)
    await writeFile("decrypted_" + encryptedFileName, decryptedData)
}

async function main() {
    var sdk = new Peps.SDK("https://192.168.99.100:32511")
    var [resourceId, encryptedFileName] = await aliceSend(sdk, "awmore.jpg")
    await bobReceive(sdk, resourceId, encryptedFileName)
}

main().catch((_) => process.stdout.write("An error has occurred\n"))

Finally, create the following *package.json* in the project directory:
{
    "name": "hello-datapeps",
    "version": "0.0.1",
    "main": "hello-datapeps.js",
    "scripts": {
        "start": "node hello-datapeps.js"
    },
    "dependencies": {
        "pepssdk": "^0.0.5",
        "@types/text-encoding": "0.0.31",
        "atob": "^2.0.3",
        "btoa": "^1.1.2",
        "text-encoding": "^0.6.4",
        "ws": "^3.3.2",
        "xhr2": "^0.1.4"
    }
}

and the following tsconfig.json (for compiling TypeScript):

{
    "compilerOptions": {
        "lib": [
            "dom",
            "es2015.promise",
            "es6"
        ],
        "module": "commonjs",
        "target": "es5"
    }
}

Compile and run the code:

npm install && tsc && npm start

If you use yarn instead of npm, run:

yarn install && tsc && yarn run start

datapeps-sdk-js's People

Contributors

fxaguessy avatar

Watchers

 avatar  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.