|
| 1 | +[](https://www.nuget.org/packages/Databridges.NaCl.Wrapper)[](https://docs.microsoft.com/en-us/dotnet/standard/net-standard#net-implementation-support) |
| 2 | + |
| 3 | +# Databridges C# NaCl Wrapper Library |
| 4 | + |
| 5 | +Databridges NACL wrapper gives you a simple write and read function using implementation of the secretbox encryption standard defined in NaCl. |
| 6 | + |
| 7 | +Databridges NACL wrapper is available for |
| 8 | + |
| 9 | +- [JavaScript](https://github.com/databridges-io/lib.javascript.nacl.wrapper.git) |
| 10 | +- [NodeJS](https://github.com/databridges-io/lib.nodejs.nacl.wrapper.git) |
| 11 | +- [C#](https://github.com/databridges-io/lib.csharp.nacl.wrapper.git) |
| 12 | +- [Python](https://github.com/databridges-io/lib.python.nacl.wrapper.git) |
| 13 | +- [Java for Android](https://github.com/databridges-io/lib.android.nacl.wrapper.git) |
| 14 | +- [iOS Swift](https://github.com/databridges-io/lib.ios.nacl.wrapper.git) |
| 15 | + |
| 16 | +The above wrappers can be used to send encrypted messages between them. |
| 17 | + |
| 18 | +> The Databridges NACL wrapper for C# Language binding uses `NaCl.Net` to deliver implementation of the secretbox encryption standard defined in NaCl. |
| 19 | +
|
| 20 | +## Usage Overview |
| 21 | + |
| 22 | +The following topics are covered: |
| 23 | + |
| 24 | + - [Supported platforms](#supported-platforms) |
| 25 | + - [Installation.](#installation) |
| 26 | + - [Initialization](#initialization) |
| 27 | + - [Global Configuration](#global-configuration) |
| 28 | + - [How to use with Databridges C# Library](#how-to-use-with-databridges-nodejs-library) |
| 29 | + - [Change Log](#change-log) |
| 30 | + - [License](#license) |
| 31 | + |
| 32 | +## Supported platforms |
| 33 | + |
| 34 | +- .NET and .NET Core - 2.0, 2.1, 2.2, 3.0, 3.1, 5.0, 6.0 |
| 35 | +- .NET Framework 1 - 4.6.1 2, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8 |
| 36 | + |
| 37 | +## Installation |
| 38 | + |
| 39 | +You can use any visual studio package manager. |
| 40 | + |
| 41 | +```bash |
| 42 | +Install-Package Databridges.NaCl.Wrapper |
| 43 | +``` |
| 44 | + |
| 45 | +## Initialization |
| 46 | + |
| 47 | +```c# |
| 48 | +using Databridges.NaCl.Wrapper; |
| 49 | +DataBridges_NaCl_Wrapper secretData = new DataBridges_NaCl_Wrapper(); |
| 50 | +``` |
| 51 | + |
| 52 | +## Global Configuration |
| 53 | + |
| 54 | +### Required |
| 55 | + |
| 56 | +The following is the required properties before using to dataBridges NaCl wrapper. |
| 57 | + |
| 58 | +```c# |
| 59 | +secretData.secret = '32 char alphanumeric string'; |
| 60 | +``` |
| 61 | + |
| 62 | +| Properties | Description | |
| 63 | +| ---------- | ------------------------------------------------------------ | |
| 64 | +| `secret` | *(string)* 32 char alpha numeric string. NaCl encryption secret. | |
| 65 | + |
| 66 | +## Encrypt data |
| 67 | + |
| 68 | +To encrypt data using NaCl, databridges wrapper exposes a method named `write`, This will return encrypted data if successful else it will throw error. |
| 69 | + |
| 70 | +#### write() |
| 71 | + |
| 72 | +```c# |
| 73 | +try { |
| 74 | + string encData = secretData.write("Your Data.."); |
| 75 | + Console.WriteLine('Encrypted: {0}', encData); |
| 76 | +} catch (dbnwError err) { |
| 77 | + console.log('Errors : {0} {1} {2}', err.source, err.code, err.message); |
| 78 | +} |
| 79 | +``` |
| 80 | + |
| 81 | +| Parameter | Description | |
| 82 | +| --------- | ---------------------------------- | |
| 83 | +| `data` | *(string)* *data* to be encrypted. | |
| 84 | + |
| 85 | +| Return Values | Description | |
| 86 | +| ------------- | ------------------ | |
| 87 | +| `string` | Encrypted string. | |
| 88 | + |
| 89 | +##### Exceptions: |
| 90 | + |
| 91 | +| Source | Code | Description | |
| 92 | +| ------------------ | -------------- | ---------------------------------------------- | |
| 93 | +| DBLIB_NACL_WRAPPER | INVALID_SECRET | `secret` is not set with the wrapper instance. | |
| 94 | +| DBLIB_NACL_WRAPPER | INVALID_DATA | If `data` is not passed to the function. | |
| 95 | +| DBLIB_NACL_WRAPPER | NACL_EXCEPTION | Exceptions generated by NaCl library. | |
| 96 | + |
| 97 | +## Decrypt data |
| 98 | + |
| 99 | +To decrypt data using NaCl, databridges wrapper exposes a method named `read`, This will return decrypted data if successful else it will throw error. |
| 100 | + |
| 101 | +#### read() |
| 102 | + |
| 103 | +```c# |
| 104 | +try { |
| 105 | + string decData = secretData.read("<Encrypted data.>"); |
| 106 | + console.log('Decrypted: {0}', decData); |
| 107 | +} catch (dbnwError err) { |
| 108 | + console.log('Errors : {0} {1} {2}', err.source, err.code, err.message); |
| 109 | +} |
| 110 | +``` |
| 111 | + |
| 112 | +| Parameter | Description | |
| 113 | +| --------- | ---------------------------------- | |
| 114 | +| `data` | *(string)* *data* to be encrypted. | |
| 115 | + |
| 116 | +| Return Values | Description | |
| 117 | +| ------------- | ------------------ | |
| 118 | +| `string` | Encrypted string. | |
| 119 | + |
| 120 | +##### Exceptions: |
| 121 | + |
| 122 | +| Source | Code | Description | |
| 123 | +| ------------------ | ------------------- | ------------------------------------------------------------ | |
| 124 | +| DBLIB_NACL_WRAPPER | INVALID_SECRET | `secret` is not set with the wrapper instance. | |
| 125 | +| DBLIB_NACL_WRAPPER | INVALID_DATA | If `data` is not passed to the function OR `data` is not a valid encrypted string. | |
| 126 | +| DBLIB_NACL_WRAPPER | NACL_EXCEPTION | Exceptions generated by NaCl library. | |
| 127 | +| DBLIB_NACL_WRAPPER | NACL_DECRYPT_FAILED | If decryption fails due to invalid secret or manipulated data. | |
| 128 | + |
| 129 | + |
| 130 | + |
| 131 | +## How to use with Databridges C# Library |
| 132 | + |
| 133 | +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. |
| 134 | + |
| 135 | +```c# |
| 136 | +// Initialize both databridges-sio-client-lib and databridges-nacl-wrapper |
| 137 | +using dBridges; |
| 138 | +using Databridges.NaCl.Wrapper; |
| 139 | + |
| 140 | +dBridges.dBridges dbridge = new dBridges.dBridges(); |
| 141 | +DataBridges_NaCl_Wrapper secretData = new DataBridges_NaCl_Wrapper(); |
| 142 | +secretData.secret = "Your32 char secret."; |
| 143 | + |
| 144 | +// .... Your databridges code comes here. |
| 145 | +
|
| 146 | +// On Subscription success event. |
| 147 | + Action<object, object> subscribesuccess; |
| 148 | +subscribedChannel.bind("dbridges:subscribe.success", subscribesuccess = (payload, metadata) => { |
| 149 | + bool mflag = false; |
| 150 | + string encData = ""; |
| 151 | + try { |
| 152 | + // Encrypt data to publish |
| 153 | + encData = secretData.write("Your Data.."); |
| 154 | + mflag = true; |
| 155 | + } catch(dbnwError err) { |
| 156 | + Console.WriteLine("Error: {0} {1} {2}", err.source, err.code, err.message); |
| 157 | + } |
| 158 | + if (mflag) { |
| 159 | + subscribedChannel.publish("eventName", encData, "1"); |
| 160 | + } |
| 161 | +}); |
| 162 | + |
| 163 | +// On payload Received event. |
| 164 | +Action<object, object> on_eventrecd; |
| 165 | +subscribedChannel.bind("eventName", on_eventrecd = (payload, metadata) => { |
| 166 | + try { |
| 167 | + // Decrypt data received in the event. |
| 168 | + string decData = secretData.read(payload as string); |
| 169 | + Console.WriteLine("Decrypted: {0}", decData); |
| 170 | + } catch (dbnwError err) { |
| 171 | + Console.WriteLine("Error: {0} {1} {2}", err.source, err.code, err.message); |
| 172 | + } |
| 173 | + |
| 174 | +}); |
| 175 | +``` |
| 176 | + |
| 177 | + |
| 178 | + |
| 179 | +## Change Log |
| 180 | + * [Change log](CHANGELOG.md): Changes in the recent versions |
| 181 | + |
| 182 | +## License |
| 183 | + |
| 184 | +DataBridges NaCl Wrapper is released under the [MIT license](LICENSE). |
| 185 | + |
| 186 | +``` |
| 187 | + Copyright 2022 Optomate Technologies Private Limited. |
| 188 | +
|
| 189 | + Permission is hereby granted, free of charge, to any person obtaining |
| 190 | + a copy of this software and associated documentation files (the |
| 191 | + "Software"), to deal in the Software without restriction, including |
| 192 | + without limitation the rights to use, copy, modify, merge, publish, |
| 193 | + distribute, sublicense, and/or sell copies of the Software, and to |
| 194 | + permit persons to whom the Software is furnished to do so, subject to |
| 195 | + the following conditions: |
| 196 | +
|
| 197 | + The above copyright notice and this permission notice shall be |
| 198 | + included in all copies or substantial portions of the Software. |
| 199 | +
|
| 200 | + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 201 | + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 202 | + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 203 | + NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 204 | + LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 205 | + OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 206 | + WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 207 | +``` |
| 208 | + |
0 commit comments