Exemplo n.º 1
0
 internal static bool VerifySignature(this ISignable signable)
 {
     UInt160[] hashes;
     try
     {
         hashes = signable.GetScriptHashesForVerifying();
     }
     catch (InvalidOperationException)
     {
         return(false);
     }
     if (hashes.Length != signable.Scripts.Length)
     {
         return(false);
     }
     for (int i = 0; i < hashes.Length; i++)
     {
         if (hashes[i] != signable.Scripts[i].RedeemScript.ToScriptHash())
         {
             return(false);
         }
         ScriptEngine engine = new ScriptEngine(signable.Scripts[i], signable.GetHashForSigning());
         if (!engine.Execute())
         {
             return(false);
         }
     }
     return(true);
 }
Exemplo n.º 2
0
        /// <summary>
        /// 根据传入的公私钥,对可签名的对象进行签名
        /// </summary>
        /// <param name="signable">要签名的数据</param>
        /// <param name="prikey">私钥</param>
        /// <param name="pubkey">公钥</param>
        /// <returns>返回签名后的结果</returns>
        internal static byte[] Sign(this ISignable signable, byte[] prikey, byte[] pubkey)
        {
            const int ECDSA_PRIVATE_P256_MAGIC = 0x32534345;

            prikey = BitConverter.GetBytes(ECDSA_PRIVATE_P256_MAGIC).Concat(BitConverter.GetBytes(32)).Concat(pubkey).Concat(prikey).ToArray();
            using (CngKey key = CngKey.Import(prikey, CngKeyBlobFormat.EccPrivateBlob))
                using (ECDsaCng ecdsa = new ECDsaCng(key))
                {
                    return(ecdsa.SignHash(signable.GetHashForSigning()));
                }
        }
Exemplo n.º 3
0
        /// <summary>
        /// 根据传入的公钥与签名,对可签名对象的签名进行验证
        /// </summary>
        /// <param name="signable">要验证的数据</param>
        /// <param name="pubkey">公钥</param>
        /// <param name="signature">签名</param>
        /// <returns>返回验证结果</returns>
        public static bool VerifySignature(this ISignable signable, ECPoint pubkey, byte[] signature)
        {
            const int ECDSA_PUBLIC_P256_MAGIC = 0x31534345;

            byte[] bytes = BitConverter.GetBytes(ECDSA_PUBLIC_P256_MAGIC).Concat(BitConverter.GetBytes(32)).Concat(pubkey.EncodePoint(false).Skip(1)).ToArray();
            using (CngKey key = CngKey.Import(bytes, CngKeyBlobFormat.EccPublicBlob))
                using (ECDsaCng ecdsa = new ECDsaCng(key))
                {
                    return(ecdsa.VerifyHash(signable.GetHashForSigning(), signature));
                }
        }
Exemplo n.º 4
0
 public byte[] Sign(ISignable signable)
 {
     byte[] signature;
     ProtectedMemory.Unprotect(key_exported, MemoryProtectionScope.SameProcess);
     using (CngKey key = CngKey.Import(key_exported, CngKeyBlobFormat.EccPrivateBlob))
         using (ECDsaCng ecdsa = new ECDsaCng(key))
         {
             signature = ecdsa.SignHash(signable.GetHashForSigning());
         }
     ProtectedMemory.Protect(key_exported, MemoryProtectionScope.SameProcess);
     return(signature);
 }
Exemplo n.º 5
0
        /// <summary>
        /// 根据传入的公私钥,对可签名的对象进行签名
        /// </summary>
        /// <param name="signable">要签名的数据</param>
        /// <param name="prikey">私钥</param>
        /// <param name="pubkey">公钥</param>
        /// <returns>返回签名后的结果</returns>
        internal static byte[] Sign(this ISignable signable, byte[] prikey, byte[] pubkey)
        {
#if NET461
            const int ECDSA_PRIVATE_P256_MAGIC = 0x32534345;
            prikey = BitConverter.GetBytes(ECDSA_PRIVATE_P256_MAGIC).Concat(BitConverter.GetBytes(32)).Concat(pubkey).Concat(prikey).ToArray();
            using (CngKey key = CngKey.Import(prikey, CngKeyBlobFormat.EccPrivateBlob))
                using (ECDsaCng ecdsa = new ECDsaCng(key))
#else
            using (var ecdsa = ECDsa.Create(new ECParameters
            {
                Curve = ECCurve.NamedCurves.nistP256,
                D = prikey,
                Q = new ECPoint
                {
                    X = pubkey.Take(32).ToArray(),
                    Y = pubkey.Skip(32).ToArray()
                }
            }))
#endif
                {
                    return(ecdsa.SignHash(signable.GetHashForSigning()));
                }
        }
Exemplo n.º 6
0
        /// <summary>
        /// 根据传入的公钥与签名,对可签名对象的签名进行验证
        /// </summary>
        /// <param name="signable">要验证的数据</param>
        /// <param name="pubkey">公钥</param>
        /// <param name="signature">签名</param>
        /// <returns>返回验证结果</returns>
        public static bool VerifySignature(this ISignable signable, Cryptography.ECC.ECPoint pubkey, byte[] signature)
        {
            byte[] pubk = pubkey.EncodePoint(false).Skip(1).ToArray();
#if NET461
            const int ECDSA_PUBLIC_P256_MAGIC = 0x31534345;
            pubk = BitConverter.GetBytes(ECDSA_PUBLIC_P256_MAGIC).Concat(BitConverter.GetBytes(32)).Concat(pubk).ToArray();
            using (CngKey key = CngKey.Import(pubk, CngKeyBlobFormat.EccPublicBlob))
                using (ECDsaCng ecdsa = new ECDsaCng(key))
#else
            using (var ecdsa = ECDsa.Create(new ECParameters
            {
                Curve = ECCurve.NamedCurves.nistP256,
                Q = new ECPoint
                {
                    X = pubk.Take(32).ToArray(),
                    Y = pubk.Skip(32).ToArray()
                }
            }))
#endif
                {
                    return(ecdsa.VerifyHash(signable.GetHashForSigning(), signature));
                }
        }
Exemplo n.º 7
0
 public byte[] Sign(ISignable signable)
 {
     byte[] signature;
     ProtectedMemory.Unprotect(key_exported, MemoryProtectionScope.SameProcess);
     using (CngKey key = CngKey.Import(key_exported, CngKeyBlobFormat.EccPrivateBlob))
     using (ECDsaCng ecdsa = new ECDsaCng(key))
     {
         signature = ecdsa.SignHash(signable.GetHashForSigning());
     }
     ProtectedMemory.Protect(key_exported, MemoryProtectionScope.SameProcess);
     return signature;
 }