/Cryptography/AES.cs |
@@ -8,18 +8,18 @@ |
using System.Collections.Generic; |
using System.IO; |
using System.Linq; |
using System.Threading.Tasks; |
using System.Security.Cryptography; |
using System.Text; |
|
namespace wasSharpNET.Cryptography |
{ |
public class AES |
public static class AES |
{ |
private static readonly RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider(); |
private const int AES_BLOCK_SIZE = 128; |
private const CipherMode AES_CIPHER_MODE = CipherMode.CBC; |
private const PaddingMode AES_PADDING_MODE = PaddingMode.PKCS7; |
private const int AES_KEY_SALT_BYTES = 16; |
private static readonly RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider(); |
|
/////////////////////////////////////////////////////////////////////////// |
// Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3 // |
@@ -31,7 +31,7 @@ |
/// <param name="key">the encryption key</param> |
/// <param name="separator">the separator to use between the cyphertext and the IV</param> |
/// <returns>Base64 encoded encrypted data</returns> |
public string wasAESEncrypt(string data, string key, string separator = ":") |
public static async Task<string> Encrypt(string data, string key, string separator = ":") |
{ |
using (var rijdanelManaged = new RijndaelManaged()) |
{ |
@@ -44,10 +44,9 @@ |
var salt = new byte[AES_KEY_SALT_BYTES]; |
rng.GetBytes(salt); |
var derivedKey = new Rfc2898DeriveBytes(key, salt); |
rijdanelManaged.Key = derivedKey.GetBytes(rijdanelManaged.KeySize/8); |
rijdanelManaged.IV = derivedKey.GetBytes(rijdanelManaged.BlockSize/8); |
rijdanelManaged.Key = derivedKey.GetBytes(rijdanelManaged.KeySize / 8); |
rijdanelManaged.IV = derivedKey.GetBytes(rijdanelManaged.BlockSize / 8); |
|
byte[] encryptedData; |
using (var encryptor = rijdanelManaged.CreateEncryptor(rijdanelManaged.Key, rijdanelManaged.IV)) |
{ |
using (var memoryStream = new MemoryStream()) |
@@ -56,13 +55,12 @@ |
{ |
using (var streamWriter = new StreamWriter(cryptoStream)) |
{ |
streamWriter.Write(data); |
await streamWriter.WriteAsync(data); |
return string.Join(separator, Convert.ToBase64String(salt), Convert.ToBase64String(memoryStream.ToArray())); |
} |
} |
encryptedData = memoryStream.ToArray(); |
} |
} |
return string.Join(separator, Convert.ToBase64String(salt), Convert.ToBase64String(encryptedData)); |
} |
} |
|
@@ -79,14 +77,13 @@ |
/// <param name="key">the encryption key</param> |
/// <param name="separator">the separator to use between the cyphertext and the IV</param> |
/// <returns>the decrypted data</returns> |
public string wasAESDecrypt(string data, string key, string separator = ":") |
public static async Task<string> Decrypt(string data, string key, string separator = ":") |
{ |
// retrieve the salt from the data. |
var segments = new List<string>(data.Split(new[] {separator}, StringSplitOptions.None)); |
if (!segments.Count.Equals(2)) |
if (!segments.Count().Equals(2)) |
throw new ArgumentException("Invalid data."); |
|
string plaintext; |
using (var rijdanelManaged = new RijndaelManaged()) |
{ |
// FIPS-197 / CBC |
@@ -96,8 +93,8 @@ |
|
// Retrieve the key and the IV from the salt. |
var derivedKey = new Rfc2898DeriveBytes(key, Convert.FromBase64String(segments.First().Trim())); |
rijdanelManaged.Key = derivedKey.GetBytes(rijdanelManaged.KeySize/8); |
rijdanelManaged.IV = derivedKey.GetBytes(rijdanelManaged.BlockSize/8); |
rijdanelManaged.Key = derivedKey.GetBytes(rijdanelManaged.KeySize / 8); |
rijdanelManaged.IV = derivedKey.GetBytes(rijdanelManaged.BlockSize / 8); |
|
using (var decryptor = rijdanelManaged.CreateDecryptor(rijdanelManaged.Key, rijdanelManaged.IV)) |
{ |
@@ -107,13 +104,12 @@ |
{ |
using (var streamReader = new StreamReader(cryptoStream)) |
{ |
plaintext = streamReader.ReadToEnd(); |
return await streamReader.ReadToEndAsync(); |
} |
} |
} |
} |
} |
return plaintext; |
} |
} |
} |
/Cryptography/SHA1.cs |
@@ -0,0 +1,101 @@ |
/////////////////////////////////////////////////////////////////////////// |
// Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3 // |
// Please see: http://www.gnu.org/licenses/gpl.html for legal details, // |
// rights of fair usage, the disclaimer and warranty conditions. // |
/////////////////////////////////////////////////////////////////////////// |
|
using System; |
using System.Collections.Generic; |
using System.IO; |
using System.Linq; |
using System.Text; |
using System.Threading.Tasks; |
|
namespace wasSharpNET.Cryptography |
{ |
public static class SHA1 |
{ |
/// <summary> |
/// Return a 40 character hex representation of a SHA1 hash. |
/// </summary> |
/// <param name="sha1">the SHA1 hash object</param> |
/// <param name="data">the byte data to compute the hash from</param> |
public static string ToHex(this System.Security.Cryptography.SHA1 sha1, byte[] data) |
{ |
return BitConverter.ToString(sha1.ComputeHash(data)).Replace("-", ""); |
} |
|
/// <summary> |
/// Return a 40 character hex representation of a SHA1 hash from multiple sequential byte arrays. |
/// </summary> |
/// <param name="sha1">the SHA1 hash object</param> |
/// <param name="data">the byte data to compute the hash from</param> |
public static string ToHex(this System.Security.Cryptography.SHA1 sha1, IEnumerable<byte[]> data) |
{ |
return sha1.ToHex(data.Where(i => i != null).SelectMany(i => i).ToArray()); |
} |
|
/// <summary> |
/// Return a 40 character hex representation of a SHA1 hash. |
/// </summary> |
/// <param name="sha1">the SHA1 hash object</param> |
/// <param name="data">the string data to compute the hash from</param> |
public static string ToHex(this System.Security.Cryptography.SHA1 sha1, string data) |
{ |
return BitConverter.ToString(sha1.ComputeHash(Encoding.UTF8.GetBytes(data))).Replace("-", ""); |
} |
|
/// <summary> |
/// Return a 40 character hex representation of a SHA1 hash. |
/// </summary> |
/// <param name="sha1">the SHA1 hash object</param> |
/// <param name="data">the memorystream to compute the hash from</param> |
public static string ToHex(this System.Security.Cryptography.SHA1 sha1, Stream data) |
{ |
return BitConverter.ToString(sha1.ComputeHash(data)).Replace("-", ""); |
} |
|
/// <summary> |
/// Return a 40 character hex representation of a SHA1 hash. |
/// </summary> |
/// <param name="sha1">the SHA1 hash object</param> |
/// <param name="data">a filestream to read the file from</param> |
public static string ToHex(this System.Security.Cryptography.SHA1 sha1, FileStream data) |
{ |
return BitConverter.ToString(sha1.ComputeHash(data)).Replace("-", ""); |
} |
|
/////////////////////////////////////////////////////////////////////////// |
// Copyright (C) Wizardry and Steamworks 2014 - License: GNU GPLv3 // |
/////////////////////////////////////////////////////////////////////////// |
/// <summary> |
/// Compute the SHA1 hash of an array of bytes and copy the result to the output stream as a hexadecimal string. |
/// </summary> |
/// <param name="sha1">the SHA1 object to use</param> |
/// <param name="data">the data to hash</param> |
/// <param name="outStream">the output stream</param> |
public static async Task CopyToAsync(this System.Security.Cryptography.SHA1 sha1, byte[] data, Stream outStream) |
{ |
var buffer = new char[1]; |
using (var SHA1OutputStream = new MemoryStream()) |
{ |
using (var SHA1InputStream = |
new StringReader(BitConverter.ToString(sha1.ComputeHash(data)).Replace("-", ""))) |
{ |
int count; |
while ((count = await SHA1InputStream.ReadAsync(buffer, 0, 1)) != 0) |
switch (buffer[0]) |
{ |
case '-': |
continue; |
default: |
SHA1OutputStream.WriteByte((byte) buffer[0]); |
break; |
} |
} |
SHA1OutputStream.Position = 0L; |
await SHA1OutputStream.CopyToAsync(outStream); |
} |
} |
} |
} |