diff options
| author | ivarlovlie <git@ivarlovlie.no> | 2021-03-31 18:00:22 +0200 |
|---|---|---|
| committer | ivarlovlie <git@ivarlovlie.no> | 2021-03-31 18:00:22 +0200 |
| commit | e6ca7a484d9cc213fb00c34ebf7cb55ace892c04 (patch) | |
| tree | e885d823c224d55503286b1dab207fcc7c5c68d1 /src/IOL.VippsEcommerce/Helpers.cs | |
| download | dotnet-vipps-ecommerce-e6ca7a484d9cc213fb00c34ebf7cb55ace892c04.tar.xz dotnet-vipps-ecommerce-e6ca7a484d9cc213fb00c34ebf7cb55ace892c04.zip | |
Initial commit
Diffstat (limited to 'src/IOL.VippsEcommerce/Helpers.cs')
| -rw-r--r-- | src/IOL.VippsEcommerce/Helpers.cs | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/src/IOL.VippsEcommerce/Helpers.cs b/src/IOL.VippsEcommerce/Helpers.cs new file mode 100644 index 0000000..8d6fb50 --- /dev/null +++ b/src/IOL.VippsEcommerce/Helpers.cs @@ -0,0 +1,86 @@ +using System; +using System.IO; +using System.Linq; +using System.Security.Cryptography; +using System.Text; + +namespace IOL.VippsEcommerce +{ + internal static class Helpers + { + private const int AES_BLOCK_BYTE_SIZE = 128 / 8; + private static readonly RandomNumberGenerator _random = RandomNumberGenerator.Create(); + + public static bool IsNullOrWhiteSpace(this string value) => string.IsNullOrWhiteSpace(value); + public static bool IsPresent(this string value) => !string.IsNullOrWhiteSpace(value); + + public static bool IsDirectoryWritable(this string dirPath, bool throwIfFails = false) { + try { + using (var fs = File.Create( + Path.Combine(dirPath, Path.GetRandomFileName()), + 1, + FileOptions.DeleteOnClose) + ) { } + + return true; + } catch { + if (throwIfFails) + throw; + + return false; + } + } + + //https://tomrucki.com/posts/aes-encryption-in-csharp/ + public static string EncryptWithAes(this string toEncrypt, string password) { + var key = GetKey(password); + + using var aes = CreateAes(); + var iv = GenerateRandomBytes(AES_BLOCK_BYTE_SIZE); + var plainText = Encoding.UTF8.GetBytes(toEncrypt); + + using var encryptor = aes.CreateEncryptor(key, iv); + var cipherText = encryptor + .TransformFinalBlock(plainText, 0, plainText.Length); + + var result = new byte[iv.Length + cipherText.Length]; + iv.CopyTo(result, 0); + cipherText.CopyTo(result, iv.Length); + + return Convert.ToBase64String(result); + } + + private static Aes CreateAes() { + var aes = Aes.Create(); + aes.Mode = CipherMode.CBC; + aes.Padding = PaddingMode.PKCS7; + return aes; + } + + public static string DecryptWithAes(this string input, string password) { + var key = GetKey(password); + var encryptedData = Convert.FromBase64String(input); + + using var aes = CreateAes(); + var iv = encryptedData.Take(AES_BLOCK_BYTE_SIZE).ToArray(); + var cipherText = encryptedData.Skip(AES_BLOCK_BYTE_SIZE).ToArray(); + + using var decryptor = aes.CreateDecryptor(key, iv); + var decryptedBytes = decryptor + .TransformFinalBlock(cipherText, 0, cipherText.Length); + return Encoding.UTF8.GetString(decryptedBytes); + } + + private static byte[] GetKey(string password) { + var keyBytes = Encoding.UTF8.GetBytes(password); + using var md5 = MD5.Create(); + return md5.ComputeHash(keyBytes); + } + + private static byte[] GenerateRandomBytes(int numberOfBytes) { + var randomBytes = new byte[numberOfBytes]; + _random.GetBytes(randomBytes); + return randomBytes; + } + } +}
\ No newline at end of file |
