ByteDev.Crypto
5.0.0
See the version list below for details.
dotnet add package ByteDev.Crypto --version 5.0.0
NuGet\Install-Package ByteDev.Crypto -Version 5.0.0
<PackageReference Include="ByteDev.Crypto" Version="5.0.0" />
paket add ByteDev.Crypto --version 5.0.0
#r "nuget: ByteDev.Crypto, 5.0.0"
// Install ByteDev.Crypto as a Cake Addin
#addin nuget:?package=ByteDev.Crypto&version=5.0.0
// Install ByteDev.Crypto as a Cake Tool
#tool nuget:?package=ByteDev.Crypto&version=5.0.0
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.
Code
The repo can be cloned from git bash:
git clone https://github.com/ByteDev/ByteDev.Crypto
Usage
The library's main classes:
- HashingService
- EncryptionService
- CryptoRandom
Hashing
Use namespace ByteDev.Crypto.Hashing
.
HashService class methods:
- Hash
- Verify
- CalcFileChecksum
Hash some clear text (returned as base 64 encoded string) and verify a guess is equal:
var service = new HashService(new Md5Algorithm(), EncodingType.Base64);
string hash = service.Hash(new ClearPhrase("Password1"));
bool isLoginSuccessful = service.Verify(new ClearPhrase("passwordGuess"), hash);
Calculate a checksum for a file (returned as hex encoded string):
var service = new HashService(new Md5Algorithm(), EncodingType.Hex);
string checksum = service.CalcFileChecksum(@"C:\somefile.txt");
Encryption
Use namespace ByteDev.Crypto.Encryption
.
EncryptionService class methods:
- Encrypt
- Decrypt
- EncryptProperties
- DecryptProperties
Create the EncryptionService
class:
var algo = new RijndaelAlgorithm();
var keyFactory = new EncryptionKeyIvFactory(algo);
var keyIv = keyFactory.Create("Password1", Encoding.UTF8.GetBytes("someSalt"));
var service = new EncryptionService(algo, keyIv);
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.Crypto.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);
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" };
services.EncryptProperties(info, EncodingType.Hex);
// info.Secrets is now encrypted and encoded as hex
services.DecryptProperties(info, EncodingType.Hex);
// info.Secrets == "Some secrets"
Random
Use namespace ByteDev.Crypto.Random
.
CryptoRandom class methods:
- GenerateString
- GenerateArray
Generate a random string of a specified length using only the character set specified:
const int length = 5;
using (var r = new CryptoRandom(CharacterSets.AlphaNumeric))
{
string randomString = r.GenerateString(length);
}
You can also call GenerateString
with a min and max length.
Product | Versions |
---|---|
.NET | net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows net7.0 net7.0-android net7.0-ios net7.0-maccatalyst net7.0-macos net7.0-tvos net7.0-windows |
.NET Core | netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1 |
.NET Standard | netstandard2.0 netstandard2.1 |
.NET Framework | net461 net462 net463 net47 net471 net472 net48 net481 |
MonoAndroid | monoandroid |
MonoMac | monomac |
MonoTouch | monotouch |
Tizen | tizen40 tizen60 |
Xamarin.iOS | xamarinios |
Xamarin.Mac | xamarinmac |
Xamarin.TVOS | xamarintvos |
Xamarin.WatchOS | xamarinwatchos |
-
.NETStandard 2.0
- No dependencies.
NuGet packages (2)
Showing the top 2 NuGet packages that depend on ByteDev.Crypto:
Package | Downloads |
---|---|
ByteDev.Testing.NUnit
.NET Standard library that provides some extra NUnit functionality. |
|
ByteDev.AirVpn.IpLeak
.NET Standard library to help communicate with the IP Leak API. |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
8.0.0 | 5,466 | 10/14/2021 |
7.0.0 | 416 | 5/31/2021 |
6.1.0 | 280 | 5/24/2021 |
6.0.0 | 306 | 1/20/2021 |
5.1.1 | 308 | 1/18/2021 |
5.1.0 | 356 | 12/18/2020 |
5.0.2 | 896 | 7/2/2020 |
5.0.1 | 427 | 7/2/2020 |
5.0.0 | 388 | 6/8/2020 |
4.0.0 | 448 | 4/29/2020 |
3.3.0 | 372 | 4/25/2020 |
3.2.0 | 491 | 3/28/2020 |
3.1.0 | 438 | 3/25/2020 |
3.0.3 | 405 | 2/28/2020 |
3.0.1 | 438 | 11/8/2019 |
3.0.0 | 489 | 4/10/2019 |
2.1.0 | 499 | 3/7/2019 |
2.0.0 | 567 | 2/12/2019 |
1.2.0 | 572 | 12/17/2018 |
1.1.0 | 666 | 9/24/2018 |
1.0.0 | 728 | 8/17/2018 |