2 using System.Collections.Generic;
5 using System.Threading.Tasks;
7 using Org.BouncyCastle.Math;
8 using Org.BouncyCastle.Math.EC;
10 using Org.BouncyCastle.Crypto;
11 using Org.BouncyCastle.Crypto.Generators;
12 using Org.BouncyCastle.Crypto.Parameters;
14 using Org.BouncyCastle.Asn1.X9;
15 using Org.BouncyCastle.Security;
16 using Org.BouncyCastle.Asn1.Sec;
21 /// Representation of ECDSA public key
26 private static X9ECParameters curve = SecNamedCurves.GetByName("secp256k1");
27 private static ECDomainParameters domain = new ECDomainParameters(curve.Curve, curve.G, curve.N, curve.H, curve.GetSeed());
30 /// Initializes a new instance of CPubKey class as the copy of another instance
32 /// <param name="pubKey">Another CPubKey instance</param>
33 public CPubKey(CPubKey pubKey)
39 /// Initializes a new instance of CPubKey class using supplied sequence of bytes
41 /// <param name="bytes">Byte sequence</param>
42 public CPubKey(IEnumerable<byte> bytes)
44 Q = ((ECPublicKeyParameters)PublicKeyFactory.CreateKey(bytes.ToArray())).Q;
47 public CPubKey(ECPoint pQ)
53 /// Quick validity test
55 /// <returns>Validation result</returns>
58 get { return !Q.IsInfinity; }
62 /// Is this a compressed public key?
64 /// <returns></returns>
65 public bool IsCompressed
67 get { return Q.IsCompressed; }
71 /// Calculate Hash160 and create new CKeyID instance.
73 /// <returns>New key ID</returns>
74 public CKeyID GetKeyID()
76 return new CKeyID(Hash160.Compute160(Raw));
79 public bool Verify(IEnumerable<byte> data, IEnumerable<byte> signature)
81 byte[] dataBytes = data.ToArray();
83 ISigner signer = SignerUtilities.GetSigner("SHA-256withECDSA");
84 ECPublicKeyParameters keyParameters = new ECPublicKeyParameters(Q, domain);
85 signer.Init(false, keyParameters);
86 signer.BlockUpdate(dataBytes, 0, dataBytes.Length);
88 return signer.VerifySignature(signature.ToArray());
92 /// Accessor for internal representation
94 public IEnumerable<byte> Raw
96 get { return Q.GetEncoded(); }
99 public override string ToString()
101 return Interop.ToHex(Raw);