Skip to content

Commit

Permalink
Start using SkiaSharp
Browse files Browse the repository at this point in the history
  • Loading branch information
VahidN committed Dec 24, 2022
1 parent dbb64f5 commit ac62233
Show file tree
Hide file tree
Showing 20 changed files with 373 additions and 274 deletions.
4 changes: 4 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Code files
[*.{cs,csx,vb,vbx,razor,html,htm,js,md,cshtml,xaml,vbhtml,aspx,txt,asax,ashx,asmx,master,config}]
charset = utf-8-bom

[*.cs]

dotnet_diagnostic.SYSLIB0021.severity = suggestion
Expand Down
2 changes: 1 addition & 1 deletion .github/FUNDING.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ community_bridge: # Replace with a single Community Bridge project-name e.g., cl
liberapay: # Replace with a single Liberapay username
issuehunt: # Replace with a single IssueHunt username
otechie: # Replace with a single Otechie username
custom: ['https://coffeebede.ir/%D9%88%D8%AD%D9%8A%D8%AF%D9%86%D8%B5%D9%8A%D8%B1%D9%8A']
custom: ['https://coffeebede.ir/%D9%88%D8%AD%D9%8A%D8%AF%D9%86%D8%B5%D9%8A%D8%B1%D9%8A', 'https://www.buymeacoffee.com/vahidn']
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ jobs:
- name: Setup .NET Core
uses: actions/setup-dotnet@v1
with:
dotnet-version: 6.0.101
dotnet-version: 7.0.101
- name: Build DNTCaptcha.Core lib
run: dotnet build ./src/DNTCaptcha.Core/DNTCaptcha.Core.csproj --configuration Release
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,6 @@ services.AddDNTCaptcha();

## Note:

- You will need this [NuGet.config](/src/DNTCaptcha.Core/NuGet.config) file to restore the required dependencies.
- Don't use this setting, because it will destroy the encrypted part of the captcha's token:

```C#
Expand Down
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"sdk": {
"version": "6.0.101"
"version": "7.0.101"
}
}
5 changes: 4 additions & 1 deletion src/DNTCaptcha.AngularClient/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
root = true

[*]
charset = utf-8
indent_style = space
indent_size = 2
insert_final_newline = true
Expand All @@ -11,3 +10,7 @@ trim_trailing_whitespace = true
[*.md]
max_line_length = off
trim_trailing_whitespace = false

# Code files
[*.{cs,csx,vb,vbx,razor,html,htm,js,md,cshtml,xaml,vbhtml,aspx,txt,asax,ashx,asmx,master,config}]
charset = utf-8-bom
81 changes: 43 additions & 38 deletions src/DNTCaptcha.Core/CaptchaCryptoProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,36 @@
namespace DNTCaptcha.Core
{
/// <summary>
/// The default captcha protection provider
/// The default captcha protection provider
/// </summary>
public class CaptchaCryptoProvider : ICaptchaCryptoProvider
{
private readonly byte[] _keyBytes;

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

_keyBytes = getDesKey(options.Value.EncryptionKey);
}

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

/// <summary>
/// Decrypts the message
/// Decrypts the message
/// </summary>
public string? Decrypt(string inputText)
{
Expand All @@ -56,7 +54,7 @@ public CaptchaCryptoProvider(IOptions<DNTCaptchaOptions> options)
}

/// <summary>
/// Encrypts the message
/// Encrypts the message
/// </summary>
public string Encrypt(string inputText)
{
Expand All @@ -71,63 +69,69 @@ public string Encrypt(string inputText)
}

[SuppressMessage("Microsoft.Usage", "S5547:encrypt uses a weak cryptographic algorithm TripleDES",
Justification = "That's enough for our usecase!")]
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!")]
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!")]
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
})
{
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 binaryWriter = new BinaryWriter(cryptoStream))
{
// prepend IV to data
cipherStream.Write(des.IV); // This is an auto-generated random key
binaryWriter.Write(data);
cryptoStream.FlushFinalBlock();
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();
}
}

return cipherStream.ToArray();
}
}

[SuppressMessage("Microsoft.Usage", "S5547:encrypt uses a weak cryptographic algorithm TripleDES",
Justification = "That's enough for our usecase!")]
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!")]
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!")]
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
})
{
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);

using var ms = new MemoryStream();
using (var decryptor = new CryptoStream(ms, des.CreateDecryptor(_keyBytes, iv), CryptoStreamMode.Write))
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
);
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
);
}
}

return ms.ToArray();
}
}
Expand All @@ -138,6 +142,7 @@ private byte[] getDesKey(string? 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.
Expand Down
Loading

0 comments on commit ac62233

Please sign in to comment.