various simplifications
This commit is contained in:
parent
edec988e05
commit
e9f3631985
20 changed files with 16 additions and 585 deletions
|
@ -3,7 +3,6 @@ using System.IO;
|
||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
using BirdsiteLive.Common.Settings;
|
using BirdsiteLive.Common.Settings;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using Org.BouncyCastle.Asn1.IsisMtt.X509;
|
|
||||||
|
|
||||||
namespace BSLManager.Tools
|
namespace BSLManager.Tools
|
||||||
{
|
{
|
||||||
|
|
|
@ -6,8 +6,6 @@
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Asn1" Version="1.0.9" />
|
<PackageReference Include="Asn1" Version="1.0.9" />
|
||||||
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
|
|
||||||
<PackageReference Include="Portable.BouncyCastle" Version="1.8.6.7" />
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|
|
@ -1,28 +1,12 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Security.Cryptography;
|
using System.Security.Cryptography;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using Newtonsoft.Json;
|
using System.Text.Json;
|
||||||
|
|
||||||
namespace BirdsiteLive.Cryptography
|
namespace BirdsiteLive.Cryptography
|
||||||
{
|
{
|
||||||
public class MagicKey
|
public class MagicKey
|
||||||
{
|
{
|
||||||
//public class WebfingerLink
|
|
||||||
//{
|
|
||||||
// public string rel { get; set; }
|
|
||||||
// public string type { get; set; }
|
|
||||||
// public string href { get; set; }
|
|
||||||
// public string template { get; set; }
|
|
||||||
//}
|
|
||||||
|
|
||||||
//public class WebfingerResult
|
|
||||||
//{
|
|
||||||
// public string subject { get; set; }
|
|
||||||
// public List<string> aliases { get; set; }
|
|
||||||
// public List<WebfingerLink> links { get; set; }
|
|
||||||
//}
|
|
||||||
|
|
||||||
private string[] _parts;
|
private string[] _parts;
|
||||||
private RSA _rsa;
|
private RSA _rsa;
|
||||||
|
|
||||||
|
@ -38,14 +22,14 @@ namespace BirdsiteLive.Cryptography
|
||||||
|
|
||||||
private class RSAKeyParms
|
private class RSAKeyParms
|
||||||
{
|
{
|
||||||
public byte[] D;
|
public byte[] D { get; set; }
|
||||||
public byte[] DP;
|
public byte[] DP {get; set; }
|
||||||
public byte[] DQ;
|
public byte[] DQ {get; set; }
|
||||||
public byte[] Exponent;
|
public byte[] Exponent {get; set; }
|
||||||
public byte[] InverseQ;
|
public byte[] InverseQ {get; set; }
|
||||||
public byte[] Modulus;
|
public byte[] Modulus {get; set; }
|
||||||
public byte[] P;
|
public byte[] P {get; set; }
|
||||||
public byte[] Q;
|
public byte[] Q {get; set; }
|
||||||
|
|
||||||
public static RSAKeyParms From(RSAParameters parms)
|
public static RSAKeyParms From(RSAParameters parms)
|
||||||
{
|
{
|
||||||
|
@ -81,7 +65,9 @@ namespace BirdsiteLive.Cryptography
|
||||||
if (key[0] == '{')
|
if (key[0] == '{')
|
||||||
{
|
{
|
||||||
_rsa = RSA.Create();
|
_rsa = RSA.Create();
|
||||||
_rsa.ImportParameters(JsonConvert.DeserializeObject<RSAKeyParms>(key).Make());
|
Console.WriteLine(key);
|
||||||
|
Console.WriteLine(JsonSerializer.Deserialize<RSAKeyParms>(key).Make());
|
||||||
|
_rsa.ImportParameters(JsonSerializer.Deserialize<RSAKeyParms>(key).Make());
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -102,7 +88,7 @@ namespace BirdsiteLive.Cryptography
|
||||||
var rsa = RSA.Create();
|
var rsa = RSA.Create();
|
||||||
rsa.KeySize = 2048;
|
rsa.KeySize = 2048;
|
||||||
|
|
||||||
return new MagicKey(JsonConvert.SerializeObject(RSAKeyParms.From(rsa.ExportParameters(true))));
|
return new MagicKey(JsonSerializer.Serialize<RSAKeyParms>(RSAKeyParms.From(rsa.ExportParameters(true))));
|
||||||
}
|
}
|
||||||
|
|
||||||
public byte[] BuildSignedData(string data, string dataType, string encoding, string algorithm)
|
public byte[] BuildSignedData(string data, string dataType, string encoding, string algorithm)
|
||||||
|
@ -140,7 +126,7 @@ namespace BirdsiteLive.Cryptography
|
||||||
|
|
||||||
public string PrivateKey
|
public string PrivateKey
|
||||||
{
|
{
|
||||||
get { return JsonConvert.SerializeObject(RSAKeyParms.From(_rsa.ExportParameters(true))); }
|
get { return JsonSerializer.Serialize(RSAKeyParms.From(_rsa.ExportParameters(true))); }
|
||||||
}
|
}
|
||||||
|
|
||||||
public string PublicKey
|
public string PublicKey
|
||||||
|
|
|
@ -1,99 +0,0 @@
|
||||||
using System;
|
|
||||||
using System.IO;
|
|
||||||
using System.Security.Cryptography;
|
|
||||||
|
|
||||||
namespace BirdsiteLive.Cryptography
|
|
||||||
{
|
|
||||||
//https://gist.github.com/ststeiger/f4b29a140b1e3fd618679f89b7f3ff4a
|
|
||||||
//https://gist.github.com/valep27/4a720c25b35fff83fbf872516f847863
|
|
||||||
//https://gist.github.com/therightstuff/aa65356e95f8d0aae888e9f61aa29414
|
|
||||||
//https://stackoverflow.com/questions/52468125/export-rsa-public-key-in-der-format-and-decrypt-data
|
|
||||||
public class RsaGenerator
|
|
||||||
{
|
|
||||||
public string GetRsa()
|
|
||||||
{
|
|
||||||
var rsa = RSA.Create();
|
|
||||||
|
|
||||||
var outputStream = new StringWriter();
|
|
||||||
var parameters = rsa.ExportParameters(true);
|
|
||||||
using (var stream = new MemoryStream())
|
|
||||||
{
|
|
||||||
var writer = new BinaryWriter(stream);
|
|
||||||
writer.Write((byte)0x30); // SEQUENCE
|
|
||||||
using (var innerStream = new MemoryStream())
|
|
||||||
{
|
|
||||||
var innerWriter = new BinaryWriter(innerStream);
|
|
||||||
innerWriter.Write((byte)0x30); // SEQUENCE
|
|
||||||
EncodeLength(innerWriter, 13);
|
|
||||||
innerWriter.Write((byte)0x06); // OBJECT IDENTIFIER
|
|
||||||
var rsaEncryptionOid = new byte[] { 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01 };
|
|
||||||
EncodeLength(innerWriter, rsaEncryptionOid.Length);
|
|
||||||
innerWriter.Write(rsaEncryptionOid);
|
|
||||||
innerWriter.Write((byte)0x05); // NULL
|
|
||||||
EncodeLength(innerWriter, 0);
|
|
||||||
innerWriter.Write((byte)0x03); // BIT STRING
|
|
||||||
using (var bitStringStream = new MemoryStream())
|
|
||||||
{
|
|
||||||
var bitStringWriter = new BinaryWriter(bitStringStream);
|
|
||||||
bitStringWriter.Write((byte)0x00); // # of unused bits
|
|
||||||
bitStringWriter.Write((byte)0x30); // SEQUENCE
|
|
||||||
using (var paramsStream = new MemoryStream())
|
|
||||||
{
|
|
||||||
var paramsWriter = new BinaryWriter(paramsStream);
|
|
||||||
//EncodeIntegerBigEndian(paramsWriter, parameters.Modulus); // Modulus
|
|
||||||
//EncodeIntegerBigEndian(paramsWriter, parameters.Exponent); // Exponent
|
|
||||||
var paramsLength = (int)paramsStream.Length;
|
|
||||||
EncodeLength(bitStringWriter, paramsLength);
|
|
||||||
bitStringWriter.Write(paramsStream.GetBuffer(), 0, paramsLength);
|
|
||||||
}
|
|
||||||
var bitStringLength = (int)bitStringStream.Length;
|
|
||||||
EncodeLength(innerWriter, bitStringLength);
|
|
||||||
innerWriter.Write(bitStringStream.GetBuffer(), 0, bitStringLength);
|
|
||||||
}
|
|
||||||
var length = (int)innerStream.Length;
|
|
||||||
EncodeLength(writer, length);
|
|
||||||
writer.Write(innerStream.GetBuffer(), 0, length);
|
|
||||||
}
|
|
||||||
|
|
||||||
var base64 = Convert.ToBase64String(stream.GetBuffer(), 0, (int)stream.Length).ToCharArray();
|
|
||||||
// WriteLine terminates with \r\n, we want only \n
|
|
||||||
outputStream.Write("-----BEGIN PUBLIC KEY-----\n");
|
|
||||||
for (var i = 0; i < base64.Length; i += 64)
|
|
||||||
{
|
|
||||||
outputStream.Write(base64, i, Math.Min(64, base64.Length - i));
|
|
||||||
outputStream.Write("\n");
|
|
||||||
}
|
|
||||||
outputStream.Write("-----END PUBLIC KEY-----");
|
|
||||||
}
|
|
||||||
|
|
||||||
return outputStream.ToString();
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void EncodeLength(BinaryWriter stream, int length)
|
|
||||||
{
|
|
||||||
if (length < 0) throw new ArgumentOutOfRangeException("length", "Length must be non-negative");
|
|
||||||
if (length < 0x80)
|
|
||||||
{
|
|
||||||
// Short form
|
|
||||||
stream.Write((byte)length);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// Long form
|
|
||||||
var temp = length;
|
|
||||||
var bytesRequired = 0;
|
|
||||||
while (temp > 0)
|
|
||||||
{
|
|
||||||
temp >>= 8;
|
|
||||||
bytesRequired++;
|
|
||||||
}
|
|
||||||
stream.Write((byte)(bytesRequired | 0x80));
|
|
||||||
for (var i = bytesRequired - 1; i >= 0; i--)
|
|
||||||
{
|
|
||||||
stream.Write((byte)(length >> (8 * i) & 0xff));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,225 +0,0 @@
|
||||||
using Org.BouncyCastle.Crypto;
|
|
||||||
using Org.BouncyCastle.Crypto.Parameters;
|
|
||||||
using Org.BouncyCastle.OpenSsl;
|
|
||||||
using Org.BouncyCastle.Security;
|
|
||||||
using System;
|
|
||||||
using System.IO;
|
|
||||||
using System.Security.Cryptography;
|
|
||||||
|
|
||||||
namespace MyProject.Data.Encryption
|
|
||||||
{
|
|
||||||
public class RSAKeys
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Import OpenSSH PEM private key string into MS RSACryptoServiceProvider
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="pem"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
public static RSACryptoServiceProvider ImportPrivateKey(string pem)
|
|
||||||
{
|
|
||||||
PemReader pr = new PemReader(new StringReader(pem));
|
|
||||||
AsymmetricCipherKeyPair KeyPair = (AsymmetricCipherKeyPair)pr.ReadObject();
|
|
||||||
RSAParameters rsaParams = DotNetUtilities.ToRSAParameters((RsaPrivateCrtKeyParameters)KeyPair.Private);
|
|
||||||
|
|
||||||
RSACryptoServiceProvider csp = new RSACryptoServiceProvider();// cspParams);
|
|
||||||
csp.ImportParameters(rsaParams);
|
|
||||||
return csp;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Import OpenSSH PEM public key string into MS RSACryptoServiceProvider
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="pem"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
public static RSACryptoServiceProvider ImportPublicKey(string pem)
|
|
||||||
{
|
|
||||||
PemReader pr = new PemReader(new StringReader(pem));
|
|
||||||
AsymmetricKeyParameter publicKey = (AsymmetricKeyParameter)pr.ReadObject();
|
|
||||||
RSAParameters rsaParams = DotNetUtilities.ToRSAParameters((RsaKeyParameters)publicKey);
|
|
||||||
|
|
||||||
RSACryptoServiceProvider csp = new RSACryptoServiceProvider();// cspParams);
|
|
||||||
csp.ImportParameters(rsaParams);
|
|
||||||
return csp;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Export private (including public) key from MS RSACryptoServiceProvider into OpenSSH PEM string
|
|
||||||
/// slightly modified from https://stackoverflow.com/a/23739932/2860309
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="csp"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
public static string ExportPrivateKey(RSACryptoServiceProvider csp)
|
|
||||||
{
|
|
||||||
StringWriter outputStream = new StringWriter();
|
|
||||||
if (csp.PublicOnly) throw new ArgumentException("CSP does not contain a private key", "csp");
|
|
||||||
var parameters = csp.ExportParameters(true);
|
|
||||||
using (var stream = new MemoryStream())
|
|
||||||
{
|
|
||||||
var writer = new BinaryWriter(stream);
|
|
||||||
writer.Write((byte)0x30); // SEQUENCE
|
|
||||||
using (var innerStream = new MemoryStream())
|
|
||||||
{
|
|
||||||
var innerWriter = new BinaryWriter(innerStream);
|
|
||||||
EncodeIntegerBigEndian(innerWriter, new byte[] { 0x00 }); // Version
|
|
||||||
EncodeIntegerBigEndian(innerWriter, parameters.Modulus);
|
|
||||||
EncodeIntegerBigEndian(innerWriter, parameters.Exponent);
|
|
||||||
EncodeIntegerBigEndian(innerWriter, parameters.D);
|
|
||||||
EncodeIntegerBigEndian(innerWriter, parameters.P);
|
|
||||||
EncodeIntegerBigEndian(innerWriter, parameters.Q);
|
|
||||||
EncodeIntegerBigEndian(innerWriter, parameters.DP);
|
|
||||||
EncodeIntegerBigEndian(innerWriter, parameters.DQ);
|
|
||||||
EncodeIntegerBigEndian(innerWriter, parameters.InverseQ);
|
|
||||||
var length = (int)innerStream.Length;
|
|
||||||
EncodeLength(writer, length);
|
|
||||||
writer.Write(innerStream.GetBuffer(), 0, length);
|
|
||||||
}
|
|
||||||
|
|
||||||
var base64 = Convert.ToBase64String(stream.GetBuffer(), 0, (int)stream.Length).ToCharArray();
|
|
||||||
// WriteLine terminates with \r\n, we want only \n
|
|
||||||
outputStream.Write("-----BEGIN RSA PRIVATE KEY-----\n");
|
|
||||||
// Output as Base64 with lines chopped at 64 characters
|
|
||||||
for (var i = 0; i < base64.Length; i += 64)
|
|
||||||
{
|
|
||||||
outputStream.Write(base64, i, Math.Min(64, base64.Length - i));
|
|
||||||
outputStream.Write("\n");
|
|
||||||
}
|
|
||||||
outputStream.Write("-----END RSA PRIVATE KEY-----");
|
|
||||||
}
|
|
||||||
|
|
||||||
return outputStream.ToString();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Export public key from MS RSACryptoServiceProvider into OpenSSH PEM string
|
|
||||||
/// slightly modified from https://stackoverflow.com/a/28407693
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="csp"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
public static string ExportPublicKey(RSACryptoServiceProvider csp)
|
|
||||||
{
|
|
||||||
StringWriter outputStream = new StringWriter();
|
|
||||||
var parameters = csp.ExportParameters(false);
|
|
||||||
using (var stream = new MemoryStream())
|
|
||||||
{
|
|
||||||
var writer = new BinaryWriter(stream);
|
|
||||||
writer.Write((byte)0x30); // SEQUENCE
|
|
||||||
using (var innerStream = new MemoryStream())
|
|
||||||
{
|
|
||||||
var innerWriter = new BinaryWriter(innerStream);
|
|
||||||
innerWriter.Write((byte)0x30); // SEQUENCE
|
|
||||||
EncodeLength(innerWriter, 13);
|
|
||||||
innerWriter.Write((byte)0x06); // OBJECT IDENTIFIER
|
|
||||||
var rsaEncryptionOid = new byte[] { 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01 };
|
|
||||||
EncodeLength(innerWriter, rsaEncryptionOid.Length);
|
|
||||||
innerWriter.Write(rsaEncryptionOid);
|
|
||||||
innerWriter.Write((byte)0x05); // NULL
|
|
||||||
EncodeLength(innerWriter, 0);
|
|
||||||
innerWriter.Write((byte)0x03); // BIT STRING
|
|
||||||
using (var bitStringStream = new MemoryStream())
|
|
||||||
{
|
|
||||||
var bitStringWriter = new BinaryWriter(bitStringStream);
|
|
||||||
bitStringWriter.Write((byte)0x00); // # of unused bits
|
|
||||||
bitStringWriter.Write((byte)0x30); // SEQUENCE
|
|
||||||
using (var paramsStream = new MemoryStream())
|
|
||||||
{
|
|
||||||
var paramsWriter = new BinaryWriter(paramsStream);
|
|
||||||
EncodeIntegerBigEndian(paramsWriter, parameters.Modulus); // Modulus
|
|
||||||
EncodeIntegerBigEndian(paramsWriter, parameters.Exponent); // Exponent
|
|
||||||
var paramsLength = (int)paramsStream.Length;
|
|
||||||
EncodeLength(bitStringWriter, paramsLength);
|
|
||||||
bitStringWriter.Write(paramsStream.GetBuffer(), 0, paramsLength);
|
|
||||||
}
|
|
||||||
var bitStringLength = (int)bitStringStream.Length;
|
|
||||||
EncodeLength(innerWriter, bitStringLength);
|
|
||||||
innerWriter.Write(bitStringStream.GetBuffer(), 0, bitStringLength);
|
|
||||||
}
|
|
||||||
var length = (int)innerStream.Length;
|
|
||||||
EncodeLength(writer, length);
|
|
||||||
writer.Write(innerStream.GetBuffer(), 0, length);
|
|
||||||
}
|
|
||||||
|
|
||||||
var base64 = Convert.ToBase64String(stream.GetBuffer(), 0, (int)stream.Length).ToCharArray();
|
|
||||||
// WriteLine terminates with \r\n, we want only \n
|
|
||||||
outputStream.Write("-----BEGIN PUBLIC KEY-----\n");
|
|
||||||
for (var i = 0; i < base64.Length; i += 64)
|
|
||||||
{
|
|
||||||
outputStream.Write(base64, i, Math.Min(64, base64.Length - i));
|
|
||||||
outputStream.Write("\n");
|
|
||||||
}
|
|
||||||
outputStream.Write("-----END PUBLIC KEY-----");
|
|
||||||
}
|
|
||||||
|
|
||||||
return outputStream.ToString();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// https://stackoverflow.com/a/23739932/2860309
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="stream"></param>
|
|
||||||
/// <param name="length"></param>
|
|
||||||
private static void EncodeLength(BinaryWriter stream, int length)
|
|
||||||
{
|
|
||||||
if (length < 0) throw new ArgumentOutOfRangeException("length", "Length must be non-negative");
|
|
||||||
if (length < 0x80)
|
|
||||||
{
|
|
||||||
// Short form
|
|
||||||
stream.Write((byte)length);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// Long form
|
|
||||||
var temp = length;
|
|
||||||
var bytesRequired = 0;
|
|
||||||
while (temp > 0)
|
|
||||||
{
|
|
||||||
temp >>= 8;
|
|
||||||
bytesRequired++;
|
|
||||||
}
|
|
||||||
stream.Write((byte)(bytesRequired | 0x80));
|
|
||||||
for (var i = bytesRequired - 1; i >= 0; i--)
|
|
||||||
{
|
|
||||||
stream.Write((byte)(length >> (8 * i) & 0xff));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// https://stackoverflow.com/a/23739932/2860309
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="stream"></param>
|
|
||||||
/// <param name="value"></param>
|
|
||||||
/// <param name="forceUnsigned"></param>
|
|
||||||
private static void EncodeIntegerBigEndian(BinaryWriter stream, byte[] value, bool forceUnsigned = true)
|
|
||||||
{
|
|
||||||
stream.Write((byte)0x02); // INTEGER
|
|
||||||
var prefixZeros = 0;
|
|
||||||
for (var i = 0; i < value.Length; i++)
|
|
||||||
{
|
|
||||||
if (value[i] != 0) break;
|
|
||||||
prefixZeros++;
|
|
||||||
}
|
|
||||||
if (value.Length - prefixZeros == 0)
|
|
||||||
{
|
|
||||||
EncodeLength(stream, 1);
|
|
||||||
stream.Write((byte)0);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (forceUnsigned && value[prefixZeros] > 0x7f)
|
|
||||||
{
|
|
||||||
// Add a prefix zero to force unsigned if the MSB is 1
|
|
||||||
EncodeLength(stream, value.Length - prefixZeros + 1);
|
|
||||||
stream.Write((byte)0);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
EncodeLength(stream, value.Length - prefixZeros);
|
|
||||||
}
|
|
||||||
for (var i = prefixZeros; i < value.Length; i++)
|
|
||||||
{
|
|
||||||
stream.Write(value[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -11,8 +11,6 @@ using BirdsiteLive.Domain.Repository;
|
||||||
using BirdsiteLive.Domain.Statistics;
|
using BirdsiteLive.Domain.Statistics;
|
||||||
using BirdsiteLive.Domain.Tools;
|
using BirdsiteLive.Domain.Tools;
|
||||||
using BirdsiteLive.Twitter.Models;
|
using BirdsiteLive.Twitter.Models;
|
||||||
using Tweetinvi.Models;
|
|
||||||
using Tweetinvi.Models.Entities;
|
|
||||||
|
|
||||||
namespace BirdsiteLive.Domain
|
namespace BirdsiteLive.Domain
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
using BirdsiteLive.Domain.Repository;
|
using BirdsiteLive.Domain.Repository;
|
||||||
using Org.BouncyCastle.Pkcs;
|
|
||||||
|
|
||||||
namespace BirdsiteLive.Domain.Tools
|
namespace BirdsiteLive.Domain.Tools
|
||||||
{
|
{
|
||||||
|
|
|
@ -17,8 +17,6 @@ using BirdsiteLive.Domain.Statistics;
|
||||||
using BirdsiteLive.Domain.Tools;
|
using BirdsiteLive.Domain.Tools;
|
||||||
using BirdsiteLive.Twitter;
|
using BirdsiteLive.Twitter;
|
||||||
using BirdsiteLive.Twitter.Models;
|
using BirdsiteLive.Twitter.Models;
|
||||||
using Tweetinvi.Core.Exceptions;
|
|
||||||
using Tweetinvi.Models;
|
|
||||||
|
|
||||||
namespace BirdsiteLive.Domain
|
namespace BirdsiteLive.Domain
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
using BirdsiteLive.DAL.Models;
|
using BirdsiteLive.DAL.Models;
|
||||||
using BirdsiteLive.Twitter.Models;
|
using BirdsiteLive.Twitter.Models;
|
||||||
using Tweetinvi.Models;
|
|
||||||
|
|
||||||
namespace BirdsiteLive.Pipeline.Models
|
namespace BirdsiteLive.Pipeline.Models
|
||||||
{
|
{
|
||||||
|
|
|
@ -12,7 +12,6 @@ using BirdsiteLive.Twitter;
|
||||||
using BirdsiteLive.Twitter.Models;
|
using BirdsiteLive.Twitter.Models;
|
||||||
using BirdsiteLive.Common.Settings;
|
using BirdsiteLive.Common.Settings;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using Tweetinvi.Models;
|
|
||||||
|
|
||||||
namespace BirdsiteLive.Pipeline.Processors
|
namespace BirdsiteLive.Pipeline.Processors
|
||||||
{
|
{
|
||||||
|
|
|
@ -16,7 +16,6 @@ using BirdsiteLive.Pipeline.Processors.SubTasks;
|
||||||
using BirdsiteLive.Twitter;
|
using BirdsiteLive.Twitter;
|
||||||
using BirdsiteLive.Twitter.Models;
|
using BirdsiteLive.Twitter.Models;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using Tweetinvi.Models;
|
|
||||||
|
|
||||||
namespace BirdsiteLive.Pipeline.Processors
|
namespace BirdsiteLive.Pipeline.Processors
|
||||||
{
|
{
|
||||||
|
|
|
@ -24,7 +24,4 @@
|
||||||
<ProjectReference Include="..\BirdsiteLive.Twitter\BirdsiteLive.Twitter.csproj" />
|
<ProjectReference Include="..\BirdsiteLive.Twitter\BirdsiteLive.Twitter.csproj" />
|
||||||
<ProjectReference Include="..\DataAccessLayers\BirdsiteLive.DAL.Postgres\BirdsiteLive.DAL.Postgres.csproj" />
|
<ProjectReference Include="..\DataAccessLayers\BirdsiteLive.DAL.Postgres\BirdsiteLive.DAL.Postgres.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|
|
@ -1,83 +0,0 @@
|
||||||
using System;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using BirdsiteLive.DAL.Contracts;
|
|
||||||
using BirdsiteLive.DAL.Models;
|
|
||||||
using BirdsiteLive.DAL.Postgres.DataAccessLayers.Base;
|
|
||||||
using BirdsiteLive.DAL.Postgres.Settings;
|
|
||||||
using BirdsiteLive.DAL.Postgres.Tools;
|
|
||||||
using Dapper;
|
|
||||||
using Newtonsoft.Json;
|
|
||||||
using Tweetinvi.Models;
|
|
||||||
|
|
||||||
namespace BirdsiteLive.DAL.Postgres.DataAccessLayers
|
|
||||||
{
|
|
||||||
public class CachedTweetsPostgresDal : PostgresBase, ICachedTweetsDal
|
|
||||||
{
|
|
||||||
#region Ctor
|
|
||||||
public CachedTweetsPostgresDal(PostgresSettings settings) : base(settings)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
public async Task CreateTweetAsync(long tweetId, int userId, CachedTweet tweet)
|
|
||||||
{
|
|
||||||
if(tweetId == default) throw new ArgumentException("tweetId");
|
|
||||||
if(userId == default) throw new ArgumentException("userId");
|
|
||||||
|
|
||||||
var serializedData = JsonConvert.SerializeObject(tweet);
|
|
||||||
|
|
||||||
using (var dbConnection = Connection)
|
|
||||||
{
|
|
||||||
dbConnection.Open();
|
|
||||||
|
|
||||||
await dbConnection.ExecuteAsync(
|
|
||||||
$"INSERT INTO {_settings.CachedTweetsTableName} (id,twitterUserId,data) VALUES(@id,@twitterUserId,CAST(@data as json))",
|
|
||||||
new { id = tweetId, twitterUserId = userId, data = serializedData });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task<CachedTweet> GetTweetAsync(long tweetId)
|
|
||||||
{
|
|
||||||
if (tweetId == default) throw new ArgumentException("tweetId");
|
|
||||||
|
|
||||||
var query = $"SELECT * FROM {_settings.CachedTweetsTableName} WHERE id = @id";
|
|
||||||
|
|
||||||
using (var dbConnection = Connection)
|
|
||||||
{
|
|
||||||
dbConnection.Open();
|
|
||||||
|
|
||||||
var result = (await dbConnection.QueryAsync<SerializedTweet>(query, new { id = tweetId })).FirstOrDefault();
|
|
||||||
return Convert(result);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task DeleteTweetAsync(long tweetId)
|
|
||||||
{
|
|
||||||
if (tweetId == default) throw new ArgumentException("tweetId");
|
|
||||||
|
|
||||||
var query = $"DELETE FROM {_settings.CachedTweetsTableName} WHERE id = @id";
|
|
||||||
|
|
||||||
using (var dbConnection = Connection)
|
|
||||||
{
|
|
||||||
dbConnection.Open();
|
|
||||||
|
|
||||||
await dbConnection.QueryAsync(query, new { id = tweetId });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private CachedTweet Convert(SerializedTweet result)
|
|
||||||
{
|
|
||||||
if (result == null || string.IsNullOrWhiteSpace(result.Data)) return null;
|
|
||||||
return JsonConvert.DeserializeObject<CachedTweet>(result.Data);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
internal class SerializedTweet
|
|
||||||
{
|
|
||||||
public long Id { get; set; }
|
|
||||||
public int TwitterUserId { get; set; }
|
|
||||||
public string Data { get; set; }
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -7,7 +7,6 @@ using BirdsiteLive.DAL.Models;
|
||||||
using BirdsiteLive.DAL.Postgres.DataAccessLayers.Base;
|
using BirdsiteLive.DAL.Postgres.DataAccessLayers.Base;
|
||||||
using BirdsiteLive.DAL.Postgres.Settings;
|
using BirdsiteLive.DAL.Postgres.Settings;
|
||||||
using Dapper;
|
using Dapper;
|
||||||
using Newtonsoft.Json;
|
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using Npgsql;
|
using Npgsql;
|
||||||
|
|
||||||
|
@ -27,7 +26,7 @@ namespace BirdsiteLive.DAL.Postgres.DataAccessLayers
|
||||||
if(followings == null) followings = new int[0];
|
if(followings == null) followings = new int[0];
|
||||||
if(followingSyncStatus == null) followingSyncStatus = new Dictionary<int, long>();
|
if(followingSyncStatus == null) followingSyncStatus = new Dictionary<int, long>();
|
||||||
|
|
||||||
var serializedDic = JsonConvert.SerializeObject(followingSyncStatus);
|
var serializedDic = JsonSerializer.Serialize(followingSyncStatus);
|
||||||
|
|
||||||
acct = acct.ToLowerInvariant();
|
acct = acct.ToLowerInvariant();
|
||||||
host = host.ToLowerInvariant();
|
host = host.ToLowerInvariant();
|
||||||
|
@ -205,7 +204,7 @@ namespace BirdsiteLive.DAL.Postgres.DataAccessLayers
|
||||||
ActorId = follower.ActorId,
|
ActorId = follower.ActorId,
|
||||||
SharedInboxRoute = follower.SharedInboxRoute,
|
SharedInboxRoute = follower.SharedInboxRoute,
|
||||||
Followings = follower.Followings.ToList(),
|
Followings = follower.Followings.ToList(),
|
||||||
FollowingsSyncStatus = JsonConvert.DeserializeObject<Dictionary<int,long>>(follower.FollowingsSyncStatus),
|
FollowingsSyncStatus = JsonSerializer.Deserialize<Dictionary<int,long>>(follower.FollowingsSyncStatus),
|
||||||
PostingErrorCount = follower.PostingErrorCount
|
PostingErrorCount = follower.PostingErrorCount
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,6 @@
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="TweetinviAPI" Version="4.0.3" />
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using BirdsiteLive.DAL.Models;
|
using BirdsiteLive.DAL.Models;
|
||||||
using Tweetinvi.Models;
|
|
||||||
|
|
||||||
namespace BirdsiteLive.DAL.Contracts
|
namespace BirdsiteLive.DAL.Contracts
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,15 +0,0 @@
|
||||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
||||||
|
|
||||||
namespace BirdsiteLive.Cryptography.Tests
|
|
||||||
{
|
|
||||||
[TestClass]
|
|
||||||
public class RsaGeneratorTests
|
|
||||||
{
|
|
||||||
[TestMethod]
|
|
||||||
public void TestMethod1()
|
|
||||||
{
|
|
||||||
var rsaGen = new RsaGenerator();
|
|
||||||
var rsa = rsaGen.GetRsa();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,34 +0,0 @@
|
||||||
//using System.Security.Cryptography;
|
|
||||||
//using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
||||||
//using MyProject.Data.Encryption;
|
|
||||||
|
|
||||||
//namespace BirdsiteLive.Cryptography.Tests
|
|
||||||
//{
|
|
||||||
// [TestClass]
|
|
||||||
// public class RsaKeysTests
|
|
||||||
// {
|
|
||||||
// [TestMethod]
|
|
||||||
// public void TestMethod1()
|
|
||||||
// {
|
|
||||||
// var rsa = RSA.Create();
|
|
||||||
|
|
||||||
// var cspParams = new CspParameters();
|
|
||||||
// cspParams.ProviderType = 1; // PROV_RSA_FULL
|
|
||||||
// cspParams.Flags = CspProviderFlags.CreateEphemeralKey;
|
|
||||||
// var rsaProvider = new RSACryptoServiceProvider(2048, cspParams);
|
|
||||||
|
|
||||||
// var rsaPublicKey = RSAKeys.ExportPublicKey(rsaProvider);
|
|
||||||
// var rsaPrivateKey = RSAKeys.ExportPrivateKey(rsaProvider);
|
|
||||||
|
|
||||||
// //rsaProvider.
|
|
||||||
|
|
||||||
// var pem = RSAKeys.ImportPublicKey(rsaPrivateKey);
|
|
||||||
// }
|
|
||||||
|
|
||||||
// [TestMethod]
|
|
||||||
// public void TestMethod2()
|
|
||||||
// {
|
|
||||||
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//}
|
|
|
@ -1,81 +0,0 @@
|
||||||
using System;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using BirdsiteLive.DAL.Models;
|
|
||||||
using BirdsiteLive.DAL.Postgres.DataAccessLayers;
|
|
||||||
using BirdsiteLive.DAL.Postgres.Tests.DataAccessLayers.Base;
|
|
||||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
||||||
|
|
||||||
namespace BirdsiteLive.DAL.Postgres.Tests.DataAccessLayers
|
|
||||||
{
|
|
||||||
[TestClass]
|
|
||||||
public class CachedTweetsPostgresDalTests : PostgresTestingBase
|
|
||||||
{
|
|
||||||
[TestInitialize]
|
|
||||||
public async Task TestInit()
|
|
||||||
{
|
|
||||||
var dal = new DbInitializerPostgresDal(_settings, _tools);
|
|
||||||
var init = new DatabaseInitializer(dal);
|
|
||||||
await init.InitAndMigrateDbAsync();
|
|
||||||
}
|
|
||||||
|
|
||||||
[TestCleanup]
|
|
||||||
public async Task CleanUp()
|
|
||||||
{
|
|
||||||
var dal = new DbInitializerPostgresDal(_settings, _tools);
|
|
||||||
await dal.DeleteAllAsync();
|
|
||||||
}
|
|
||||||
|
|
||||||
[TestMethod]
|
|
||||||
public async Task CreateAndGet()
|
|
||||||
{
|
|
||||||
var id = 152L;
|
|
||||||
var userId = 15;
|
|
||||||
|
|
||||||
var tweet = new CachedTweet
|
|
||||||
{
|
|
||||||
UserId = userId,
|
|
||||||
Id = id,
|
|
||||||
Text = "text data",
|
|
||||||
FullText = "full text data",
|
|
||||||
CreatedAt = DateTime.UtcNow
|
|
||||||
};
|
|
||||||
|
|
||||||
var dal = new CachedTweetsPostgresDal(_settings);
|
|
||||||
await dal.CreateTweetAsync(id, userId, tweet);
|
|
||||||
|
|
||||||
var result = await dal.GetTweetAsync(id);
|
|
||||||
|
|
||||||
Assert.IsNotNull(result);
|
|
||||||
Assert.AreEqual(id, result.Id);
|
|
||||||
Assert.AreEqual(tweet.Text, result.Text);
|
|
||||||
Assert.AreEqual(tweet.FullText, result.FullText);
|
|
||||||
Assert.AreEqual(tweet.CreatedAt, result.CreatedAt);
|
|
||||||
}
|
|
||||||
|
|
||||||
[TestMethod]
|
|
||||||
public async Task CreateAndDelete()
|
|
||||||
{
|
|
||||||
var id = 152L;
|
|
||||||
var userId = 15;
|
|
||||||
|
|
||||||
var tweet = new CachedTweet
|
|
||||||
{
|
|
||||||
UserId = userId,
|
|
||||||
Id = id,
|
|
||||||
Text = "text data",
|
|
||||||
FullText = "full text data",
|
|
||||||
CreatedAt = DateTime.UtcNow
|
|
||||||
};
|
|
||||||
|
|
||||||
var dal = new CachedTweetsPostgresDal(_settings);
|
|
||||||
await dal.CreateTweetAsync(id, userId, tweet);
|
|
||||||
|
|
||||||
var result = await dal.GetTweetAsync(id);
|
|
||||||
Assert.IsNotNull(result);
|
|
||||||
|
|
||||||
await dal.DeleteTweetAsync(id);
|
|
||||||
result = await dal.GetTweetAsync(id);
|
|
||||||
Assert.IsNull(result);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -5,7 +5,6 @@ using BirdsiteLive.DAL.Models;
|
||||||
using BirdsiteLive.Domain.BusinessUseCases;
|
using BirdsiteLive.Domain.BusinessUseCases;
|
||||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||||
using Moq;
|
using Moq;
|
||||||
using Org.BouncyCastle.Crypto.Prng;
|
|
||||||
|
|
||||||
namespace BirdsiteLive.Domain.Tests.BusinessUseCases
|
namespace BirdsiteLive.Domain.Tests.BusinessUseCases
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Reference in a new issue