├── src
└── CryptHash.Net
│ ├── lib
│ ├── CryptHash.Net
│ │ ├── Icon
│ │ │ ├── crypthash-icon.png
│ │ │ └── crypthash-icon-new.png
│ │ ├── Encryption
│ │ │ ├── AES
│ │ │ │ ├── Enums
│ │ │ │ │ ├── EncodingType.cs
│ │ │ │ │ └── AesCipherMode.cs
│ │ │ │ └── EncryptionResults
│ │ │ │ │ ├── AesDecryptionResult.cs
│ │ │ │ │ └── AesEncryptionResult.cs
│ │ │ └── RSA
│ │ │ │ ├── EncryptionResults
│ │ │ │ ├── RSADecryptionResult.cs
│ │ │ │ └── RSAEncryptionResult.cs
│ │ │ │ └── Base
│ │ │ │ └── RSABase.cs
│ │ ├── Hash
│ │ │ ├── Enums
│ │ │ │ ├── HashAlgorithm.cs
│ │ │ │ └── HMACAlgorithm.cs
│ │ │ ├── HashResults
│ │ │ │ ├── HMACHashResult.cs
│ │ │ │ ├── GenericHashResult.cs
│ │ │ │ ├── PBKDF2HashResult.cs
│ │ │ │ └── Argon2idHashResult.cs
│ │ │ ├── PBKDF2_HMAC_SHA_256.cs
│ │ │ ├── Argon2id.cs
│ │ │ ├── PBKDF2_HMAC_SHA_1.cs
│ │ │ ├── MD5.cs
│ │ │ ├── SHA1.cs
│ │ │ ├── SHA256.cs
│ │ │ ├── SHA384.cs
│ │ │ ├── SHA512.cs
│ │ │ ├── Base
│ │ │ │ └── PBKDF2Base.cs
│ │ │ ├── HMAC_MD5.cs
│ │ │ ├── HMAC_SHA_1.cs
│ │ │ ├── BCrypt.cs
│ │ │ ├── HMAC_SHA_256.cs
│ │ │ ├── HMAC_SHA_512.cs
│ │ │ └── HMAC_SHA_384.cs
│ │ ├── Util
│ │ │ ├── EventHandlers.cs
│ │ │ ├── HMACOutputLengthDictionary.cs
│ │ │ └── MessageDictionary.cs
│ │ ├── Encoding
│ │ │ ├── Base64.cs
│ │ │ ├── Hexadecimal.cs
│ │ │ └── HighPerformanceHexadecimal.cs
│ │ └── CryptHash.Net.csproj
│ └── CryptHash.Net.Tests
│ │ ├── Encoding
│ │ └── Hexadecimal_Tests.cs
│ │ ├── CryptHash.Net.Tests.csproj
│ │ ├── Hash
│ │ ├── BCrypt_Tests.cs
│ │ ├── HMAC_MD5_Tests.cs
│ │ ├── HMAC_SHA_1_Tests.cs
│ │ ├── HMAC_SHA_256_Tests.cs
│ │ ├── HMAC_SHA_384_Tests.cs
│ │ ├── HMAC_SHA_512_Tests.cs
│ │ ├── PBKDF2_HMAC_SHA_1_Tests.cs
│ │ ├── MD5_Tests.cs
│ │ ├── SHA384_Tests.cs
│ │ ├── SHA512_Tests.cs
│ │ ├── SHA1_Tests.cs
│ │ ├── SHA256_Tests.cs
│ │ └── Argon2id_Tests.cs
│ │ └── Encryption
│ │ └── AES
│ │ └── AEAD
│ │ ├── AEAD_AES_128_GCM_Tests.cs
│ │ ├── AEAD_AES_192_GCM_Tests.cs
│ │ └── AEAD_AES_256_GCM_Tests.cs
│ ├── bin
│ └── crypthash
│ │ ├── ConsoleUtil
│ │ ├── ConsoleUtil.cs
│ │ └── ProgressBar.cs
│ │ ├── build.bat
│ │ ├── crypthash.csproj
│ │ └── CommandLineParserOptions
│ │ └── Options.cs
│ └── CryptHash.Net.sln
├── .github
└── workflows
│ ├── build-and-test.yml
│ └── build-test-and-deploy.yml
├── LICENSE.TXT
├── .gitattributes
├── img
├── icon-rider.svg
└── icon-resharper.svg
├── README.md
└── .gitignore
/src/CryptHash.Net/lib/CryptHash.Net/Icon/crypthash-icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/alecgn/crypthash-net/HEAD/src/CryptHash.Net/lib/CryptHash.Net/Icon/crypthash-icon.png
--------------------------------------------------------------------------------
/src/CryptHash.Net/lib/CryptHash.Net/Icon/crypthash-icon-new.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/alecgn/crypthash-net/HEAD/src/CryptHash.Net/lib/CryptHash.Net/Icon/crypthash-icon-new.png
--------------------------------------------------------------------------------
/src/CryptHash.Net/bin/crypthash/ConsoleUtil/ConsoleUtil.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Alessandro Cagliostro, 2021
3 | *
4 | * https://github.com/alecgn
5 | */
6 |
7 | namespace CryptHash.Net.CLI.ConsoleUtil
8 | {
9 | public enum ExitCode { Sucess = 0, Error = 1 }
10 | }
--------------------------------------------------------------------------------
/src/CryptHash.Net/lib/CryptHash.Net/Encryption/AES/Enums/EncodingType.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Alessandro Cagliostro, 2021
3 | *
4 | * https://github.com/alecgn
5 | */
6 |
7 | namespace CryptHash.Net.Encryption.AES.Enums
8 | {
9 | public enum EncodingType { Base64, Hexadecimal };
10 | }
11 |
--------------------------------------------------------------------------------
/src/CryptHash.Net/lib/CryptHash.Net/Hash/Enums/HashAlgorithm.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Alessandro Cagliostro, 2021
3 | *
4 | * https://github.com/alecgn
5 | */
6 |
7 | namespace CryptHash.Net.Hash.Enums
8 | {
9 | public enum HashAlgorithm { MD5, SHA1, SHA256, SHA384, SHA512, BCrypt }
10 | }
11 |
--------------------------------------------------------------------------------
/src/CryptHash.Net/lib/CryptHash.Net/Hash/Enums/HMACAlgorithm.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Alessandro Cagliostro, 2021
3 | *
4 | * https://github.com/alecgn
5 | */
6 |
7 | namespace CryptHash.Net.Hash.Enums
8 | {
9 | public enum HMACAlgorithm { HMACMD5, HMACSHA1, HMACSHA256, HMACSHA384, HMACSHA512 }
10 | }
11 |
--------------------------------------------------------------------------------
/src/CryptHash.Net/lib/CryptHash.Net/Encryption/AES/Enums/AesCipherMode.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Alessandro Cagliostro, 2021
3 | *
4 | * https://github.com/alecgn
5 | */
6 |
7 | using System.Security.Cryptography;
8 |
9 | namespace CryptHash.Net.Encryption.AES.Enums
10 | {
11 | public enum AesCipherMode { CBC = CipherMode.CBC, ECB = CipherMode.ECB, OFB = CipherMode.OFB, CFB = CipherMode.CFB, CTS = CipherMode.CTS, GCM };
12 | }
13 |
--------------------------------------------------------------------------------
/src/CryptHash.Net/lib/CryptHash.Net/Hash/HashResults/HMACHashResult.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Alessandro Cagliostro, 2021
3 | *
4 | * https://github.com/alecgn
5 | */
6 |
7 | using CryptHash.Net.Hash.Enums;
8 |
9 | namespace CryptHash.Net.Hash.HashResults
10 | {
11 | public class HMACHashResult : GenericHashResult
12 | {
13 | public byte[] Key { get; set; }
14 | public HMACAlgorithm PRF { get; set; }
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/src/CryptHash.Net/lib/CryptHash.Net/Hash/HashResults/GenericHashResult.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Alessandro Cagliostro, 2021
3 | *
4 | * https://github.com/alecgn
5 | */
6 |
7 | namespace CryptHash.Net.Hash.HashResults
8 | {
9 | public class GenericHashResult
10 | {
11 | public bool Success { get; set; }
12 | public string Message { get; set; }
13 | public string HashString { get; set; }
14 | public byte[] HashBytes { get; set; }
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/src/CryptHash.Net/lib/CryptHash.Net/Encryption/RSA/EncryptionResults/RSADecryptionResult.cs:
--------------------------------------------------------------------------------
1 | //using System.Security.Cryptography;
2 |
3 | //namespace CryptHash.Net.Encryption.RSA.EncryptionResults
4 | //{
5 | // public class RSADecryptionResult
6 | // {
7 | // public bool Success { get; set; }
8 | // public string Message { get; set; }
9 | // public RSAParameters RSAParameters { get; set; }
10 | // public byte[] DecryptedDataBytes { get; set; }
11 | // }
12 | //}
13 |
--------------------------------------------------------------------------------
/src/CryptHash.Net/lib/CryptHash.Net/Encryption/RSA/EncryptionResults/RSAEncryptionResult.cs:
--------------------------------------------------------------------------------
1 | //using System.Security.Cryptography;
2 |
3 | //namespace CryptHash.Net.Encryption.RSA.EncryptionResults
4 | //{
5 | // public class RSAEncryptionResult
6 | // {
7 | // public bool Success { get; set; }
8 | // public string Message { get; set; }
9 | // public RSAParameters RSAParameters { get; set; }
10 | // public byte[] EncryptedDataBytes { get; set; }
11 | // }
12 | //}
13 |
--------------------------------------------------------------------------------
/src/CryptHash.Net/lib/CryptHash.Net/Hash/HashResults/PBKDF2HashResult.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Alessandro Cagliostro, 2021
3 | *
4 | * https://github.com/alecgn
5 | */
6 |
7 | using CryptHash.Net.Hash.Enums;
8 |
9 | namespace CryptHash.Net.Hash.HashResults
10 | {
11 | public class PBKDF2HashResult : GenericHashResult
12 | {
13 | public byte[] Salt { get; set; }
14 | public HMACAlgorithm PRF { get; set; }
15 | public int Iterations { get; set; }
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/src/CryptHash.Net/lib/CryptHash.Net/Hash/HashResults/Argon2idHashResult.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Alessandro Cagliostro, 2021
3 | *
4 | * https://github.com/alecgn
5 | */
6 |
7 | namespace CryptHash.Net.Hash.HashResults
8 | {
9 | public class Argon2idHashResult : GenericHashResult
10 | {
11 | public byte[] SaltBytes { get; set; }
12 | public int Iterations { get; set; }
13 | public int DegreeOfParallelism { get; set; }
14 | public int KBMemorySize { get; set; }
15 | public byte[] AssociatedData { get; set; }
16 | public byte[] KnownSecret { get; set; }
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/.github/workflows/build-and-test.yml:
--------------------------------------------------------------------------------
1 | name: build-and-test
2 | on:
3 | push:
4 | branches:
5 | - develop
6 | jobs:
7 | build-and-test:
8 | runs-on: ubuntu-latest
9 | steps:
10 | - uses: actions/checkout@v2
11 |
12 | - name: Setup .NET Core
13 | uses: actions/setup-dotnet@v1
14 | with:
15 | dotnet-version: '3.1.x'
16 |
17 | - name: Build
18 | run: dotnet build --configuration Release src/CryptHash.Net/lib/CryptHash.Net/CryptHash.Net.csproj
19 |
20 | - name: Run tests
21 | run: dotnet test src/CryptHash.Net/lib/CryptHash.Net.Tests/CryptHash.Net.Tests.csproj
22 |
--------------------------------------------------------------------------------
/src/CryptHash.Net/lib/CryptHash.Net/Hash/PBKDF2_HMAC_SHA_256.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Alessandro Cagliostro, 2021
3 | *
4 | * https://github.com/alecgn
5 | */
6 |
7 | //using CryptHash.Net.Hash.HashResults;
8 |
9 | //namespace CryptHash.Net.Hash
10 | //{
11 | // public class PBKDF2_HMAC_SHA_256 : PBKDF2Base
12 | // {
13 | // public PBKDF2HashResult ComputeHash(string stringToComputeHash, byte[] salt = null, int iterationsForKeyDerivation = 0)
14 | // {
15 | // return base.ComputeHash(Enums.HMACAlgorithm.HMACSHA256, stringToComputeHash, salt, iterationsForKeyDerivation);
16 | // }
17 | // }
18 | //}
19 |
--------------------------------------------------------------------------------
/src/CryptHash.Net/lib/CryptHash.Net/Util/EventHandlers.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Alessandro Cagliostro, 2021
3 | *
4 | * https://github.com/alecgn
5 | */
6 |
7 | namespace CryptHash.Net.Util.EventHandlers
8 | {
9 | public delegate void OnEncryptionMessageHandler(string message);
10 |
11 | public delegate void OnDecryptionMessageHandler(string message);
12 |
13 | public delegate void OnEncryptionProgressHandler(int percentageDone, string message);
14 |
15 | public delegate void OnDecryptionProgressHandler(int percentageDone, string message);
16 |
17 | public delegate void OnHashProgressHandler(int percentageDone, string message);
18 | }
19 |
--------------------------------------------------------------------------------
/.github/workflows/build-test-and-deploy.yml:
--------------------------------------------------------------------------------
1 | name: build-test-and-deploy
2 | on:
3 | push:
4 | branches:
5 | - master
6 | jobs:
7 | build-test-and-deploy:
8 | runs-on: ubuntu-latest
9 | steps:
10 | - uses: actions/checkout@v2
11 |
12 | - name: Build
13 | run: dotnet build --configuration Release src/CryptHash.Net/lib/CryptHash.Net/CryptHash.Net.csproj
14 |
15 | - name: Run tests
16 | run: dotnet test src/CryptHash.Net/lib/CryptHash.Net.Tests/CryptHash.Net.Tests.csproj
17 |
18 | - name: Publish to NuGet
19 | uses: brandedoutcast/publish-nuget@v2
20 | with:
21 | PROJECT_FILE_PATH: src/CryptHash.Net/lib/CryptHash.Net/CryptHash.Net.csproj
22 | NUGET_KEY: ${{secrets.NUGET_TOKEN}}
--------------------------------------------------------------------------------
/src/CryptHash.Net/lib/CryptHash.Net.Tests/Encoding/Hexadecimal_Tests.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.VisualStudio.TestTools.UnitTesting;
2 |
3 | namespace CryptHash.Net.Tests.Encoding
4 | {
5 | [TestClass]
6 | public class Hexadecimal_Tests
7 | {
8 | [TestMethod]
9 | public void Managed_and_unmanaged_generated_hex_matches()
10 | {
11 | var randomBytes = CryptHash.Net.Util.CommonMethods.GenerateRandomBytes(32);
12 |
13 | var managedGeneratedHex = CryptHash.Net.Encoding.Hexadecimal.ToHexString(randomBytes);
14 | var unmanagedGeneratedHex = CryptHash.Net.Encoding.HighPerformanceHexadecimal.ToHexString(randomBytes);
15 |
16 | Assert.AreEqual(managedGeneratedHex, unmanagedGeneratedHex);
17 | }
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/src/CryptHash.Net/bin/crypthash/build.bat:
--------------------------------------------------------------------------------
1 | dotnet publish -c Release -r win-x64 /p:PublishSingleFile=true /p:PublishTrimmed=true -o D:\dotnet-publish\crypthash-net\win-x64
2 | dotnet publish -c Release -r win-x86 /p:PublishSingleFile=true /p:PublishTrimmed=true -o D:\dotnet-publish\crypthash-net\win-x86
3 | dotnet publish -c Release -r win-arm /p:PublishSingleFile=true /p:PublishTrimmed=true -o D:\dotnet-publish\crypthash-net\win-arm
4 | dotnet publish -c Release -r linux-x64 /p:PublishSingleFile=true /p:PublishTrimmed=true -o D:\dotnet-publish\crypthash-net\linux-x64
5 | dotnet publish -c Release -r linux-arm /p:PublishSingleFile=true /p:PublishTrimmed=true -o D:\dotnet-publish\crypthash-net\linux-arm
6 | dotnet publish -c Release -r osx-x64 /p:PublishSingleFile=true /p:PublishTrimmed=true -o D:\dotnet-publish\crypthash-net\osx-x64
7 |
--------------------------------------------------------------------------------
/src/CryptHash.Net/lib/CryptHash.Net.Tests/CryptHash.Net.Tests.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netcoreapp3.1
5 | false
6 | 3.6.1
7 | 3.6.1
8 | 3.6.1
9 |
10 |
11 |
12 | false
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
--------------------------------------------------------------------------------
/src/CryptHash.Net/lib/CryptHash.Net/Encryption/AES/EncryptionResults/AesDecryptionResult.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Alessandro Cagliostro, 2021
3 | *
4 | * https://github.com/alecgn
5 | */
6 |
7 | using CryptHash.Net.Encryption.AES.Enums;
8 | using System.Security.Cryptography;
9 |
10 | namespace CryptHash.Net.Encryption.AES.EncryptionResults
11 | {
12 | public class AesDecryptionResult
13 | {
14 | public bool Success { get; set; }
15 | public string Message { get; set; }
16 | public byte[] DecryptedDataBytes { get; set; }
17 | public string DecryptedDataString { get; set; }
18 | public byte[] Key { get; set; }
19 | public byte[] IV { get; set; }
20 | public byte[] Nonce { get; set; }
21 | public AesCipherMode AesCipherMode { get; set; }
22 | public PaddingMode PaddingMode { get; set; }
23 | public byte[] Salt { get; set; }
24 | public byte[] AuthenticationKey { get; set; }
25 | public byte[] Tag { get; set; }
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/src/CryptHash.Net/lib/CryptHash.Net/Encryption/AES/EncryptionResults/AesEncryptionResult.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Alessandro Cagliostro, 2021
3 | *
4 | * https://github.com/alecgn
5 | */
6 |
7 | using CryptHash.Net.Encryption.AES.Enums;
8 | using System.Security.Cryptography;
9 |
10 | namespace CryptHash.Net.Encryption.AES.EncryptionResults
11 | {
12 | public class AesEncryptionResult
13 | {
14 | public bool Success { get; set; }
15 | public string Message { get; set; }
16 | public byte[] EncryptedDataBytes { get; set; }
17 | public string EncryptedDataBase64String { get; set; }
18 | public byte[] Key { get; set; }
19 | public byte[] IV { get; set; }
20 | public byte[] Nonce { get; set; }
21 | public AesCipherMode AesCipherMode { get; set; }
22 | public PaddingMode PaddingMode { get; set; }
23 | public byte[] Salt { get; set; }
24 | public byte[] AuthenticationKey { get; set; }
25 | public byte[] Tag { get; set; }
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/LICENSE.TXT:
--------------------------------------------------------------------------------
1 | Copyright 2019 Alessandro Cagliostro Gonçalves Neves
2 |
3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4 |
5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6 |
7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
--------------------------------------------------------------------------------
/src/CryptHash.Net/lib/CryptHash.Net.Tests/Hash/BCrypt_Tests.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Alessandro Cagliostro, 2021
3 | *
4 | * https://github.com/alecgn
5 | */
6 |
7 | using CryptHash.Net.Hash.HashResults;
8 | using Microsoft.VisualStudio.TestTools.UnitTesting;
9 |
10 | namespace CryptHash.Net.Tests.Hash
11 | {
12 | [TestClass]
13 | public class BCrypt_Tests
14 | {
15 | private readonly CryptHash.Net.Hash.BCrypt _bcrypt = new CryptHash.Net.Hash.BCrypt();
16 | private readonly string _testString = "This is a test string!";
17 |
18 | [TestMethod]
19 | public void ComputeAndVerifyHash_String()
20 | {
21 | var verifyResult = new GenericHashResult();
22 | var errorMessage = "";
23 |
24 | var hashResult = _bcrypt.ComputeHash(_testString);
25 |
26 | if (hashResult.Success)
27 | {
28 | verifyResult = _bcrypt.VerifyHash(_testString, hashResult.HashString);
29 |
30 | if (!verifyResult.Success)
31 | {
32 | errorMessage = verifyResult.Message;
33 | }
34 | }
35 | else
36 | {
37 | errorMessage = hashResult.Message;
38 | }
39 |
40 | Assert.IsTrue((hashResult.Success && verifyResult.Success), errorMessage);
41 | }
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/src/CryptHash.Net/lib/CryptHash.Net.Tests/Hash/HMAC_MD5_Tests.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Alessandro Cagliostro, 2021
3 | *
4 | * https://github.com/alecgn
5 | */
6 |
7 | using CryptHash.Net.Hash;
8 | using CryptHash.Net.Hash.HashResults;
9 | using Microsoft.VisualStudio.TestTools.UnitTesting;
10 |
11 | namespace CryptHash.Net.Tests.Hash
12 | {
13 | [TestClass]
14 | public class HMAC_MD5_Tests
15 | {
16 | private readonly HMAC_MD5 _hmacMd5 = new HMAC_MD5();
17 | private readonly string _testString = "This is a test string!";
18 |
19 | [TestMethod]
20 | public void ComputeAndVerifyHMAC_String()
21 | {
22 | var verifyResult = new HMACHashResult();
23 | var errorMessage = "";
24 |
25 | var hmacResult = _hmacMd5.ComputeHMAC(_testString);
26 |
27 | if (hmacResult.Success)
28 | {
29 | verifyResult = _hmacMd5.VerifyHMAC(hmacResult.HashString, _testString, hmacResult.Key);
30 |
31 | if (!verifyResult.Success)
32 | {
33 | errorMessage = verifyResult.Message;
34 | }
35 | }
36 | else
37 | {
38 | errorMessage = hmacResult.Message;
39 | }
40 |
41 | Assert.IsTrue((hmacResult.Success && verifyResult.Success), errorMessage);
42 | }
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/src/CryptHash.Net/lib/CryptHash.Net.Tests/Hash/HMAC_SHA_1_Tests.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Alessandro Cagliostro, 2021
3 | *
4 | * https://github.com/alecgn
5 | */
6 |
7 | using CryptHash.Net.Hash;
8 | using CryptHash.Net.Hash.HashResults;
9 | using Microsoft.VisualStudio.TestTools.UnitTesting;
10 |
11 | namespace CryptHash.Net.Tests.Hash
12 | {
13 | [TestClass]
14 | public class HMAC_SHA_1_Tests
15 | {
16 | private readonly HMAC_SHA_1 _hmacSha1 = new HMAC_SHA_1();
17 | private readonly string _testString = "This is a test string!";
18 |
19 | [TestMethod]
20 | public void ComputeAndVerifyHMAC_String()
21 | {
22 | var verifyResult = new HMACHashResult();
23 | var errorMessage = "";
24 |
25 | var hmacResult = _hmacSha1.ComputeHMAC(_testString);
26 |
27 | if (hmacResult.Success)
28 | {
29 | verifyResult = _hmacSha1.VerifyHMAC(hmacResult.HashString, _testString, hmacResult.Key);
30 |
31 | if (!verifyResult.Success)
32 | {
33 | errorMessage = verifyResult.Message;
34 | }
35 | }
36 | else
37 | {
38 | errorMessage = hmacResult.Message;
39 | }
40 |
41 | Assert.IsTrue((hmacResult.Success && verifyResult.Success), errorMessage);
42 | }
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/src/CryptHash.Net/lib/CryptHash.Net.Tests/Hash/HMAC_SHA_256_Tests.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Alessandro Cagliostro, 2021
3 | *
4 | * https://github.com/alecgn
5 | */
6 |
7 | using CryptHash.Net.Hash;
8 | using CryptHash.Net.Hash.HashResults;
9 | using Microsoft.VisualStudio.TestTools.UnitTesting;
10 |
11 | namespace CryptHash.Net.Tests.Hash
12 | {
13 | [TestClass]
14 | public class HMAC_SHA_256_Tests
15 | {
16 | private readonly HMAC_SHA_256 _hmacSha256 = new HMAC_SHA_256();
17 | private readonly string _testString = "This is a test string!";
18 |
19 | [TestMethod]
20 | public void ComputeAndVerifyHMAC_String()
21 | {
22 | var verifyResult = new HMACHashResult();
23 | var errorMessage = "";
24 |
25 | var hmacResult = _hmacSha256.ComputeHMAC(_testString);
26 |
27 | if (hmacResult.Success)
28 | {
29 | verifyResult = _hmacSha256.VerifyHMAC(hmacResult.HashString, _testString, hmacResult.Key);
30 |
31 | if (!verifyResult.Success)
32 | {
33 | errorMessage = verifyResult.Message;
34 | }
35 | }
36 | else
37 | {
38 | errorMessage = hmacResult.Message;
39 | }
40 |
41 | Assert.IsTrue((hmacResult.Success && verifyResult.Success), errorMessage);
42 | }
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/src/CryptHash.Net/lib/CryptHash.Net.Tests/Hash/HMAC_SHA_384_Tests.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Alessandro Cagliostro, 2021
3 | *
4 | * https://github.com/alecgn
5 | */
6 |
7 | using CryptHash.Net.Hash;
8 | using CryptHash.Net.Hash.HashResults;
9 | using Microsoft.VisualStudio.TestTools.UnitTesting;
10 |
11 | namespace CryptHash.Net.Tests.Hash
12 | {
13 | [TestClass]
14 | public class HMAC_SHA_384_Tests
15 | {
16 | private readonly HMAC_SHA_384 _hmacSha384 = new HMAC_SHA_384();
17 | private readonly string _testString = "This is a test string!";
18 |
19 | [TestMethod]
20 | public void ComputeAndVerifyHMAC_String()
21 | {
22 | var verifyResult = new HMACHashResult();
23 | var errorMessage = "";
24 |
25 | var hmacResult = _hmacSha384.ComputeHMAC(_testString);
26 |
27 | if (hmacResult.Success)
28 | {
29 | verifyResult = _hmacSha384.VerifyHMAC(hmacResult.HashString, _testString, hmacResult.Key);
30 |
31 | if (!verifyResult.Success)
32 | {
33 | errorMessage = verifyResult.Message;
34 | }
35 | }
36 | else
37 | {
38 | errorMessage = hmacResult.Message;
39 | }
40 |
41 | Assert.IsTrue((hmacResult.Success && verifyResult.Success), errorMessage);
42 | }
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/src/CryptHash.Net/lib/CryptHash.Net.Tests/Hash/HMAC_SHA_512_Tests.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Alessandro Cagliostro, 2021
3 | *
4 | * https://github.com/alecgn
5 | */
6 |
7 | using CryptHash.Net.Hash;
8 | using CryptHash.Net.Hash.HashResults;
9 | using Microsoft.VisualStudio.TestTools.UnitTesting;
10 |
11 | namespace CryptHash.Net.Tests.Hash
12 | {
13 | [TestClass]
14 | public class HMAC_SHA_512_Tests
15 | {
16 | private readonly HMAC_SHA_512 _hmacSha512 = new HMAC_SHA_512();
17 | private readonly string _testString = "This is a test string!";
18 |
19 | [TestMethod]
20 | public void ComputeAndVerifyHMAC_String()
21 | {
22 | var verifyResult = new HMACHashResult();
23 | var errorMessage = "";
24 |
25 | var hmacResult = _hmacSha512.ComputeHMAC(_testString);
26 |
27 | if (hmacResult.Success)
28 | {
29 | verifyResult = _hmacSha512.VerifyHMAC(hmacResult.HashString, _testString, hmacResult.Key);
30 |
31 | if (!verifyResult.Success)
32 | {
33 | errorMessage = verifyResult.Message;
34 | }
35 | }
36 | else
37 | {
38 | errorMessage = hmacResult.Message;
39 | }
40 |
41 | Assert.IsTrue((hmacResult.Success && verifyResult.Success), errorMessage);
42 | }
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/src/CryptHash.Net/lib/CryptHash.Net.Tests/Hash/PBKDF2_HMAC_SHA_1_Tests.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Alessandro Cagliostro, 2021
3 | *
4 | * https://github.com/alecgn
5 | */
6 |
7 | using CryptHash.Net.Hash;
8 | using CryptHash.Net.Hash.HashResults;
9 | using Microsoft.VisualStudio.TestTools.UnitTesting;
10 |
11 | namespace CryptHash.Net.Tests.Hash
12 | {
13 | [TestClass]
14 | public class PBKDF2_HMAC_SHA_1_Tests
15 | {
16 | private readonly PBKDF2_HMAC_SHA_1 _pbkdf2HmacSha1 = new PBKDF2_HMAC_SHA_1();
17 | private readonly string _testString = "This is a test string!";
18 |
19 | [TestMethod]
20 | public void ComputeAndVerifyHash_String()
21 | {
22 | var verifyResult = new PBKDF2HashResult();
23 | var errorMessage = "";
24 |
25 | var hashResult = _pbkdf2HmacSha1.ComputeHash(_testString);
26 |
27 | if (hashResult.Success)
28 | {
29 | verifyResult = _pbkdf2HmacSha1.VerifyHash(_testString, hashResult.HashString);
30 |
31 | if (!verifyResult.Success)
32 | {
33 | errorMessage = verifyResult.Message;
34 | }
35 | }
36 | else
37 | {
38 | errorMessage = hashResult.Message;
39 | }
40 |
41 | Assert.IsTrue((hashResult.Success && verifyResult.Success), errorMessage);
42 | }
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/src/CryptHash.Net/lib/CryptHash.Net/Util/HMACOutputLengthDictionary.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Alessandro Cagliostro, 2021
3 | *
4 | * https://github.com/alecgn
5 | */
6 |
7 | using CryptHash.Net.Hash.Enums;
8 | using System;
9 | using System.Collections.Concurrent;
10 | using System.Collections.Generic;
11 |
12 | namespace CryptHash.Net.Util
13 | {
14 | public sealed class HMACOutputLengthDictionary
15 | {
16 | private static readonly Lazy _lazyHMACOutputLengthDictionary = new Lazy(() => new HMACOutputLengthDictionary());
17 |
18 | private readonly IDictionary _dicHMACAlgorithmOutputLengths = new ConcurrentDictionary()
19 | {
20 | [HMACAlgorithm.HMACMD5] = 128,
21 | [HMACAlgorithm.HMACSHA1] = 160,
22 | [HMACAlgorithm.HMACSHA256] = 256,
23 | [HMACAlgorithm.HMACSHA384] = 384,
24 | [HMACAlgorithm.HMACSHA512] = 512
25 | };
26 |
27 | public static HMACOutputLengthDictionary Instance => _lazyHMACOutputLengthDictionary.Value;
28 |
29 | private HMACOutputLengthDictionary() { }
30 |
31 | private int GetOutputLength(HMACAlgorithm key)
32 | {
33 |
34 | if (!_dicHMACAlgorithmOutputLengths.TryGetValue(key, out var outputBytesLength))
35 | {
36 | outputBytesLength = 0;
37 | }
38 |
39 | return outputBytesLength;
40 | }
41 |
42 | public int this[HMACAlgorithm key] => GetOutputLength(key);
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/src/CryptHash.Net/lib/CryptHash.Net/Encoding/Base64.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Alessandro Cagliostro, 2021
3 | *
4 | * https://github.com/alecgn
5 | */
6 |
7 | using System;
8 |
9 | namespace CryptHash.Net.Encoding
10 | {
11 | public static class Base64
12 | {
13 | public static string ToBase64String(string plainString)
14 | {
15 | if (string.IsNullOrWhiteSpace(plainString))
16 | {
17 | return null;
18 | }
19 |
20 | var plainStringBytes = System.Text.Encoding.UTF8.GetBytes(plainString);
21 |
22 | return ToBase64String(plainStringBytes);
23 | }
24 |
25 | public static string ToBase64String(byte[] byteArray)
26 | {
27 | if (byteArray == null || byteArray.Length <= 0)
28 | {
29 | return null;
30 | }
31 |
32 | return Convert.ToBase64String(byteArray);
33 | }
34 |
35 | public static string ToString(string base64String)
36 | {
37 | if (string.IsNullOrWhiteSpace(base64String))
38 | {
39 | return null;
40 | }
41 |
42 | var byteArray = Convert.FromBase64String(base64String);
43 |
44 | return System.Text.Encoding.UTF8.GetString(byteArray);
45 | }
46 |
47 | public static byte[] ToByteArray(string base64String)
48 | {
49 | if (string.IsNullOrWhiteSpace(base64String))
50 | {
51 | return null;
52 | }
53 |
54 | return Convert.FromBase64String(base64String);
55 | }
56 | }
57 | }
58 |
--------------------------------------------------------------------------------
/src/CryptHash.Net/lib/CryptHash.Net.Tests/Hash/MD5_Tests.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Alessandro Cagliostro, 2021
3 | *
4 | * https://github.com/alecgn
5 | */
6 |
7 | using CryptHash.Net.Hash.Hash;
8 | using CryptHash.Net.Hash.HashResults;
9 | using Microsoft.VisualStudio.TestTools.UnitTesting;
10 | using System.IO;
11 |
12 | namespace CryptHash.Net.Tests.Hash
13 | {
14 | [TestClass]
15 | public class MD5_Tests
16 | {
17 | private readonly MD5 _md5 = new MD5();
18 | private readonly string _testString = "This is a test string!";
19 |
20 | [TestMethod]
21 | public void ComputeAndVerifyHash_String()
22 | {
23 | var verifyResult = new GenericHashResult();
24 | var errorMessage = "";
25 |
26 | var hashResult = _md5.ComputeHash(_testString);
27 |
28 | if (hashResult.Success)
29 | {
30 | verifyResult = _md5.VerifyHash(hashResult.HashString, _testString);
31 |
32 | if (!verifyResult.Success)
33 | {
34 | errorMessage = verifyResult.Message;
35 | }
36 | }
37 | else
38 | {
39 | errorMessage = hashResult.Message;
40 | }
41 |
42 | Assert.IsTrue((hashResult.Success && verifyResult.Success), errorMessage);
43 | }
44 |
45 | [TestMethod]
46 | public void ComputeAndVerifyHash_File()
47 | {
48 | var testFilePath = Path.GetTempFileName();
49 | var verifyResult = new GenericHashResult();
50 | var errorMessage = "";
51 |
52 | File.WriteAllText(testFilePath, _testString);
53 |
54 | var hashResult = _md5.ComputeFileHash(testFilePath);
55 |
56 | if (hashResult.Success)
57 | {
58 | verifyResult = _md5.VerifyFileHash(hashResult.HashString, testFilePath);
59 |
60 | if (!verifyResult.Success)
61 | {
62 | errorMessage = verifyResult.Message;
63 | }
64 | }
65 | else
66 | {
67 | errorMessage = hashResult.Message;
68 | }
69 |
70 | Assert.IsTrue((hashResult.Success && verifyResult.Success), errorMessage);
71 | }
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/src/CryptHash.Net/lib/CryptHash.Net.Tests/Hash/SHA384_Tests.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Alessandro Cagliostro, 2021
3 | *
4 | * https://github.com/alecgn
5 | */
6 |
7 | using CryptHash.Net.Hash.Hash;
8 | using CryptHash.Net.Hash.HashResults;
9 | using Microsoft.VisualStudio.TestTools.UnitTesting;
10 | using System.IO;
11 |
12 | namespace CryptHash.Net.Tests.Hash
13 | {
14 | [TestClass]
15 | public class SHA384_Tests
16 | {
17 | private readonly SHA384 _sha384 = new SHA384();
18 | private readonly string _testString = "This is a test string!";
19 |
20 | [TestMethod]
21 | public void ComputeAndVerifyHash_String()
22 | {
23 | var verifyResult = new GenericHashResult();
24 | var errorMessage = "";
25 |
26 | var hashResult = _sha384.ComputeHash(_testString);
27 |
28 | if (hashResult.Success)
29 | {
30 | verifyResult = _sha384.VerifyHash(hashResult.HashString, _testString);
31 |
32 | if (!verifyResult.Success)
33 | {
34 | errorMessage = verifyResult.Message;
35 | }
36 | }
37 | else
38 | {
39 | errorMessage = hashResult.Message;
40 | }
41 |
42 | Assert.IsTrue((hashResult.Success && verifyResult.Success), errorMessage);
43 | }
44 |
45 | public void ComputeAndVerifyHash_File()
46 | {
47 | var testFilePath = Path.GetTempFileName();
48 | var verifyResult = new GenericHashResult();
49 | var errorMessage = "";
50 |
51 | File.WriteAllText(testFilePath, _testString);
52 |
53 | var hashResult = _sha384.ComputeFileHash(testFilePath);
54 |
55 | if (hashResult.Success)
56 | {
57 | verifyResult = _sha384.VerifyFileHash(hashResult.HashString, testFilePath);
58 |
59 | if (!verifyResult.Success)
60 | {
61 | errorMessage = verifyResult.Message;
62 | }
63 | }
64 | else
65 | {
66 | errorMessage = hashResult.Message;
67 | }
68 |
69 | Assert.IsTrue((hashResult.Success && verifyResult.Success), errorMessage);
70 | }
71 | }
72 | }
73 |
--------------------------------------------------------------------------------
/src/CryptHash.Net/lib/CryptHash.Net.Tests/Hash/SHA512_Tests.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Alessandro Cagliostro, 2021
3 | *
4 | * https://github.com/alecgn
5 | */
6 |
7 | using CryptHash.Net.Hash.Hash;
8 | using CryptHash.Net.Hash.HashResults;
9 | using Microsoft.VisualStudio.TestTools.UnitTesting;
10 | using System.IO;
11 |
12 | namespace CryptHash.Net.Tests.Hash
13 | {
14 | [TestClass]
15 | public class SHA512_Tests
16 | {
17 | private readonly SHA512 _sha512 = new SHA512();
18 | private readonly string _testString = "This is a test string!";
19 |
20 | [TestMethod]
21 | public void ComputeAndVerifyHash_String()
22 | {
23 | var verifyResult = new GenericHashResult();
24 | var errorMessage = "";
25 |
26 | var hashResult = _sha512.ComputeHash(_testString);
27 |
28 | if (hashResult.Success)
29 | {
30 | verifyResult = _sha512.VerifyHash(hashResult.HashString, _testString);
31 |
32 | if (!verifyResult.Success)
33 | {
34 | errorMessage = verifyResult.Message;
35 | }
36 | }
37 | else
38 | {
39 | errorMessage = hashResult.Message;
40 | }
41 |
42 | Assert.IsTrue((hashResult.Success && verifyResult.Success), errorMessage);
43 | }
44 |
45 | public void ComputeAndVerifyHash_File()
46 | {
47 | var testFilePath = Path.GetTempFileName();
48 | var verifyResult = new GenericHashResult();
49 | var errorMessage = "";
50 |
51 | File.WriteAllText(testFilePath, _testString);
52 |
53 | var hashResult = _sha512.ComputeFileHash(testFilePath);
54 |
55 | if (hashResult.Success)
56 | {
57 | verifyResult = _sha512.VerifyFileHash(hashResult.HashString, testFilePath);
58 |
59 | if (!verifyResult.Success)
60 | {
61 | errorMessage = verifyResult.Message;
62 | }
63 | }
64 | else
65 | {
66 | errorMessage = hashResult.Message;
67 | }
68 |
69 | Assert.IsTrue((hashResult.Success && verifyResult.Success), errorMessage);
70 | }
71 | }
72 | }
73 |
--------------------------------------------------------------------------------
/src/CryptHash.Net/lib/CryptHash.Net.Tests/Hash/SHA1_Tests.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Alessandro Cagliostro, 2021
3 | *
4 | * https://github.com/alecgn
5 | */
6 |
7 | using CryptHash.Net.Hash.Hash;
8 | using CryptHash.Net.Hash.HashResults;
9 | using Microsoft.VisualStudio.TestTools.UnitTesting;
10 | using System.IO;
11 |
12 | namespace CryptHash.Net.Tests.Hash
13 | {
14 | [TestClass]
15 | public class SHA1_Tests
16 | {
17 | private readonly SHA1 _sha1 = new SHA1();
18 | private readonly string _testString = "This is a test string!";
19 |
20 | [TestMethod]
21 | public void ComputeAndVerifyHash_String()
22 | {
23 | var verifyResult = new GenericHashResult();
24 | var errorMessage = "";
25 |
26 | var hashResult = _sha1.ComputeHash(_testString);
27 |
28 | if (hashResult.Success)
29 | {
30 | verifyResult = _sha1.VerifyHash(hashResult.HashString, _testString);
31 |
32 | if (!verifyResult.Success)
33 | {
34 | errorMessage = verifyResult.Message;
35 | }
36 | }
37 | else
38 | {
39 | errorMessage = hashResult.Message;
40 | }
41 |
42 | Assert.IsTrue((hashResult.Success && verifyResult.Success), errorMessage);
43 | }
44 |
45 | [TestMethod]
46 | public void ComputeAndVerifyHash_File()
47 | {
48 | var testFilePath = Path.GetTempFileName();
49 | var verifyResult = new GenericHashResult();
50 | var errorMessage = "";
51 |
52 | File.WriteAllText(testFilePath, _testString);
53 |
54 | var hashResult = _sha1.ComputeFileHash(testFilePath);
55 |
56 | if (hashResult.Success)
57 | {
58 | verifyResult = _sha1.VerifyFileHash(hashResult.HashString, testFilePath);
59 |
60 | if (!verifyResult.Success)
61 | {
62 | errorMessage = verifyResult.Message;
63 | }
64 | }
65 | else
66 | {
67 | errorMessage = hashResult.Message;
68 | }
69 |
70 | Assert.IsTrue((hashResult.Success && verifyResult.Success), errorMessage);
71 | }
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/src/CryptHash.Net/lib/CryptHash.Net.Tests/Hash/SHA256_Tests.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Alessandro Cagliostro, 2021
3 | *
4 | * https://github.com/alecgn
5 | */
6 |
7 | using CryptHash.Net.Hash.Hash;
8 | using CryptHash.Net.Hash.HashResults;
9 | using Microsoft.VisualStudio.TestTools.UnitTesting;
10 | using System.IO;
11 |
12 | namespace CryptHash.Net.Tests.Hash
13 | {
14 | [TestClass]
15 | public class SHA256_Tests
16 | {
17 | private readonly SHA256 _sha256 = new SHA256();
18 | private readonly string _testString = "This is a test string!";
19 |
20 | [TestMethod]
21 | public void ComputeAndVerifyHash_String()
22 | {
23 | var verifyResult = new GenericHashResult();
24 | var errorMessage = "";
25 |
26 | var hashResult = _sha256.ComputeHash(_testString);
27 |
28 | if (hashResult.Success)
29 | {
30 | verifyResult = _sha256.VerifyHash(hashResult.HashString, _testString);
31 |
32 | if (!verifyResult.Success)
33 | {
34 | errorMessage = verifyResult.Message;
35 | }
36 | }
37 | else
38 | {
39 | errorMessage = hashResult.Message;
40 | }
41 |
42 | Assert.IsTrue((hashResult.Success && verifyResult.Success), errorMessage);
43 | }
44 |
45 | [TestMethod]
46 | public void ComputeAndVerifyHash_File()
47 | {
48 | var testFilePath = Path.GetTempFileName();
49 | var verifyResult = new GenericHashResult();
50 | var errorMessage = "";
51 |
52 | File.WriteAllText(testFilePath, _testString);
53 |
54 | var hashResult = _sha256.ComputeFileHash(testFilePath);
55 |
56 | if (hashResult.Success)
57 | {
58 | verifyResult = _sha256.VerifyFileHash(hashResult.HashString, testFilePath);
59 |
60 | if (!verifyResult.Success)
61 | {
62 | errorMessage = verifyResult.Message;
63 | }
64 | }
65 | else
66 | {
67 | errorMessage = hashResult.Message;
68 | }
69 |
70 | Assert.IsTrue((hashResult.Success && verifyResult.Success), errorMessage);
71 | }
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/src/CryptHash.Net/bin/crypthash/crypthash.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp3.1
6 | 3.6.1
7 | 3.6.1
8 | 3.6.1
9 | Alessandro Cagliostro Goncalves Neves
10 | crypthash is a .NET Core 3.1 multiplatform console utility (designed to run in Windows, Linux and Mac) for text and files symmetric (AES_128_CBC / AES_192_CBC / AES_256_CBC / AES_128_GCM / AES_192_GCM / AES_256_GCM) authenticated encryption/decryption, text/files hashing (MD5, SHA1, SHA256, SHA384, SHA512, HMAC-MD5, HMAC-SHA1, HMAC-SHA256, HMAC-SHA384, HMAC-SHA512, PBKDF2, BCrypt and Argon2id) and text encoding/decoding (Base64 and Hexadecimal).
11 | Alessandro Cagliostro Goncalves Neves, 2021
12 | https://github.com/alecgn/crypthash-net
13 | https://raw.githubusercontent.com/alecgn/crypthash-net/master/src/CryptHash.Net/lib/CryptHash.Net/Icon/crypthash-icon.png
14 | https://github.com/alecgn/crypthash-net
15 | encryption decryption cryptography aes aes128 aes192 aes256 aes128cbc aes192cbc aes256cbc aes128gcm aes192gcm aes256gcm aescbc aesgcm aescbc128 aescbc192 aescbc256 aesgcm128 aesgcm192 aesgcm256 cbc gcm hash md5 sha1 sha256 sha384 sha512 hmacmd5 hmac-md5 hmacsha1 hmac-sha1 hmacsha256 hmac-sha256 hmacsha384 hmac-sha384 hmacsha512 hmac-sha512 pbkdf2 bcrypt argon2 argon2id c# c-sharp security password authentication authenticated base64 hex hexadecimal encode decode
16 | MIT
17 | - Updated depencies.
18 | - Changed internal string messages retrieve scheme.
19 |
20 |
21 |
22 | false
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
--------------------------------------------------------------------------------
/src/CryptHash.Net/lib/CryptHash.Net/Encoding/Hexadecimal.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Alessandro Cagliostro, 2021
3 | *
4 | * https://github.com/alecgn
5 | */
6 |
7 | using CryptHash.Net.Resources;
8 | using System;
9 | using System.Collections.Generic;
10 | using System.Linq;
11 |
12 | namespace CryptHash.Net.Encoding
13 | {
14 | public static class Hexadecimal
15 | {
16 | public static string ToHexString(string plainString)
17 | {
18 | if (string.IsNullOrWhiteSpace(plainString))
19 | {
20 | return null;
21 | }
22 |
23 | var plainStringBytes = System.Text.Encoding.UTF8.GetBytes(plainString);
24 |
25 | return ToHexString(plainStringBytes);
26 | }
27 |
28 | public static string ToHexString(byte[] byteArray)
29 | {
30 | if (byteArray == null || byteArray.Length <= 0)
31 | {
32 | return null;
33 | }
34 |
35 | //var sb = new StringBuilder();
36 |
37 | //for (int i = 0; i < byteArray.Length; i++)
38 | //{
39 | // sb.Append(byteArray[i].ToString("X2"));
40 | //}
41 |
42 | //return sb.ToString();
43 |
44 | return string.Concat(byteArray.Select(b => b.ToString("X2")));
45 | }
46 |
47 | public static string ToString(string hexString)
48 | {
49 | if (string.IsNullOrWhiteSpace(hexString))
50 | {
51 | return null;
52 | }
53 |
54 | var byteArray = ToByteArray(hexString);
55 |
56 | return System.Text.Encoding.UTF8.GetString(byteArray);
57 | }
58 |
59 | public static byte[] ToByteArray(string hexString)
60 | {
61 | if (string.IsNullOrWhiteSpace(hexString))
62 | {
63 | return null;
64 | }
65 |
66 | if (hexString.Length % 2 != 0)
67 | {
68 | throw new ArgumentException(MessageStrings.Common_IncorrectHexadecimalString, nameof(hexString));
69 | }
70 |
71 | var byteArray = new byte[hexString.Length / 2];
72 | var i = 0;
73 |
74 | foreach (var hexVal in ChunkHexString(hexString))
75 | {
76 | byteArray[i] = Convert.ToByte(hexVal, 16);
77 | i++;
78 | }
79 |
80 | return byteArray;
81 | }
82 |
83 | private static IEnumerable ChunkHexString(string hexString)
84 | {
85 | for (var i = 0; i < hexString.Length; i += 2)
86 | {
87 | yield return hexString.Substring(i, 2);
88 | }
89 | }
90 | }
91 | }
92 |
--------------------------------------------------------------------------------
/.gitattributes:
--------------------------------------------------------------------------------
1 | ###############################################################################
2 | # Set default behavior to automatically normalize line endings.
3 | ###############################################################################
4 | * text=auto
5 |
6 | ###############################################################################
7 | # Set default behavior for command prompt diff.
8 | #
9 | # This is need for earlier builds of msysgit that does not have it on by
10 | # default for csharp files.
11 | # Note: This is only used by command line
12 | ###############################################################################
13 | #*.cs diff=csharp
14 |
15 | ###############################################################################
16 | # Set the merge driver for project and solution files
17 | #
18 | # Merging from the command prompt will add diff markers to the files if there
19 | # are conflicts (Merging from VS is not affected by the settings below, in VS
20 | # the diff markers are never inserted). Diff markers may cause the following
21 | # file extensions to fail to load in VS. An alternative would be to treat
22 | # these files as binary and thus will always conflict and require user
23 | # intervention with every merge. To do so, just uncomment the entries below
24 | ###############################################################################
25 | #*.sln merge=binary
26 | #*.csproj merge=binary
27 | #*.vbproj merge=binary
28 | #*.vcxproj merge=binary
29 | #*.vcproj merge=binary
30 | #*.dbproj merge=binary
31 | #*.fsproj merge=binary
32 | #*.lsproj merge=binary
33 | #*.wixproj merge=binary
34 | #*.modelproj merge=binary
35 | #*.sqlproj merge=binary
36 | #*.wwaproj merge=binary
37 |
38 | ###############################################################################
39 | # behavior for image files
40 | #
41 | # image files are treated as binary by default.
42 | ###############################################################################
43 | #*.jpg binary
44 | #*.png binary
45 | #*.gif binary
46 |
47 | ###############################################################################
48 | # diff behavior for common document formats
49 | #
50 | # Convert binary document formats to text before diffing them. This feature
51 | # is only available from the command line. Turn it on by uncommenting the
52 | # entries below.
53 | ###############################################################################
54 | #*.doc diff=astextplain
55 | #*.DOC diff=astextplain
56 | #*.docx diff=astextplain
57 | #*.DOCX diff=astextplain
58 | #*.dot diff=astextplain
59 | #*.DOT diff=astextplain
60 | #*.pdf diff=astextplain
61 | #*.PDF diff=astextplain
62 | #*.rtf diff=astextplain
63 | #*.RTF diff=astextplain
64 |
--------------------------------------------------------------------------------
/src/CryptHash.Net/CryptHash.Net.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 16
4 | VisualStudioVersion = 16.0.29020.237
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "lib", "lib", "{D637E865-42A1-4AED-8B00-DDE7AE87F495}"
7 | EndProject
8 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CryptHash.Net", "lib\CryptHash.Net\CryptHash.Net.csproj", "{7B61C728-9056-441E-9BA1-F230EF68FA3D}"
9 | EndProject
10 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "bin", "bin", "{32203E19-E62F-427D-8134-4B3DD7716B19}"
11 | EndProject
12 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "crypthash", "bin\crypthash\crypthash.csproj", "{D1ADED4E-2C11-4F18-8883-DA4613B09446}"
13 | EndProject
14 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CryptHash.Net.Tests", "lib\CryptHash.Net.Tests\CryptHash.Net.Tests.csproj", "{B4BD2E65-7A37-4027-88AD-30B217B42E02}"
15 | EndProject
16 | Global
17 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
18 | Debug|Any CPU = Debug|Any CPU
19 | Release|Any CPU = Release|Any CPU
20 | EndGlobalSection
21 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
22 | {7B61C728-9056-441E-9BA1-F230EF68FA3D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
23 | {7B61C728-9056-441E-9BA1-F230EF68FA3D}.Debug|Any CPU.Build.0 = Debug|Any CPU
24 | {7B61C728-9056-441E-9BA1-F230EF68FA3D}.Release|Any CPU.ActiveCfg = Release|Any CPU
25 | {7B61C728-9056-441E-9BA1-F230EF68FA3D}.Release|Any CPU.Build.0 = Release|Any CPU
26 | {D1ADED4E-2C11-4F18-8883-DA4613B09446}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27 | {D1ADED4E-2C11-4F18-8883-DA4613B09446}.Debug|Any CPU.Build.0 = Debug|Any CPU
28 | {D1ADED4E-2C11-4F18-8883-DA4613B09446}.Release|Any CPU.ActiveCfg = Release|Any CPU
29 | {D1ADED4E-2C11-4F18-8883-DA4613B09446}.Release|Any CPU.Build.0 = Release|Any CPU
30 | {B4BD2E65-7A37-4027-88AD-30B217B42E02}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
31 | {B4BD2E65-7A37-4027-88AD-30B217B42E02}.Debug|Any CPU.Build.0 = Debug|Any CPU
32 | {B4BD2E65-7A37-4027-88AD-30B217B42E02}.Release|Any CPU.ActiveCfg = Release|Any CPU
33 | {B4BD2E65-7A37-4027-88AD-30B217B42E02}.Release|Any CPU.Build.0 = Release|Any CPU
34 | EndGlobalSection
35 | GlobalSection(SolutionProperties) = preSolution
36 | HideSolutionNode = FALSE
37 | EndGlobalSection
38 | GlobalSection(NestedProjects) = preSolution
39 | {7B61C728-9056-441E-9BA1-F230EF68FA3D} = {D637E865-42A1-4AED-8B00-DDE7AE87F495}
40 | {D1ADED4E-2C11-4F18-8883-DA4613B09446} = {32203E19-E62F-427D-8134-4B3DD7716B19}
41 | {B4BD2E65-7A37-4027-88AD-30B217B42E02} = {D637E865-42A1-4AED-8B00-DDE7AE87F495}
42 | EndGlobalSection
43 | GlobalSection(ExtensibilityGlobals) = postSolution
44 | SolutionGuid = {43037084-02DE-4862-BCCA-60EA6D19E0D3}
45 | EndGlobalSection
46 | EndGlobal
47 |
--------------------------------------------------------------------------------
/img/icon-rider.svg:
--------------------------------------------------------------------------------
1 |
2 |
3 |
43 |
--------------------------------------------------------------------------------
/img/icon-resharper.svg:
--------------------------------------------------------------------------------
1 |
2 |
3 |
51 |
--------------------------------------------------------------------------------
/src/CryptHash.Net/lib/CryptHash.Net/CryptHash.Net.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netstandard2.0;netstandard2.1
5 | Alessandro Cagliostro Goncalves Neves
6 | CryptHash.Net is a .NET multi-target class library (.NET Standard 2.0/2.1) that can be used in projects with any .NET implementation, like .NET Framework, .NET Core, Mono, Xamarin, etc., for text and files symmetric (AES_128_CBC / AES_192_CBC / AES_256_CBC / AES_128_GCM / AES_192_GCM / AES_256_GCM) authenticated encryption/decryption, text/files hashing (MD5, SHA1, SHA256, SHA384, SHA512, HMAC-MD5, HMAC-SHA1, HMAC-SHA256, HMAC-SHA384, HMAC-SHA512, PBKDF2, BCrypt and Argon2id) and text encoding/decoding (Base64 and Hexadecimal).
7 | Alessandro Cagliostro Goncalves Neves, 2020
8 | MIT
9 | crypthash-icon-new.png
10 | https://github.com/alecgn/crypthash-net
11 | encryption decryption cryptography aes aes128 aes192 aes256 aes128cbc aes192cbc aes256cbc aes128gcm aes192gcm aes256gcm aescbc aesgcm aescbc128 aescbc192 aescbc256 aesgcm128 aesgcm192 aesgcm256 cbc gcm hash md5 sha1 sha256 sha384 sha512 hmacmd5 hmac-md5 hmacsha1 hmac-sha1 hmacsha256 hmac-sha256 hmacsha384 hmac-sha384 hmacsha512 hmac-sha512 pbkdf2 bcrypt argon2 argon2id c# c-sharp security password authentication authenticated base64 hex hexadecimal encode decode
12 | 3.6.1
13 | 3.6.1
14 | true
15 | https://github.com/alecgn/crypthash-net
16 | 3.6.1
17 |
18 |
19 |
20 |
21 | true
22 | TRACE
23 |
24 |
25 |
26 | true
27 |
28 |
29 |
30 | true
31 |
32 |
33 |
34 | true
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 | True
47 | True
48 | MessageStrings.resx
49 |
50 |
51 |
52 |
53 |
54 | ResXFileCodeGenerator
55 | MessageStrings.Designer.cs
56 |
57 |
58 |
59 |
60 |
--------------------------------------------------------------------------------
/src/CryptHash.Net/lib/CryptHash.Net.Tests/Hash/Argon2id_Tests.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Alessandro Cagliostro, 2021
3 | *
4 | * https://github.com/alecgn
5 | */
6 |
7 | using CryptHash.Net.Hash;
8 | using CryptHash.Net.Hash.HashResults;
9 | using Microsoft.VisualStudio.TestTools.UnitTesting;
10 |
11 | namespace CryptHash.Net.Tests.Hash
12 | {
13 | [TestClass]
14 | public class Argon2id_Tests
15 | {
16 | private readonly Argon2id _argon2id = new Argon2id();
17 | private readonly string _testString = "This is a test string!";
18 |
19 | [TestMethod]
20 | public void ComputeAndVerifyHash_String_without_associated_data()
21 | {
22 | var verifyResult = new Argon2idHashResult();
23 | var errorMessage = "";
24 |
25 | var testStringBytes = System.Text.Encoding.UTF8.GetBytes(_testString);
26 | var iterations = 4;
27 | var kbMemorySize = 1024;
28 | var degreeOfParallelism = 0; // auto-generate based on number of the processor's cores
29 | var amountBytesToReturn = 16;
30 | byte[] salt = null; // auto-generate
31 | byte[] associatedData = null;
32 | byte[] knownSecret = null;
33 |
34 | var hashResult = _argon2id.ComputeHash(testStringBytes, iterations, kbMemorySize, degreeOfParallelism, amountBytesToReturn, salt,
35 | associatedData, knownSecret);
36 |
37 | if (hashResult.Success)
38 | {
39 | verifyResult = _argon2id.VerifyHash(hashResult.HashBytes, testStringBytes, iterations, kbMemorySize, degreeOfParallelism,
40 | amountBytesToReturn, hashResult.SaltBytes, associatedData, knownSecret);
41 |
42 | if (!verifyResult.Success)
43 | {
44 | errorMessage = verifyResult.Message;
45 | }
46 | }
47 | else
48 | {
49 | errorMessage = hashResult.Message;
50 | }
51 |
52 | Assert.IsTrue((hashResult.Success && verifyResult.Success), errorMessage);
53 | }
54 |
55 | [TestMethod]
56 | public void ComputeAndVerifyHash_String_with_associated_data()
57 | {
58 | var verifyResult = new Argon2idHashResult();
59 | var errorMessage = "";
60 |
61 | var testStringBytes = System.Text.Encoding.UTF8.GetBytes(_testString);
62 | var iterations = 4;
63 | var kbMemorySize = 1024;
64 | var degreeOfParallelism = 0; // auto-generate based on number of the processor's cores
65 | var amountBytesToReturn = 16;
66 | byte[] salt = null; // auto-generate
67 | var associatedData = System.Text.Encoding.UTF8.GetBytes("0f8fad5b-d9cb-469f-a165-70867728950e");
68 | byte[] knownSecret = null;
69 |
70 | var hashResult = _argon2id.ComputeHash(testStringBytes, iterations, kbMemorySize, degreeOfParallelism, amountBytesToReturn, salt,
71 | associatedData, knownSecret);
72 |
73 | if (hashResult.Success)
74 | {
75 | verifyResult = _argon2id.VerifyHash(hashResult.HashBytes, testStringBytes, iterations, kbMemorySize, degreeOfParallelism,
76 | amountBytesToReturn, hashResult.SaltBytes, associatedData, knownSecret);
77 |
78 | if (!verifyResult.Success)
79 | {
80 | errorMessage = verifyResult.Message;
81 | }
82 | }
83 | else
84 | {
85 | errorMessage = hashResult.Message;
86 | }
87 |
88 | Assert.IsTrue((hashResult.Success && verifyResult.Success), errorMessage);
89 | }
90 | }
91 | }
92 |
--------------------------------------------------------------------------------
/src/CryptHash.Net/lib/CryptHash.Net/Encryption/RSA/Base/RSABase.cs:
--------------------------------------------------------------------------------
1 | ///*
2 | // * Alessandro Cagliostro, 2021
3 | // *
4 | // * https://github.com/alecgn
5 | // */
6 |
7 | //using CryptHash.Net.Encryption.RSA.EncryptionResults;
8 | //using CryptHash.Net.Resources;
9 | //using System;
10 | //using System.Security.Cryptography;
11 |
12 | //namespace CryptHash.Net.Encryption.RSA.Base
13 | //{
14 | // /*internal abstract*/
15 | // public class RSABase
16 | // {
17 | // public static KeySizes KeySizes = new KeySizes(384, 16384, 8);
18 |
19 | // public static bool IsValidKeySize(int keySize)
20 | // {
21 | // return (keySize >= KeySizes.MinSize && keySize <= KeySizes.MaxSize && keySize % KeySizes.SkipSize == 0);
22 | // }
23 |
24 | // public RSAEncryptionResult Encrypt(byte[] sourceData, int keySize, RSAParameters rsaParameters, bool doOAEPPadding = false)
25 | // {
26 | // if (!IsValidKeySize(keySize))
27 | // {
28 | // return new RSAEncryptionResult()
29 | // {
30 | // Success = false,
31 | // Message = $"{MessageStrings.Common_InvalidKeySizeError} ({keySize})."
32 | // };
33 | // }
34 |
35 | // byte[] encryptedData = null;
36 |
37 | // try
38 | // {
39 | // using (var rsaCsp = new RSACryptoServiceProvider(keySize))
40 | // {
41 | // rsaCsp.ImportParameters(rsaParameters);
42 | // encryptedData = rsaCsp.Encrypt(sourceData, doOAEPPadding);
43 | // }
44 | // }
45 | // catch (Exception ex)
46 | // {
47 | // return new RSAEncryptionResult()
48 | // {
49 | // Success = false,
50 | // Message = $"{MessageStrings.Encryption_ExceptionError}\n{ex.ToString()}"
51 | // };
52 | // }
53 |
54 | // return new RSAEncryptionResult()
55 | // {
56 | // Success = true,
57 | // Message = MessageStrings.Encryption_EncryptSuccess,
58 | // RSAParameters = rsaParameters,
59 | // EncryptedDataBytes = encryptedData
60 | // };
61 | // }
62 |
63 | // public RSADecryptionResult Decrypt(byte[] encryptedData, int keySize, RSAParameters rsaParameters, bool doOAEPPadding)
64 | // {
65 | // if (!IsValidKeySize(keySize))
66 | // {
67 | // return new RSADecryptionResult()
68 | // {
69 | // Success = false,
70 | // Message = $"{MessageStrings.Common_InvalidKeySizeError} ({keySize})."
71 | // };
72 | // }
73 |
74 | // byte[] decryptedData = null;
75 |
76 | // try
77 | // {
78 | // using (var rsaCsp = new RSACryptoServiceProvider(keySize))
79 | // {
80 | // rsaCsp.ImportParameters(rsaParameters);
81 | // decryptedData = rsaCsp.Decrypt(encryptedData, doOAEPPadding);
82 | // }
83 | // }
84 | // catch (Exception ex)
85 | // {
86 | // return new RSADecryptionResult()
87 | // {
88 | // Success = false,
89 | // Message = $"{MessageStrings.Encryption_ExceptionError}\n{ex.ToString()}"
90 | // };
91 | // }
92 |
93 | // return new RSADecryptionResult()
94 | // {
95 | // Success = true,
96 | // Message = MessageStrings.Encryption_EncryptSuccess,
97 | // RSAParameters = rsaParameters,
98 | // DecryptedDataBytes = decryptedData
99 | // };
100 | // }
101 | // }
102 | //}
103 |
--------------------------------------------------------------------------------
/src/CryptHash.Net/lib/CryptHash.Net/Hash/Argon2id.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Alessandro Cagliostro, 2021
3 | *
4 | * https://github.com/alecgn
5 | */
6 |
7 | using CryptHash.Net.Hash.HashResults;
8 | using CryptHash.Net.Resources;
9 | using CryptHash.Net.Util;
10 | using System;
11 | using System.Linq;
12 |
13 | namespace CryptHash.Net.Hash
14 | {
15 | public class Argon2id
16 | {
17 | public Argon2idHashResult ComputeHash(byte[] stringToComputeHashBytes, int iterations, int kBmemorySize, int degreeOfParallelism, int amountBytesToReturn,
18 | byte[] salt = null, byte[] associatedData = null, byte[] knownSecret = null)
19 | {
20 | try
21 | {
22 | salt = salt ?? CommonMethods.GenerateSalt(); // generate a 128 bits salt, if not provided
23 | degreeOfParallelism = (degreeOfParallelism <= 0 ? Environment.ProcessorCount : degreeOfParallelism);
24 |
25 | using (var argon2id = new Konscious.Security.Cryptography.Argon2id(stringToComputeHashBytes)
26 | {
27 | Salt = salt,
28 | DegreeOfParallelism = degreeOfParallelism,
29 | Iterations = iterations,
30 | MemorySize = kBmemorySize,
31 | AssociatedData = associatedData,
32 | KnownSecret = knownSecret
33 | })
34 | {
35 | var hashBytes = argon2id.GetBytes(amountBytesToReturn);
36 |
37 | return new Argon2idHashResult()
38 | {
39 | Success = true,
40 | HashBytes = hashBytes,
41 | HashString = Convert.ToBase64String(hashBytes),
42 | Message = MessageStrings.Hash_ComputeSuccess,
43 | SaltBytes = salt,
44 | Iterations = iterations,
45 | DegreeOfParallelism = degreeOfParallelism,
46 | KBMemorySize = kBmemorySize,
47 | AssociatedData = associatedData,
48 | KnownSecret = knownSecret
49 | };
50 | }
51 | }
52 | catch (Exception ex)
53 | {
54 | return new Argon2idHashResult()
55 | {
56 | Success = false,
57 | Message = ex.ToString()
58 | };
59 | }
60 | }
61 |
62 | public Argon2idHashResult VerifyHash(byte[] hash, byte[] stringToComputeHashBytes, int iterations, int kBmemorySize, int degreeOfParallelism, int amountBytesToReturn,
63 | byte[] salt = null, byte[] associatedData = null, byte[] knownSecret = null)
64 | {
65 | try
66 | {
67 | var newHash = ComputeHash(stringToComputeHashBytes, iterations, kBmemorySize, degreeOfParallelism, amountBytesToReturn, salt, associatedData, knownSecret);
68 |
69 | return new Argon2idHashResult()
70 | {
71 | Success = newHash.HashBytes.SequenceEqual(hash),
72 | HashBytes = newHash.HashBytes,
73 | HashString = newHash.HashString,
74 | Message = $"{(newHash.HashBytes.SequenceEqual(hash) ? MessageStrings.Hash_Match : MessageStrings.Hash_DoesNotMatch)}",
75 | SaltBytes = salt,
76 | Iterations = iterations,
77 | DegreeOfParallelism = degreeOfParallelism,
78 | KBMemorySize = kBmemorySize,
79 | AssociatedData = associatedData,
80 | KnownSecret = knownSecret
81 | };
82 | }
83 | catch (Exception ex)
84 | {
85 | return new Argon2idHashResult()
86 | {
87 | Success = false,
88 | Message = ex.ToString()
89 | };
90 | }
91 | }
92 | }
93 | }
94 |
--------------------------------------------------------------------------------
/src/CryptHash.Net/lib/CryptHash.Net/Util/MessageDictionary.cs:
--------------------------------------------------------------------------------
1 | ///*
2 | // * Alessandro Cagliostro, 2021
3 | // *
4 | // * https://github.com/alecgn
5 | // */
6 |
7 | //using System;
8 | //using System.Collections.Concurrent;
9 | //using System.Collections.Generic;
10 | //using System.Linq;
11 |
12 | //namespace CryptHash.Net.Util
13 | //{
14 | // public sealed class MessageDictionary
15 | // {
16 | // private static readonly Lazy _lazyMessageDictionary = new Lazy(() => new MessageDictionary());
17 |
18 | // private readonly IDictionary _cdicMessages = new ConcurrentDictionary()
19 | // {
20 | // ["Common.FileNotFound"] = "File not found:",
21 | // ["Common.AlgorithmNotSupported"] = $"Algorithm not supported:",
22 | // ["Common.InvalidKeySizeError"] = "Invalid key bit size:",
23 | // ["Common.InvalidAuthKeySizeError"] = "Invalid auth key bit size:",
24 | // ["Common.IncorrectInputLengthError"] = "Incorrect data length, data probably tampered with.",
25 | // ["Common.InvalidDataLengthError"] = "Invalid data length: ",
26 | // ["Common.IncorrectTagsLength"] = "Tags length must be equal.",
27 | // ["Common.IncorrectHexadecimalString"] = "Incorret hexadecimal string.",
28 |
29 | // ["Encryption.InputRequired"] = "Input to encrypt required.",
30 | // ["Encryption.PasswordRequired"] = "Password to encrypt required.",
31 | // ["Encryption.ExceptionError"] = "Error while trying to encrypt data:",
32 | // ["Encryption.FileAdditionalDataWriting"] = "Writing additional data to file...",
33 | // ["Encryption.FileAdditionalDataWritten"] = "Additional data written to file.",
34 | // ["Encryption.MaxInputSizeError"] = "Max. input size cannot be greater in bytes than:",
35 | // ["Encryption.MaxAssociatedDataSizeError"] = "Max. associated data size cannot be greater in bytes than:",
36 | // ["Encryption.EncryptSuccess"] = "Data succesfully encrypted.",
37 | // ["Encryption.EncryptedFilePathError"] = "Encrypted file path required.",
38 | // ["Encryption.DestinationDirectoryNotFound"] = "Destination directory not found:",
39 | // ["Encryption.FileEncryptSuccess"] = "File \"{0}\" successfully encrypted to \"{1}\".",
40 | // ["Encryption.FileDeleted"] = "File \"{0}\" deleted.",
41 |
42 | // ["Decryption.InputRequired"] = "Input to decrypt required.",
43 | // ["Decryption.PasswordRequired"] = "Password to decrypt required.",
44 | // ["Decryption.ExceptionError"] = "Error while trying to decrypt data:",
45 | // ["Decryption.AuthenticationTagsMismatchError"] = "Authentication for decryption failed, wrong password or data probably tampered with.",
46 | // ["Decryption.EncryptedFileNotFound"] = "Encrypted file not found:",
47 | // ["Decryption.MaxEncryptedInputSizeError"] = "Max. encrypted input size cannot be greater in bytes than:",
48 | // ["Decryption.DecryptSuccess"] = "Data succesfully decrypted.",
49 | // ["Decryption.NullKeyError"] = "Key cannot be null.",
50 | // ["Decryption.NullIVError"] = "IV cannot be null.",
51 | // ["Decryption.DecryptedFilePathError"] = "Decrypted file path required.",
52 | // ["Decryption.EndPositionLessThanStartError"] = "End position (\"{0}\") cannot be less than start position (\"{1}\").",
53 | // ["Decryption.FileDecryptSuccess"] = "File \"{0}\" successfully decrypted to \"{1}\".",
54 |
55 | // ["Hash.ComputeSuccess"] = "Input hash computed succesfully.",
56 | // ["Hash.InputRequired"] = "Input to compute hash required.",
57 | // ["Hash.VerificationHashRequired"] = "Verification hash required.",
58 | // ["Hash.Match"] = "Input hash and verification hash match.",
59 | // ["Hash.DoesNotMatch"] = "Input hash and verification hash does not match.",
60 |
61 | // ["HMAC.InputRequired"] = "Input to compute HMAC required.",
62 | // ["HMAC.ComputeSuccess"] = "Input HMAC computed succesfully.",
63 | // };
64 |
65 | // public static MessageDictionary Instance => _lazyMessageDictionary.Value;
66 |
67 | // private MessageDictionary() { }
68 |
69 | // private string GetMessage(string key)
70 | // {
71 |
72 | // if (!_cdicMessages.TryGetValue(key, out var message))
73 | // {
74 | // message = $"Unknown key \"{key}\" in OutputMessages Dictionary.\nAvailable keys: ({string.Join(", ", _cdicMessages.Select(i => $"\"{i.Key}\""))}).";
75 | // }
76 |
77 | // return message;
78 | // }
79 |
80 | // public string this[string key] => GetMessage(key);
81 | // }
82 | //}
83 |
--------------------------------------------------------------------------------
/src/CryptHash.Net/bin/crypthash/ConsoleUtil/ProgressBar.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Thanks to Daniel Wolf for this great console progress bar
3 | * A little modification was made by me to fit the progress bar to the console width
4 | *
5 | * GitHub profile: https://github.com/DanielSWolf
6 | * Source code available in: https://gist.github.com/DanielSWolf/0ab6a96899cc5377bf54
7 | *
8 | * Console progress bar.Code is under the MIT License: http://opensource.org/licenses/MIT
9 | */
10 |
11 | using System;
12 | using System.Text;
13 | using System.Threading;
14 |
15 | namespace CryptHash.Net.CLI.ConsoleUtil
16 | {
17 | ///
18 | /// An ASCII progress bar
19 | ///
20 | public class ProgressBar : IDisposable, IProgress
21 | {
22 | //private const int blockCount = 10;
23 | private readonly int blockCount;
24 | private readonly TimeSpan animationInterval = TimeSpan.FromSeconds(1.0 / 8);
25 | private const string animation = @"|/-\";
26 |
27 | private readonly Timer timer;
28 |
29 | private double currentProgress = 0;
30 | private string currentText = string.Empty;
31 | private bool disposed = false;
32 | private int animationIndex = 0;
33 |
34 | public ProgressBar(int blockCount = 0)
35 | {
36 | this.blockCount = (blockCount == 0 ? (Console.WindowWidth - 10) : blockCount);
37 | timer = new Timer(TimerHandler);
38 | //timer = new Timer(TimerHandler, new AutoResetEvent(false), TimeSpan.FromSeconds(1.0 / 8), TimeSpan.FromSeconds(1.0 / 8));
39 |
40 | // A progress bar is only for temporary display in a console window.
41 | // If the console output is redirected to a file, draw nothing.
42 | // Otherwise, we'll end up with a lot of garbage in the target file.
43 | if (!Console.IsOutputRedirected)
44 | {
45 | ResetTimer();
46 | }
47 | }
48 |
49 | public void Report(double value)
50 | {
51 | // Make sure value is in [0..1] range
52 | value = Math.Max(0, Math.Min(1, value));
53 | Interlocked.Exchange(ref currentProgress, value);
54 | }
55 |
56 | public void WriteLine(string text)
57 | {
58 | lock (timer)
59 | {
60 | UpdateText(string.Empty);
61 | Console.WriteLine(text);
62 | UpdateText(currentText);
63 | }
64 | }
65 |
66 | private void TimerHandler(object state)
67 | {
68 | lock (timer)
69 | {
70 | if (disposed)
71 | {
72 | return;
73 | }
74 |
75 | var progressBlockCount = (int)(currentProgress * blockCount);
76 | var percent = (int)(currentProgress * 100);
77 | var text = string.Format("[{0}{1}] {2,3}% {3}",
78 | new string('#', progressBlockCount), new string('-', blockCount - progressBlockCount),
79 | percent,
80 | animation[animationIndex++ % animation.Length]);
81 | UpdateText(text);
82 |
83 | ResetTimer();
84 | }
85 | }
86 |
87 | private void UpdateText(string text)
88 | {
89 | // Get length of common portion
90 | var commonPrefixLength = 0;
91 | var commonLength = Math.Min(currentText.Length, text.Length);
92 |
93 | while (commonPrefixLength < commonLength && text[commonPrefixLength] == currentText[commonPrefixLength])
94 | {
95 | commonPrefixLength++;
96 | }
97 |
98 | // Backtrack to the first differing character
99 | var outputBuilder = new StringBuilder();
100 | outputBuilder.Append('\b', currentText.Length - commonPrefixLength);
101 |
102 | // Output new suffix
103 | outputBuilder.Append(text.Substring(commonPrefixLength));
104 |
105 | // If the new text is shorter than the old one: delete overlapping characters
106 | var overlapCount = currentText.Length - text.Length;
107 |
108 | if (overlapCount > 0)
109 | {
110 | outputBuilder.Append(' ', overlapCount);
111 | outputBuilder.Append('\b', overlapCount);
112 | }
113 |
114 | Console.Write(outputBuilder);
115 | currentText = text;
116 | }
117 |
118 | private void ResetTimer()
119 | {
120 | timer.Change(animationInterval, TimeSpan.FromMilliseconds(-1));
121 | }
122 |
123 | public void Dispose()
124 | {
125 | lock (timer)
126 | {
127 | disposed = true;
128 | UpdateText(string.Empty);
129 | timer.Dispose();
130 | }
131 | }
132 | }
133 | }
134 |
--------------------------------------------------------------------------------
/src/CryptHash.Net/lib/CryptHash.Net/Hash/PBKDF2_HMAC_SHA_1.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Alessandro Cagliostro, 2021
3 | *
4 | * https://github.com/alecgn
5 | */
6 |
7 | using CryptHash.Net.Hash.HashResults;
8 | using CryptHash.Net.Resources;
9 | using CryptHash.Net.Util;
10 | using System;
11 | using System.Security.Cryptography;
12 |
13 | namespace CryptHash.Net.Hash
14 | {
15 | //[Obsolete("This class is obsolete. Use PBKDF2.cs class instead.")]
16 | public class PBKDF2_HMAC_SHA_1
17 | {
18 | private static readonly int _hashBitSize = HMACOutputLengthDictionary.Instance[Enums.HMACAlgorithm.HMACSHA1];
19 | private static readonly int _hashBytesLength = (_hashBitSize / 8);
20 |
21 | private static readonly int _saltBitSize = 128;
22 | private static readonly int _saltBytesLength = (_saltBitSize / 8);
23 |
24 | private const int _iterations = 100000;
25 |
26 | public PBKDF2HashResult ComputeHash(string stringToComputeHash, byte[] salt = null, int iterationsForKeyDerivation = _iterations)
27 | {
28 | if (string.IsNullOrWhiteSpace(stringToComputeHash))
29 | {
30 | return new PBKDF2HashResult()
31 | {
32 | Success = false,
33 | Message = MessageStrings.Hash_InputRequired
34 | };
35 | }
36 |
37 | //salt = salt ?? CommonMethods.GenerateRandomBytes(_saltBytesLength);
38 | salt = salt ?? CommonMethods.GenerateSalt();
39 | byte[] hash;
40 |
41 | try
42 | {
43 | using (var rfc2898DeriveBytes = new Rfc2898DeriveBytes(stringToComputeHash, salt))
44 | {
45 | rfc2898DeriveBytes.IterationCount = iterationsForKeyDerivation;
46 | hash = rfc2898DeriveBytes.GetBytes(_hashBytesLength);
47 | }
48 |
49 | var hashBytes = new byte[(_saltBytesLength + _hashBytesLength)];
50 | Array.Copy(salt, 0, hashBytes, 0, _saltBytesLength);
51 | Array.Copy(hash, 0, hashBytes, _saltBytesLength, _hashBytesLength);
52 |
53 | return new PBKDF2HashResult()
54 | {
55 | Success = true,
56 | Message = MessageStrings.Hash_ComputeSuccess,
57 | HashString = $"{Convert.ToBase64String(hashBytes)}",
58 | HashBytes = hashBytes,
59 | Iterations = iterationsForKeyDerivation,
60 | Salt = salt,
61 | PRF = Enums.HMACAlgorithm.HMACSHA1
62 | };
63 | }
64 | catch (Exception ex)
65 | {
66 | return new PBKDF2HashResult()
67 | {
68 | Success = false,
69 | Message = ex.ToString()
70 | };
71 | }
72 | }
73 |
74 | public PBKDF2HashResult VerifyHash(string stringToBeVerified, string hash, int iterationsForKeyDerivation = _iterations)
75 | {
76 | if (string.IsNullOrWhiteSpace(stringToBeVerified))
77 | {
78 | return new PBKDF2HashResult()
79 | {
80 | Success = false,
81 | Message = MessageStrings.Hash_InputRequired
82 | };
83 | }
84 |
85 | if (string.IsNullOrWhiteSpace(hash))
86 | {
87 | return new PBKDF2HashResult()
88 | {
89 | Success = false,
90 | Message = MessageStrings.Hash_InputRequired
91 | };
92 | }
93 |
94 | var hashWithSaltBytes = Convert.FromBase64String(hash);
95 |
96 | if (hashWithSaltBytes.Length != (_saltBytesLength + _hashBytesLength))
97 | {
98 | return new PBKDF2HashResult()
99 | {
100 | Success = false,
101 | Message = MessageStrings.Common_IncorrectInputLengthError
102 | };
103 | }
104 |
105 | var saltBytes = new byte[_saltBytesLength];
106 | Array.Copy(hashWithSaltBytes, 0, saltBytes, 0, _saltBytesLength);
107 |
108 | var hashBytes = new byte[_hashBytesLength];
109 | Array.Copy(hashWithSaltBytes, _saltBytesLength, hashBytes, 0, _hashBytesLength);
110 |
111 | var result = ComputeHash(stringToBeVerified, saltBytes, iterationsForKeyDerivation);
112 |
113 | if (string.Equals(result.HashString, hash))
114 | {
115 | return new PBKDF2HashResult()
116 | {
117 | Success = true,
118 | Message = MessageStrings.Hash_Match,
119 | HashString = hash,
120 | HashBytes = result.HashBytes,
121 | Iterations = result.Iterations,
122 | Salt = result.Salt,
123 | PRF = result.PRF
124 | };
125 | }
126 | else
127 | {
128 | return new PBKDF2HashResult()
129 | {
130 | Success = false,
131 | Message = MessageStrings.Hash_DoesNotMatch
132 | };
133 | }
134 | }
135 | }
136 | }
137 |
--------------------------------------------------------------------------------
/src/CryptHash.Net/lib/CryptHash.Net/Hash/MD5.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Alessandro Cagliostro, 2021
3 | *
4 | * https://github.com/alecgn
5 | */
6 |
7 | using CryptHash.Net.Hash.Base;
8 | using CryptHash.Net.Hash.HashResults;
9 |
10 | namespace CryptHash.Net.Hash.Hash
11 | {
12 | public class MD5 : HashBase
13 | {
14 | ///
15 | /// Computes the MD5 hash of an input byte array.
16 | ///
17 | /// The input byte array to compute the MD5 hash.
18 | /// The offset into the byte array from wich to begin reading data.
19 | /// The number of bytes in the array to read after the offset.
20 | /// GenericHashResult
21 | public GenericHashResult ComputeHash(byte[] bytesToComputeHash, int offset = 0, int count = 0)
22 | {
23 | return base.ComputeHash(Enums.HashAlgorithm.MD5, bytesToComputeHash, offset, count);
24 | }
25 |
26 | ///
27 | /// Computes the MD5 hash of an input string.
28 | ///
29 | /// The input string to compute the MD5 hash.
30 | /// The offset into the byte array from wich to begin reading data.
31 | /// The number of bytes in the array to read after the offset.
32 | /// GenericHashResult
33 | public GenericHashResult ComputeHash(string stringToComputeHash, int offset = 0, int count = 0)
34 | {
35 | return base.ComputeHash(Enums.HashAlgorithm.MD5, stringToComputeHash, offset, count);
36 | }
37 |
38 | ///
39 | /// Computes the MD5 hash of an input file.
40 | ///
41 | /// The input file path to compute the MD5 hash.
42 | /// The offset into the FileStream from wich to begin reading data.
43 | /// The number of bytes in the FileStream to read after the offset.
44 | /// GenericHashResult
45 | public GenericHashResult ComputeFileHash(string filePathToComputeHash, long offset = 0, long count = 0)
46 | {
47 | return base.ComputeFileHash(Enums.HashAlgorithm.MD5, filePathToComputeHash, offset, count);
48 | }
49 |
50 |
51 | ///
52 | /// Verifies the MD5 hash of an input byte array.
53 | ///
54 | /// The pre-computed MD5 hash byte array.
55 | /// The input byte array to compute and verify the MD5 hash.
56 | /// The offset into the byte array from wich to begin reading data.
57 | /// The number of bytes in the array to read after the offset.
58 | /// GenericHashResult
59 | public GenericHashResult VerifyHash(byte[] hashBytes, byte[] bytesToVerifyHash, int offset = 0, int count = 0)
60 | {
61 | return base.VerifyHash(Enums.HashAlgorithm.MD5, hashBytes, bytesToVerifyHash, offset, count);
62 | }
63 |
64 | ///
65 | /// Verifies the MD5 hash of an input string.
66 | ///
67 | /// The pre-computed MD5 hash hexadecimal encoded string.
68 | /// The input string to compute and verify the MD5 hash.
69 | /// The offset into the byte array from wich to begin reading data.
70 | /// The number of bytes in the array to read after the offset.
71 | /// GenericHashResult
72 | public GenericHashResult VerifyHash(string hashHexString, string stringToVerifyHash, int offset = 0, int count = 0)
73 | {
74 | return base.VerifyHash(Enums.HashAlgorithm.MD5, hashHexString, stringToVerifyHash, offset, count);
75 | }
76 |
77 | ///
78 | /// Verifies the MD5 of an input file.
79 | ///
80 | /// The pre-computed MD5 hash hexadecimal encoded string.
81 | /// The input file path to compute and verify the MD5 hash.
82 | /// The offset into the FileStream from wich to begin reading data.
83 | /// The number of bytes in the FileStream to read after the offset.
84 | /// GenericHashResult
85 | public GenericHashResult VerifyFileHash(string hashHexString, string filePathToVerifyHash, long offset = 0, long count = 0)
86 | {
87 | return base.VerifyFileHash(Enums.HashAlgorithm.MD5, hashHexString, filePathToVerifyHash, offset, count);
88 | }
89 |
90 | ///
91 | /// Verifies the MD5 of an input file.
92 | ///
93 | /// The pre-computed MD5 hash byte array.
94 | /// The input file path to compute and verify the MD5 hash.
95 | /// The offset into the FileStream from wich to begin reading data.
96 | /// The number of bytes in the FileStream to read after the offset.
97 | /// GenericHashResult
98 | public GenericHashResult VerifyFileHash(byte[] hashBytes, string filePathToVerifyHash, long offset = 0, long count = 0)
99 | {
100 | return base.VerifyFileHash(Enums.HashAlgorithm.MD5, hashBytes, filePathToVerifyHash, offset, count);
101 | }
102 | }
103 | }
104 |
--------------------------------------------------------------------------------
/src/CryptHash.Net/lib/CryptHash.Net/Hash/SHA1.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Alessandro Cagliostro, 2021
3 | *
4 | * https://github.com/alecgn
5 | */
6 |
7 | using CryptHash.Net.Hash.Base;
8 | using CryptHash.Net.Hash.HashResults;
9 |
10 | namespace CryptHash.Net.Hash.Hash
11 | {
12 | public class SHA1 : HashBase
13 | {
14 | ///
15 | /// Computes the SHA1 hash of an input byte array.
16 | ///
17 | /// The input byte array to compute the SHA1 hash.
18 | /// The offset into the byte array from wich to begin reading data.
19 | /// The number of bytes in the array to read after the offset.
20 | /// GenericHashResult
21 | public GenericHashResult ComputeHash(byte[] bytesToComputeHash, int offset = 0, int count = 0)
22 | {
23 | return base.ComputeHash(Enums.HashAlgorithm.SHA1, bytesToComputeHash, offset, count);
24 | }
25 |
26 |
27 | ///
28 | /// Computes the SHA1 hash of an input string.
29 | ///
30 | /// The input string to compute the SHA1 hash.
31 | /// The offset into the byte array from wich to begin reading data.
32 | /// The number of bytes in the array to read after the offset.
33 | /// GenericHashResult
34 | public GenericHashResult ComputeHash(string stringToComputeHash, int offset = 0, int count = 0)
35 | {
36 | return base.ComputeHash(Enums.HashAlgorithm.SHA1, stringToComputeHash, offset, count);
37 | }
38 |
39 | ///
40 | /// Computes the SHA1 hash of an input file.
41 | ///
42 | /// The input file path to compute the SHA1 hash.
43 | /// The offset into the FileStream from wich to begin reading data.
44 | /// The number of bytes in the FileStream to read after the offset.
45 | /// GenericHashResult
46 | public GenericHashResult ComputeFileHash(string filePathToComputeHash, long offset = 0, long count = 0)
47 | {
48 | return base.ComputeFileHash(Enums.HashAlgorithm.SHA1, filePathToComputeHash, offset, count);
49 | }
50 |
51 | ///
52 | /// Verifies the SHA1 hash of an input byte array.
53 | ///
54 | /// The pre-computed SHA1 hash byte array.
55 | /// The input byte array to compute and verify the SHA1 hash.
56 | /// The offset into the byte array from wich to begin reading data.
57 | /// The number of bytes in the array to read after the offset.
58 | /// GenericHashResult
59 | public GenericHashResult VerifyHash(byte[] hashBytes, byte[] bytesToVerifyHash, int offset = 0, int count = 0)
60 | {
61 | return base.VerifyHash(Enums.HashAlgorithm.SHA1, hashBytes, bytesToVerifyHash, offset, count);
62 | }
63 |
64 | ///
65 | /// Verifies the SHA1 hash of an input string.
66 | ///
67 | /// The pre-computed SHA1 hash hexadecimal encoded string.
68 | /// The input string to compute and verify the SHA1 hash.
69 | /// The offset into the byte array from wich to begin reading data.
70 | /// The number of bytes in the array to read after the offset.
71 | /// GenericHashResult
72 | public GenericHashResult VerifyHash(string hashHexString, string stringToVerifyHash, int offset = 0, int count = 0)
73 | {
74 | return base.VerifyHash(Enums.HashAlgorithm.SHA1, hashHexString, stringToVerifyHash, offset, count);
75 | }
76 |
77 | ///
78 | /// Verifies the SHA1 of an input file.
79 | ///
80 | /// The pre-computed SHA1 hash hexadecimal encoded string.
81 | /// The input file path to compute and verify the SHA1 hash.
82 | /// The offset into the FileStream from wich to begin reading data.
83 | /// The number of bytes in the FileStream to read after the offset.
84 | /// GenericHashResult
85 | public GenericHashResult VerifyFileHash(string hashHexString, string filePathToVerifyHash, long offset = 0, long count = 0)
86 | {
87 | return base.VerifyFileHash(Enums.HashAlgorithm.SHA1, hashHexString, filePathToVerifyHash, offset, count);
88 | }
89 |
90 | ///
91 | /// Verifies the SHA1 of an input file.
92 | ///
93 | /// The pre-computed SHA1 hash byte array.
94 | /// The input file path to compute and verify the SHA1 hash.
95 | /// The offset into the FileStream from wich to begin reading data.
96 | /// The number of bytes in the FileStream to read after the offset.
97 | /// GenericHashResult
98 | public GenericHashResult VerifyFileHash(byte[] hashBytes, string filePathToVerifyHash, long offset = 0, long count = 0)
99 | {
100 | return base.VerifyFileHash(Enums.HashAlgorithm.SHA1, hashBytes, filePathToVerifyHash, offset, count);
101 | }
102 | }
103 | }
104 |
--------------------------------------------------------------------------------
/src/CryptHash.Net/lib/CryptHash.Net/Hash/SHA256.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Alessandro Cagliostro, 2021
3 | *
4 | * https://github.com/alecgn
5 | */
6 |
7 | using CryptHash.Net.Hash.Base;
8 | using CryptHash.Net.Hash.HashResults;
9 |
10 | namespace CryptHash.Net.Hash.Hash
11 | {
12 | public class SHA256 : HashBase
13 | {
14 | ///
15 | /// Computes the SHA256 hash of an input byte array.
16 | ///
17 | /// The input byte array to compute the SHA256 hash.
18 | /// The offset into the byte array from wich to begin reading data.
19 | /// The number of bytes in the array to read after the offset.
20 | /// GenericHashResult
21 | public GenericHashResult ComputeHash(byte[] bytesToComputeHash, int offset = 0, int count = 0)
22 | {
23 | return base.ComputeHash(Enums.HashAlgorithm.SHA256, bytesToComputeHash, offset, count);
24 | }
25 |
26 | ///
27 | /// Computes the SHA256 hash of an input string.
28 | ///
29 | /// The input string to compute the SHA256 hash.
30 | /// The offset into the byte array from wich to begin reading data.
31 | /// The number of bytes in the array to read after the offset.
32 | /// GenericHashResult
33 | public GenericHashResult ComputeHash(string stringToComputeHash, int offset = 0, int count = 0)
34 | {
35 | return base.ComputeHash(Enums.HashAlgorithm.SHA256, stringToComputeHash, offset, count);
36 | }
37 |
38 | ///
39 | /// Computes the SHA256 hash of an input file.
40 | ///
41 | /// The input file path to compute the SHA256 hash.
42 | /// The offset into the FileStream from wich to begin reading data.
43 | /// The number of bytes in the FileStream to read after the offset.
44 | /// GenericHashResult
45 | public GenericHashResult ComputeFileHash(string filePathToComputeHash, long offset = 0, long count = 0)
46 | {
47 | return base.ComputeFileHash(Enums.HashAlgorithm.SHA256, filePathToComputeHash, offset, count);
48 | }
49 |
50 | ///
51 | /// Verifies the SHA256 hash of an input byte array.
52 | ///
53 | /// The pre-computed SHA256 hash byte array.
54 | /// The input byte array to compute and verify the SHA256 hash.
55 | /// The offset into the byte array from wich to begin reading data.
56 | /// The number of bytes in the array to read after the offset.
57 | /// GenericHashResult
58 | public GenericHashResult VerifyHash(byte[] hashBytes, byte[] bytesToVerifyHash, int offset = 0, int count = 0)
59 | {
60 | return base.VerifyHash(Enums.HashAlgorithm.SHA256, hashBytes, bytesToVerifyHash, offset, count);
61 | }
62 |
63 | ///
64 | /// Verifies the SHA256 hash of an input string.
65 | ///
66 | /// The pre-computed SHA256 hash hexadecimal encoded string.
67 | /// The input string to compute and verify the SHA256 hash.
68 | /// The offset into the byte array from wich to begin reading data.
69 | /// The number of bytes in the array to read after the offset.
70 | /// GenericHashResult
71 | public GenericHashResult VerifyHash(string hashHexString, string stringToVerifyHash, int offset = 0, int count = 0)
72 | {
73 | return base.VerifyHash(Enums.HashAlgorithm.SHA256, hashHexString, stringToVerifyHash, offset, count);
74 | }
75 |
76 | ///
77 | /// Verifies the SHA256 of an input file.
78 | ///
79 | /// The pre-computed SHA256 hash hexadecimal encoded string.
80 | /// The input file path to compute and verify the SHA256 hash.
81 | /// The offset into the FileStream from wich to begin reading data.
82 | /// The number of bytes in the FileStream to read after the offset.
83 | /// GenericHashResult
84 | public GenericHashResult VerifyFileHash(string hashHexString, string filePathToVerifyHash, long offset = 0, long count = 0)
85 | {
86 | return base.VerifyFileHash(Enums.HashAlgorithm.SHA256, hashHexString, filePathToVerifyHash, offset, count);
87 | }
88 |
89 | ///
90 | /// Verifies the SHA256 of an input file.
91 | ///
92 | /// The pre-computed SHA256 hash byte array.
93 | /// The input file path to compute and verify the SHA256 hash.
94 | /// The offset into the FileStream from wich to begin reading data.
95 | /// The number of bytes in the FileStream to read after the offset.
96 | /// GenericHashResult
97 | public GenericHashResult VerifyFileHash(byte[] hashBytes, string filePathToVerifyHash, long offset = 0, long count = 0)
98 | {
99 | return base.VerifyFileHash(Enums.HashAlgorithm.SHA256, hashBytes, filePathToVerifyHash, offset, count);
100 | }
101 | }
102 | }
103 |
--------------------------------------------------------------------------------
/src/CryptHash.Net/lib/CryptHash.Net/Hash/SHA384.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Alessandro Cagliostro, 2021
3 | *
4 | * https://github.com/alecgn
5 | */
6 |
7 | using CryptHash.Net.Hash.Base;
8 | using CryptHash.Net.Hash.HashResults;
9 |
10 | namespace CryptHash.Net.Hash.Hash
11 | {
12 | public class SHA384 : HashBase
13 | {
14 | ///
15 | /// Computes the SHA384 hash of an input byte array.
16 | ///
17 | /// The input byte array to compute the SHA384 hash.
18 | /// The offset into the byte array from wich to begin reading data.
19 | /// The number of bytes in the array to read after the offset.
20 | /// GenericHashResult
21 | public GenericHashResult ComputeHash(byte[] bytesToComputeHash, int offset = 0, int count = 0)
22 | {
23 | return base.ComputeHash(Enums.HashAlgorithm.SHA384, bytesToComputeHash, offset, count);
24 | }
25 |
26 | ///
27 | /// Computes the SHA384 hash of an input string.
28 | ///
29 | /// The input string to compute the SHA384 hash.
30 | /// The offset into the byte array from wich to begin reading data.
31 | /// The number of bytes in the array to read after the offset.
32 | /// GenericHashResult
33 | public GenericHashResult ComputeHash(string stringToComputeHash, int offset = 0, int count = 0)
34 | {
35 | return base.ComputeHash(Enums.HashAlgorithm.SHA384, stringToComputeHash, offset, count);
36 | }
37 |
38 | ///
39 | /// Computes the SHA384 hash of an input file.
40 | ///
41 | /// The input file path to compute the SHA384 hash.
42 | /// The offset into the FileStream from wich to begin reading data.
43 | /// The number of bytes in the FileStream to read after the offset.
44 | /// GenericHashResult
45 | public GenericHashResult ComputeFileHash(string filePathToComputeHash, long offset = 0, long count = 0)
46 | {
47 | return base.ComputeFileHash(Enums.HashAlgorithm.SHA384, filePathToComputeHash, offset, count);
48 | }
49 |
50 | ///
51 | /// Verifies the SHA384 hash of an input byte array.
52 | ///
53 | /// The pre-computed SHA384 hash byte array.
54 | /// The input byte array to compute and verify the SHA384 hash.
55 | /// The offset into the byte array from wich to begin reading data.
56 | /// The number of bytes in the array to read after the offset.
57 | /// GenericHashResult
58 | public GenericHashResult VerifyHash(byte[] hashBytes, byte[] bytesToVerifyHash, int offset = 0, int count = 0)
59 | {
60 | return base.VerifyHash(Enums.HashAlgorithm.SHA384, hashBytes, bytesToVerifyHash, offset, count);
61 | }
62 |
63 | ///
64 | /// Verifies the SHA384 hash of an input string.
65 | ///
66 | /// The pre-computed SHA384 hash hexadecimal encoded string.
67 | /// The input string to compute and verify the SHA384 hash.
68 | /// The offset into the byte array from wich to begin reading data.
69 | /// The number of bytes in the array to read after the offset.
70 | /// GenericHashResult
71 | public GenericHashResult VerifyHash(string hashHexString, string stringToVerifyHash, int offset = 0, int count = 0)
72 | {
73 | return base.VerifyHash(Enums.HashAlgorithm.SHA384, hashHexString, stringToVerifyHash, offset, count);
74 | }
75 |
76 | ///
77 | /// Verifies the SHA384 of an input file.
78 | ///
79 | /// The pre-computed SHA384 hash hexadecimal encoded string.
80 | /// The input file path to compute and verify the SHA384 hash.
81 | /// The offset into the FileStream from wich to begin reading data.
82 | /// The number of bytes in the FileStream to read after the offset.
83 | /// GenericHashResult
84 | public GenericHashResult VerifyFileHash(string hashHexString, string filePathToVerifyHash, long offset = 0, long count = 0)
85 | {
86 | return base.VerifyFileHash(Enums.HashAlgorithm.SHA384, hashHexString, filePathToVerifyHash, offset, count);
87 | }
88 |
89 | ///
90 | /// Verifies the SHA384 of an input file.
91 | ///
92 | /// The pre-computed SHA384 hash byte array.
93 | /// The input file path to compute and verify the SHA384 hash.
94 | /// The offset into the FileStream from wich to begin reading data.
95 | /// The number of bytes in the FileStream to read after the offset.
96 | /// GenericHashResult
97 | public GenericHashResult VerifyFileHash(byte[] hashBytes, string filePathToVerifyHash, long offset = 0, long count = 0)
98 | {
99 | return base.VerifyFileHash(Enums.HashAlgorithm.SHA384, hashBytes, filePathToVerifyHash, offset, count);
100 | }
101 | }
102 | }
103 |
--------------------------------------------------------------------------------
/src/CryptHash.Net/lib/CryptHash.Net/Hash/SHA512.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Alessandro Cagliostro, 2021
3 | *
4 | * https://github.com/alecgn
5 | */
6 |
7 | using CryptHash.Net.Hash.Base;
8 | using CryptHash.Net.Hash.HashResults;
9 |
10 | namespace CryptHash.Net.Hash.Hash
11 | {
12 | public class SHA512 : HashBase
13 | {
14 | ///
15 | /// Computes the SHA512 hash of an input byte array.
16 | ///
17 | /// The input byte array to compute the SHA512 hash.
18 | /// The offset into the byte array from wich to begin reading data.
19 | /// The number of bytes in the array to read after the offset.
20 | /// GenericHashResult
21 | public GenericHashResult ComputeHash(byte[] bytesToComputeHash, int offset = 0, int count = 0)
22 | {
23 | return base.ComputeHash(Enums.HashAlgorithm.SHA512, bytesToComputeHash, offset, count);
24 | }
25 |
26 | ///
27 | /// Computes the SHA512 hash of an input string.
28 | ///
29 | /// The input string to compute the SHA512 hash.
30 | /// The offset into the byte array from wich to begin reading data.
31 | /// The number of bytes in the array to read after the offset.
32 | /// GenericHashResult
33 | public GenericHashResult ComputeHash(string stringToComputeHash, int offset = 0, int count = 0)
34 | {
35 | return base.ComputeHash(Enums.HashAlgorithm.SHA512, stringToComputeHash, offset, count);
36 | }
37 |
38 | ///
39 | /// Computes the SHA512 hash of an input file.
40 | ///
41 | /// The input file path to compute the SHA512 hash.
42 | /// The offset into the FileStream from wich to begin reading data.
43 | /// The number of bytes in the FileStream to read after the offset.
44 | /// GenericHashResult
45 | public GenericHashResult ComputeFileHash(string filePathToComputeHash, long offset = 0, long count = 0)
46 | {
47 | return base.ComputeFileHash(Enums.HashAlgorithm.SHA512, filePathToComputeHash, offset, count);
48 | }
49 |
50 | ///
51 | /// Verifies the SHA512 hash of an input byte array.
52 | ///
53 | /// The pre-computed SHA512 hash byte array.
54 | /// The input byte array to compute and verify the SHA512 hash.
55 | /// The offset into the byte array from wich to begin reading data.
56 | /// The number of bytes in the array to read after the offset.
57 | /// GenericHashResult
58 | public GenericHashResult VerifyHash(byte[] hashBytes, byte[] bytesToVerifyHash, int offset = 0, int count = 0)
59 | {
60 | return base.VerifyHash(Enums.HashAlgorithm.SHA512, hashBytes, bytesToVerifyHash, offset, count);
61 | }
62 |
63 | ///
64 | /// Verifies the SHA512 hash of an input string.
65 | ///
66 | /// The pre-computed SHA512 hash hexadecimal encoded string.
67 | /// The input string to compute and verify the SHA512 hash.
68 | /// The offset into the byte array from wich to begin reading data.
69 | /// The number of bytes in the array to read after the offset.
70 | /// GenericHashResult
71 | public GenericHashResult VerifyHash(string hashHexString, string stringToVerifyHash, int offset = 0, int count = 0)
72 | {
73 | return base.VerifyHash(Enums.HashAlgorithm.SHA512, hashHexString, stringToVerifyHash, offset, count);
74 | }
75 |
76 | ///
77 | /// Verifies the SHA512 of an input file.
78 | ///
79 | /// The pre-computed SHA512 hash hexadecimal encoded string.
80 | /// The input file path to compute and verify the SHA512 hash.
81 | /// The offset into the FileStream from wich to begin reading data.
82 | /// The number of bytes in the FileStream to read after the offset.
83 | /// GenericHashResult
84 | public GenericHashResult VerifyFileHash(string hashHexString, string filePathToVerifyHash, long offset = 0, long count = 0)
85 | {
86 | return base.VerifyFileHash(Enums.HashAlgorithm.SHA512, hashHexString, filePathToVerifyHash, offset, count);
87 | }
88 |
89 | ///
90 | /// Verifies the SHA512 of an input file.
91 | ///
92 | /// The pre-computed SHA512 hash byte array.
93 | /// The input file path to compute and verify the SHA512 hash.
94 | /// The offset into the FileStream from wich to begin reading data.
95 | /// The number of bytes in the FileStream to read after the offset.
96 | /// GenericHashResult
97 | public GenericHashResult VerifyFileHash(byte[] hashBytes, string filePathToVerifyHash, long offset = 0, long count = 0)
98 | {
99 | return base.VerifyFileHash(Enums.HashAlgorithm.SHA512, hashBytes, filePathToVerifyHash, offset, count);
100 | }
101 | }
102 | }
103 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # CryptHash.NET
2 |
3 | ### \*Warning\*: this repo is being migrated to [https://github.com/alecgn/AleCGN.Security.Cryptography](https://github.com/alecgn/AleCGN.Security.Cryptography)
4 | [](#)
5 | [](https://nuget.org/packages/CryptHash.Net)
6 | [](https://nuget.org/packages/CryptHash.Net)
7 | [](https://ci.appveyor.com/project/alecgn/crypthash-net)
8 |
9 | ### A .NET multi-target Library and .NET Core Console Application utility for encryption/decryption, hashing and encoding/decoding.
10 |
11 | The .NET Core console utility is designed to run in Windows, Linux and Mac, for text and files symmetric authenticated encryption/decryption, text/files hashing and text encoding/decoding. File checksum functionality is also available, you can calculate and verify the integrity of downloaded files from the internet with the source supplied hash.
12 | Both file encryption and hash (HMAC inclusive) have a progress event notifier for big files processing.
13 |
14 | The multi-target libray (.NET Standard 2.0/2.1) can be used in projects with any .NET implementation like **.NET Framework**, **.NET Core**, **Mono**, **Xamarin**, etc. Verify the .NET Standard compatibility table here: https://github.com/dotnet/standard/blob/master/docs/versions.md
15 |
16 | Currently symmetric encryption algorithms are:
17 | * **AES 128 bits** in **CBC Mode** with **HMACSHA256 Authentication**, using the **Encrypt-then-MAC (EtM)** strategy.
18 | * **AES 192 bits** in **CBC Mode** with **HMACSHA384 Authentication**, using the **Encrypt-then-MAC (EtM)** strategy.
19 | * **AES 256 bits** in **CBC Mode** with **HMACSHA384 Authentication**, using the **Encrypt-then-MAC (EtM)** strategy.
20 | * **AES 256 bits** in **CBC Mode** with **HMACSHA512 Authentication**, using the **Encrypt-then-MAC (EtM)** strategy.
21 | * **AES 128 bits** in **GCM Mode** with **Authentication** and **Associated Data** (**AEAD**).
22 | * **AES 192 bits** in **GCM Mode** with **Authentication** and **Associated Data** (**AEAD**).
23 | * **AES 256 bits** in **GCM Mode** with **Authentication** and **Associated Data** (**AEAD**).
24 |
25 | Currently supported hash/KDF algorithms are:
26 | * **MD5**
27 | * **SHA1**
28 | * **SHA256**
29 | * **SHA384**
30 | * **SHA512**
31 | * **HMAC-MD5**
32 | * **HMAC-SHA1**
33 | * **HMAC-SHA256**
34 | * **HMAC-SHA384**
35 | * **HMAC-SHA512**
36 | * **PBKDF2**
37 | * **BCrypt**
38 | * **Argon2id**
39 |
40 | Currently supported encoding types are:
41 | * **Base64**
42 | * **Hexadecimal**
43 |
44 | Other encryption/hashing/kdf/encoding algorithms will be implemented in the future.
45 |
46 | NuGet package: https://www.nuget.org/packages/CryptHash.Net
47 |
48 | Compiled console utility binaries (single file self-contained / no framework dependent) for Windows (x86/x64/ARM), Linux (x64/ARM -> Raspberry Pi) and Mac (x64): https://github.com/alecgn/crypthash-net/releases/tag/v3.6.0. When running on Linux or Mac, don't forget to navigate to the program's folder and "**chmod +x crypthash**". For usage help, call the program without patameters or pass the "--help" parameter.
49 |
50 | **WARNING:** PER SEMANTIC VERSIONING, THE ABOVE RELEASE (3.x.x) IS NOT COMPATIBLE WITH PREVIOUS RELEASES (1.x.x and 2.x.x), AND AS SUCH MIGHT NOT PROPERLY DECRYPT DATA YOU ENCRYPTED WITH PREVIOUS VERSIONS.
51 | From this version (3.x.x) onwards, any new implementations will be planned so as to maintain compatibility and stability. There should be no more breaking-changes, as the project's architecture and design are already well defined. If there is a need to make a breaking-change going forward then a method for properly decryting data you encryted with version 3.x.x will be provided.
52 |
53 | Publish it yourself using the following dotnet client command-line:
54 |
55 | >**dotnet publish -c Release -r \ /p:PublishSingleFile=true /p:PublishTrimmed=true**
56 | --------------------------------------------------
57 | **WINDOWS RIDs**
58 |
59 | **Portable**
60 | - win-x86
61 | - win-x64
62 |
63 | **Windows 7 / Windows Server 2008 R2**
64 | - win7-x64
65 | - win7-x86
66 |
67 | **Windows 8 / Windows Server 2012**
68 | - win8-x64
69 | - win8-x86
70 | - win8-arm
71 |
72 | **Windows 8.1 / Windows Server 2012 R2**
73 | - win81-x64
74 | - win81-x86
75 | - win81-arm
76 |
77 | **Windows 10 / Windows Server 2016**
78 | - win10-x64
79 | - win10-x86
80 | - win10-arm
81 | - win10-arm64
82 |
83 | --------------------------------------------------
84 |
85 | **LINUX RIDs**
86 |
87 | **ARM / Raspberry Pi (Raspbian)**
88 | - linux-arm
89 |
90 | **Portable**
91 | - linux-x64
92 |
93 | **CentOS**
94 | - centos-x64
95 | - centos.7-x64
96 |
97 | **Debian**
98 | - debian-x64
99 | - debian.8-x64
100 |
101 | **Fedora**
102 | - fedora-x64
103 | - fedora.24-x64
104 | - fedora.25-x64 (.NET Core 2.0 or later versions)
105 | - fedora.26-x64 (.NET Core 2.0 or later versions)
106 |
107 | **Gentoo (.NET Core 2.0 or later versions)**
108 | - gentoo-x64
109 |
110 | **openSUSE**
111 | - opensuse-x64
112 | - opensuse.42.1-x64
113 |
114 | **Oracle Linux**
115 | - ol-x64
116 | - ol.7-x64
117 | - ol.7.0-x64
118 | - ol.7.1-x64
119 | - ol.7.2-x64
120 |
121 | **Red Hat Enterprise Linux**
122 | - rhel-x64
123 | - rhel.6-x64 (.NET Core 2.0 or later versions)
124 | - rhel.7-x64
125 | - rhel.7.1-x64
126 | - rhel.7.2-x64
127 | - rhel.7.3-x64 (.NET Core 2.0 or later versions)
128 | - rhel.7.4-x64 (.NET Core 2.0 or later versions)
129 |
130 | **Tizen (.NET Core 2.0 or later versions)**
131 | - tizen
132 |
133 | **Ubuntu**
134 | - ubuntu-x64
135 | - ubuntu.14.04-x64
136 | - ubuntu.14.10-x64
137 | - ubuntu.15.04-x64
138 | - ubuntu.15.10-x64
139 | - ubuntu.16.04-x64
140 | - ubuntu.16.10-x64
141 |
142 | **Ubuntu derivatives**
143 | - linuxmint.17-x64
144 | - linuxmint.17.1-x64
145 | - linuxmint.17.2-x64
146 | - linuxmint.17.3-x64
147 | - linuxmint.18-x64
148 | - linuxmint.18.1-x64 (.NET Core 2.0 or later versions)
149 |
150 | --------------------------------------------------
151 |
152 | **macOS RIDs**
153 |
154 | **macOS RIDs use the older "OSX" branding.**
155 | - osx-x64 (.NET Core 2.0 or later versions, minimum version is osx.10.12-x64)
156 | - osx.10.10-x64
157 | - osx.10.11-x64
158 | - osx.10.12-x64 (.NET Core 1.1 or later versions)
159 | - osx.10.13-x64
160 |
161 | --------------------------------------------------
162 |
163 | **Complete RID LIST**
164 | (https://docs.microsoft.com/en-us/dotnet/core/rid-catalog)
165 |
--------------------------------------------------------------------------------
/src/CryptHash.Net/lib/CryptHash.Net/Hash/Base/PBKDF2Base.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Alessandro Cagliostro, 2021
3 | *
4 | * https://github.com/alecgn
5 | */
6 |
7 | //using CryptHash.Net.Util;
8 | //using CryptHash.Net.Hash.HashResults;
9 | //using Microsoft.AspNetCore.Cryptography.KeyDerivation;
10 | //using System;
11 |
12 | //namespace CryptHash.Net.Hash
13 | //{
14 | // public abstract class PBKDF2Base
15 | // {
16 | // private static readonly int _saltBitSize = 128;
17 | // private static readonly int _saltBytesLength = (_saltBitSize / 8);
18 |
19 | // private static readonly int _iterationsForKeyDerivationFunction = 100000;
20 |
21 | // internal PBKDF2HashResult ComputeHash(Enums.HMACAlgorithm hmacAlgorithm, string stringToComputeHash, byte[] salt = null,
22 | // int iterationCount = 0)
23 | // {
24 | // if (string.IsNullOrWhiteSpace(stringToComputeHash))
25 | // {
26 | // return new PBKDF2HashResult()
27 | // {
28 | // Success = false,
29 | // Message = MessageStrings.Hash_InputRequired
30 | // };
31 | // }
32 |
33 | // KeyDerivationPrf prf;
34 |
35 | // switch (hmacAlgorithm)
36 | // {
37 | // case Enums.HMACAlgorithm.HMACSHA1:
38 | // {
39 | // prf = KeyDerivationPrf.HMACSHA1;
40 | // }
41 | // break;
42 | // case Enums.HMACAlgorithm.HMACSHA256:
43 | // {
44 | // prf = KeyDerivationPrf.HMACSHA256;
45 | // }
46 | // break;
47 | // case Enums.HMACAlgorithm.HMACSHA512:
48 | // {
49 | // prf = KeyDerivationPrf.HMACSHA512;
50 | // }
51 | // break;
52 | // default:
53 | // {
54 | // return new PBKDF2HashResult()
55 | // {
56 | // Success = false,
57 | // Message = $"{MessageStrings.Common_AlgorithmNotSupported} \"{hmacAlgorithm.ToString()}\"."
58 | // };
59 | // }
60 | // }
61 |
62 | // //salt = salt ?? CommonMethods.GenerateSalt(_saltBytesLength);
63 | // salt = salt ?? CommonMethods.GenerateSalt();
64 | // iterationCount = (iterationCount == 0 ? _iterationsForKeyDerivationFunction : iterationCount);
65 | // byte[] hash;
66 |
67 | // try
68 | // {
69 | // hash = KeyDerivation.Pbkdf2(
70 | // password: stringToComputeHash,
71 | // salt: salt,
72 | // prf: prf,
73 | // iterationCount: iterationCount,
74 | // numBytesRequested: HMACOutputLengthDictionary.Instance[hmacAlgorithm]
75 | // );
76 |
77 | // var hashBytes = new byte[(_saltBytesLength + HMACOutputLengthDictionary.Instance[hmacAlgorithm])];
78 | // Array.Copy(salt, 0, hashBytes, 0, _saltBytesLength);
79 | // Array.Copy(hash, 0, hashBytes, _saltBytesLength, HMACOutputLengthDictionary.Instance[hmacAlgorithm]);
80 |
81 | // return new PBKDF2HashResult()
82 | // {
83 | // Success = true,
84 | // Message = MessageDictionary.Instance["Hash.Compute.Success"],
85 | // HashString = Convert.ToBase64String(hashBytes),
86 | // HashBytes = hashBytes,
87 | // Salt = salt,
88 | // PRF = hmacAlgorithm,
89 | // Iterations = iterationCount
90 | // };
91 | // }
92 | // catch (Exception ex)
93 | // {
94 | // return new PBKDF2HashResult()
95 | // {
96 | // Success = false,
97 | // Message = ex.ToString()
98 | // };
99 | // }
100 | // }
101 |
102 | // internal PBKDF2HashResult VerifyHash(Enums.HMACAlgorithm hmacAlgorithm, string stringToBeVerified, string hash,
103 | // int iterationCount = 0)
104 | // {
105 | // if (string.IsNullOrWhiteSpace(stringToBeVerified))
106 | // {
107 | // return new PBKDF2HashResult()
108 | // {
109 | // Success = false,
110 | // Message = MessageStrings.Hash_InputRequired
111 | // };
112 | // }
113 |
114 | // if (string.IsNullOrWhiteSpace(hash))
115 | // {
116 | // return new PBKDF2HashResult()
117 | // {
118 | // Success = false,
119 | // Message = MessageStrings.Hash_VerificationHashRequired
120 | // };
121 | // }
122 |
123 | // var hashWithSaltBytes = Convert.FromBase64String(hash);
124 |
125 | // if (hashWithSaltBytes.Length != (_saltBytesLength + HMACOutputLengthDictionary.Instance[hmacAlgorithm]))
126 | // {
127 | // return new PBKDF2HashResult()
128 | // {
129 | // Success = false,
130 | // Message = MessageStrings.Common_IncorrectInputLengthError
131 | // };
132 | // }
133 |
134 | // var saltBytes = new byte[_saltBytesLength];
135 | // Array.Copy(hashWithSaltBytes, 0, saltBytes, 0, _saltBytesLength);
136 |
137 | // var hashBytes = new byte[HMACOutputLengthDictionary.Instance[hmacAlgorithm]];
138 | // Array.Copy(hashWithSaltBytes, _saltBytesLength, hashBytes, 0, HMACOutputLengthDictionary.Instance[hmacAlgorithm]);
139 |
140 | // var result = ComputeHash(hmacAlgorithm, stringToBeVerified, saltBytes, iterationCount);
141 |
142 | // if (string.Equals(result.HashString, hash))
143 | // {
144 | // return new PBKDF2HashResult()
145 | // {
146 | // Success = true,
147 | // Message = MessageStrings.Hash_Match,
148 | // HashString = hash,
149 | // HashBytes = result.HashBytes,
150 | // PRF = hmacAlgorithm,
151 | // Salt = saltBytes
152 | // };
153 | // }
154 | // else
155 | // {
156 | // return new PBKDF2HashResult()
157 | // {
158 | // Success = false,
159 | // Message = MessageStrings.Hash_DoesNotMatch
160 | // };
161 | // }
162 | // }
163 | // }
164 | //}
165 |
--------------------------------------------------------------------------------
/src/CryptHash.Net/lib/CryptHash.Net/Hash/HMAC_MD5.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Alessandro Cagliostro, 2021
3 | *
4 | * https://github.com/alecgn
5 | */
6 |
7 | using CryptHash.Net.Hash.Base;
8 | using CryptHash.Net.Hash.HashResults;
9 |
10 | namespace CryptHash.Net.Hash
11 | {
12 | public class HMAC_MD5 : HMACBase
13 | {
14 | ///
15 | /// Computes the HMACMD5 of an input byte array using a key.
16 | ///
17 | /// The input byte array to compute the HMAC.
18 | /// The input key byte array. Leave empty or pass null to auto-generate a secure random key. The key will be present in the HMACHashResult return.
19 | /// The offset into the byte array from wich to begin reading data.
20 | /// The number of bytes in the array to read after the offset.
21 | /// HMACHashResult
22 | public HMACHashResult ComputeHMAC(byte[] bytesToComputeHMAC, byte[] key = null, int offset = 0, int count = 0)
23 | {
24 | return base.ComputeHMAC(Enums.HMACAlgorithm.HMACMD5, bytesToComputeHMAC, key, offset, count);
25 | }
26 |
27 | ///
28 | /// Computes the HMACMD5 of an input string using a key.
29 | ///
30 | /// The input string to compute the HMAC.
31 | /// The input key byte array. Leave empty or pass null to auto-generate a secure random key. The key will be present in the HMACHashResult return.
32 | /// The offset into the byte array from wich to begin reading data.
33 | /// The number of bytes in the array to read after the offset.
34 | /// HMACHashResult
35 | public HMACHashResult ComputeHMAC(string stringToComputeHMAC, byte[] key = null, int offset = 0, int count = 0)
36 | {
37 | return base.ComputeHMAC(Enums.HMACAlgorithm.HMACMD5, stringToComputeHMAC, key, offset, count);
38 | }
39 |
40 | ///
41 | /// Computes the HMACMD5 of an input file using a key.
42 | ///
43 | /// The input file path to compute the HMAC.
44 | /// The input key byte array. Leave empty or pass null to auto-generate a secure random key. The key will be present in the HMACHashResult return.
45 | /// The offset into the FileStream from wich to begin reading data.
46 | /// The number of bytes in the FileStream to read after the offset.
47 | /// HMACHashResult
48 | public HMACHashResult ComputeFileHMAC(string filePathToComputeHMAC, byte[] key = null, long offset = 0, long count = 0)
49 | {
50 | return base.ComputeFileHMAC(Enums.HMACAlgorithm.HMACMD5, filePathToComputeHMAC, key, offset, count);
51 | }
52 |
53 |
54 | ///
55 | /// Verifies the HMACMD5 of an input byte array using a key.
56 | ///
57 | /// The pre-computed HMACMD5 byte array.
58 | /// The input byte array to compute and verify the HMACMD5.
59 | /// The input key byte array.
60 | /// The offset into the byte array from wich to begin reading data.
61 | /// The number of bytes in the array to read after the offset.
62 | /// HMACHashResult
63 | public HMACHashResult VerifyHMAC(byte[] hmacBytes, byte[] bytesToVerifyHMAC, byte[] key, int offset = 0, int count = 0)
64 | {
65 | return base.VerifyHMAC(Enums.HMACAlgorithm.HMACMD5, hmacBytes, bytesToVerifyHMAC, key, offset, count);
66 | }
67 |
68 | ///
69 | /// Verifies the HMACMD5 of an input string using a key.
70 | ///
71 | /// The pre-computed HMACMD5 hexadecimal encoded string.
72 | /// The input string to compute and verify the HMACMD5.
73 | /// The input key byte array.
74 | /// The offset into the byte array from wich to begin reading data.
75 | /// The number of bytes in the array to read after the offset.
76 | /// HMACHashResult
77 | public HMACHashResult VerifyHMAC(string hmacHexString, string stringToVerifyHMAC, byte[] key, int offset = 0, int count = 0)
78 | {
79 | return base.VerifyHMAC(Enums.HMACAlgorithm.HMACMD5, hmacHexString, stringToVerifyHMAC, key, offset, count);
80 | }
81 |
82 | ///
83 | /// Verifies the HMACMD5 of an input file using a key.
84 | ///
85 | /// The pre-computed HMACMD5 hexadecimal encoded string.
86 | /// The input file path to compute and verify the HMACMD5.
87 | /// The input key byte array.
88 | /// The offset into the FileStream from wich to begin reading data.
89 | /// The number of bytes in the FileStream to read after the offset.
90 | /// HMACHashResult
91 | public HMACHashResult VerifyFileHMAC(string hmacHexString, string filePathToVerifyHMAC, byte[] key, long offset = 0, long count = 0)
92 | {
93 | return base.VerifyFileHMAC(Enums.HMACAlgorithm.HMACMD5, hmacHexString, filePathToVerifyHMAC, key, offset, count);
94 | }
95 |
96 | ///
97 | /// Verifies the HMACMD5 of an input file using a key.
98 | ///
99 | /// The pre-computed HMACMD5 byte array.
100 | /// The input file path to compute and verify the HMACMD5.
101 | /// The input key byte array.
102 | /// The offset into the FileStream from wich to begin reading data.
103 | /// The number of bytes in the FileStream to read after the offset.
104 | /// HMACHashResult
105 | public HMACHashResult VerifyFileHMAC(byte[] hmacBytes, string filePathToVerifyHMAC, byte[] key, long offset = 0, long count = 0)
106 | {
107 | return base.VerifyFileHMAC(Enums.HMACAlgorithm.HMACMD5, hmacBytes, filePathToVerifyHMAC, key, offset, count);
108 | }
109 | }
110 | }
--------------------------------------------------------------------------------
/src/CryptHash.Net/lib/CryptHash.Net/Hash/HMAC_SHA_1.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Alessandro Cagliostro, 2021
3 | *
4 | * https://github.com/alecgn
5 | */
6 |
7 | using CryptHash.Net.Hash.Base;
8 | using CryptHash.Net.Hash.HashResults;
9 |
10 | namespace CryptHash.Net.Hash
11 | {
12 | public class HMAC_SHA_1 : HMACBase
13 | {
14 | ///
15 | /// Computes the HMACSHA1 of an input byte array using a key.
16 | ///
17 | /// The input byte array to compute the HMACSHA1.
18 | /// The input key byte array. Leave empty or pass null to auto-generate a secure random key. The key will be present in the HMACHashResult return.
19 | /// The offset into the byte array from wich to begin reading data.
20 | /// The number of bytes in the array to read after the offset.
21 | /// HMACHashResult
22 | public HMACHashResult ComputeHMAC(byte[] bytesToComputeHMAC, byte[] key = null, int offset = 0, int count = 0)
23 | {
24 | return base.ComputeHMAC(Enums.HMACAlgorithm.HMACSHA1, bytesToComputeHMAC, key, offset, count);
25 | }
26 |
27 | ///
28 | /// Computes the HMACSHA1 of an input string using a key.
29 | ///
30 | /// The input string to compute the HMACSHA1.
31 | /// The input key byte array. Leave empty or pass null to auto-generate a secure random key. The key will be present in the HMACHashResult return.
32 | /// The offset into the byte array from wich to begin reading data.
33 | /// The number of bytes in the array to read after the offset.
34 | /// HMACHashResult
35 | public HMACHashResult ComputeHMAC(string stringToComputeHMAC, byte[] key = null, int offset = 0, int count = 0)
36 | {
37 | return base.ComputeHMAC(Enums.HMACAlgorithm.HMACSHA1, stringToComputeHMAC, key, offset, count);
38 | }
39 |
40 | ///
41 | /// Computes the HMACSHA1 of an input file using a key.
42 | ///
43 | /// The input file path to compute the HMACSHA1.
44 | /// The input key byte array. Leave empty or pass null to auto-generate a secure random key. The key will be present in the HMACHashResult return.
45 | /// The offset into the FileStream from wich to begin reading data.
46 | /// The number of bytes in the FileStream to read after the offset.
47 | /// HMACHashResult
48 | public HMACHashResult ComputeFileHMAC(string filePathToComputeHMAC, byte[] key = null, long offset = 0, long count = 0)
49 | {
50 | return base.ComputeFileHMAC(Enums.HMACAlgorithm.HMACSHA1, filePathToComputeHMAC, key, offset, count);
51 | }
52 |
53 |
54 | ///
55 | /// Verifies the HMACSHA1 of an input byte array using a key.
56 | ///
57 | /// The pre-computed HMACSHA1 byte array.
58 | /// The input byte array to compute and verify the HMACSHA1.
59 | /// The input key byte array.
60 | /// The offset into the byte array from wich to begin reading data.
61 | /// The number of bytes in the array to read after the offset.
62 | /// HMACHashResult
63 | public HMACHashResult VerifyHMAC(byte[] hmacBytes, byte[] bytesToVerifyHMAC, byte[] key, int offset = 0, int count = 0)
64 | {
65 | return base.VerifyHMAC(Enums.HMACAlgorithm.HMACSHA1, hmacBytes, bytesToVerifyHMAC, key, offset, count);
66 | }
67 |
68 | ///
69 | /// Verifies the HMACSHA1 of an input string using a key.
70 | ///
71 | /// The pre-computed HMACSHA1 hexadecimal encoded string.
72 | /// The input string to compute and verify the HMACSHA1.
73 | /// The input key byte array.
74 | /// The offset into the byte array from wich to begin reading data.
75 | /// The number of bytes in the array to read after the offset.
76 | /// HMACHashResult
77 | public HMACHashResult VerifyHMAC(string hmacHexString, string stringToVerifyHMAC, byte[] key, int offset = 0, int count = 0)
78 | {
79 | return base.VerifyHMAC(Enums.HMACAlgorithm.HMACSHA1, hmacHexString, stringToVerifyHMAC, key, offset, count);
80 | }
81 |
82 | ///
83 | /// Verifies the HMACSHA1 of an input file using a key.
84 | ///
85 | /// The pre-computed HMACSHA1 hexadecimal encoded string.
86 | /// The input file path to compute and verify the HMACSHA1.
87 | /// The input key byte array.
88 | /// The offset into the FileStream from wich to begin reading data.
89 | /// The number of bytes in the FileStream to read after the offset.
90 | /// HMACHashResult
91 | public HMACHashResult VerifyFileHMAC(string hmacHexString, string sourceFilePath, byte[] key, long offset = 0, long count = 0)
92 | {
93 | return base.VerifyFileHMAC(Enums.HMACAlgorithm.HMACSHA1, hmacHexString, sourceFilePath, key, offset, count);
94 | }
95 |
96 | ///
97 | /// Verifies the HMACSHA1 of an input file using a key.
98 | ///
99 | /// The pre-computed HMACSHA1 byte array.
100 | /// The input file path to compute and verify the HMACSHA1.
101 | /// The input key byte array.
102 | /// The offset into the FileStream from wich to begin reading data.
103 | /// The number of bytes in the FileStream to read after the offset.
104 | /// HMACHashResult
105 | public HMACHashResult VerifyFileHMAC(byte[] hmacBytes, string sourceFilePath, byte[] key, long offset = 0, long count = 0)
106 | {
107 | return base.VerifyFileHMAC(Enums.HMACAlgorithm.HMACSHA1, hmacBytes, sourceFilePath, key, offset, count);
108 | }
109 | }
110 | }
--------------------------------------------------------------------------------
/src/CryptHash.Net/lib/CryptHash.Net/Hash/BCrypt.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Alessandro Cagliostro, 2021
3 | *
4 | * https://github.com/alecgn
5 | */
6 |
7 | using CryptHash.Net.Hash.HashResults;
8 | using CryptHash.Net.Resources;
9 | using System;
10 | using BCryptNet = BCrypt.Net;
11 |
12 | namespace CryptHash.Net.Hash
13 | {
14 | public class BCrypt
15 | {
16 | public GenericHashResult ComputeHash(string stringToComputeHash)
17 | {
18 | if (string.IsNullOrWhiteSpace(stringToComputeHash))
19 | {
20 | return new GenericHashResult()
21 | {
22 | Success = false,
23 | Message = MessageStrings.Hash_InputRequired
24 | };
25 | }
26 |
27 | try
28 | {
29 | var hashedString = BCryptNet.BCrypt.HashPassword(stringToComputeHash);
30 |
31 | return new GenericHashResult()
32 | {
33 | Success = true,
34 | Message = MessageStrings.Hash_ComputeSuccess,
35 | HashString = hashedString
36 | };
37 | }
38 | catch (Exception ex)
39 | {
40 | return new GenericHashResult()
41 | {
42 | Success = false,
43 | Message = ex.ToString()
44 | };
45 | }
46 | }
47 |
48 | public GenericHashResult ComputeHash(string stringToComputeHash, string salt)
49 | {
50 | if (string.IsNullOrWhiteSpace(stringToComputeHash))
51 | {
52 | return new GenericHashResult()
53 | {
54 | Success = false,
55 | Message = MessageStrings.Hash_InputRequired
56 | };
57 | }
58 |
59 | try
60 | {
61 | var hashedString = BCryptNet.BCrypt.HashPassword(stringToComputeHash, salt);
62 |
63 | return new GenericHashResult()
64 | {
65 | Success = true,
66 | Message = MessageStrings.Hash_ComputeSuccess,
67 | HashString = hashedString
68 | };
69 | }
70 | catch (Exception ex)
71 | {
72 | return new GenericHashResult()
73 | {
74 | Success = false,
75 | Message = ex.ToString()
76 | };
77 | }
78 | }
79 |
80 | public GenericHashResult ComputeHash(string stringToComputeHash, string salt, bool enhancedEntropy, BCryptNet.HashType hashType = BCryptNet.HashType.SHA384)
81 | {
82 | if (string.IsNullOrWhiteSpace(stringToComputeHash))
83 | {
84 | return new GenericHashResult()
85 | {
86 | Success = false,
87 | Message = MessageStrings.Hash_InputRequired
88 | };
89 | }
90 |
91 | try
92 | {
93 | var hashedString = BCryptNet.BCrypt.HashPassword(stringToComputeHash, salt, enhancedEntropy, hashType);
94 |
95 | return new GenericHashResult()
96 | {
97 | Success = true,
98 | Message = MessageStrings.Hash_ComputeSuccess,
99 | HashString = hashedString
100 | };
101 | }
102 | catch (Exception ex)
103 | {
104 | return new GenericHashResult()
105 | {
106 | Success = false,
107 | Message = ex.ToString()
108 | };
109 | }
110 | }
111 |
112 | public GenericHashResult ComputeHash(string stringToComputeHash, int workFactor, bool enhancedEntropy = false)
113 | {
114 | if (string.IsNullOrWhiteSpace(stringToComputeHash))
115 | {
116 | return new GenericHashResult()
117 | {
118 | Success = false,
119 | Message = MessageStrings.Hash_InputRequired
120 | };
121 | }
122 |
123 | try
124 | {
125 | var hashedString = BCryptNet.BCrypt.HashPassword(stringToComputeHash, workFactor, enhancedEntropy);
126 |
127 | return new GenericHashResult()
128 | {
129 | Success = true,
130 | Message = MessageStrings.Hash_ComputeSuccess,
131 | HashString = hashedString
132 | };
133 | }
134 | catch (Exception ex)
135 | {
136 | return new GenericHashResult()
137 | {
138 | Success = false,
139 | Message = ex.ToString()
140 | };
141 | }
142 | }
143 |
144 | public GenericHashResult VerifyHash(string stringToComputeHash, string hash, bool enhancedEntropy = false, BCryptNet.HashType hashType = BCryptNet.HashType.SHA384)
145 | {
146 | if (string.IsNullOrWhiteSpace(stringToComputeHash))
147 | {
148 | return new GenericHashResult()
149 | {
150 | Success = false,
151 | Message = MessageStrings.Hash_InputRequired
152 | };
153 | }
154 |
155 | if (string.IsNullOrWhiteSpace(hash))
156 | {
157 | return new GenericHashResult()
158 | {
159 | Success = false,
160 | Message = MessageStrings.Hash_VerificationHashRequired
161 | };
162 | }
163 |
164 | try
165 | {
166 | var match = BCryptNet.BCrypt.Verify(stringToComputeHash, hash, enhancedEntropy, hashType);
167 |
168 | if (match)
169 | {
170 | return new GenericHashResult()
171 | {
172 | Success = true,
173 | Message = MessageStrings.Hash_Match,
174 | HashString = hash
175 | };
176 | }
177 | else
178 | {
179 | return new GenericHashResult()
180 | {
181 | Success = false,
182 | Message = MessageStrings.Hash_DoesNotMatch
183 | };
184 | }
185 | }
186 | catch (Exception ex)
187 | {
188 | return new GenericHashResult()
189 | {
190 | Success = false,
191 | Message = ex.ToString()
192 | };
193 | }
194 | }
195 | }
196 | }
197 |
--------------------------------------------------------------------------------
/src/CryptHash.Net/lib/CryptHash.Net/Hash/HMAC_SHA_256.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Alessandro Cagliostro, 2021
3 | *
4 | * https://github.com/alecgn
5 | */
6 |
7 | using CryptHash.Net.Hash.Base;
8 | using CryptHash.Net.Hash.HashResults;
9 |
10 | namespace CryptHash.Net.Hash
11 | {
12 | public class HMAC_SHA_256 : HMACBase
13 | {
14 | ///
15 | /// Computes the HMACSHA256 of an input byte array using a key.
16 | ///
17 | /// The input byte array to compute the HMACSHA256.
18 | /// The input key byte array. Leave empty or pass null to auto-generate a secure random key. The key will be present in the HMACHashResult return.
19 | /// The offset into the byte array from wich to begin reading data.
20 | /// The number of bytes in the array to read after the offset.
21 | /// HMACHashResult
22 | public HMACHashResult ComputeHMAC(byte[] bytesToComputeHMAC, byte[] key = null, int offset = 0, int count = 0)
23 | {
24 | return base.ComputeHMAC(Enums.HMACAlgorithm.HMACSHA256, bytesToComputeHMAC, key, offset, count);
25 | }
26 |
27 | ///
28 | /// Computes the HMACSHA256 of an input string using a key.
29 | ///
30 | /// The input string to compute the HMACSHA256.
31 | /// The input key byte array. Leave empty or pass null to auto-generate a secure random key. The key will be present in the HMACHashResult return.
32 | /// The offset into the byte array from wich to begin reading data.
33 | /// The number of bytes in the array to read after the offset.
34 | /// HMACHashResult
35 | public HMACHashResult ComputeHMAC(string stringToComputeHMAC, byte[] key = null, int offset = 0, int count = 0)
36 | {
37 | return base.ComputeHMAC(Enums.HMACAlgorithm.HMACSHA256, stringToComputeHMAC, key, offset, count);
38 | }
39 |
40 | ///
41 | /// Computes the HMACSHA256 of an input file using a key.
42 | ///
43 | /// The input file path to compute the HMACSHA256.
44 | /// The input key byte array. Leave empty or pass null to auto-generate a secure random key. The key will be present in the HMACHashResult return.
45 | /// The offset into the FileStream from wich to begin reading data.
46 | /// The number of bytes in the FileStream to read after the offset.
47 | /// HMACHashResult
48 | public HMACHashResult ComputeFileHMAC(string filePathToComputeHMAC, byte[] key = null, long offset = 0, long count = 0)
49 | {
50 | return base.ComputeFileHMAC(Enums.HMACAlgorithm.HMACSHA256, filePathToComputeHMAC, key, offset, count);
51 | }
52 |
53 | ///
54 | /// Verifies the HMACSHA256 of an input byte array using a key.
55 | ///
56 | /// The pre-computed HMACSHA256 byte array.
57 | /// The input byte array to compute and verify the HMACSHA256.
58 | /// The input key byte array.
59 | /// The offset into the byte array from wich to begin reading data.
60 | /// The number of bytes in the array to read after the offset.
61 | /// HMACHashResult
62 | public HMACHashResult VerifyHMAC(byte[] hmacBytes, byte[] bytesToVerifyHMAC, byte[] key, int offset = 0, int count = 0)
63 | {
64 | return base.VerifyHMAC(Enums.HMACAlgorithm.HMACSHA256, hmacBytes, bytesToVerifyHMAC, key, offset, count);
65 | }
66 |
67 | ///
68 | /// Verifies the HMACSHA256 of an input string using a key.
69 | ///
70 | /// The pre-computed HMACSHA256 hexadecimal encoded string.
71 | /// The input string to compute and verify the HMACSHA256.
72 | /// The input key byte array.
73 | /// The offset into the byte array from wich to begin reading data.
74 | /// The number of bytes in the array to read after the offset.
75 | /// HMACHashResult
76 | public HMACHashResult VerifyHMAC(string hmacHexString, string stringToVerifyHMAC, byte[] key, int offset = 0, int count = 0)
77 | {
78 | return base.VerifyHMAC(Enums.HMACAlgorithm.HMACSHA256, hmacHexString, stringToVerifyHMAC, key, offset, count);
79 | }
80 |
81 | ///
82 | /// Verifies the HMACSHA256 of an input file using a key.
83 | ///
84 | /// The pre-computed HMACSHA256 hexadecimal encoded string.
85 | /// The input file path to compute and verify the HMACSHA256.
86 | /// The input key byte array.
87 | /// The offset into the FileStream from wich to begin reading data.
88 | /// The number of bytes in the FileStream to read after the offset.
89 | /// HMACHashResult
90 | public HMACHashResult VerifyFileHMAC(string hmacHexString, string sourceFilePath, byte[] key, long offset = 0, long count = 0)
91 | {
92 | return base.VerifyFileHMAC(Enums.HMACAlgorithm.HMACSHA256, hmacHexString, sourceFilePath, key, offset, count);
93 | }
94 |
95 | ///
96 | /// Verifies the HMACSHA256 of an input file using a key.
97 | ///
98 | /// The pre-computed HMACSHA256 byte array.
99 | /// The input file path to compute and verify the HMACSHA256.
100 | /// The input key byte array.
101 | /// The offset into the FileStream from wich to begin reading data.
102 | /// The number of bytes in the FileStream to read after the offset.
103 | /// HMACHashResult
104 | public HMACHashResult VerifyFileHMAC(byte[] hmacBytes, string sourceFilePath, byte[] key, long offset = 0, long count = 0)
105 | {
106 | return base.VerifyFileHMAC(Enums.HMACAlgorithm.HMACSHA256, hmacBytes, sourceFilePath, key, offset, count);
107 | }
108 | }
109 | }
--------------------------------------------------------------------------------
/src/CryptHash.Net/lib/CryptHash.Net/Hash/HMAC_SHA_512.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Alessandro Cagliostro, 2021
3 | *
4 | * https://github.com/alecgn
5 | */
6 |
7 | using CryptHash.Net.Hash.Base;
8 | using CryptHash.Net.Hash.HashResults;
9 |
10 | namespace CryptHash.Net.Hash
11 | {
12 | public class HMAC_SHA_512 : HMACBase
13 | {
14 | ///
15 | /// Computes the HMACSHA512 of an input byte array using a key.
16 | ///
17 | /// The input byte array to compute the HMACSHA512.
18 | /// The input key byte array. Leave empty or pass null to auto-generate a secure random key. The key will be present in the HMACHashResult return.
19 | /// The offset into the byte array from wich to begin reading data.
20 | /// The number of bytes in the array to read after the offset.
21 | /// HMACHashResult
22 | public HMACHashResult ComputeHMAC(byte[] bytesToComputeHMAC, byte[] key = null, int offset = 0, int count = 0)
23 | {
24 | return base.ComputeHMAC(Enums.HMACAlgorithm.HMACSHA512, bytesToComputeHMAC, key, offset, count);
25 | }
26 |
27 | ///
28 | /// Computes the HMACSHA512 of an input string using a key.
29 | ///
30 | /// The input string to compute the HMACSHA512.
31 | /// The input key byte array. Leave empty or pass null to auto-generate a secure random key. The key will be present in the HMACHashResult return.
32 | /// The offset into the byte array from wich to begin reading data.
33 | /// The number of bytes in the array to read after the offset.
34 | /// HMACHashResult
35 | public HMACHashResult ComputeHMAC(string stringToComputeHMAC, byte[] key = null, int offset = 0, int count = 0)
36 | {
37 | return base.ComputeHMAC(Enums.HMACAlgorithm.HMACSHA512, stringToComputeHMAC, key, offset, count);
38 | }
39 |
40 | ///
41 | /// Computes the HMACSHA512 of an input file using a key.
42 | ///
43 | /// The input file path to compute the HMACSHA512.
44 | /// The input key byte array. Leave empty or pass null to auto-generate a secure random key. The key will be present in the HMACHashResult return.
45 | /// The offset into the FileStream from wich to begin reading data.
46 | /// The number of bytes in the FileStream to read after the offset.
47 | /// HMACHashResult
48 | public HMACHashResult ComputeFileHMAC(string filePathToComputeHMAC, byte[] key = null, long offset = 0, long count = 0)
49 | {
50 | return base.ComputeFileHMAC(Enums.HMACAlgorithm.HMACSHA512, filePathToComputeHMAC, key, offset, count);
51 | }
52 |
53 | ///
54 | /// Verifies the HMACSHA512 of an input byte array using a key.
55 | ///
56 | /// The pre-computed HMACSHA512 byte array.
57 | /// The input byte array to compute and verify the HMACSHA512.
58 | /// The input key byte array.
59 | /// The offset into the byte array from wich to begin reading data.
60 | /// The number of bytes in the array to read after the offset.
61 | /// HMACHashResult
62 | public HMACHashResult VerifyHMAC(byte[] hmacBytes, byte[] bytesToVerifyHMAC, byte[] key, int offset = 0, int count = 0)
63 | {
64 | return base.VerifyHMAC(Enums.HMACAlgorithm.HMACSHA512, hmacBytes, bytesToVerifyHMAC, key, offset, count);
65 | }
66 |
67 | ///
68 | /// Verifies the HMACSHA512 of an input string using a key.
69 | ///
70 | /// The pre-computed HMACSHA512 hexadecimal encoded string.
71 | /// The input string to compute and verify the HMACSHA512.
72 | /// The input key byte array.
73 | /// The offset into the byte array from wich to begin reading data.
74 | /// The number of bytes in the array to read after the offset.
75 | /// HMACHashResult
76 | public HMACHashResult VerifyHMAC(string hmacHexString, string stringToVerifyHMAC, byte[] key, int offset = 0, int count = 0)
77 | {
78 | return base.VerifyHMAC(Enums.HMACAlgorithm.HMACSHA512, hmacHexString, stringToVerifyHMAC, key, offset, count);
79 | }
80 |
81 | ///
82 | /// Verifies the HMACSHA512 of an input file using a key.
83 | ///
84 | /// The pre-computed HMACSHA512 hexadecimal encoded string.
85 | /// The input file path to compute and verify the HMACSHA512.
86 | /// The input key byte array.
87 | /// The offset into the FileStream from wich to begin reading data.
88 | /// The number of bytes in the FileStream to read after the offset.
89 | /// HMACHashResult
90 | public HMACHashResult VerifyFileHMAC(string hmacHexString, string filePathToVerifyHMAC, byte[] key, long offset = 0, long count = 0)
91 | {
92 | return base.VerifyFileHMAC(Enums.HMACAlgorithm.HMACSHA512, hmacHexString, filePathToVerifyHMAC, key, offset, count);
93 | }
94 |
95 | ///
96 | /// Verifies the HMACSHA512 of an input file using a key.
97 | ///
98 | /// The pre-computed HMACSHA512 byte array.
99 | /// The input file path to compute and verify the HMACSHA512.
100 | /// The input key byte array.
101 | /// The offset into the FileStream from wich to begin reading data.
102 | /// The number of bytes in the FileStream to read after the offset.
103 | /// HMACHashResult
104 | public HMACHashResult VerifyFileHMAC(byte[] hmacBytes, string sourceFilePath, byte[] key, long offset = 0, long count = 0)
105 | {
106 | return base.VerifyFileHMAC(Enums.HMACAlgorithm.HMACSHA512, hmacBytes, sourceFilePath, key, offset, count);
107 | }
108 | }
109 | }
--------------------------------------------------------------------------------
/src/CryptHash.Net/lib/CryptHash.Net/Hash/HMAC_SHA_384.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Alessandro Cagliostro, 2021
3 | *
4 | * https://github.com/alecgn
5 | */
6 |
7 | using CryptHash.Net.Hash.Base;
8 | using CryptHash.Net.Hash.HashResults;
9 |
10 | namespace CryptHash.Net.Hash
11 | {
12 | public class HMAC_SHA_384 : HMACBase
13 | {
14 | ///
15 | /// Computes the HMACSHA384 of an input byte array using a key.
16 | ///
17 | /// The input byte array to compute the HMACSHA384.
18 | /// The input key byte array. Leave empty or pass null to auto-generate a secure random key. The key will be present in the HMACHashResult return.
19 | /// The offset into the byte array from wich to begin reading data.
20 | /// The number of bytes in the array to read after the offset.
21 | /// HMACHashResult
22 | public HMACHashResult ComputeHMAC(byte[] bytesToComputeHMAC, byte[] key = null, int offset = 0, int count = 0)
23 | {
24 | return base.ComputeHMAC(Enums.HMACAlgorithm.HMACSHA384, bytesToComputeHMAC, key, offset, count);
25 | }
26 |
27 | ///
28 | /// Computes the HMACSHA384 of an input string using a key.
29 | ///
30 | /// The input string to compute the HMACSHA384.
31 | /// The input key byte array. Leave empty or pass null to auto-generate a secure random key. The key will be present in the HMACHashResult return.
32 | /// The offset into the byte array from wich to begin reading data.
33 | /// The number of bytes in the array to read after the offset.
34 | /// HMACHashResult
35 | public HMACHashResult ComputeHMAC(string stringToComputeHMAC, byte[] key = null, int offset = 0, int count = 0)
36 | {
37 | return base.ComputeHMAC(Enums.HMACAlgorithm.HMACSHA384, stringToComputeHMAC, key, offset, count);
38 | }
39 |
40 | ///
41 | /// Computes the HMACSHA384 of an input file using a key.
42 | ///
43 | /// The input file path to compute the HMACSHA384.
44 | /// The input key byte array. Leave empty or pass null to auto-generate a secure random key. The key will be present in the HMACHashResult return.
45 | /// The offset into the FileStream from wich to begin reading data.
46 | /// The number of bytes in the FileStream to read after the offset.
47 | /// HMACHashResult
48 | public HMACHashResult ComputeFileHMAC(string filePathToComputeHMAC, byte[] key = null, long offset = 0, long count = 0)
49 | {
50 | return base.ComputeFileHMAC(Enums.HMACAlgorithm.HMACSHA384, filePathToComputeHMAC, key, offset, count);
51 | }
52 |
53 | ///
54 | /// Verifies the HMACSHA384 of an input byte array using a key.
55 | ///
56 | /// The pre-computed HMACSHA384 byte array.
57 | /// The input byte array to compute and verify the HMACSHA384.
58 | /// The input key byte array.
59 | /// The offset into the byte array from wich to begin reading data.
60 | /// The number of bytes in the array to read after the offset.
61 | /// HMACHashResult
62 | public HMACHashResult VerifyHMAC(byte[] hmacBytes, byte[] bytesToVerifyHMAC, byte[] key, int offset = 0, int count = 0)
63 | {
64 | return base.VerifyHMAC(Enums.HMACAlgorithm.HMACSHA384, hmacBytes, bytesToVerifyHMAC, key, offset, count);
65 | }
66 |
67 | ///
68 | /// Verifies the HMACSHA384 of an input string using a key.
69 | ///
70 | /// The pre-computed HMACSHA384 hexadecimal encoded string.
71 | /// The input string to compute and verify the HMACSHA384.
72 | /// The input key byte array.
73 | /// The offset into the byte array from wich to begin reading data.
74 | /// The number of bytes in the array to read after the offset.
75 | /// HMACHashResult
76 | public HMACHashResult VerifyHMAC(string hmacHexString, string stringToVerifyHMAC, byte[] key, int offset = 0, int count = 0)
77 | {
78 | return base.VerifyHMAC(Enums.HMACAlgorithm.HMACSHA384, hmacHexString, stringToVerifyHMAC, key, offset, count);
79 | }
80 |
81 | ///
82 | /// Verifies the HMACSHA384 of an input file using a key.
83 | ///
84 | /// The pre-computed HMACSHA384 hexadecimal encoded string.
85 | /// The input file path to compute and verify the HMACSHA384.
86 | /// The input key byte array.
87 | /// The offset into the FileStream from wich to begin reading data.
88 | /// The number of bytes in the FileStream to read after the offset.
89 | /// HMACHashResult
90 | public HMACHashResult VerifyFileHMAC(string hmacHexString, string filePathToVerifyHMAC, byte[] key, long offset = 0, long count = 0)
91 | {
92 | return base.VerifyFileHMAC(Enums.HMACAlgorithm.HMACSHA384, hmacHexString, filePathToVerifyHMAC, key, offset, count);
93 | }
94 |
95 | ///
96 | /// Verifies the HMACSHA384 of an input file using a key.
97 | ///
98 | /// The pre-computed HMACSHA384 byte array.
99 | /// The input file path to compute and verify the HMACSHA384.
100 | /// The input key byte array.
101 | /// The offset into the FileStream from wich to begin reading data.
102 | /// The number of bytes in the FileStream to read after the offset.
103 | /// HMACHashResult
104 | public HMACHashResult VerifyFileHMAC(byte[] hmacBytes, string filePathToVerifyHMAC, byte[] key, long offset = 0, long count = 0)
105 | {
106 | return base.VerifyFileHMAC(Enums.HMACAlgorithm.HMACSHA384, hmacBytes, filePathToVerifyHMAC, key, offset, count);
107 | }
108 | }
109 | }
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | ## Ignore Visual Studio temporary files, build results, and
2 | ## files generated by popular Visual Studio add-ons.
3 | ##
4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
5 |
6 | # User-specific files
7 | *.rsuser
8 | *.suo
9 | *.user
10 | *.userosscache
11 | *.sln.docstates
12 |
13 | # User-specific files (MonoDevelop/Xamarin Studio)
14 | *.userprefs
15 |
16 | # Build results
17 | [Dd]ebug/
18 | [Dd]ebugPublic/
19 | [Rr]elease/
20 | [Rr]eleases/
21 | x64/
22 | x86/
23 | [Aa][Rr][Mm]/
24 | [Aa][Rr][Mm]64/
25 | bld/
26 | [Bb]in/
27 | [Oo]bj/
28 | [Ll]og/
29 |
30 | # Visual Studio 2015/2017 cache/options directory
31 | .vs/
32 | # Uncomment if you have tasks that create the project's static files in wwwroot
33 | #wwwroot/
34 |
35 | # Visual Studio 2017 auto generated files
36 | Generated\ Files/
37 |
38 | # MSTest test Results
39 | [Tt]est[Rr]esult*/
40 | [Bb]uild[Ll]og.*
41 |
42 | # NUNIT
43 | *.VisualState.xml
44 | TestResult.xml
45 |
46 | # Build Results of an ATL Project
47 | [Dd]ebugPS/
48 | [Rr]eleasePS/
49 | dlldata.c
50 |
51 | # Benchmark Results
52 | BenchmarkDotNet.Artifacts/
53 |
54 | # .NET Core
55 | project.lock.json
56 | project.fragment.lock.json
57 | artifacts/
58 |
59 | # StyleCop
60 | StyleCopReport.xml
61 |
62 | # Files built by Visual Studio
63 | *_i.c
64 | *_p.c
65 | *_h.h
66 | *.ilk
67 | *.meta
68 | *.obj
69 | *.iobj
70 | *.pch
71 | *.pdb
72 | *.ipdb
73 | *.pgc
74 | *.pgd
75 | *.rsp
76 | *.sbr
77 | *.tlb
78 | *.tli
79 | *.tlh
80 | *.tmp
81 | *.tmp_proj
82 | *_wpftmp.csproj
83 | *.log
84 | *.vspscc
85 | *.vssscc
86 | .builds
87 | *.pidb
88 | *.svclog
89 | *.scc
90 |
91 | # Chutzpah Test files
92 | _Chutzpah*
93 |
94 | # Visual C++ cache files
95 | ipch/
96 | *.aps
97 | *.ncb
98 | *.opendb
99 | *.opensdf
100 | *.sdf
101 | *.cachefile
102 | *.VC.db
103 | *.VC.VC.opendb
104 |
105 | # Visual Studio profiler
106 | *.psess
107 | *.vsp
108 | *.vspx
109 | *.sap
110 |
111 | # Visual Studio Trace Files
112 | *.e2e
113 |
114 | # TFS 2012 Local Workspace
115 | $tf/
116 |
117 | # Guidance Automation Toolkit
118 | *.gpState
119 |
120 | # ReSharper is a .NET coding add-in
121 | _ReSharper*/
122 | *.[Rr]e[Ss]harper
123 | *.DotSettings.user
124 |
125 | # JustCode is a .NET coding add-in
126 | .JustCode
127 |
128 | # TeamCity is a build add-in
129 | _TeamCity*
130 |
131 | # DotCover is a Code Coverage Tool
132 | *.dotCover
133 |
134 | # AxoCover is a Code Coverage Tool
135 | .axoCover/*
136 | !.axoCover/settings.json
137 |
138 | # Visual Studio code coverage results
139 | *.coverage
140 | *.coveragexml
141 |
142 | # NCrunch
143 | _NCrunch_*
144 | .*crunch*.local.xml
145 | nCrunchTemp_*
146 |
147 | # MightyMoose
148 | *.mm.*
149 | AutoTest.Net/
150 |
151 | # Web workbench (sass)
152 | .sass-cache/
153 |
154 | # Installshield output folder
155 | [Ee]xpress/
156 |
157 | # DocProject is a documentation generator add-in
158 | DocProject/buildhelp/
159 | DocProject/Help/*.HxT
160 | DocProject/Help/*.HxC
161 | DocProject/Help/*.hhc
162 | DocProject/Help/*.hhk
163 | DocProject/Help/*.hhp
164 | DocProject/Help/Html2
165 | DocProject/Help/html
166 |
167 | # Click-Once directory
168 | publish/
169 |
170 | # Publish Web Output
171 | *.[Pp]ublish.xml
172 | *.azurePubxml
173 | # Note: Comment the next line if you want to checkin your web deploy settings,
174 | # but database connection strings (with potential passwords) will be unencrypted
175 | *.pubxml
176 | *.publishproj
177 |
178 | # Microsoft Azure Web App publish settings. Comment the next line if you want to
179 | # checkin your Azure Web App publish settings, but sensitive information contained
180 | # in these scripts will be unencrypted
181 | PublishScripts/
182 |
183 | # NuGet Packages
184 | *.nupkg
185 | # The packages folder can be ignored because of Package Restore
186 | **/[Pp]ackages/*
187 | # except build/, which is used as an MSBuild target.
188 | !**/[Pp]ackages/build/
189 | # Uncomment if necessary however generally it will be regenerated when needed
190 | #!**/[Pp]ackages/repositories.config
191 | # NuGet v3's project.json files produces more ignorable files
192 | *.nuget.props
193 | *.nuget.targets
194 |
195 | # Microsoft Azure Build Output
196 | csx/
197 | *.build.csdef
198 |
199 | # Microsoft Azure Emulator
200 | ecf/
201 | rcf/
202 |
203 | # Windows Store app package directories and files
204 | AppPackages/
205 | BundleArtifacts/
206 | Package.StoreAssociation.xml
207 | _pkginfo.txt
208 | *.appx
209 |
210 | # Visual Studio cache files
211 | # files ending in .cache can be ignored
212 | *.[Cc]ache
213 | # but keep track of directories ending in .cache
214 | !?*.[Cc]ache/
215 |
216 | # Others
217 | ClientBin/
218 | ~$*
219 | *~
220 | *.dbmdl
221 | *.dbproj.schemaview
222 | *.jfm
223 | *.pfx
224 | *.publishsettings
225 | orleans.codegen.cs
226 |
227 | # Including strong name files can present a security risk
228 | # (https://github.com/github/gitignore/pull/2483#issue-259490424)
229 | #*.snk
230 |
231 | # Since there are multiple workflows, uncomment next line to ignore bower_components
232 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
233 | #bower_components/
234 |
235 | # RIA/Silverlight projects
236 | Generated_Code/
237 |
238 | # Backup & report files from converting an old project file
239 | # to a newer Visual Studio version. Backup files are not needed,
240 | # because we have git ;-)
241 | _UpgradeReport_Files/
242 | Backup*/
243 | UpgradeLog*.XML
244 | UpgradeLog*.htm
245 | ServiceFabricBackup/
246 | *.rptproj.bak
247 |
248 | # SQL Server files
249 | *.mdf
250 | *.ldf
251 | *.ndf
252 |
253 | # Business Intelligence projects
254 | *.rdl.data
255 | *.bim.layout
256 | *.bim_*.settings
257 | *.rptproj.rsuser
258 | *- Backup*.rdl
259 |
260 | # Microsoft Fakes
261 | FakesAssemblies/
262 |
263 | # GhostDoc plugin setting file
264 | *.GhostDoc.xml
265 |
266 | # Node.js Tools for Visual Studio
267 | .ntvs_analysis.dat
268 | node_modules/
269 |
270 | # Visual Studio 6 build log
271 | *.plg
272 |
273 | # Visual Studio 6 workspace options file
274 | *.opt
275 |
276 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
277 | *.vbw
278 |
279 | # Visual Studio LightSwitch build output
280 | **/*.HTMLClient/GeneratedArtifacts
281 | **/*.DesktopClient/GeneratedArtifacts
282 | **/*.DesktopClient/ModelManifest.xml
283 | **/*.Server/GeneratedArtifacts
284 | **/*.Server/ModelManifest.xml
285 | _Pvt_Extensions
286 |
287 | # Paket dependency manager
288 | .paket/paket.exe
289 | paket-files/
290 |
291 | # FAKE - F# Make
292 | .fake/
293 |
294 | # JetBrains Rider
295 | .idea/
296 | *.sln.iml
297 |
298 | # CodeRush personal settings
299 | .cr/personal
300 |
301 | # Python Tools for Visual Studio (PTVS)
302 | __pycache__/
303 | *.pyc
304 |
305 | # Cake - Uncomment if you are using it
306 | # tools/**
307 | # !tools/packages.config
308 |
309 | # Tabs Studio
310 | *.tss
311 |
312 | # Telerik's JustMock configuration file
313 | *.jmconfig
314 |
315 | # BizTalk build output
316 | *.btp.cs
317 | *.btm.cs
318 | *.odx.cs
319 | *.xsd.cs
320 |
321 | # OpenCover UI analysis results
322 | OpenCover/
323 |
324 | # Azure Stream Analytics local run output
325 | ASALocalRun/
326 |
327 | # MSBuild Binary and Structured Log
328 | *.binlog
329 |
330 | # NVidia Nsight GPU debugger configuration file
331 | *.nvuser
332 |
333 | # MFractors (Xamarin productivity tool) working folder
334 | .mfractor/
335 |
336 | # Local History for Visual Studio
337 | .localhistory/
338 |
339 | # BeatPulse healthcheck temp database
340 | healthchecksdb
--------------------------------------------------------------------------------
/src/CryptHash.Net/bin/crypthash/CommandLineParserOptions/Options.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Alessandro Cagliostro, 2021
3 | *
4 | * https://github.com/alecgn
5 | */
6 |
7 | using CommandLine;
8 |
9 | namespace CryptHash.Net.CLI.CommandLineParser
10 | {
11 | [Verb("crypt", HelpText = "String and file encryption (Authenticated AES 128/192/256 CBC/GCM).")]
12 | public class CryptOptions
13 | {
14 | [Option('t', "input-type", Required = true, HelpText = "Input type (\"string\" or \"file\").")]
15 | public string InputType { get; set; }
16 |
17 | [Option('a', "algorithm", Required = true, HelpText = "Algorithm (\"aes128cbc\", \"aes192cbc\", \"aes256cbc\", \"aes128gcm\", \"aes192gcm\" and \"aes256gcm\").")]
18 | public string Algorithm { get; set; }
19 |
20 | [Option('i', "input", Required = true, HelpText = "Input to be encrypted (string or file path).")]
21 | public string InputToBeEncrypted { get; set; }
22 |
23 | [Option('p', "password", Required = true, HelpText = "Password for encryption.")]
24 | public string Password { get; set; }
25 |
26 | [Option('o', "output", HelpText = "Output encrypted file (only for \"file\" input type).")]
27 | public string OutputFilePath { get; set; }
28 |
29 | [Option('d', "delete-source-file", Default = false, HelpText = "Delete source file after encryption (only for \"file\" input type).")]
30 | public bool DeleteSourceFile { get; set; }
31 |
32 | [Option("associated-data", Default = null, HelpText = "Associated Data (only for \"AES128GCM\", \"AES192GCM\" and \"AES256GCM\" AEAD encryption algorithm).")]
33 | public string AssociatedData { get; set; }
34 | }
35 |
36 | [Verb("decrypt", HelpText = "String and file decryption (Authenticated AES 128/192/256 CBC/GCM).")]
37 | public class DecryptOptions
38 | {
39 | [Option('t', "input-type", Required = true, HelpText = "Input type (\"string\" or \"file\").")]
40 | public string InputType { get; set; }
41 |
42 | [Option('a', "algorithm", Required = true, HelpText = "Algorithm (\"aes128cbc\", \"aes192cbc\", \"aes256cbc\", \"aes128gcm\", \"aes192gcm\" and \"aes256gcm\").")]
43 | public string Algorithm { get; set; }
44 |
45 | [Option('i', "input", Required = true, HelpText = "Input to be decrypted (string or file path).")]
46 | public string InputToBeDecrypted { get; set; }
47 |
48 | [Option('p', "password", Required = true, HelpText = "Password for decryption.")]
49 | public string Password { get; set; }
50 |
51 | [Option('o', "output", HelpText = "Output decrypted file (only for \"file\" input type).")]
52 | public string OutputFilePath { get; set; }
53 |
54 | [Option('d', "delete-encrypted-file", Default = false, HelpText = "Delete encrypted file after decryption (only for \"file\" input type).")]
55 | public bool DeleteEncryptedFile { get; set; }
56 |
57 | [Option("associated-data", Default = null, HelpText = "Associated Data (only for \"AES128GCM\", \"AES192GCM\" and \"AES256GCM\" AEAD encryption algorithm).")]
58 | public string AssociatedData { get; set; }
59 | }
60 |
61 | [Verb("hash", HelpText = "String and file hash (MD5, SHA1, SHA256, SHA384, SHA512, PBKDF2 and BCrypt).")]
62 | public class HashOptions
63 | {
64 | [Option('t', "input-type", Required = true, HelpText = "Input type (\"string\" or \"file\").")]
65 | public string InputType { get; set; }
66 |
67 | [Option('a', "algorithm", Required = true, HelpText = "Algorithm (\"md5\", \"sha1\", \"sha256\", \"sha384\", \"sha512\", \"pbkdf2\" and \"bcrypt\").")]
68 | public string Algorithm { get; set; }
69 |
70 | [Option('i', "input", Required = true, HelpText = "Input to compute hash (string or file path).")]
71 | public string InputToComputeHash { get; set; }
72 |
73 | [Option('c', "compare-hash", Default = null, HelpText = "Previously generated hash for comparation with computed hash.")]
74 | public string CompareHash { get; set; }
75 | }
76 |
77 | [Verb("hmac", HelpText = "String and file HMAC (HMAC-MD5, HMAC-SHA1, HMAC-SHA256, HMAC-SHA384 and HMAC-SHA512).")]
78 | public class HMACOptions
79 | {
80 | [Option('t', "input-type", Required = true, HelpText = "Input type (\"string\" or \"file\").")]
81 | public string InputType { get; set; }
82 |
83 | [Option('a', "algorithm", Required = true, HelpText = "Algorithm (\"hmacmd5\", \"hmacsha1\", \"hmacsha256\", \"hmacsha384\" and \"hmacsha512\").")]
84 | public string Algorithm { get; set; }
85 |
86 | [Option('i', "input", Required = true, HelpText = "Input to compute HMAC (string or file path).")]
87 | public string InputToComputeHMAC { get; set; }
88 |
89 | [Option('k', "key", Default = null, HelpText = "Input key.")]
90 | public string Key { get; set; }
91 |
92 | [Option('c', "compare-hmac", Default = null, HelpText = "Previously generated HMAC for comparation with computed HMAC.")]
93 | public string CompareHash { get; set; }
94 | }
95 |
96 | [Verb("argon2id", HelpText = "Argon2id string hash.")]
97 | public class Argon2idHashOptions
98 | {
99 | [Option('i', "input", Required = true, HelpText = "Input string to compute Argon2id hash.")]
100 | public string InputToComputeHash { get; set; }
101 |
102 | [Option("iterations", Required = true, HelpText = "Number of iterations.")]
103 | public int Iterations { get; set; }
104 |
105 | [Option('m', "memory-size", Required = true, HelpText = "Memory size in kilobytes.")]
106 | public int MemorySize { get; set; }
107 |
108 | [Option('s', "salt", Default = null, HelpText = "Salt.")]
109 | public string Salt { get; set; }
110 |
111 | [Option('a', "associated-data", Default = null, HelpText = "Associated data.")]
112 | public string AssociatedData { get; set; }
113 |
114 | [Option('d', "degree-of-parallelism", Default = 0, HelpText = "Degree of parallelism (number of processor's cores to use).")]
115 | public int DegreeOfParallelism { get; set; }
116 |
117 | [Option('b', "amount-bytes-to-return", Required = true, HelpText = "Amount bytes to return.")]
118 | public int AmountBytesToReturn { get; set; }
119 |
120 | [Option('c', "compare-hash", Default = null, HelpText = "Previously generated HMAC for comparation with computed HMAC.")]
121 | public string CompareHash { get; set; }
122 | }
123 |
124 | [Verb("encode", HelpText = "String encode (Base64 and Hexadecimal).")]
125 | public class EncodeOptions
126 | {
127 | [Option('t', "encode-type", Required = true, HelpText = "Encode type (\"base64\" or \"hex\").")]
128 | public string EncodeType { get; set; }
129 |
130 | [Option('i', "input", Required = true, HelpText = "Input string to encode.")]
131 | public string InputToEncode { get; set; }
132 | }
133 |
134 | [Verb("decode", HelpText = "String decode (Base64 and Hexadecimal).")]
135 | public class DecodeOptions
136 | {
137 | [Option('t', "decode-type", Required = true, HelpText = "Decode type (\"base64\" or \"hex\").")]
138 | public string DecodeType { get; set; }
139 |
140 | [Option('i', "input", Required = true, HelpText = "Input string to decode.")]
141 | public string InputToDecode { get; set; }
142 | }
143 | }
144 |
--------------------------------------------------------------------------------
/src/CryptHash.Net/lib/CryptHash.Net.Tests/Encryption/AES/AEAD/AEAD_AES_128_GCM_Tests.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Alessandro Cagliostro, 2021
3 | *
4 | * https://github.com/alecgn
5 | */
6 |
7 | using CryptHash.Net.Encryption.AES.AEAD;
8 | using CryptHash.Net.Encryption.AES.EncryptionResults;
9 | using Microsoft.VisualStudio.TestTools.UnitTesting;
10 |
11 | namespace CryptHash.Net.Tests.Encryption.AES.AEAD
12 | {
13 | [TestClass]
14 | public class AEAD_AES_128_GCM_Tests
15 | {
16 | private readonly AEAD_AES_128_GCM _aes128gcm = new AEAD_AES_128_GCM();
17 | private readonly string _testString = "This is a test string!";
18 | private readonly string _password = "P4$$w0rd#123";
19 |
20 | [TestMethod]
21 | public void Test_EncryptString_without_append_encryption_data_without_associated_data()
22 | {
23 | string associatedData = null;
24 | var appendEncryptionData = false;
25 |
26 | var aesEncryptionResult = _aes128gcm.EncryptString(_testString, _password, associatedData, appendEncryptionData);
27 |
28 | Assert.IsTrue(aesEncryptionResult.Success, aesEncryptionResult.Message);
29 | }
30 |
31 | [TestMethod]
32 | public void Test_EncryptString_without_append_encryption_data_with_associated_data()
33 | {
34 | var associatedData = "0f8fad5b-d9cb-469f-a165-70867728950e";
35 | var appendEncryptionData = false;
36 |
37 | var aesEncryptionResult = _aes128gcm.EncryptString(_testString, _password, associatedData, appendEncryptionData);
38 |
39 | Assert.IsTrue(aesEncryptionResult.Success, aesEncryptionResult.Message);
40 | }
41 |
42 | [TestMethod]
43 | public void Test_EncryptString_with_append_encryption_data_without_associated_data()
44 | {
45 | string associatedData = null;
46 | var appendEncryptionData = true;
47 |
48 | var aesEncryptionResult = _aes128gcm.EncryptString(_testString, _password, associatedData, appendEncryptionData);
49 |
50 | Assert.IsTrue(aesEncryptionResult.Success, aesEncryptionResult.Message);
51 | }
52 |
53 | [TestMethod]
54 | public void Test_EncryptString_with_append_encryption_data_with_associated_data()
55 | {
56 | var associatedData = "0f8fad5b-d9cb-469f-a165-70867728950e";
57 | var appendEncryptionData = true;
58 |
59 | var aesEncryptionResult = _aes128gcm.EncryptString(_testString, _password, associatedData, appendEncryptionData);
60 |
61 | Assert.IsTrue(aesEncryptionResult.Success, aesEncryptionResult.Message);
62 | }
63 |
64 | [TestMethod]
65 | public void Test_DecryptString_without_encryption_data_appended_without_associated_data()
66 | {
67 | string associatedData = null;
68 | var appendEncryptionData = false;
69 | var aesDecryptionResult = new AesDecryptionResult();
70 | var errorMessage = "";
71 |
72 | var aesEncryptionResult = _aes128gcm.EncryptString(_testString, _password, associatedData, appendEncryptionDataToOutput: appendEncryptionData);
73 |
74 | if (aesEncryptionResult.Success)
75 | {
76 | aesDecryptionResult = _aes128gcm.DecryptString(aesEncryptionResult.EncryptedDataBytes, System.Text.Encoding.UTF8.GetBytes(_password), null, hasEncryptionDataAppendedInInput: appendEncryptionData,
77 | aesEncryptionResult.Tag, aesEncryptionResult.Salt, aesEncryptionResult.Nonce);
78 |
79 | if (!aesDecryptionResult.Success)
80 | {
81 | errorMessage = aesDecryptionResult.Message;
82 | }
83 | }
84 | else
85 | {
86 | errorMessage = aesEncryptionResult.Message;
87 | }
88 |
89 | Assert.IsTrue((aesEncryptionResult.Success && aesDecryptionResult.Success && aesDecryptionResult.DecryptedDataString.Equals(_testString)), errorMessage);
90 | }
91 |
92 | [TestMethod]
93 | public void Test_DecryptString_without_encryption_data_appended_with_associated_data()
94 | {
95 | var associatedData = "0f8fad5b-d9cb-469f-a165-70867728950e";
96 | var appendEncryptionData = false;
97 | var aesDecryptionResult = new AesDecryptionResult();
98 | var errorMessage = "";
99 |
100 | var aesEncryptionResult = _aes128gcm.EncryptString(_testString, _password, associatedData, appendEncryptionDataToOutput: appendEncryptionData);
101 |
102 | if (aesEncryptionResult.Success)
103 | {
104 | aesDecryptionResult = _aes128gcm.DecryptString(aesEncryptionResult.EncryptedDataBytes, System.Text.Encoding.UTF8.GetBytes(_password), System.Text.Encoding.UTF8.GetBytes(associatedData), hasEncryptionDataAppendedInInput: appendEncryptionData,
105 | aesEncryptionResult.Tag, aesEncryptionResult.Salt, aesEncryptionResult.Nonce);
106 |
107 | if (!aesDecryptionResult.Success)
108 | {
109 | errorMessage = aesDecryptionResult.Message;
110 | }
111 | }
112 | else
113 | {
114 | errorMessage = aesEncryptionResult.Message;
115 | }
116 |
117 | Assert.IsTrue((aesEncryptionResult.Success && aesDecryptionResult.Success && aesDecryptionResult.DecryptedDataString.Equals(_testString)), errorMessage);
118 | }
119 |
120 | [TestMethod]
121 | public void Test_DecryptString_with_encryption_data_appended_without_associated_data()
122 | {
123 | string associatedData = null;
124 | var appendEncryptionData = true;
125 | var aesDecryptionResult = new AesDecryptionResult();
126 | var errorMessage = "";
127 |
128 | var aesEncryptionResult = _aes128gcm.EncryptString(_testString, _password, associatedData, appendEncryptionDataToOutput: appendEncryptionData);
129 |
130 | if (aesEncryptionResult.Success)
131 | {
132 | aesDecryptionResult = _aes128gcm.DecryptString(aesEncryptionResult.EncryptedDataBase64String, _password, associatedData, hasEncryptionDataAppendedInInput: appendEncryptionData);
133 |
134 | if (!aesDecryptionResult.Success)
135 | {
136 | errorMessage = aesDecryptionResult.Message;
137 | }
138 | }
139 | else
140 | {
141 | errorMessage = aesEncryptionResult.Message;
142 | }
143 |
144 | Assert.IsTrue((aesEncryptionResult.Success && aesDecryptionResult.Success && aesDecryptionResult.DecryptedDataString.Equals(_testString)), errorMessage);
145 | }
146 |
147 | [TestMethod]
148 | public void Test_DecryptString_with_encryption_data_appended_with_associated_data()
149 | {
150 | var associatedData = "0f8fad5b-d9cb-469f-a165-70867728950e";
151 | var appendEncryptionData = true;
152 | var aesDecryptionResult = new AesDecryptionResult();
153 | var errorMessage = "";
154 |
155 | var aesEncryptionResult = _aes128gcm.EncryptString(_testString, _password, associatedData, appendEncryptionDataToOutput: appendEncryptionData);
156 |
157 | if (aesEncryptionResult.Success)
158 | {
159 | aesDecryptionResult = _aes128gcm.DecryptString(aesEncryptionResult.EncryptedDataBase64String, _password, associatedData, hasEncryptionDataAppendedInInput: appendEncryptionData);
160 |
161 | if (!aesDecryptionResult.Success)
162 | {
163 | errorMessage = aesDecryptionResult.Message;
164 | }
165 | }
166 | else
167 | {
168 | errorMessage = aesEncryptionResult.Message;
169 | }
170 |
171 | Assert.IsTrue((aesEncryptionResult.Success && aesDecryptionResult.Success && aesDecryptionResult.DecryptedDataString.Equals(_testString)), errorMessage);
172 | }
173 | }
174 | }
175 |
--------------------------------------------------------------------------------
/src/CryptHash.Net/lib/CryptHash.Net.Tests/Encryption/AES/AEAD/AEAD_AES_192_GCM_Tests.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Alessandro Cagliostro, 2021
3 | *
4 | * https://github.com/alecgn
5 | */
6 |
7 | using CryptHash.Net.Encryption.AES.AEAD;
8 | using CryptHash.Net.Encryption.AES.EncryptionResults;
9 | using Microsoft.VisualStudio.TestTools.UnitTesting;
10 |
11 | namespace CryptHash.Net.Tests.Encryption.AES.AEAD
12 | {
13 | [TestClass]
14 | public class AEAD_AES_192_GCM_Tests
15 | {
16 | private readonly AEAD_AES_192_GCM _aes192gcm = new AEAD_AES_192_GCM();
17 | private readonly string _testString = "This is a test string!";
18 | private readonly string _password = "P4$$w0rd#123";
19 |
20 | [TestMethod]
21 | public void Test_EncryptString_without_append_encryption_data_without_associated_data()
22 | {
23 | string associatedData = null;
24 | var appendEncryptionData = false;
25 |
26 | var aesEncryptionResult = _aes192gcm.EncryptString(_testString, _password, associatedData, appendEncryptionData);
27 |
28 | Assert.IsTrue(aesEncryptionResult.Success, aesEncryptionResult.Message);
29 | }
30 |
31 | [TestMethod]
32 | public void Test_EncryptString_without_append_encryption_data_with_associated_data()
33 | {
34 | var associatedData = "0f8fad5b-d9cb-469f-a165-70867728950e";
35 | var appendEncryptionData = false;
36 |
37 | var aesEncryptionResult = _aes192gcm.EncryptString(_testString, _password, associatedData, appendEncryptionData);
38 |
39 | Assert.IsTrue(aesEncryptionResult.Success, aesEncryptionResult.Message);
40 | }
41 |
42 | [TestMethod]
43 | public void Test_EncryptString_with_append_encryption_data_without_associated_data()
44 | {
45 | string associatedData = null;
46 | var appendEncryptionData = true;
47 |
48 | var aesEncryptionResult = _aes192gcm.EncryptString(_testString, _password, associatedData, appendEncryptionData);
49 |
50 | Assert.IsTrue(aesEncryptionResult.Success, aesEncryptionResult.Message);
51 | }
52 |
53 | [TestMethod]
54 | public void Test_EncryptString_with_append_encryption_data_with_associated_data()
55 | {
56 | var associatedData = "0f8fad5b-d9cb-469f-a165-70867728950e";
57 | var appendEncryptionData = true;
58 |
59 | var aesEncryptionResult = _aes192gcm.EncryptString(_testString, _password, associatedData, appendEncryptionData);
60 |
61 | Assert.IsTrue(aesEncryptionResult.Success, aesEncryptionResult.Message);
62 | }
63 |
64 | [TestMethod]
65 | public void Test_DecryptString_without_encryption_data_appended_without_associated_data()
66 | {
67 | string associatedData = null;
68 | var appendEncryptionData = false;
69 | var aesDecryptionResult = new AesDecryptionResult();
70 | var errorMessage = "";
71 |
72 | var aesEncryptionResult = _aes192gcm.EncryptString(_testString, _password, associatedData, appendEncryptionDataToOutput: appendEncryptionData);
73 |
74 | if (aesEncryptionResult.Success)
75 | {
76 | aesDecryptionResult = _aes192gcm.DecryptString(aesEncryptionResult.EncryptedDataBytes, System.Text.Encoding.UTF8.GetBytes(_password), null, hasEncryptionDataAppendedInInput: appendEncryptionData,
77 | aesEncryptionResult.Tag, aesEncryptionResult.Salt, aesEncryptionResult.Nonce);
78 |
79 | if (!aesDecryptionResult.Success)
80 | {
81 | errorMessage = aesDecryptionResult.Message;
82 | }
83 | }
84 | else
85 | {
86 | errorMessage = aesEncryptionResult.Message;
87 | }
88 |
89 | Assert.IsTrue((aesEncryptionResult.Success && aesDecryptionResult.Success && aesDecryptionResult.DecryptedDataString.Equals(_testString)), errorMessage);
90 | }
91 |
92 | [TestMethod]
93 | public void Test_DecryptString_without_encryption_data_appended_with_associated_data()
94 | {
95 | var associatedData = "0f8fad5b-d9cb-469f-a165-70867728950e";
96 | var appendEncryptionData = false;
97 | var aesDecryptionResult = new AesDecryptionResult();
98 | var errorMessage = "";
99 |
100 | var aesEncryptionResult = _aes192gcm.EncryptString(_testString, _password, associatedData, appendEncryptionDataToOutput: appendEncryptionData);
101 |
102 | if (aesEncryptionResult.Success)
103 | {
104 | aesDecryptionResult = _aes192gcm.DecryptString(aesEncryptionResult.EncryptedDataBytes, System.Text.Encoding.UTF8.GetBytes(_password), System.Text.Encoding.UTF8.GetBytes(associatedData), hasEncryptionDataAppendedInInput: appendEncryptionData,
105 | aesEncryptionResult.Tag, aesEncryptionResult.Salt, aesEncryptionResult.Nonce);
106 |
107 | if (!aesDecryptionResult.Success)
108 | {
109 | errorMessage = aesDecryptionResult.Message;
110 | }
111 | }
112 | else
113 | {
114 | errorMessage = aesEncryptionResult.Message;
115 | }
116 |
117 | Assert.IsTrue((aesEncryptionResult.Success && aesDecryptionResult.Success && aesDecryptionResult.DecryptedDataString.Equals(_testString)), errorMessage);
118 | }
119 |
120 | [TestMethod]
121 | public void Test_DecryptString_with_encryption_data_appended_without_associated_data()
122 | {
123 | string associatedData = null;
124 | var appendEncryptionData = true;
125 | var aesDecryptionResult = new AesDecryptionResult();
126 | var errorMessage = "";
127 |
128 | var aesEncryptionResult = _aes192gcm.EncryptString(_testString, _password, associatedData, appendEncryptionDataToOutput: appendEncryptionData);
129 |
130 | if (aesEncryptionResult.Success)
131 | {
132 | aesDecryptionResult = _aes192gcm.DecryptString(aesEncryptionResult.EncryptedDataBase64String, _password, associatedData, hasEncryptionDataAppendedInInput: appendEncryptionData);
133 |
134 | if (!aesDecryptionResult.Success)
135 | {
136 | errorMessage = aesDecryptionResult.Message;
137 | }
138 | }
139 | else
140 | {
141 | errorMessage = aesEncryptionResult.Message;
142 | }
143 |
144 | Assert.IsTrue((aesEncryptionResult.Success && aesDecryptionResult.Success && aesDecryptionResult.DecryptedDataString.Equals(_testString)), errorMessage);
145 | }
146 |
147 | [TestMethod]
148 | public void Test_DecryptString_with_encryption_data_appended_with_associated_data()
149 | {
150 | var associatedData = "0f8fad5b-d9cb-469f-a165-70867728950e";
151 | var appendEncryptionData = true;
152 | var aesDecryptionResult = new AesDecryptionResult();
153 | var errorMessage = "";
154 |
155 | var aesEncryptionResult = _aes192gcm.EncryptString(_testString, _password, associatedData, appendEncryptionDataToOutput: appendEncryptionData);
156 |
157 | if (aesEncryptionResult.Success)
158 | {
159 | aesDecryptionResult = _aes192gcm.DecryptString(aesEncryptionResult.EncryptedDataBase64String, _password, associatedData, hasEncryptionDataAppendedInInput: appendEncryptionData);
160 |
161 | if (!aesDecryptionResult.Success)
162 | {
163 | errorMessage = aesDecryptionResult.Message;
164 | }
165 | }
166 | else
167 | {
168 | errorMessage = aesEncryptionResult.Message;
169 | }
170 |
171 | Assert.IsTrue((aesEncryptionResult.Success && aesDecryptionResult.Success && aesDecryptionResult.DecryptedDataString.Equals(_testString)), errorMessage);
172 | }
173 | }
174 | }
175 |
--------------------------------------------------------------------------------
/src/CryptHash.Net/lib/CryptHash.Net.Tests/Encryption/AES/AEAD/AEAD_AES_256_GCM_Tests.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Alessandro Cagliostro, 2021
3 | *
4 | * https://github.com/alecgn
5 | */
6 |
7 | using CryptHash.Net.Encryption.AES.AEAD;
8 | using CryptHash.Net.Encryption.AES.EncryptionResults;
9 | using Microsoft.VisualStudio.TestTools.UnitTesting;
10 |
11 | namespace CryptHash.Net.Tests.Encryption.AES.AEAD
12 | {
13 | [TestClass]
14 | public class AEAD_AES_256_GCM_Tests
15 | {
16 | private readonly AEAD_AES_256_GCM _aes256gcm = new AEAD_AES_256_GCM();
17 | private readonly string _testString = "This is a test string!";
18 | private readonly string _password = "P4$$w0rd#123";
19 |
20 | [TestMethod]
21 | public void Test_EncryptString_without_append_encryption_data_without_associated_data()
22 | {
23 | string associatedData = null;
24 | var appendEncryptionData = false;
25 |
26 | var aesEncryptionResult = _aes256gcm.EncryptString(_testString, _password, associatedData, appendEncryptionData);
27 |
28 | Assert.IsTrue(aesEncryptionResult.Success, aesEncryptionResult.Message);
29 | }
30 |
31 | [TestMethod]
32 | public void Test_EncryptString_without_append_encryption_data_with_associated_data()
33 | {
34 | var associatedData = "0f8fad5b-d9cb-469f-a165-70867728950e";
35 | var appendEncryptionData = false;
36 |
37 | var aesEncryptionResult = _aes256gcm.EncryptString(_testString, _password, associatedData, appendEncryptionData);
38 |
39 | Assert.IsTrue(aesEncryptionResult.Success, aesEncryptionResult.Message);
40 | }
41 |
42 | [TestMethod]
43 | public void Test_EncryptString_with_append_encryption_data_without_associated_data()
44 | {
45 | string associatedData = null;
46 | var appendEncryptionData = true;
47 |
48 | var aesEncryptionResult = _aes256gcm.EncryptString(_testString, _password, associatedData, appendEncryptionData);
49 |
50 | Assert.IsTrue(aesEncryptionResult.Success, aesEncryptionResult.Message);
51 | }
52 |
53 | [TestMethod]
54 | public void Test_EncryptString_with_append_encryption_data_with_associated_data()
55 | {
56 | var associatedData = "0f8fad5b-d9cb-469f-a165-70867728950e";
57 | var appendEncryptionData = true;
58 |
59 | var aesEncryptionResult = _aes256gcm.EncryptString(_testString, _password, associatedData, appendEncryptionData);
60 |
61 | Assert.IsTrue(aesEncryptionResult.Success, aesEncryptionResult.Message);
62 | }
63 |
64 | [TestMethod]
65 | public void Test_DecryptString_without_encryption_data_appended_without_associated_data()
66 | {
67 | string associatedData = null;
68 | var appendEncryptionData = false;
69 | var aesDecryptionResult = new AesDecryptionResult();
70 | var errorMessage = "";
71 |
72 | var aesEncryptionResult = _aes256gcm.EncryptString(_testString, _password, associatedData, appendEncryptionDataToOutput: appendEncryptionData);
73 |
74 | if (aesEncryptionResult.Success)
75 | {
76 | aesDecryptionResult = _aes256gcm.DecryptString(aesEncryptionResult.EncryptedDataBytes, System.Text.Encoding.UTF8.GetBytes(_password), null, hasEncryptionDataAppendedInInput: appendEncryptionData,
77 | aesEncryptionResult.Tag, aesEncryptionResult.Salt, aesEncryptionResult.Nonce);
78 |
79 | if (!aesDecryptionResult.Success)
80 | {
81 | errorMessage = aesDecryptionResult.Message;
82 | }
83 | }
84 | else
85 | {
86 | errorMessage = aesEncryptionResult.Message;
87 | }
88 |
89 | Assert.IsTrue((aesEncryptionResult.Success && aesDecryptionResult.Success && aesDecryptionResult.DecryptedDataString.Equals(_testString)), errorMessage);
90 | }
91 |
92 | [TestMethod]
93 | public void Test_DecryptString_without_encryption_data_appended_with_associated_data()
94 | {
95 | var associatedData = "0f8fad5b-d9cb-469f-a165-70867728950e";
96 | var appendEncryptionData = false;
97 | var aesDecryptionResult = new AesDecryptionResult();
98 | var errorMessage = "";
99 |
100 | var aesEncryptionResult = _aes256gcm.EncryptString(_testString, _password, associatedData, appendEncryptionDataToOutput: appendEncryptionData);
101 |
102 | if (aesEncryptionResult.Success)
103 | {
104 | aesDecryptionResult = _aes256gcm.DecryptString(aesEncryptionResult.EncryptedDataBytes, System.Text.Encoding.UTF8.GetBytes(_password), System.Text.Encoding.UTF8.GetBytes(associatedData), hasEncryptionDataAppendedInInput: appendEncryptionData,
105 | aesEncryptionResult.Tag, aesEncryptionResult.Salt, aesEncryptionResult.Nonce);
106 |
107 | if (!aesDecryptionResult.Success)
108 | {
109 | errorMessage = aesDecryptionResult.Message;
110 | }
111 | }
112 | else
113 | {
114 | errorMessage = aesEncryptionResult.Message;
115 | }
116 |
117 | Assert.IsTrue((aesEncryptionResult.Success && aesDecryptionResult.Success && aesDecryptionResult.DecryptedDataString.Equals(_testString)), errorMessage);
118 | }
119 |
120 | [TestMethod]
121 | public void Test_DecryptString_with_encryption_data_appended_without_associated_data()
122 | {
123 | string associatedData = null;
124 | var appendEncryptionData = true;
125 | var aesDecryptionResult = new AesDecryptionResult();
126 | var errorMessage = "";
127 |
128 | var aesEncryptionResult = _aes256gcm.EncryptString(_testString, _password, associatedData, appendEncryptionDataToOutput: appendEncryptionData);
129 |
130 | if (aesEncryptionResult.Success)
131 | {
132 | aesDecryptionResult = _aes256gcm.DecryptString(aesEncryptionResult.EncryptedDataBase64String, _password, associatedData, hasEncryptionDataAppendedInInput: appendEncryptionData);
133 |
134 | if (!aesDecryptionResult.Success)
135 | {
136 | errorMessage = aesDecryptionResult.Message;
137 | }
138 | }
139 | else
140 | {
141 | errorMessage = aesEncryptionResult.Message;
142 | }
143 |
144 | Assert.IsTrue((aesEncryptionResult.Success && aesDecryptionResult.Success && aesDecryptionResult.DecryptedDataString.Equals(_testString)), errorMessage);
145 | }
146 |
147 | [TestMethod]
148 | public void Test_DecryptString_with_encryption_data_appended_with_associated_data()
149 | {
150 | var associatedData = "0f8fad5b-d9cb-469f-a165-70867728950e";
151 | var appendEncryptionData = true;
152 | var aesDecryptionResult = new AesDecryptionResult();
153 | var errorMessage = "";
154 |
155 | var aesEncryptionResult = _aes256gcm.EncryptString(_testString, _password, associatedData, appendEncryptionDataToOutput: appendEncryptionData);
156 |
157 | if (aesEncryptionResult.Success)
158 | {
159 | aesDecryptionResult = _aes256gcm.DecryptString(aesEncryptionResult.EncryptedDataBase64String, _password, associatedData, hasEncryptionDataAppendedInInput: appendEncryptionData);
160 |
161 | if (!aesDecryptionResult.Success)
162 | {
163 | errorMessage = aesDecryptionResult.Message;
164 | }
165 | }
166 | else
167 | {
168 | errorMessage = aesEncryptionResult.Message;
169 | }
170 |
171 | Assert.IsTrue((aesEncryptionResult.Success && aesDecryptionResult.Success && aesDecryptionResult.DecryptedDataString.Equals(_testString)), errorMessage);
172 | }
173 | }
174 | }
175 |
--------------------------------------------------------------------------------
/src/CryptHash.Net/lib/CryptHash.Net/Encoding/HighPerformanceHexadecimal.cs:
--------------------------------------------------------------------------------
1 | /*
2 | * Based on "the retired" unsafe fast implementation
3 | *
4 | * https://www.codeproject.com/Tips/447938/High-performance-Csharp-byte-array-to-hex-string-t
5 | */
6 |
7 | using CryptHash.Net.Resources;
8 | using System;
9 |
10 | namespace CryptHash.Net.Encoding
11 | {
12 | ///
13 | /// This class produces the same results as "Hexadecimal.cs" class but uses unsafe code to do it really really faster.
14 | ///
15 | public static unsafe class HighPerformanceHexadecimal
16 | {
17 | #region fields
18 |
19 | private static readonly int[] _toHexTable = new int[] {
20 | 3145776, 3211312, 3276848, 3342384, 3407920, 3473456, 3538992, 3604528, 3670064, 3735600,
21 | 4259888, 4325424, 4390960, 4456496, 4522032, 4587568, 3145777, 3211313, 3276849, 3342385,
22 | 3407921, 3473457, 3538993, 3604529, 3670065, 3735601, 4259889, 4325425, 4390961, 4456497,
23 | 4522033, 4587569, 3145778, 3211314, 3276850, 3342386, 3407922, 3473458, 3538994, 3604530,
24 | 3670066, 3735602, 4259890, 4325426, 4390962, 4456498, 4522034, 4587570, 3145779, 3211315,
25 | 3276851, 3342387, 3407923, 3473459, 3538995, 3604531, 3670067, 3735603, 4259891, 4325427,
26 | 4390963, 4456499, 4522035, 4587571, 3145780, 3211316, 3276852, 3342388, 3407924, 3473460,
27 | 3538996, 3604532, 3670068, 3735604, 4259892, 4325428, 4390964, 4456500, 4522036, 4587572,
28 | 3145781, 3211317, 3276853, 3342389, 3407925, 3473461, 3538997, 3604533, 3670069, 3735605,
29 | 4259893, 4325429, 4390965, 4456501, 4522037, 4587573, 3145782, 3211318, 3276854, 3342390,
30 | 3407926, 3473462, 3538998, 3604534, 3670070, 3735606, 4259894, 4325430, 4390966, 4456502,
31 | 4522038, 4587574, 3145783, 3211319, 3276855, 3342391, 3407927, 3473463, 3538999, 3604535,
32 | 3670071, 3735607, 4259895, 4325431, 4390967, 4456503, 4522039, 4587575, 3145784, 3211320,
33 | 3276856, 3342392, 3407928, 3473464, 3539000, 3604536, 3670072, 3735608, 4259896, 4325432,
34 | 4390968, 4456504, 4522040, 4587576, 3145785, 3211321, 3276857, 3342393, 3407929, 3473465,
35 | 3539001, 3604537, 3670073, 3735609, 4259897, 4325433, 4390969, 4456505, 4522041, 4587577,
36 | 3145793, 3211329, 3276865, 3342401, 3407937, 3473473, 3539009, 3604545, 3670081, 3735617,
37 | 4259905, 4325441, 4390977, 4456513, 4522049, 4587585, 3145794, 3211330, 3276866, 3342402,
38 | 3407938, 3473474, 3539010, 3604546, 3670082, 3735618, 4259906, 4325442, 4390978, 4456514,
39 | 4522050, 4587586, 3145795, 3211331, 3276867, 3342403, 3407939, 3473475, 3539011, 3604547,
40 | 3670083, 3735619, 4259907, 4325443, 4390979, 4456515, 4522051, 4587587, 3145796, 3211332,
41 | 3276868, 3342404, 3407940, 3473476, 3539012, 3604548, 3670084, 3735620, 4259908, 4325444,
42 | 4390980, 4456516, 4522052, 4587588, 3145797, 3211333, 3276869, 3342405, 3407941, 3473477,
43 | 3539013, 3604549, 3670085, 3735621, 4259909, 4325445, 4390981, 4456517, 4522053, 4587589,
44 | 3145798, 3211334, 3276870, 3342406, 3407942, 3473478, 3539014, 3604550, 3670086, 3735622,
45 | 4259910, 4325446, 4390982, 4456518, 4522054, 4587590
46 | };
47 |
48 | private static readonly byte[] _fromHexTable = new byte[] {
49 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
50 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
51 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
52 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
53 | 255, 255, 255, 255, 255, 255, 255, 255, 0, 1,
54 | 2, 3, 4, 5, 6, 7, 8, 9, 255, 255,
55 | 255, 255, 255, 255, 255, 10, 11, 12, 13, 14,
56 | 15, 255, 255, 255, 255, 255, 255, 255, 255, 255,
57 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
58 | 255, 255, 255, 255, 255, 255, 255, 10, 11, 12,
59 | 13, 14, 15
60 | };
61 |
62 | private static readonly byte[] _fromHexTable16 = new byte[] {
63 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
64 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
65 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
66 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
67 | 255, 255, 255, 255, 255, 255, 255, 255, 0, 16,
68 | 32, 48, 64, 80, 96, 112, 128, 144, 255, 255,
69 | 255, 255, 255, 255, 255, 160, 176, 192, 208, 224,
70 | 240, 255, 255, 255, 255, 255, 255, 255, 255, 255,
71 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
72 | 255, 255, 255, 255, 255, 255, 255, 160, 176, 192,
73 | 208, 224, 240
74 | };
75 |
76 | #endregion fields
77 |
78 |
79 | #region public methods
80 |
81 | public static string ToHexString(string plainString, bool useHexIndicatorPrefix = false)
82 | {
83 | if (string.IsNullOrWhiteSpace(plainString))
84 | {
85 | return null;
86 | }
87 |
88 | var plainStringBytes = System.Text.Encoding.UTF8.GetBytes(plainString);
89 |
90 | return ToHexString(plainStringBytes, useHexIndicatorPrefix);
91 | }
92 |
93 | public static string ToHexString(byte[] byteArray, bool useHexIndicatorPrefix = false)
94 | {
95 | fixed (int* hexRef = _toHexTable)
96 | fixed (byte* sourceRef = byteArray)
97 | {
98 | var s = sourceRef;
99 | var resultLen = (byteArray.Length << 1);
100 |
101 | if (useHexIndicatorPrefix)
102 | {
103 | resultLen += 2;
104 | }
105 |
106 | var result = new string(' ', resultLen);
107 |
108 | fixed (char* resultRef = result)
109 | {
110 | var pair = (int*)resultRef;
111 |
112 | if (useHexIndicatorPrefix)
113 | {
114 | *pair++ = 7864368;
115 | }
116 |
117 | while (*pair != 0)
118 | {
119 | *pair++ = hexRef[*s++];
120 | }
121 |
122 | return result;
123 | }
124 | }
125 | }
126 |
127 | public static string ToString(string hexString)
128 | {
129 | if (string.IsNullOrWhiteSpace(hexString))
130 | {
131 | return null;
132 | }
133 |
134 | var byteArray = ToByteArray(hexString);
135 |
136 | return System.Text.Encoding.UTF8.GetString(byteArray);
137 | }
138 |
139 | public static byte[] ToByteArray(string hexString)
140 | {
141 | if (string.IsNullOrWhiteSpace(hexString))
142 | {
143 | return null;
144 | }
145 |
146 | if (hexString.Length % 2 != 0)
147 | {
148 | throw new ArgumentException(MessageStrings.Common_IncorrectHexadecimalString, nameof(hexString));
149 | }
150 |
151 | int index = 0, len = hexString.Length >> 1;
152 |
153 | fixed (char* sourceRef = hexString)
154 | {
155 | if (*(int*)sourceRef == 7864368)
156 | {
157 | if (hexString.Length == 2)
158 | {
159 | throw new ArgumentException();
160 | }
161 |
162 | index += 2;
163 | len -= 1;
164 | }
165 |
166 | byte add = 0;
167 | var result = new byte[len];
168 | fixed (byte* hiRef = _fromHexTable16)
169 | fixed (byte* lowRef = _fromHexTable)
170 | fixed (byte* resultRef = result)
171 | {
172 | var s = &sourceRef[index];
173 | var r = resultRef;
174 |
175 | while (*s != 0)
176 | {
177 | if (*s > 102 || (*r = hiRef[*s++]) == 255 || *s > 102 || (add = lowRef[*s++]) == 255)
178 | {
179 | throw new ArgumentException();
180 | }
181 |
182 | *r++ += add;
183 | }
184 |
185 | return result;
186 | }
187 | }
188 | }
189 |
190 | #endregion public methods
191 | }
192 | }
193 |
--------------------------------------------------------------------------------