Created
October 25, 2018 14:47
-
-
Save serdarb/cf6bcc012ff6a817621b118782990a72 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Security.Cryptography; | |
using System.Text; | |
namespace ConsoleApp | |
{ | |
class Program | |
{ | |
class ECDsaHelper | |
{ | |
public CngAlgorithm Algorithm { get; set; } | |
public Encoding Encoding { get; set; } | |
public byte[] Key { get; set; } | |
public ECDsaHelper(CngAlgorithm algorithm, Encoding encoding) | |
{ | |
Algorithm = algorithm; | |
Encoding = encoding; | |
} | |
public void CreateKey() | |
{ | |
using (var dsa = new ECDsaCng()) | |
{ | |
dsa.HashAlgorithm = Algorithm; | |
SetKey(dsa); | |
} | |
} | |
internal byte[] SignData(byte[] dataInBytes) | |
{ | |
byte[] signature = null; | |
using (var dsa = GetECDsaCng()) | |
{ | |
dsa.HashAlgorithm = Algorithm; | |
if (Key == null) | |
{ | |
SetKey(dsa); | |
} | |
signature = dsa.SignData(dataInBytes); | |
} | |
return signature; | |
} | |
private void SetKey(ECDsaCng dsa) | |
{ | |
Key = dsa.Key.Export(CngKeyBlobFormat.EccPublicBlob); | |
} | |
private static ECDsaCng GetECDsaCng() | |
{ | |
return new ECDsaCng(); | |
} | |
internal bool Validate(byte[] dataInBytes, byte[] signature) | |
{ | |
using (var dsa = new ECDsaCng(CngKey.Import(Key, CngKeyBlobFormat.EccPublicBlob))) | |
{ | |
dsa.HashAlgorithm = Algorithm; | |
return dsa.VerifyData(dataInBytes, signature); | |
} | |
} | |
} | |
class SendPackage | |
{ | |
public ECDsaHelper ECDsaHelper { get; set; } | |
public string Data { get; set; } | |
public byte[] Signature { get; set; } | |
public SendPackage(ECDsaHelper eCDsaHelper) | |
{ | |
ECDsaHelper = eCDsaHelper; | |
} | |
internal void SignData() | |
{ | |
var dataInBytes = Encoding.UTF8.GetBytes(Data); | |
Signature = ECDsaHelper.SignData(dataInBytes); | |
} | |
} | |
class ReceivedPackage | |
{ | |
public ECDsaHelper ECDsaHelper { get; set; } | |
public string Data { get; set; } | |
public byte[] Signature { get; set; } | |
public ReceivedPackage(ECDsaHelper eCDsaHelper, string data, byte[] signature) | |
{ | |
ECDsaHelper = eCDsaHelper; | |
Data = data; | |
Signature = signature; | |
} | |
internal bool ValidateData() | |
{ | |
var result = ECDsaHelper.Validate(ECDsaHelper.Encoding.GetBytes(Data), Signature); | |
return result; | |
} | |
} | |
static void Main(string[] args) | |
{ | |
var helper = new ECDsaHelper(CngAlgorithm.Sha512, Encoding.UTF8); | |
helper.CreateKey(); | |
var sendPackage = new SendPackage(helper); | |
sendPackage.Data = "data to send"; | |
sendPackage.SignData(); | |
var receiver = new ReceivedPackage(helper, sendPackage.Data, sendPackage.Signature); | |
var result = receiver.ValidateData(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
import System.Security.Cryptography.Cng to use this.