Advertisement
TizzyT

ECDH256 -TizzyT

Jun 24th, 2018
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.88 KB | None | 0 0
  1.     public class ECDH256 : IDisposable
  2.     {
  3.         internal readonly ECDiffieHellmanCng ecdh;
  4.  
  5.         public byte[] PublicKey { get; }
  6.  
  7.         public ECDH256()
  8.         {
  9.             ecdh = new ECDiffieHellmanCng
  10.             {
  11.                 KeyDerivationFunction = ECDiffieHellmanKeyDerivationFunction.Hash,
  12.                 HashAlgorithm = CngAlgorithm.Sha256
  13.             };
  14.             PublicKey = ecdh.PublicKey.ToByteArray();
  15.         }
  16.  
  17.         public void Dispose() => ecdh.Dispose();
  18.     }
  19.  
  20.     public static class ECDH256Extensions
  21.     {
  22.         public static byte[] DeriveSharedKey(this ECDH256 ECDH, byte[] PublicKey)
  23.         {
  24.             byte[] SharedKey
  25.                 = ECDH.ecdh.DeriveKeyMaterial(ECDiffieHellmanCngPublicKey.FromByteArray(PublicKey, CngKeyBlobFormat.EccPublicBlob));
  26.             ECDH.Dispose();
  27.             return SharedKey;
  28.         }
  29.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement