Skip to content

Commit 1bea969

Browse files
committed
Initial release
0 parents  commit 1bea969

File tree

10 files changed

+862
-0
lines changed

10 files changed

+862
-0
lines changed

.gitignore

Lines changed: 404 additions & 0 deletions
Large diffs are not rendered by default.

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Changelog
2+
3+
## 1.0.1
4+
5+
* Initial Release.

Dependencies.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# External Dependencies
2+
3+
This file lists the dependencies used in this repository.
4+
5+
| Dependency | License |
6+
| --------------------- | ---------- |
7+
|NaCl.Net@0.1.13 | MPL-2.0 |

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
Copyright 2022 Optomate Technologies Private Limited.
3+
4+
Permission is hereby granted, free of charge, to any person obtaining
5+
a copy of this software and associated documentation files (the
6+
"Software"), to deal in the Software without restriction, including
7+
without limitation the rights to use, copy, modify, merge, publish,
8+
distribute, sublicense, and/or sell copies of the Software, and to
9+
permit persons to whom the Software is furnished to do so, subject to
10+
the following conditions:
11+
12+
The above copyright notice and this permission notice shall be
13+
included in all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
![img](https://img.shields.io/badge/Licence-MIT-green.svg)[![NuGet](https://img.shields.io/badge/NuGet-Databridges.NaCl.Wrapper-%23004880)](https://www.nuget.org/packages/Databridges.NaCl.Wrapper)[![Target Framework](https://img.shields.io/badge/Target%20Framework-.NET%20Standard%202.0-%237014e8)](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+

dataBridges_nacl_wrapper.sln

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.31729.503
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Databridges.NaCl.Wrapper", "src\dataBridges_nacl_wrapper.csproj", "{DB29ECD7-482A-462C-AAE4-F17A01213469}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{DB29ECD7-482A-462C-AAE4-F17A01213469}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{DB29ECD7-482A-462C-AAE4-F17A01213469}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{DB29ECD7-482A-462C-AAE4-F17A01213469}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{DB29ECD7-482A-462C-AAE4-F17A01213469}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {94B42B61-6EBD-40EE-AD46-05A07427AA40}
24+
EndGlobalSection
25+
EndGlobal

optomate.png

12.5 KB
Loading

src/dataBridges_nacl_wrapper.cs

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
DataBridges C# NaCl wrapper for databridges library.
3+
https://www.databridges.io/
4+
5+
6+
Copyright 2022 Optomate Technologies Private Limited.
7+
8+
Permission is hereby granted, free of charge, to any person obtaining
9+
a copy of this software and associated documentation files (the
10+
"Software"), to deal in the Software without restriction, including
11+
without limitation the rights to use, copy, modify, merge, publish,
12+
distribute, sublicense, and/or sell copies of the Software, and to
13+
permit persons to whom the Software is furnished to do so, subject to
14+
the following conditions:
15+
16+
The above copyright notice and this permission notice shall be
17+
included in all copies or substantial portions of the Software.
18+
19+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26+
*/
27+
28+
using System;
29+
using System.Security.Cryptography;
30+
using System.Text;
31+
using NaCl;
32+
33+
34+
namespace Databridges.NaCl.Wrapper
35+
{
36+
37+
38+
public class DataBridges_NaCl_Wrapper
39+
{
40+
public String secret = "";
41+
42+
public DataBridges_NaCl_Wrapper()
43+
{
44+
this.secret = "";
45+
}
46+
47+
public String write(String message)
48+
{
49+
50+
if (String.IsNullOrEmpty(this.secret)) { throw new dbnwError("INVALID_SECRET", ""); }
51+
if (String.IsNullOrEmpty(message)) { throw new dbnwError("INVALID_DATA", ""); }
52+
try
53+
{
54+
byte[] secretKey = Encoding.UTF8.GetBytes(this.secret);
55+
byte[] m_message = Encoding.UTF8.GetBytes(message);
56+
var range = RandomNumberGenerator.Create();
57+
byte[] nonce = new byte[Curve25519XSalsa20Poly1305.NonceLength];
58+
range.GetBytes(nonce);
59+
using (XSalsa20Poly1305 secretBox = new XSalsa20Poly1305(secretKey))
60+
{
61+
byte[] encrypteddBytes = new byte[message.Length + XSalsa20Poly1305.TagLength];
62+
secretBox.Encrypt(encrypteddBytes, m_message, nonce);
63+
String result = Encoding.UTF8.GetString(encrypteddBytes);
64+
return Convert.ToBase64String(nonce) + ":" + Convert.ToBase64String(encrypteddBytes);
65+
}
66+
}
67+
catch (Exception e)
68+
{
69+
throw new dbnwError("NACL_EXCEPTION", e.Message);
70+
}
71+
}
72+
73+
public string read(String data)
74+
{
75+
76+
string decryptedText = null;
77+
byte[] cipher = null;
78+
byte[] nonce = null;
79+
80+
if (String.IsNullOrEmpty(this.secret)) { throw new dbnwError("INVALID_SECRET", ""); }
81+
if (String.IsNullOrEmpty(data)) { throw new dbnwError("INVALID_DATA", ""); }
82+
83+
String[] splitdata = data.Split(":".ToCharArray());
84+
if (splitdata.Length != 2) { throw new dbnwError("INVALID_DATA", ""); }
85+
86+
try
87+
{
88+
cipher = Convert.FromBase64String(splitdata[1]);
89+
nonce = Convert.FromBase64String(splitdata[0]);
90+
91+
byte[] secretKey = Encoding.UTF8.GetBytes(this.secret);
92+
using (XSalsa20Poly1305 secretBox = new XSalsa20Poly1305(secretKey))
93+
{
94+
byte[] decryptedBytes = new byte[cipher.Length - XSalsa20Poly1305.TagLength];
95+
if (secretBox.TryDecrypt(decryptedBytes, cipher, nonce))
96+
{
97+
decryptedText = Encoding.UTF8.GetString(decryptedBytes);
98+
return decryptedText;
99+
}
100+
else
101+
{
102+
return data;
103+
}
104+
}
105+
}
106+
catch (Exception e)
107+
{
108+
throw new dbnwError("NACL_EXCEPTION", e.Message);
109+
}
110+
}
111+
}
112+
}

0 commit comments

Comments
 (0)