public static DigitalSignature FromKey(byte[] privateKey, byte[] publicKey) { if (privateKey == null || privateKey.Length != 32) { return(null); } if (publicKey == null || publicKey.Length != 64) { return(null); } var param = new ECParameters() { Curve = ECCurve.CreateFromFriendlyName("secp256k1"), D = privateKey, Q = new ECPoint() { X = publicKey.Take(32).ToArray(), Y = publicKey.Skip(32).Take(32).ToArray(), }, }; var ds = new DigitalSignature(privateKey, publicKey); try { ds.dsa = ECDsa.Create(param); } catch { return(null); } return(ds); }
public static DigitalSignature Generate() { var dsa = ECDsa.Create("ECDsaCng"); dsa.GenerateKey(ECCurve.CreateFromFriendlyName("secp256k1")); var param = dsa.ExportParameters(true); var ds = new DigitalSignature(param.D, param.Q.X.Concat(param.Q.Y).ToArray()); ds.dsa = dsa; return(ds); }
public bool Verify() { try { if (PublicKey == null || Sign == null) { return(false); } var clone = Clone(); var json = Util.SerializeJsonBinary(clone, false); var ds = DigitalSignature.FromKey(Util.FromBase58(PublicKey)); return(ds.Verify(json, Util.FromBase58(Sign))); } catch { } return(false); }