Giter Site home page Giter Site logo

databridges-io / lib.csharp.nacl.wrapper Goto Github PK

View Code? Open in Web Editor NEW
0.0 0.0 0.0 22 KB

DataBridges C# NaCl wrapper for databridges library.

Home Page: https://www.databridges.io

License: MIT License

C# 100.00%
optomate databridges pubsub rpc websocket ws events realtime real-time nacl encryption

lib.csharp.nacl.wrapper's Introduction

imgNuGetTarget Framework

Databridges C# NaCl Wrapper Library

Databridges NACL wrapper gives you a simple write and read function using implementation of the secretbox encryption standard defined in NaCl.

Databridges NACL wrapper is available for

The above wrappers can be used to send encrypted messages between them.

The Databridges NACL wrapper for C# Language binding uses NaCl.Net to deliver implementation of the secretbox encryption standard defined in NaCl.

Usage Overview

The following topics are covered:

Supported platforms

  • .NET and .NET Core - 2.0, 2.1, 2.2, 3.0, 3.1, 5.0, 6.0
  • .NET Framework 1 - 4.6.1 2, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8

Installation

You can use any visual studio package manager.

Install-Package Databridges.NaCl.Wrapper

Initialization

using Databridges.NaCl.Wrapper; 
DataBridges_NaCl_Wrapper secretData = new DataBridges_NaCl_Wrapper();

Global Configuration

Required

The following is the required properties before using to dataBridges NaCl wrapper.

secretData.secret = '32 char alphanumeric string';
Properties Description
secret (string) 32 char alpha numeric string. NaCl encryption secret.

Encrypt data

To encrypt data using NaCl, databridges wrapper exposes a method named write, This will return encrypted data if successful else it will throw error.

write()

try {
    string encData = secretData.write("Your Data..");
    Console.WriteLine('Encrypted: {0}', encData);
} catch (dbnwError err) {
    console.log('Errors : {0} {1} {2}', err.source, err.code, err.message);
}
Parameter Description
data (string) data to be encrypted.
Return Values Description
string Encrypted string.
Exceptions:
Source Code Description
DBLIB_NACL_WRAPPER INVALID_SECRET secret is not set with the wrapper instance.
DBLIB_NACL_WRAPPER INVALID_DATA If data is not passed to the function.
DBLIB_NACL_WRAPPER NACL_EXCEPTION Exceptions generated by NaCl library.

Decrypt data

To decrypt data using NaCl, databridges wrapper exposes a method named read, This will return decrypted data if successful else it will throw error.

read()

try {
    string decData = secretData.read("<Encrypted data.>");
    console.log('Decrypted: {0}', decData);
} catch (dbnwError err) {
    console.log('Errors : {0} {1} {2}', err.source, err.code, err.message);
}
Parameter Description
data (string) data to be encrypted.
Return Values Description
string Encrypted string.
Exceptions:
Source Code Description
DBLIB_NACL_WRAPPER INVALID_SECRET secret is not set with the wrapper instance.
DBLIB_NACL_WRAPPER INVALID_DATA If data is not passed to the function OR data is not a valid encrypted string.
DBLIB_NACL_WRAPPER NACL_EXCEPTION Exceptions generated by NaCl library.
DBLIB_NACL_WRAPPER NACL_DECRYPT_FAILED If decryption fails due to invalid secret or manipulated data.

How to use with Databridges C# Library

Below code shows how to integrate the NaCl wrapper with the Databridges library. After initialize you can use the wrapper library to encrypt and decrypt the data when publishing and receiving events.

// Initialize both databridges-sio-client-lib and databridges-nacl-wrapper
using dBridges;
using Databridges.NaCl.Wrapper;

dBridges.dBridges dbridge = new dBridges.dBridges();
DataBridges_NaCl_Wrapper secretData = new DataBridges_NaCl_Wrapper(); 
secretData.secret = "Your32 char secret.";

// .... Your databridges code comes here.

// On Subscription success event.
 Action<object, object> subscribesuccess;
subscribedChannel.bind("dbridges:subscribe.success", subscribesuccess = (payload, metadata) => {
    bool mflag = false;
	string encData = "";
    try {
		// Encrypt data to publish
    	encData = secretData.write("Your Data..");
        mflag = true;
    } catch(dbnwError err) {
        Console.WriteLine("Error:  {0} {1} {2}", err.source, err.code, err.message);
    }
    if (mflag) {
        subscribedChannel.publish("eventName", encData, "1");
    }
});

// On payload Received event.
Action<object, object> on_eventrecd;
subscribedChannel.bind("eventName", on_eventrecd = (payload, metadata) => {
    try {
        // Decrypt data received in the event.
        string decData = secretData.read(payload as string);
        Console.WriteLine("Decrypted: {0}", decData);
    } catch (dbnwError err) {
        Console.WriteLine("Error: {0} {1} {2}", err.source, err.code, err.message);
    }

});

Change Log

License

DataBridges NaCl Wrapper is released under the MIT license.

    Copyright 2022 Optomate Technologies Private Limited.

    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.

lib.csharp.nacl.wrapper's People

Contributors

optomate-tech 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.