nanoframework / System.Security.Cryptography

📦 .NET nanoFramework System.Security.Cryptography

Home Page:https://www.nanoframework.net

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Quality Gate Status Reliability Rating NuGet #yourfirstpr Discord

nanoFramework logo


Welcome to the .NET nanoFramework System.Security.Cryptography Library repository

This repository contains the nanoFramework System.Security.Cryptography class library.

Build status

Component Build Status NuGet Package
System.Security.Cryptography Build Status NuGet

System.Security.Cryptography usage

This library brings to .NET nanoFramework C# applications the equivalent implementations provided by Mbed TLS. The target there the code is going to be deployed has to have a firmware image built with this namespace enabled.

HMAC SHA256

This class computes a Hash-based Message Authentication Code (HMAC) by using the SHA256 hash function.

A typical usage for this, in IoT context, is to compute an hashed signature to connect to Azure IoT Hub. Like

Providing one has the _S_hared _A_ccess _K_ey and wants to encode a certain Uri the code snippet that does this is as simple has this:

var hmacsha256 = new HMACSHA256(Convert.FromBase64String(sharedAccessKey));

byte[] hash = hmacsha256.ComputeHash(Encoding.UTF8.GetBytes(encodedUri + "\n" + expiry));

string sig = Convert.ToBase64String(hash);

AES

Advanced Encryption Standard (AES)

AES is a variant of the Rijndael block cipher with different key and block sizes. For AES, NIST selected three members of the Rijndael family, each with a block size of 128 bits, but three different key lengths: 128, 192 and 256 bits.

The current version has support for the ECB mode. Others will be added. The following example demonstrates how to encrypt and decrypt sample data by using the AES class.

Note that the input data has to be multiple of 16 bits, otherwise an exception will be thrown. Data shorter than that should be padded with zeros.

//Sample Usage
string clearText = "Nanoframework";
byte[] clearTextByteArray = Encoding.UTF8.GetBytes(clearText);
// please note the array size: 16 bytes
byte[] clearTextByteArrayWithPadding = new byte[16];
Array.Copy(clearTextByteArray, 0, clearTextByteArrayWithPadding, 0, clearTextByteArray.Length);

// Create a new instance of the Aes
AES aes = new AES(CipherMode.ECB);
aes.Key = new byte[16] { 198, 49, 248, 31, 20, 7, 226, 232, 208, 100, 15, 11, 2, 32, 213, 243 };

// Encrypt the bytes to a string.
var enData = aes.Encrypt(key, clearTextByteArrayWithPadding);
string encryptedText = Encoding.UTF8.GetString(enData);
Debug.WriteLine(encryptedText);

// Decrypt the bytes to a string.
var decryptedByteArray = aes.Decrypt(enData, key);
string decryptedText = Encoding.UTF8.GetString(decryptedByteArray);
Debug.WriteLine(decryptedText);

Feedback and documentation

For documentation, providing feedback, issues and finding out how to contribute please refer to the Home repo.

Join our Discord community here.

Credits

The list of contributors to this project can be found at CONTRIBUTORS.

License

The nanoFramework Class Libraries are licensed under the MIT license.

Code of Conduct

This project has adopted the code of conduct defined by the Contributor Covenant to clarify expected behaviour in our community. For more information see the .NET Foundation Code of Conduct.

.NET Foundation

This project is supported by the .NET Foundation.

About

📦 .NET nanoFramework System.Security.Cryptography

https://www.nanoframework.net

License:MIT License


Languages

Language:C# 100.0%