diff options
| author | ivarlovlie <git@ivarlovlie.no> | 2021-11-20 21:31:30 +0100 |
|---|---|---|
| committer | ivarlovlie <git@ivarlovlie.no> | 2021-11-20 21:31:30 +0100 |
| commit | 7ff7f3902e5bdcc33bfc07fa6ad71a5798d7f8d6 (patch) | |
| tree | 6e7eaaed636558c4feb88f2bb18bbb7d4c0c87be /src/IOL.Helpers/CryptographyHelpers.cs | |
| parent | 4e41b1f31a7309b299398fd9dd53d499204fa6cf (diff) | |
| download | dotnet-helpers-7ff7f3902e5bdcc33bfc07fa6ad71a5798d7f8d6.tar.xz dotnet-helpers-7ff7f3902e5bdcc33bfc07fa6ad71a5798d7f8d6.zip | |
Change to file-scoped namespaces.
Add ConditionalWhere to QueryableHelpers.cs
Diffstat (limited to 'src/IOL.Helpers/CryptographyHelpers.cs')
| -rw-r--r-- | src/IOL.Helpers/CryptographyHelpers.cs | 263 |
1 files changed, 131 insertions, 132 deletions
diff --git a/src/IOL.Helpers/CryptographyHelpers.cs b/src/IOL.Helpers/CryptographyHelpers.cs index 6ea18b6..4821613 100644 --- a/src/IOL.Helpers/CryptographyHelpers.cs +++ b/src/IOL.Helpers/CryptographyHelpers.cs @@ -3,171 +3,170 @@ using System.Linq; using System.Security.Cryptography; using System.Text; -namespace IOL.Helpers +namespace IOL.Helpers; + +public static class CryptographyHelpers { - public static class CryptographyHelpers - { - // https://github.com/DuendeSoftware/IdentityServer/blob/main/src/IdentityServer/Extensions/HashExtensions.cs + // https://github.com/DuendeSoftware/IdentityServer/blob/main/src/IdentityServer/Extensions/HashExtensions.cs - private const int AES_BLOCK_BYTE_SIZE = 128 / 8; - private static readonly RandomNumberGenerator _random = RandomNumberGenerator.Create(); + private const int AES_BLOCK_BYTE_SIZE = 128 / 8; + private static readonly RandomNumberGenerator _random = RandomNumberGenerator.Create(); - /// <summary> - /// Creates a MD5 hash of the specified input. - /// </summary> - /// <returns>A hash</returns> - public static string Md5(this string input, string salt = default) { - if (input.IsNullOrWhiteSpace()) return string.Empty; + /// <summary> + /// Creates a MD5 hash of the specified input. + /// </summary> + /// <returns>A hash</returns> + public static string Md5(this string input, string salt = default) { + if (input.IsNullOrWhiteSpace()) return string.Empty; - var hmacMd5 = salt.HasValue() ? new HMACMD5(Encoding.UTF8.GetBytes(salt ?? "")) : new HMACMD5(); - var saltedHash = hmacMd5.ComputeHash(Encoding.UTF8.GetBytes(input)); - return Convert.ToBase64String(saltedHash); - } + var hmacMd5 = salt.HasValue() ? new HMACMD5(Encoding.UTF8.GetBytes(salt ?? "")) : new HMACMD5(); + var saltedHash = hmacMd5.ComputeHash(Encoding.UTF8.GetBytes(input)); + return Convert.ToBase64String(saltedHash); + } - /// <summary> - /// Method to perform a very simple (and classical) encryption for a string. This is NOT at - /// all secure, it is only intended to make the string value non-obvious at a first glance. - /// - /// The shiftOrUnshift argument is an arbitrary "key value", and must be a non-zero integer - /// between -65535 and 65535 (inclusive). To decrypt the encrypted string you use the negative - /// value. For example, if you encrypt with -42, then you decrypt with +42, or vice-versa. - /// - /// This is inspired by, and largely based on, this: - /// https://stackoverflow.com/a/13026595/253938 - /// </summary> - /// <param name="inputString">string to be encrypted or decrypted, must not be null</param> - /// <param name="shiftOrUnshift">see above</param> - /// <returns>encrypted or decrypted string</returns> - public static string CaesarCipher(string inputString, int shiftOrUnshift) { - const int C64_K = ushort.MaxValue + 1; - if (inputString == null) throw new ArgumentException("Must not be null.", nameof(inputString)); - switch (shiftOrUnshift) { - case 0: throw new ArgumentException("Must not be zero.", nameof(shiftOrUnshift)); - case <= -C64_K: - case >= C64_K: throw new ArgumentException("Out of range.", nameof(shiftOrUnshift)); - } + /// <summary> + /// Method to perform a very simple (and classical) encryption for a string. This is NOT at + /// all secure, it is only intended to make the string value non-obvious at a first glance. + /// + /// The shiftOrUnshift argument is an arbitrary "key value", and must be a non-zero integer + /// between -65535 and 65535 (inclusive). To decrypt the encrypted string you use the negative + /// value. For example, if you encrypt with -42, then you decrypt with +42, or vice-versa. + /// + /// This is inspired by, and largely based on, this: + /// https://stackoverflow.com/a/13026595/253938 + /// </summary> + /// <param name="inputString">string to be encrypted or decrypted, must not be null</param> + /// <param name="shiftOrUnshift">see above</param> + /// <returns>encrypted or decrypted string</returns> + public static string CaesarCipher(string inputString, int shiftOrUnshift) { + const int C64_K = ushort.MaxValue + 1; + if (inputString == null) throw new ArgumentException("Must not be null.", nameof(inputString)); + switch (shiftOrUnshift) { + case 0: throw new ArgumentException("Must not be zero.", nameof(shiftOrUnshift)); + case <= -C64_K: + case >= C64_K: throw new ArgumentException("Out of range.", nameof(shiftOrUnshift)); + } - // Perform the Caesar cipher shifting, using modulo operator to provide wrap-around - var charArray = new char[inputString.Length]; - for (var i = 0; i < inputString.Length; i++) { - charArray[i] = + // Perform the Caesar cipher shifting, using modulo operator to provide wrap-around + var charArray = new char[inputString.Length]; + for (var i = 0; i < inputString.Length; i++) { + charArray[i] = Convert.ToChar((Convert.ToInt32(inputString[i]) + shiftOrUnshift + C64_K) % C64_K); - } - - return new string(charArray); } - //https://tomrucki.com/posts/aes-encryption-in-csharp/ - public static string EncryptWithAes(this string toEncrypt, string password) { - var key = GetKey(password); + return new string(charArray); + } + + //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 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 + 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); + var result = new byte[iv.Length + cipherText.Length]; + iv.CopyTo(result, 0); + cipherText.CopyTo(result, iv.Length); - return Convert.ToBase64String(result); - } + return Convert.ToBase64String(result); + } - private static Aes CreateAes() { - var aes = Aes.Create(); - aes.Mode = CipherMode.CBC; - aes.Padding = PaddingMode.PKCS7; - return aes; - } + 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); + 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 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 + 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); - } + return Encoding.UTF8.GetString(decryptedBytes); + } - private static byte[] GenerateRandomBytes(int numberOfBytes) { - var randomBytes = new byte[numberOfBytes]; - _random.GetBytes(randomBytes); - return randomBytes; - } + 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; + } - /// <summary> - /// Creates a SHA256 hash of the specified input. - /// </summary> - /// <param name="input">The input.</param> - /// <returns>A hash</returns> - public static string Sha256(this string input) { - if (input.IsNullOrWhiteSpace()) return string.Empty; - using var sha = SHA256.Create(); - var bytes = Encoding.UTF8.GetBytes(input); - var hash = sha.ComputeHash(bytes); + /// <summary> + /// Creates a SHA256 hash of the specified input. + /// </summary> + /// <param name="input">The input.</param> + /// <returns>A hash</returns> + public static string Sha256(this string input) { + if (input.IsNullOrWhiteSpace()) return string.Empty; - return Convert.ToBase64String(hash); - } + using var sha = SHA256.Create(); + var bytes = Encoding.UTF8.GetBytes(input); + var hash = sha.ComputeHash(bytes); - /// <summary> - /// Creates a SHA256 hash of the specified input. - /// </summary> - /// <param name="input">The input.</param> - /// <returns>A hash.</returns> - public static byte[] Sha256(this byte[] input) { - if (input == null) { - return null; - } + return Convert.ToBase64String(hash); + } - using var sha = SHA256.Create(); - return sha.ComputeHash(input); + /// <summary> + /// Creates a SHA256 hash of the specified input. + /// </summary> + /// <param name="input">The input.</param> + /// <returns>A hash.</returns> + public static byte[] Sha256(this byte[] input) { + if (input == null) { + return null; } - /// <summary> - /// Creates a SHA512 hash of the specified input. - /// </summary> - /// <param name="input">The input.</param> - /// <returns>A hash</returns> - public static string Sha512(this string input) { - if (input.IsNullOrWhiteSpace()) return string.Empty; + using var sha = SHA256.Create(); + return sha.ComputeHash(input); + } - using var sha = SHA512.Create(); - var bytes = Encoding.UTF8.GetBytes(input); - var hash = sha.ComputeHash(bytes); + /// <summary> + /// Creates a SHA512 hash of the specified input. + /// </summary> + /// <param name="input">The input.</param> + /// <returns>A hash</returns> + public static string Sha512(this string input) { + if (input.IsNullOrWhiteSpace()) return string.Empty; - return Convert.ToBase64String(hash); - } + using var sha = SHA512.Create(); + var bytes = Encoding.UTF8.GetBytes(input); + var hash = sha.ComputeHash(bytes); + return Convert.ToBase64String(hash); + } - /// <summary> - /// Creates a SHA256 hash of the specified input. - /// </summary> - /// <param name="input">The input.</param> - /// <returns>A hash.</returns> - public static byte[] Sha512(this byte[] input) { - if (input == null) { - return null; - } - using var sha = SHA512.Create(); - return sha.ComputeHash(input); + /// <summary> + /// Creates a SHA256 hash of the specified input. + /// </summary> + /// <param name="input">The input.</param> + /// <returns>A hash.</returns> + public static byte[] Sha512(this byte[] input) { + if (input == null) { + return null; } + + using var sha = SHA512.Create(); + return sha.ComputeHash(input); } -}
\ No newline at end of file +} |
