ByteDev.Crypto 7.0.0

Provides simple cryptographic related classes for hashing/verifying data, encrypting/decrypting data and creating random data in .NET.

Install-Package ByteDev.Crypto -Version 7.0.0
dotnet add package ByteDev.Crypto --version 7.0.0
<PackageReference Include="ByteDev.Crypto" Version="7.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ByteDev.Crypto --version 7.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: ByteDev.Crypto, 7.0.0"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install ByteDev.Crypto as a Cake Addin
#addin nuget:?package=ByteDev.Crypto&version=7.0.0

// Install ByteDev.Crypto as a Cake Tool
#tool nuget:?package=ByteDev.Crypto&version=7.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

ByteDev.Crypto

Provides simple cryptographic related classes for hashing/verifying data, encrypting/decrypting data and creating crypto random data in .NET.

Installation

ByteDev.Crypto has been written as a .NET Standard 2.0 library, so you can consume it from a .NET Core or .NET Framework 4.6.1 (or greater) application.

ByteDev.Crypto is hosted as a package on nuget.org. To install from the Package Manager Console in Visual Studio run:

Install-Package ByteDev.Crypto

Further details can be found on the nuget page.

Release Notes

Releases follow semantic versioning.

Full details of the release notes can be viewed on GitHub.

Usage

The library is broken into three main namespaces:

  • Hashing
  • Encryption
  • Random

Hashing

Use namespace ByteDev.Crypto.Hashing.

This namespace contains two main classes: HashService and FileChecksumService.

HashService

HashService class methods:

  • Hash
  • Verify
IHashService service = new HashService(new Md5Algorithm(), EncodingType.Base64);

// Hash a phrase
string base64Hash = service.Hash(new ClearPhrase("Password1"));

// Verify a phrase against a hash
bool isSuccessful = service.Verify(new ClearPhrase("Pasword123456"), base64Hash);
FileChecksumService

FileChecksumService class methods:

  • Calculate
  • Verify
  • Matches
IFileChecksumService service = new FileChecksumService(new Md5Algorithm(), EncodingType.Hex);

// Calculate file checksum (hash)
string hexChecksum = service.Calculate(@"C:\myFile.txt");

// Verify existing checksum matches file's
bool isSuccessful = service.Verify(@"C:\myFile.txt", hexChecksum);

// Search directory for files with a particular checksum
IList<string> matches = service.Matches(@"C:\Temp", hexChecksum); 

Encryption

Use namespace ByteDev.Crypto.Encryption.

EncryptionService class methods:

  • Encrypt
  • Decrypt
  • EncryptProperties
  • DecryptProperties

Initializing EncryptionService:

IEncryptionAlgorithm algo = new RijndaelAlgorithm();

IEncryptionKeyIvFactory keyFactory = new EncryptionKeyIvFactory(algo);
EncryptionKeyIv keyIv = keyFactory.Create("Password1", Encoding.UTF8.GetBytes("someSalt"));

IEncryptionService service = new EncryptionService(algo, keyIv);
Encrypt & Decrypt

Encrypt a secret with a key and then decrypt it:

byte[] cipher = service.Encrypt("mySecret");

string clearText = service.Decrypt(cipher);	 

// clearText == "mySecret"

Once a byte array cipher has been created the ByteDev.Encoding.Encoder class can be used for any required encoding:

byte[] cipher = service.Encrypt("mySecret", keyIv);

Encoder encoder = new Encoder(EncodingType.Hex);

string hex = encoder.Encode(cipher);
EncryptProperties & DecryptProperties

The EncryptionService class also supports encrypting/decrypting an object's public string properties that use EncryptAttribute through the EncryptProperties and DecryptProperties methods.

public class MyInfo
{
    [Encrypt]
    public string Secrets { get; set; }
}

var info = new MyInfo { Secrets = "Some secrets" };

service.EncryptProperties(info, EncodingType.Hex);

// info.Secrets is now encrypted and encoded as hex

service.DecryptProperties(info, EncodingType.Hex);

// info.Secrets == "Some secrets"

Random

Use namespace ByteDev.Crypto.Random.

CryptoRandom class methods:

  • GenerateString
  • GenerateArray
GenerateString

Generate a random string of a specified length using only the character set specified (you can also call GenerateString with a min and max length):

const int length = 5;

using (var r = new CryptoRandom(CharacterSets.AlphaNumeric))
{
    string randomString = r.GenerateString(length);
}
GenerateArray

Generate a char array of random characters of a specified length using only the character set specified (you can also call GenerateArray with a min and max length):

const int length = 10;

using (var r = new CryptoRandom(CharacterSets.Digits))
{
    char[] randomChars = r.GenerateArray(length);
}

ByteDev.Crypto

Provides simple cryptographic related classes for hashing/verifying data, encrypting/decrypting data and creating crypto random data in .NET.

