Coder Social home page Coder Social logo

bytedev / bytedev.crypto Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 0.0 1.75 MB

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

License: MIT License

C# 94.76% PowerShell 5.24%
csharp crypto cryptography hashing encrypting random

bytedev.crypto's Introduction

Build status NuGet Package

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.

CryptoRandomString 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 CryptoRandomString(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 CryptoRandomString(CharacterSets.Digits))
{
    char[] randomChars = r.GenerateArray(length);
}

CryptoRandomNumber class methods:

  • GenerateInt32

GenerateInt32

Generate a random number.

using (var r = new CryptoRandomNumber())
{
    int randomInt = r.GenerateInt32();
}

bytedev.crypto's People

Contributors

bytedev avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.