mirror of
https://git.teknik.io/Teknikode/Teknik.git
synced 2023-08-02 14:16:22 +02:00
- Removed Inferno crypto library
- Converted SHA384 hashing to use standard .net library - Moved rest of crypto.cs into the cryptography folder/namespace
This commit is contained in:
parent
366ad08e83
commit
656693e0c6
@ -16,6 +16,7 @@ using Teknik.Configuration;
|
||||
using Teknik.Utilities;
|
||||
using Teknik.Models;
|
||||
using System.Threading.Tasks;
|
||||
using Teknik.Utilities.Cryptography;
|
||||
|
||||
namespace ServerMaint
|
||||
{
|
||||
@ -168,7 +169,7 @@ namespace ServerMaint
|
||||
byte[] keyBytes = Encoding.UTF8.GetBytes(upload.Key);
|
||||
byte[] ivBytes = Encoding.UTF8.GetBytes(upload.IV);
|
||||
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
|
||||
AESCryptoStream aesStream = new AESCryptoStream(fs, false, keyBytes, ivBytes);
|
||||
AesCounterStream aesStream = new AesCounterStream(fs, false, keyBytes, ivBytes);
|
||||
|
||||
// We have the data, let's scan it
|
||||
ClamScanResult scanResult = clam.SendAndScanFile(aesStream);
|
||||
|
@ -52,10 +52,6 @@
|
||||
<Prefer32Bit>false</Prefer32Bit>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="BouncyCastle.Crypto, Version=1.8.1.0, Culture=neutral, PublicKeyToken=0e99375e54769942">
|
||||
<HintPath>..\packages\BouncyCastle.1.8.1\lib\BouncyCastle.Crypto.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="CommandLine, Version=1.9.71.2, Culture=neutral, PublicKeyToken=de6f01bd326f8c32, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\CommandLineParser.1.9.71\lib\net45\CommandLine.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
@ -84,10 +80,6 @@
|
||||
<HintPath>..\packages\Newtonsoft.Json.10.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="SecurityDriven.Inferno, Version=1.4.0.0, Culture=neutral, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Inferno.1.4.0\lib\net452\SecurityDriven.Inferno.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.ComponentModel.DataAnnotations" />
|
||||
<Reference Include="System.Core" />
|
||||
|
@ -1,10 +1,8 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<packages>
|
||||
<package id="BouncyCastle" version="1.8.1" targetFramework="net452" />
|
||||
<package id="CommandLineParser" version="1.9.71" targetFramework="net452" />
|
||||
<package id="EntityFramework" version="6.1.3" targetFramework="net452" />
|
||||
<package id="GitVersionTask" version="3.6.5" targetFramework="net462" developmentDependency="true" />
|
||||
<package id="Inferno" version="1.4.0" targetFramework="net452" />
|
||||
<package id="Microsoft.AspNet.Identity.Core" version="2.2.1" targetFramework="net452" />
|
||||
<package id="Microsoft.AspNet.Identity.EntityFramework" version="2.2.1" targetFramework="net452" />
|
||||
<package id="nClam" version="2.0.6.0" targetFramework="net462" />
|
||||
|
@ -79,7 +79,7 @@ namespace Teknik.Areas.Paste.Controllers
|
||||
string hash = string.Empty;
|
||||
if (!string.IsNullOrEmpty(password))
|
||||
{
|
||||
byte[] passBytes = Utilities.SHA384.Hash(paste.Key, password);
|
||||
byte[] passBytes = Utilities.Cryptography.SHA384.Hash(paste.Key, password);
|
||||
hash = passBytes.ToHex();
|
||||
// We need to convert old pastes to the new password scheme
|
||||
if (paste.Transfers.ToList().Exists(t => t.Type == TransferTypes.ASCIIPassword))
|
||||
@ -103,8 +103,8 @@ namespace Teknik.Areas.Paste.Controllers
|
||||
data = Convert.FromBase64String(paste.Content);
|
||||
// Now we decrypt the content
|
||||
byte[] ivBytes = Encoding.Unicode.GetBytes(paste.IV);
|
||||
byte[] keyBytes = AES.CreateKey(password, ivBytes, paste.KeySize);
|
||||
data = AES.Decrypt(data, keyBytes, ivBytes);
|
||||
byte[] keyBytes = AesCounterManaged.CreateKey(password, ivBytes, paste.KeySize);
|
||||
data = AesCounterManaged.Decrypt(data, keyBytes, ivBytes);
|
||||
model.Content = Encoding.Unicode.GetString(data);
|
||||
}
|
||||
|
||||
|
@ -65,8 +65,8 @@ namespace Teknik.Areas.Paste
|
||||
// Encrypt Content
|
||||
byte[] data = Encoding.Unicode.GetBytes(content);
|
||||
byte[] ivBytes = Encoding.Unicode.GetBytes(iv);
|
||||
byte[] keyBytes = AES.CreateKey(password, ivBytes, config.PasteConfig.KeySize);
|
||||
byte[] encData = AES.Encrypt(data, keyBytes, ivBytes);
|
||||
byte[] keyBytes = AesCounterManaged.CreateKey(password, ivBytes, config.PasteConfig.KeySize);
|
||||
byte[] encData = AesCounterManaged.Encrypt(data, keyBytes, ivBytes);
|
||||
content = Convert.ToBase64String(encData);
|
||||
|
||||
paste.Key = key;
|
||||
|
@ -20,6 +20,7 @@ using Teknik.Models;
|
||||
using Teknik.Attributes;
|
||||
using System.Text;
|
||||
using Org.BouncyCastle.Crypto;
|
||||
using Teknik.Utilities.Cryptography;
|
||||
|
||||
namespace Teknik.Areas.Upload.Controllers
|
||||
{
|
||||
@ -291,7 +292,7 @@ namespace Teknik.Areas.Upload.Controllers
|
||||
|
||||
return new FileGenerateResult(url,
|
||||
contentType,
|
||||
(response) => ResponseHelper.StreamToOutput(response, true, new AESCryptoStream(fs, false, keyBytes, ivBytes), (int)length, Config.UploadConfig.ChunkSize),
|
||||
(response) => ResponseHelper.StreamToOutput(response, true, new AesCounterStream(fs, false, keyBytes, ivBytes), (int)length, Config.UploadConfig.ChunkSize),
|
||||
false);
|
||||
}
|
||||
else // Otherwise just send it
|
||||
|
@ -55,7 +55,7 @@ namespace Teknik.Areas.Upload
|
||||
byte[] ivBytes = Encoding.UTF8.GetBytes(iv);
|
||||
|
||||
// Encrypt the file to disk
|
||||
AES.EncryptToFile(filePath, file, config.UploadConfig.ChunkSize, keyBytes, ivBytes);
|
||||
AesCounterManaged.EncryptToFile(filePath, file, config.UploadConfig.ChunkSize, keyBytes, ivBytes);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -16,6 +16,7 @@ using QRCoder;
|
||||
using TwoStepsAuthenticator;
|
||||
using System.Drawing;
|
||||
using Teknik.Attributes;
|
||||
using Teknik.Utilities.Cryptography;
|
||||
|
||||
namespace Teknik.Areas.Users.Controllers
|
||||
{
|
||||
|
@ -17,6 +17,7 @@ using Teknik.Areas.Users.Models;
|
||||
using Teknik.Configuration;
|
||||
using Teknik.Utilities;
|
||||
using Teknik.Models;
|
||||
using Teknik.Utilities.Cryptography;
|
||||
|
||||
namespace Teknik.Areas.Users.Utility
|
||||
{
|
||||
|
@ -132,10 +132,6 @@
|
||||
<HintPath>..\packages\QRCoder.1.2.3\lib\net40\QRCoder.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="SecurityDriven.Inferno, Version=1.4.0.0, Culture=neutral, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Inferno.1.4.0\lib\net452\SecurityDriven.Inferno.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.Data.DataSetExtensions" />
|
||||
|
@ -8,7 +8,6 @@
|
||||
<package id="EntityFramework" version="6.1.3" targetFramework="net452" userInstalled="true" />
|
||||
<package id="FontAwesome" version="4.7.0" targetFramework="net462" userInstalled="true" />
|
||||
<package id="GitVersionTask" version="3.6.5" targetFramework="net462" developmentDependency="true" />
|
||||
<package id="Inferno" version="1.4.0" targetFramework="net452" userInstalled="true" />
|
||||
<package id="IRCSharp" version="1.0.0.1" targetFramework="net462" />
|
||||
<package id="jQuery" version="3.1.1" targetFramework="net452" userInstalled="true" />
|
||||
<package id="jQuery.Validation" version="1.16.0" targetFramework="net462" userInstalled="true" />
|
||||
|
@ -3,6 +3,7 @@ using System.IO;
|
||||
using System.Threading;
|
||||
using Newtonsoft.Json;
|
||||
using Teknik.Utilities;
|
||||
using Teknik.Utilities.Cryptography;
|
||||
|
||||
namespace Teknik.Configuration
|
||||
{
|
||||
|
@ -56,7 +56,7 @@ namespace Teknik.Utilities
|
||||
return;
|
||||
}
|
||||
|
||||
using (var hashAlgorithm = SHA256.CreateHashAlgorithm())
|
||||
using (var hashAlgorithm = Cryptography.SHA256.CreateHashAlgorithm())
|
||||
{
|
||||
var hash = HttpServerUtility.UrlTokenEncode(hashAlgorithm.ComputeHash(Encoding.Unicode.GetBytes(response.Content)));
|
||||
context.BundleCollection.GetBundleFor(context.BundleVirtualPath).CdnPath = string.Format("{0}/{1}/{2}?v={3}&group={4}", CdnHost.TrimEnd('/'), dir, file, hash, group);
|
||||
|
@ -1,227 +0,0 @@
|
||||
using System.Text;
|
||||
using SecurityDriven.Inferno.Hash;
|
||||
using SecurityDriven.Inferno.Mac;
|
||||
using System.IO;
|
||||
using System.Security.Cryptography;
|
||||
using Org.BouncyCastle.Utilities.Encoders;
|
||||
using Org.BouncyCastle.Bcpg.OpenPgp;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO.MemoryMappedFiles;
|
||||
|
||||
namespace Teknik.Utilities
|
||||
{
|
||||
public class MD5
|
||||
{
|
||||
public static string Hash(string value)
|
||||
{
|
||||
byte[] valBytes = Encoding.ASCII.GetBytes(value);
|
||||
System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
|
||||
byte[] hashBytes = md5.ComputeHash(valBytes);
|
||||
|
||||
StringBuilder sBuilder = new StringBuilder();
|
||||
|
||||
// Loop through each byte of the hashed data
|
||||
// and format each one as a hexadecimal string.
|
||||
for (int i = 0; i < hashBytes.Length; i++)
|
||||
{
|
||||
sBuilder.Append(hashBytes[i].ToString("x2"));
|
||||
}
|
||||
|
||||
// Return the hexadecimal string.
|
||||
return sBuilder.ToString();
|
||||
|
||||
}
|
||||
|
||||
public static string FileHash(string filename)
|
||||
{
|
||||
try
|
||||
{
|
||||
using (var md5 = System.Security.Cryptography.MD5.Create())
|
||||
{
|
||||
using (var stream = File.OpenRead(filename))
|
||||
{
|
||||
return BitConverter.ToString(md5.ComputeHash(stream)).Replace("-", "").ToLower();
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
||||
|
||||
public static string DataHash(string data)
|
||||
{
|
||||
try
|
||||
{
|
||||
using (var md5 = System.Security.Cryptography.MD5.Create())
|
||||
{
|
||||
// convert string to stream
|
||||
byte[] byteArray = Encoding.UTF8.GetBytes(data);
|
||||
using (MemoryStream stream = new MemoryStream(byteArray))
|
||||
{
|
||||
return BitConverter.ToString(md5.ComputeHash(stream)).Replace("-", "").ToLower();
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class SHA384
|
||||
{
|
||||
public static byte[] Hash(string key, string value)
|
||||
{
|
||||
byte[] keyBytes = Encoding.UTF8.GetBytes(key);
|
||||
byte[] data = Encoding.UTF8.GetBytes(value);
|
||||
|
||||
byte[] result = new HMAC2(HashFactories.SHA384, keyBytes).ComputeHash(data);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public class SHA256
|
||||
{
|
||||
public static string Hash(string value)
|
||||
{
|
||||
byte[] valueBytes = Encoding.Unicode.GetBytes(value);
|
||||
return Hash(valueBytes);
|
||||
}
|
||||
|
||||
public static string Hash(byte[] value)
|
||||
{
|
||||
HashAlgorithm hash = new SHA256CryptoServiceProvider();
|
||||
byte[] hashBytes = hash.ComputeHash(value);
|
||||
|
||||
return Convert.ToBase64String(hashBytes);
|
||||
}
|
||||
|
||||
public static byte[] Hash(Stream value)
|
||||
{
|
||||
HashAlgorithm hash = new SHA256CryptoServiceProvider();
|
||||
return hash.ComputeHash(value);
|
||||
}
|
||||
|
||||
public static string Hash(string value, string salt1, string salt2)
|
||||
{
|
||||
SHA256Managed hash = new SHA256Managed();
|
||||
SHA1 sha1 = new SHA1Managed();
|
||||
// gen salt2 hash
|
||||
byte[] dataSalt2 = Encoding.UTF8.GetBytes(salt2);
|
||||
byte[] salt2Bytes = hash.ComputeHash(dataSalt2);
|
||||
string salt2Str = string.Empty;
|
||||
foreach (byte x in salt2Bytes)
|
||||
{
|
||||
salt2Str += String.Format("{0:x2}", x);
|
||||
}
|
||||
string dataStr = salt1 + value + salt2Str;
|
||||
byte[] dataStrBytes = Encoding.UTF8.GetBytes(dataStr);
|
||||
byte[] shaBytes = sha1.ComputeHash(dataStrBytes);
|
||||
string sha1Str = string.Empty;
|
||||
foreach (byte x in shaBytes)
|
||||
{
|
||||
sha1Str += String.Format("{0:x2}", x);
|
||||
}
|
||||
byte[] sha1Bytes = Encoding.UTF8.GetBytes(sha1Str);
|
||||
byte[] valueBytes = hash.ComputeHash(sha1Bytes);
|
||||
string hashString = string.Empty;
|
||||
foreach (byte x in valueBytes)
|
||||
{
|
||||
hashString += String.Format("{0:x2}", x);
|
||||
}
|
||||
return hashString;
|
||||
}
|
||||
|
||||
public static System.Security.Cryptography.SHA256 CreateHashAlgorithm()
|
||||
{
|
||||
if (CryptoConfig.AllowOnlyFipsAlgorithms)
|
||||
{
|
||||
return new SHA256CryptoServiceProvider();
|
||||
}
|
||||
|
||||
return new SHA256Managed();
|
||||
}
|
||||
}
|
||||
|
||||
public static class PGP
|
||||
{
|
||||
public static bool IsPublicKey(string key)
|
||||
{
|
||||
bool isValid = false;
|
||||
|
||||
try
|
||||
{
|
||||
byte[] byteArray = Encoding.ASCII.GetBytes(key);
|
||||
using (MemoryStream stream = new MemoryStream(byteArray))
|
||||
{
|
||||
using (Stream decoderStream = PgpUtilities.GetDecoderStream(stream))
|
||||
{
|
||||
PgpPublicKeyRingBundle publicKeyBundle = new PgpPublicKeyRingBundle(decoderStream);
|
||||
PgpPublicKey foundKey = GetFirstPublicKey(publicKeyBundle);
|
||||
|
||||
if (foundKey != null)
|
||||
{
|
||||
isValid = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
isValid = false;
|
||||
}
|
||||
return isValid;
|
||||
}
|
||||
|
||||
public static string GetFingerprint(string key)
|
||||
{
|
||||
string hexString = string.Empty;
|
||||
byte[] byteArray = Encoding.ASCII.GetBytes(key);
|
||||
using (MemoryStream stream = new MemoryStream(byteArray))
|
||||
{
|
||||
using (Stream decoderStream = PgpUtilities.GetDecoderStream(stream))
|
||||
{
|
||||
PgpPublicKeyRingBundle publicKeyBundle = new PgpPublicKeyRingBundle(decoderStream);
|
||||
PgpPublicKey foundKey = GetFirstPublicKey(publicKeyBundle);
|
||||
|
||||
if (foundKey != null)
|
||||
{
|
||||
byte[] fing = foundKey.GetFingerprint();
|
||||
hexString = Hex.ToHexString(fing);
|
||||
}
|
||||
}
|
||||
}
|
||||
return hexString;
|
||||
}
|
||||
public static string GetFingerprint64(string key)
|
||||
{
|
||||
string fingerprint = GetFingerprint(key);
|
||||
if (fingerprint.Length > 16)
|
||||
fingerprint = fingerprint.Substring(fingerprint.Length - 16);
|
||||
return fingerprint;
|
||||
}
|
||||
|
||||
private static PgpPublicKey GetFirstPublicKey(PgpPublicKeyRingBundle publicKeyRingBundle)
|
||||
{
|
||||
foreach (PgpPublicKeyRing kRing in publicKeyRingBundle.GetKeyRings())
|
||||
{
|
||||
var keys = kRing.GetPublicKeys();
|
||||
foreach (var key in keys)
|
||||
{
|
||||
PgpPublicKey foundKey = (PgpPublicKey)key;
|
||||
//PgpPublicKey key = kRing.GetPublicKeys()
|
||||
//.Cast<PgpPublicKey>()
|
||||
// .Where(k => k.IsEncryptionKey)
|
||||
// .FirstOrDefault();
|
||||
if (foundKey != null && foundKey.IsEncryptionKey)
|
||||
return foundKey;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
@ -9,7 +9,7 @@ using System.Threading.Tasks;
|
||||
namespace Teknik.Utilities.Cryptography
|
||||
{
|
||||
|
||||
public class AES
|
||||
public class AesCounterManaged
|
||||
{
|
||||
public static byte[] Decrypt(byte[] data, string key, string iv)
|
||||
{
|
||||
@ -44,7 +44,7 @@ namespace Teknik.Utilities.Cryptography
|
||||
// Make sure the input stream is at the beginning
|
||||
input.Seek(0, SeekOrigin.Begin);
|
||||
|
||||
AESCryptoStream cryptoStream = new AESCryptoStream(input, encrypt, key, iv);
|
||||
AesCounterStream cryptoStream = new AesCounterStream(input, encrypt, key, iv);
|
||||
|
||||
// Initialize variables
|
||||
byte[] output = new byte[input.Length];
|
||||
@ -83,7 +83,7 @@ namespace Teknik.Utilities.Cryptography
|
||||
// Make sure the input stream is at the beginning
|
||||
input.Seek(0, SeekOrigin.Begin);
|
||||
|
||||
AESCryptoStream cryptoStream = new AESCryptoStream(input, true, key, iv);
|
||||
AesCounterStream cryptoStream = new AesCounterStream(input, true, key, iv);
|
||||
|
||||
using (FileStream fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write))
|
||||
{
|
@ -2,14 +2,12 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Teknik.Utilities.Cryptography;
|
||||
|
||||
namespace Teknik.Utilities
|
||||
namespace Teknik.Utilities.Cryptography
|
||||
{
|
||||
public class AESCryptoStream : Stream
|
||||
public class AesCounterStream : Stream
|
||||
{
|
||||
private Stream _Inner;
|
||||
private CounterModeCryptoTransform _Cipher;
|
||||
@ -23,7 +21,7 @@ namespace Teknik.Utilities
|
||||
/// <param name="encrypt"></param>
|
||||
/// <param name="key"></param>
|
||||
/// <param name="iv"></param>
|
||||
public AESCryptoStream(Stream stream, bool encrypt, byte[] key, byte[] iv)
|
||||
public AesCounterStream(Stream stream, bool encrypt, byte[] key, byte[] iv)
|
||||
{
|
||||
_Inner = stream;
|
||||
|
70
Utilities/Utilities/Cryptography/MD5.cs
Normal file
70
Utilities/Utilities/Cryptography/MD5.cs
Normal file
@ -0,0 +1,70 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Teknik.Utilities.Cryptography
|
||||
{
|
||||
public class MD5
|
||||
{
|
||||
public static string Hash(string value)
|
||||
{
|
||||
byte[] valBytes = Encoding.ASCII.GetBytes(value);
|
||||
System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
|
||||
byte[] hashBytes = md5.ComputeHash(valBytes);
|
||||
|
||||
StringBuilder sBuilder = new StringBuilder();
|
||||
|
||||
// Loop through each byte of the hashed data
|
||||
// and format each one as a hexadecimal string.
|
||||
for (int i = 0; i < hashBytes.Length; i++)
|
||||
{
|
||||
sBuilder.Append(hashBytes[i].ToString("x2"));
|
||||
}
|
||||
|
||||
// Return the hexadecimal string.
|
||||
return sBuilder.ToString();
|
||||
|
||||
}
|
||||
|
||||
public static string FileHash(string filename)
|
||||
{
|
||||
try
|
||||
{
|
||||
using (var md5 = System.Security.Cryptography.MD5.Create())
|
||||
{
|
||||
using (var stream = File.OpenRead(filename))
|
||||
{
|
||||
return BitConverter.ToString(md5.ComputeHash(stream)).Replace("-", "").ToLower();
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
||||
|
||||
public static string DataHash(string data)
|
||||
{
|
||||
try
|
||||
{
|
||||
using (var md5 = System.Security.Cryptography.MD5.Create())
|
||||
{
|
||||
// convert string to stream
|
||||
byte[] byteArray = Encoding.UTF8.GetBytes(data);
|
||||
using (MemoryStream stream = new MemoryStream(byteArray))
|
||||
{
|
||||
return BitConverter.ToString(md5.ComputeHash(stream)).Replace("-", "").ToLower();
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
90
Utilities/Utilities/Cryptography/PGP.cs
Normal file
90
Utilities/Utilities/Cryptography/PGP.cs
Normal file
@ -0,0 +1,90 @@
|
||||
using Org.BouncyCastle.Bcpg.OpenPgp;
|
||||
using Org.BouncyCastle.Utilities.Encoders;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Teknik.Utilities.Cryptography
|
||||
{
|
||||
public static class PGP
|
||||
{
|
||||
public static bool IsPublicKey(string key)
|
||||
{
|
||||
bool isValid = false;
|
||||
|
||||
try
|
||||
{
|
||||
byte[] byteArray = Encoding.ASCII.GetBytes(key);
|
||||
using (MemoryStream stream = new MemoryStream(byteArray))
|
||||
{
|
||||
using (Stream decoderStream = PgpUtilities.GetDecoderStream(stream))
|
||||
{
|
||||
PgpPublicKeyRingBundle publicKeyBundle = new PgpPublicKeyRingBundle(decoderStream);
|
||||
PgpPublicKey foundKey = GetFirstPublicKey(publicKeyBundle);
|
||||
|
||||
if (foundKey != null)
|
||||
{
|
||||
isValid = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
isValid = false;
|
||||
}
|
||||
return isValid;
|
||||
}
|
||||
|
||||
public static string GetFingerprint(string key)
|
||||
{
|
||||
string hexString = string.Empty;
|
||||
byte[] byteArray = Encoding.ASCII.GetBytes(key);
|
||||
using (MemoryStream stream = new MemoryStream(byteArray))
|
||||
{
|
||||
using (Stream decoderStream = PgpUtilities.GetDecoderStream(stream))
|
||||
{
|
||||
PgpPublicKeyRingBundle publicKeyBundle = new PgpPublicKeyRingBundle(decoderStream);
|
||||
PgpPublicKey foundKey = GetFirstPublicKey(publicKeyBundle);
|
||||
|
||||
if (foundKey != null)
|
||||
{
|
||||
byte[] fing = foundKey.GetFingerprint();
|
||||
hexString = Hex.ToHexString(fing);
|
||||
}
|
||||
}
|
||||
}
|
||||
return hexString;
|
||||
}
|
||||
|
||||
public static string GetFingerprint64(string key)
|
||||
{
|
||||
string fingerprint = GetFingerprint(key);
|
||||
if (fingerprint.Length > 16)
|
||||
fingerprint = fingerprint.Substring(fingerprint.Length - 16);
|
||||
return fingerprint;
|
||||
}
|
||||
|
||||
private static PgpPublicKey GetFirstPublicKey(PgpPublicKeyRingBundle publicKeyRingBundle)
|
||||
{
|
||||
foreach (PgpPublicKeyRing kRing in publicKeyRingBundle.GetKeyRings())
|
||||
{
|
||||
var keys = kRing.GetPublicKeys();
|
||||
foreach (var key in keys)
|
||||
{
|
||||
PgpPublicKey foundKey = (PgpPublicKey)key;
|
||||
//PgpPublicKey key = kRing.GetPublicKeys()
|
||||
//.Cast<PgpPublicKey>()
|
||||
// .Where(k => k.IsEncryptionKey)
|
||||
// .FirstOrDefault();
|
||||
if (foundKey != null && foundKey.IsEncryptionKey)
|
||||
return foundKey;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
73
Utilities/Utilities/Cryptography/SHA256.cs
Normal file
73
Utilities/Utilities/Cryptography/SHA256.cs
Normal file
@ -0,0 +1,73 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Teknik.Utilities.Cryptography
|
||||
{
|
||||
public class SHA256
|
||||
{
|
||||
public static string Hash(string value)
|
||||
{
|
||||
byte[] valueBytes = Encoding.Unicode.GetBytes(value);
|
||||
return Hash(valueBytes);
|
||||
}
|
||||
|
||||
public static string Hash(byte[] value)
|
||||
{
|
||||
HashAlgorithm hash = new SHA256CryptoServiceProvider();
|
||||
byte[] hashBytes = hash.ComputeHash(value);
|
||||
|
||||
return Convert.ToBase64String(hashBytes);
|
||||
}
|
||||
|
||||
public static byte[] Hash(Stream value)
|
||||
{
|
||||
HashAlgorithm hash = new SHA256CryptoServiceProvider();
|
||||
return hash.ComputeHash(value);
|
||||
}
|
||||
|
||||
public static string Hash(string value, string salt1, string salt2)
|
||||
{
|
||||
SHA256Managed hash = new SHA256Managed();
|
||||
SHA1 sha1 = new SHA1Managed();
|
||||
// gen salt2 hash
|
||||
byte[] dataSalt2 = Encoding.UTF8.GetBytes(salt2);
|
||||
byte[] salt2Bytes = hash.ComputeHash(dataSalt2);
|
||||
string salt2Str = string.Empty;
|
||||
foreach (byte x in salt2Bytes)
|
||||
{
|
||||
salt2Str += String.Format("{0:x2}", x);
|
||||
}
|
||||
string dataStr = salt1 + value + salt2Str;
|
||||
byte[] dataStrBytes = Encoding.UTF8.GetBytes(dataStr);
|
||||
byte[] shaBytes = sha1.ComputeHash(dataStrBytes);
|
||||
string sha1Str = string.Empty;
|
||||
foreach (byte x in shaBytes)
|
||||
{
|
||||
sha1Str += String.Format("{0:x2}", x);
|
||||
}
|
||||
byte[] sha1Bytes = Encoding.UTF8.GetBytes(sha1Str);
|
||||
byte[] valueBytes = hash.ComputeHash(sha1Bytes);
|
||||
string hashString = string.Empty;
|
||||
foreach (byte x in valueBytes)
|
||||
{
|
||||
hashString += String.Format("{0:x2}", x);
|
||||
}
|
||||
return hashString;
|
||||
}
|
||||
|
||||
public static System.Security.Cryptography.SHA256 CreateHashAlgorithm()
|
||||
{
|
||||
if (CryptoConfig.AllowOnlyFipsAlgorithms)
|
||||
{
|
||||
return new SHA256CryptoServiceProvider();
|
||||
}
|
||||
|
||||
return new SHA256Managed();
|
||||
}
|
||||
}
|
||||
}
|
22
Utilities/Utilities/Cryptography/SHA384.cs
Normal file
22
Utilities/Utilities/Cryptography/SHA384.cs
Normal file
@ -0,0 +1,22 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Teknik.Utilities.Cryptography
|
||||
{
|
||||
public class SHA384
|
||||
{
|
||||
public static byte[] Hash(string key, string value)
|
||||
{
|
||||
byte[] keyBytes = Encoding.UTF8.GetBytes(key);
|
||||
byte[] data = Encoding.UTF8.GetBytes(value);
|
||||
|
||||
var cipher = new System.Security.Cryptography.HMACSHA384(keyBytes);
|
||||
byte[] result = cipher.ComputeHash(data);
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
@ -56,10 +56,6 @@
|
||||
<HintPath>..\..\packages\Newtonsoft.Json.10.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="SecurityDriven.Inferno, Version=1.4.0.0, Culture=neutral, processorArchitecture=MSIL">
|
||||
<HintPath>..\..\packages\Inferno.1.4.0\lib\net452\SecurityDriven.Inferno.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Drawing" />
|
||||
@ -106,8 +102,13 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="AccountType.cs" />
|
||||
<Compile Include="Cryptography\AES.cs" />
|
||||
<Compile Include="Cryptography\AesCounterStream.cs" />
|
||||
<Compile Include="Cryptography\AesCounterManaged.cs" />
|
||||
<Compile Include="Cryptography\AesCounterMode.cs" />
|
||||
<Compile Include="Cryptography\MD5.cs" />
|
||||
<Compile Include="Cryptography\PGP.cs" />
|
||||
<Compile Include="Cryptography\SHA256.cs" />
|
||||
<Compile Include="Cryptography\SHA384.cs" />
|
||||
<Compile Include="CurrencyHelper.cs" />
|
||||
<Compile Include="CurrencyType.cs" />
|
||||
<Compile Include="EntityExtensions.cs" />
|
||||
@ -117,7 +118,6 @@
|
||||
<Compile Include="ByteExtensions.cs" />
|
||||
<Compile Include="ByteHelper.cs" />
|
||||
<Compile Include="Constants.cs" />
|
||||
<Compile Include="Crypto.cs" />
|
||||
<Compile Include="ExceptionExtensions.cs" />
|
||||
<Compile Include="FileHelper.cs" />
|
||||
<Compile Include="HttpRequestExtensions.cs" />
|
||||
@ -128,7 +128,6 @@
|
||||
<Compile Include="RequestHelper.cs" />
|
||||
<Compile Include="ResponseHelper.cs" />
|
||||
<Compile Include="RSSFeedResult.cs" />
|
||||
<Compile Include="StreamHelper.cs" />
|
||||
<Compile Include="StringExtensions.cs" />
|
||||
<Compile Include="StringHelper.cs" />
|
||||
<Compile Include="UrlExtensions.cs" />
|
||||
|
@ -3,7 +3,6 @@
|
||||
<package id="Antlr" version="3.5.0.2" targetFramework="net462" />
|
||||
<package id="BouncyCastle" version="1.8.1" targetFramework="net462" />
|
||||
<package id="GitVersionTask" version="3.6.5" targetFramework="net462" developmentDependency="true" />
|
||||
<package id="Inferno" version="1.4.0" targetFramework="net462" />
|
||||
<package id="MarkdownDeep.NET" version="1.5" targetFramework="net462" />
|
||||
<package id="Microsoft.AspNet.Mvc" version="5.2.3" targetFramework="net462" />
|
||||
<package id="Microsoft.AspNet.Razor" version="3.2.3" targetFramework="net462" />
|
||||
|
Loading…
Reference in New Issue
Block a user