Installation

ByteDev.Crypto has been written as a .NET Standard 2.0 library, so you can consume it from a .NET Core or .NET Framework 4.6.1 (or greater) application.

ByteDev.Crypto is hosted as a package on nuget.org. To install from the Package Manager Console in Visual Studio run:

Install-Package ByteDev.Crypto

Further details can be found on the nuget page.

Release Notes

Releases follow semantic versioning.

Full details of the release notes can be viewed on GitHub.

Usage

The library is broken into three main namespaces:

  • Hashing
  • Encryption
  • Random

Hashing

Use namespace ByteDev.Crypto.Hashing.

This namespace contains two main classes: HashService and FileChecksumService.

HashService

HashService class methods:

  • Hash
  • Verify
IHashService service = new HashService(new Md5Algorithm(), EncodingType.Base64);

// Hash a phrase
string base64Hash = service.Hash(new ClearPhrase("Password1"));

// Verify a phrase against a hash
bool isSuccessful = service.Verify(new ClearPhrase("Pasword123456"), base64Hash);
FileChecksumService

FileChecksumService class methods:

  • Calculate
  • Verify
  • Matches
IFileChecksumService service = new FileChecksumService(new Md5Algorithm(), EncodingType.Hex);

// Calculate file checksum (hash)
string hexChecksum = service.Calculate(@"C:\myFile.txt");

// Verify existing checksum matches file's
bool isSuccessful = service.Verify(@"C:\myFile.txt", hexChecksum);

// Search directory for files with a particular checksum
IList<string> matches = service.Matches(@"C:\Temp", hexChecksum); 

Encryption

Use namespace ByteDev.Crypto.Encryption.

EncryptionService class methods:

  • Encrypt
  • Decrypt
  • EncryptProperties
  • DecryptProperties

Initializing EncryptionService:

IEncryptionAlgorithm algo = new RijndaelAlgorithm();

IEncryptionKeyIvFactory keyFactory = new EncryptionKeyIvFactory(algo);
EncryptionKeyIv keyIv = keyFactory.Create("Password1", Encoding.UTF8.GetBytes("someSalt"));

IEncryptionService service = new EncryptionService(algo, keyIv);
Encrypt & Decrypt

Encrypt a secret with a key and then decrypt it:

byte[] cipher = service.Encrypt("mySecret");

string clearText = service.Decrypt(cipher);	 

// clearText == "mySecret"

Once a byte array cipher has been created the ByteDev.Encoding.Encoder class can be used for any required encoding:

byte[] cipher = service.Encrypt("mySecret", keyIv);

Encoder encoder = new Encoder(EncodingType.Hex);

string hex = encoder.Encode(cipher);
EncryptProperties & DecryptProperties

The EncryptionService class also supports encrypting/decrypting an object's public string properties that use EncryptAttribute through the EncryptProperties and DecryptProperties methods.

public class MyInfo
{
    [Encrypt]
    public string Secrets { get; set; }
}

var info = new MyInfo { Secrets = "Some secrets" };

service.EncryptProperties(info, EncodingType.Hex);

// info.Secrets is now encrypted and encoded as hex

service.DecryptProperties(info, EncodingType.Hex);

// info.Secrets == "Some secrets"

Random

Use namespace ByteDev.Crypto.Random.

CryptoRandom class methods:

  • GenerateString
  • GenerateArray
GenerateString

Generate a random string of a specified length using only the character set specified (you can also call GenerateString with a min and max length):

const int length = 5;

using (var r = new CryptoRandom(CharacterSets.AlphaNumeric))
{
    string randomString = r.GenerateString(length);
}
GenerateArray

Generate a char array of random characters of a specified length using only the character set specified (you can also call GenerateArray with a min and max length):

const int length = 10;

using (var r = new CryptoRandom(CharacterSets.Digits))
{
    char[] randomChars = r.GenerateArray(length);
}

NuGet packages (1)

Showing the top 1 NuGet packages that depend on ByteDev.Crypto:

Package Downloads
ByteDev.Testing.NUnit
.NET Standard library that provides some extra NUnit functionality.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
7.0.0 101 5/31/2021
6.1.0 99 5/24/2021
6.0.0 120 1/20/2021
5.1.1 107 1/18/2021
5.1.0 124 12/18/2020
5.0.2 420 7/2/2020
5.0.1 236 7/2/2020
5.0.0 203 6/8/2020
4.0.0 257 4/29/2020
3.3.0 194 4/25/2020
3.2.0 291 3/28/2020
3.1.0 256 3/25/2020
3.0.3 228 2/28/2020
3.0.1 241 11/8/2019
3.0.0 306 4/10/2019
2.1.0 297 3/7/2019
2.0.0 356 2/12/2019
1.2.0 373 12/17/2018
1.1.0 436 9/24/2018
1.0.0 450 8/17/2018