GabrielTofvesson bdbb1342ba Massive update
* Added new endpoint for updating password
* Added internationalization method to ContextManager and Context
* Updated contexts to use internationalization
* Added a fancy text-based UI to the server
* Added translations
* Moved Promise class to its own file
* Made BankNetInteractor its own file
* Added a lot of convenient methods
* Added many more comments
* Fixed input event management in ButtonView
* Added support for dynamic ListView content modification
* Added more layouts
* Fixed some namespaces
* Added more commands to the server
2018-05-13 20:04:01 +02:00

35 lines
1.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;
using Tofvesson.Crypto;
namespace Tofvesson.Common.Cryptography.KeyExchange
{
public sealed class DiffieHellman : IKeyExchange
{
private static readonly BigInteger EPHEMERAL_MAX = BigInteger.One << 2048;
private static readonly RandomProvider provider = new CryptoRandomProvider();
private BigInteger priv, p, q;
private readonly BigInteger pub;
public DiffieHellman(BigInteger p, BigInteger q) : this(provider.GenerateRandom(EPHEMERAL_MAX), p, q) { }
public DiffieHellman(BigInteger priv, BigInteger p, BigInteger q)
{
this.priv = priv;
this.p = p;
this.q = q;
pub = Support.ModExp(p, priv, q);
}
public byte[] GetPublicKey() => pub.ToByteArray();
public byte[] GetSharedSecret(byte[] p) {
BigInteger pub = new BigInteger(p);
return (pub <= 0 ? (BigInteger) 0 : Support.ModExp(pub, priv, q)).ToByteArray();
}
}
}