Skip to content

Commit

Permalink
Update dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
VahidN committed Feb 7, 2024
1 parent c7e711d commit c218c33
Show file tree
Hide file tree
Showing 43 changed files with 2,728 additions and 1,343 deletions.
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"sdk": {
"version": "8.0.100",
"version": "8.0.101",
"rollForward": "latestMajor",
"allowPrerelease": false
}
Expand Down
218 changes: 109 additions & 109 deletions src/DNTCaptcha.Core/CaptchaCryptoProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,146 +7,146 @@
using Microsoft.AspNetCore.WebUtilities;
using Microsoft.Extensions.Options;

namespace DNTCaptcha.Core
namespace DNTCaptcha.Core;

/// <summary>
/// The default captcha protection provider
/// </summary>
public class CaptchaCryptoProvider : ICaptchaCryptoProvider
{
private readonly byte[] _keyBytes;

/// <summary>
/// The default captcha protection provider
/// </summary>
public class CaptchaCryptoProvider : ICaptchaCryptoProvider
public CaptchaCryptoProvider(IOptions<DNTCaptchaOptions> options)
{
private readonly byte[] _keyBytes;

/// <summary>
/// The default captcha protection provider
/// </summary>
public CaptchaCryptoProvider(IOptions<DNTCaptchaOptions> options)
if (options == null)
{
if (options == null)
{
throw new ArgumentNullException(nameof(options));
}

_keyBytes = getDesKey(options.Value.EncryptionKey);
throw new ArgumentNullException(nameof(options));
}

/// <summary>
/// Creates the hash of the message
/// </summary>
public (string HashString, byte[] HashBytes) Hash(string inputText)
_keyBytes = getDesKey(options.Value.EncryptionKey);
}

/// <summary>
/// Creates the hash of the message
/// </summary>
public (string HashString, byte[] HashBytes) Hash(string inputText)
{
var hash = SHA256.HashData(Encoding.UTF8.GetBytes(inputText));

return (Encoding.UTF8.GetString(hash), hash);
}

/// <summary>
/// Decrypts the message
/// </summary>
public string? Decrypt(string inputText)
{
if (string.IsNullOrWhiteSpace(inputText))
{
var hash = SHA256.HashData(Encoding.UTF8.GetBytes(inputText));
return (Encoding.UTF8.GetString(hash), hash);
throw new ArgumentNullException(nameof(inputText));
}

/// <summary>
/// Decrypts the message
/// </summary>
public string? Decrypt(string inputText)
{
if (string.IsNullOrWhiteSpace(inputText))
{
throw new ArgumentNullException(nameof(inputText));
}
var inputBytes = WebEncoders.Base64UrlDecode(inputText);
var bytes = decrypt(inputBytes);

var inputBytes = WebEncoders.Base64UrlDecode(inputText);
var bytes = decrypt(inputBytes);
return Encoding.UTF8.GetString(bytes);
}
return Encoding.UTF8.GetString(bytes);
}

/// <summary>
/// Encrypts the message
/// </summary>
public string Encrypt(string inputText)
/// <summary>
/// Encrypts the message
/// </summary>
public string Encrypt(string inputText)
{
if (string.IsNullOrWhiteSpace(inputText))
{
if (string.IsNullOrWhiteSpace(inputText))
{
throw new ArgumentNullException(nameof(inputText));
}

var inputBytes = Encoding.UTF8.GetBytes(inputText);
var bytes = encrypt(inputBytes);
return WebEncoders.Base64UrlEncode(bytes);
throw new ArgumentNullException(nameof(inputText));
}

[SuppressMessage("Microsoft.Usage", "S5547:encrypt uses a weak cryptographic algorithm TripleDES",
Justification = "That's enough for our usecase!")]
[SuppressMessage("Microsoft.Usage", "CA5350:encrypt uses a weak cryptographic algorithm TripleDES",
Justification = "That's enough for our usecase!")]
[SuppressMessage("Microsoft.Usage", "SCS0011:encrypt uses a weak cryptographic algorithm TripleDES",
Justification = "That's enough for our usecase!")]
private byte[] encrypt(byte[] data)
var inputBytes = Encoding.UTF8.GetBytes(inputText);
var bytes = encrypt(inputBytes);

return WebEncoders.Base64UrlEncode(bytes);
}

[SuppressMessage("Microsoft.Usage", "S5547:encrypt uses a weak cryptographic algorithm TripleDES",
Justification = "That's enough for our usecase!"),
SuppressMessage("Microsoft.Usage", "CA5350:encrypt uses a weak cryptographic algorithm TripleDES",
Justification = "That's enough for our usecase!"),
SuppressMessage("Microsoft.Usage", "SCS0011:encrypt uses a weak cryptographic algorithm TripleDES",
Justification = "That's enough for our usecase!")]
private byte[] encrypt(byte[] data)
{
using (var des = new TripleDESCryptoServiceProvider
{
Key = _keyBytes,
Mode = CipherMode.CBC,
Padding = PaddingMode.PKCS7
})
{
using (var des = new TripleDESCryptoServiceProvider
{
Key = _keyBytes,
Mode = CipherMode.CBC,
Padding = PaddingMode.PKCS7,
})
using var encryptor = des.CreateEncryptor();
using var cipherStream = new MemoryStream();

using (var cryptoStream = new CryptoStream(cipherStream, encryptor, CryptoStreamMode.Write))
{
using var encryptor = des.CreateEncryptor();
using var cipherStream = new MemoryStream();
using (var cryptoStream = new CryptoStream(cipherStream, encryptor, CryptoStreamMode.Write))
using (var binaryWriter = new BinaryWriter(cryptoStream))
{
using (var binaryWriter = new BinaryWriter(cryptoStream))
{
// prepend IV to data
cipherStream.Write(des.IV); // This is an auto-generated random key
binaryWriter.Write(data);
cryptoStream.FlushFinalBlock();
}
// prepend IV to data
cipherStream.Write(des.IV); // This is an auto-generated random key
binaryWriter.Write(data);
cryptoStream.FlushFinalBlock();
}

return cipherStream.ToArray();
}

return cipherStream.ToArray();
}
}

[SuppressMessage("Microsoft.Usage", "S5547:encrypt uses a weak cryptographic algorithm TripleDES",
Justification = "That's enough for our usecase!")]
[SuppressMessage("Microsoft.Usage", "CA5350:encrypt uses a weak cryptographic algorithm TripleDES",
Justification = "That's enough for our usecase!")]
[SuppressMessage("Microsoft.Usage", "SCS0011:encrypt uses a weak cryptographic algorithm TripleDES",
Justification = "That's enough for our usecase!")]
private byte[] decrypt(byte[] data)
[SuppressMessage("Microsoft.Usage", "S5547:encrypt uses a weak cryptographic algorithm TripleDES",
Justification = "That's enough for our usecase!"),
SuppressMessage("Microsoft.Usage", "CA5350:encrypt uses a weak cryptographic algorithm TripleDES",
Justification = "That's enough for our usecase!"),
SuppressMessage("Microsoft.Usage", "SCS0011:encrypt uses a weak cryptographic algorithm TripleDES",
Justification = "That's enough for our usecase!")]
private byte[] decrypt(byte[] data)
{
using (var des = new TripleDESCryptoServiceProvider
{
Key = _keyBytes,
Mode = CipherMode.CBC,
Padding = PaddingMode.PKCS7
})
{
using (var des = new TripleDESCryptoServiceProvider
{
Key = _keyBytes,
Mode = CipherMode.CBC,
Padding = PaddingMode.PKCS7,
})
{
var iv = new byte[8]; // 3DES-IV is always 8 bytes/64 bits because block size is always 64 bits
Array.Copy(data, 0, iv, 0, iv.Length);
var iv = new byte[8]; // 3DES-IV is always 8 bytes/64 bits because block size is always 64 bits
Array.Copy(data, 0, iv, 0, iv.Length);

using var ms = new MemoryStream();

using var ms = new MemoryStream();
using (var decryptor = new CryptoStream(ms, des.CreateDecryptor(_keyBytes, iv), CryptoStreamMode.Write))
using (var decryptor = new CryptoStream(ms, des.CreateDecryptor(_keyBytes, iv), CryptoStreamMode.Write))
{
using (var binaryWriter = new BinaryWriter(decryptor))
{
using (var binaryWriter = new BinaryWriter(decryptor))
{
// decrypt cipher text from data, starting just past the IV
binaryWriter.Write(
data,
iv.Length,
data.Length - iv.Length
);
}
// decrypt cipher text from data, starting just past the IV
binaryWriter.Write(data, iv.Length, data.Length - iv.Length);
}

return ms.ToArray();
}

return ms.ToArray();
}
}

private byte[] getDesKey(string? key)
private byte[] getDesKey(string? key)
{
if (string.IsNullOrWhiteSpace(key))
{
if (string.IsNullOrWhiteSpace(key))
{
throw new InvalidOperationException("Please set the `options.WithEncryptionKey(...)`.");
}

// The key size of TripleDES is 168 bits, its len in byte is 24 Bytes (or 192 bits).
// Last bit of each byte is not used (or used as version in some hardware).
// Key len for TripleDES can also be 112 bits which is again stored in 128 bits or 16 bytes.
return Hash(key).HashBytes.Take(24).ToArray();
throw new InvalidOperationException("Please set the `options.WithEncryptionKey(...)`.");
}

// The key size of TripleDES is 168 bits, its len in byte is 24 Bytes (or 192 bits).
// Last bit of each byte is not used (or used as version in some hardware).
// Key len for TripleDES can also be 112 bits which is again stored in 128 bits or 16 bytes.
return Hash(key).HashBytes.Take(24).ToArray();
}
}
59 changes: 29 additions & 30 deletions src/DNTCaptcha.Core/CaptchaImageParams.cs
Original file line number Diff line number Diff line change
@@ -1,41 +1,40 @@
using System;

namespace DNTCaptcha.Core
namespace DNTCaptcha.Core;

/// <summary>
/// Captcha's Image Params
/// </summary>
[Serializable]
public class CaptchaImageParams
{
/// <summary>
/// Captcha's Image Params
/// The encrypted text
/// </summary>
[Serializable]
public class CaptchaImageParams
{
/// <summary>
/// The encrypted text
/// </summary>
public string Text { set; get; } = default!;
public string Text { set; get; } = default!;

/// <summary>
/// A random number
/// </summary>
public string RndDate { set; get; } = default!;
/// <summary>
/// A random number
/// </summary>
public string RndDate { set; get; } = default!;

/// <summary>
/// ForeColor of the captcha's text
/// </summary>
public string ForeColor { set; get; } = "#1B0172";
/// <summary>
/// ForeColor of the captcha's text
/// </summary>
public string ForeColor { set; get; } = "#1B0172";

/// <summary>
/// BackColor of the captcha's text
/// </summary>
public string BackColor { set; get; } = "";
/// <summary>
/// BackColor of the captcha's text
/// </summary>
public string BackColor { set; get; } = "";

/// <summary>
/// FontSize of the captcha's text
/// </summary>
public float FontSize { set; get; } = 12;
/// <summary>
/// FontSize of the captcha's text
/// </summary>
public float FontSize { set; get; } = 12;

/// <summary>
/// FontName of the captcha's text
/// </summary>
public string FontName { set; get; } = "Tahoma";
}
/// <summary>
/// FontName of the captcha's text
/// </summary>
public string FontName { set; get; } = "Tahoma";
}
Loading

0 comments on commit c218c33

Please sign in to comment.