Skip to content

Commit

Permalink
target-typed new #15
Browse files Browse the repository at this point in the history
  • Loading branch information
christiannagel committed Sep 3, 2020
1 parent 56eb2a5 commit 1092a03
Showing 1 changed file with 32 additions and 52 deletions.
84 changes: 32 additions & 52 deletions 02_Core/Security/SigningDemo/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,61 +2,41 @@
using System.Security.Cryptography;
using System.Text;

namespace SigningDemo
{
class Program
{

CngKey _aliceKeySignature;
private byte[] _alicePubKeyBlob;

static void Main()
{
var p = new Program();
p.Run();
}
(var aliceKeys, var alicePublicKey) = GetAliceKeys();

public void Run()
{
InitAliceKeys();
byte[] aliceData = Encoding.UTF8.GetBytes("Alice");
byte[] aliceSignature = CreateSignature(aliceData, aliceKeys);
Console.WriteLine($"Alice created signature: {Convert.ToBase64String(aliceSignature)}");

byte[] aliceData = Encoding.UTF8.GetBytes("Alice");
byte[] aliceSignature = CreateSignature(aliceData, _aliceKeySignature);
Console.WriteLine($"Alice created signature: {Convert.ToBase64String(aliceSignature)}");

if (VerifySignature(aliceData, aliceSignature, _alicePubKeyBlob))
{
Console.WriteLine("Alice signature verified successfully");
}
}

public void InitAliceKeys()
{
_aliceKeySignature = CngKey.Create(CngAlgorithm.ECDsaP521);
_alicePubKeyBlob = _aliceKeySignature.Export(CngKeyBlobFormat.GenericPublicBlob);
}
if (VerifySignature(aliceData, aliceSignature, alicePublicKey))
{
Console.WriteLine("Alice signature verified successfully");
}

private byte[] CreateSignature(byte[] data, CngKey key)
{
byte[] signature;
using (var signingAlg = new ECDsaCng(key))
{
signature = signingAlg.SignData(data, HashAlgorithmName.SHA512);
signingAlg.Clear();
}
return signature;
}
(CngKey KeyPair, byte[] PublicKey) GetAliceKeys()
{
var aliceKeyPair = CngKey.Create(CngAlgorithm.ECDsaP521);
var alicePublicKey = aliceKeyPair.Export(CngKeyBlobFormat.GenericPublicBlob);
return (aliceKeyPair, alicePublicKey);
}

private bool VerifySignature(byte[] data, byte[] signature, byte[] pubKey)
{
bool retValue = false;
using (CngKey key = CngKey.Import(pubKey, CngKeyBlobFormat.GenericPublicBlob))
using (var signingAlg = new ECDsaCng(key))
{
retValue = signingAlg.VerifyData(data, signature, HashAlgorithmName.SHA512);
signingAlg.Clear();
}
return retValue;
}
byte[] CreateSignature(byte[] data, CngKey key)
{
byte[] signature;
using (var signingAlg = new ECDsaCng(key))
{
signature = signingAlg.SignData(data, HashAlgorithmName.SHA512);
signingAlg.Clear();
}
return signature;
}

bool VerifySignature(byte[] data, byte[] signature, byte[] pubKey)
{
bool retValue = false;
using CngKey key = CngKey.Import(pubKey, CngKeyBlobFormat.GenericPublicBlob);
using var signingAlg = new ECDsaCng(key);
retValue = signingAlg.VerifyData(data, signature, HashAlgorithmName.SHA512);
signingAlg.Clear();
return retValue;
}

0 comments on commit 1092a03

Please sign in to comment.