Initial ...

This commit is contained in:
2026-03-22 14:08:25 +03:30
commit d3464ef39c
71 changed files with 19087 additions and 0 deletions
+168
View File
@@ -0,0 +1,168 @@
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using xCommons.Extensions;
using xCommons.Providers;
using xIds.Configurations;
using xIds.Interfaces;
namespace xIds.Providers
{
public class XSecurityProvider : IXSecurityProvider
{
//
#region Props ...
private readonly string secretKey;
private readonly byte[] iv;
private readonly byte[] key;
private readonly XValidationProvider validationProvider;
#endregion
//
#region Constructor ...
public XSecurityProvider(
XIdentityResourceConfiguration identityConfiguration,
XValidationProvider validationProvider
)
{
//
this.secretKey = identityConfiguration.XRevisionSecretKey.ToMd5String();
this.validationProvider = validationProvider;
//
var ivBytes = new byte[16];
var keyBytes = new byte[32];
var secretBytes = Encoding.UTF8.GetBytes(this.secretKey);
//
Array.Copy(
secretBytes,
ivBytes,
secretBytes.Length < ivBytes.Length ?
secretBytes.Length :
ivBytes.Length
);
this.iv = ivBytes;
//
Array.Copy(
secretBytes,
keyBytes,
secretBytes.Length < keyBytes.Length ?
secretBytes.Length :
keyBytes.Length
);
this.key = keyBytes;
// Encoding.UTF8.GetBytes (this.secretKey);
}
#endregion
//
#region Actions ...
public string Encrypt(string plainText)
{
//
validationProvider.NotEmpty(plainText);
//
var textBytes = Encoding.UTF8.GetBytes(plainText);
var result = EncryptFromBytes(textBytes);
//
return result;
}
public string Decrypt(string cipherText)
{
//
validationProvider.NotEmpty(cipherText);
//
var cipherBytes = Convert.FromBase64String(cipherText);
var result = DecryptFromBytes(cipherBytes);
//
return result;
}
public string EncryptFromBytes(byte[] textBytes)
{
//
// Validate Args ...
validationProvider.NotNull(textBytes);
// Declare the string used to hold
// the decrypted text.
byte[] encryptedBytes = null;
// Create an RijndaelManaged object
// with the specified key and IV.
using (var rijAlg = new RijndaelManaged())
{
//Settings
rijAlg.Mode = CipherMode.CBC;
rijAlg.Padding = PaddingMode.PKCS7;
// rijAlg.FeedbackSize = 128;
rijAlg.Key = key;
rijAlg.IV = iv;
//
using (var encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV))
{
encryptedBytes = encryptor.TransformFinalBlock(textBytes, 0, textBytes.Length);
}
}
//
return Convert.ToBase64String(encryptedBytes);
}
public string DecryptFromBytes(byte[] cipherBytes)
{
//
// Validate Args ...
validationProvider.NotNull(cipherBytes);
// Declare the string used to hold
// the decrypted text.
string plaintext = null;
// Create an RijndaelManaged object
// with the specified key and IV.
using (var rijAlg = new RijndaelManaged())
{
//Settings
rijAlg.Mode = CipherMode.CBC;
rijAlg.Padding = PaddingMode.PKCS7;
// rijAlg.FeedbackSize = 128;
rijAlg.Key = key;
rijAlg.IV = iv;
// Create a decrytor to perform the stream transform.
var decryptor = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV);
// Create the streams used for decryption.
using (var msDecrypt = new MemoryStream(cipherBytes))
{
using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (var srDecrypt = new StreamReader(csDecrypt))
{
// Read the decrypted bytes from the decrypting stream
// and place them in a string.
plaintext = srDecrypt.ReadToEnd();
}
}
}
}
//
return plaintext;
}
#endregion
}
}