public static byte[] SignData(
            this ECDsaCng ecdsaCng,
            byte[] data,
            int offset,
            int count)
        {
            Contract.Ensures(Contract.Result <byte[]>() != null);

            if (data == null)
            {
                throw new ArgumentNullException("data");
            }
            if (offset < 0 || offset > data.Length)
            {
                throw new ArgumentOutOfRangeException("offset");
            }
            if (count < 0 || count > data.Length - offset)
            {
                throw new ArgumentOutOfRangeException("count");
            }

            using (var hashAlgorithm = new BCryptHashAlgorithm(CngAlgorithm.Sha256, BCryptNative.ProviderName.MicrosoftPrimitiveProvider))
            {
                hashAlgorithm.HashCore(data, offset, count);
                byte[] hashValue = hashAlgorithm.HashFinal();
                return(ecdsaCng.SignHash(hashValue));
            }
        }
Exemple #2
0
 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());
     }
 }
Exemple #3
0
        public static byte[] SignData(byte[] prikey, byte[] data)
        {
            var PublicKey = ThinNeo.Cryptography.ECC.ECCurve.Secp256r1.G * prikey;

            byte[] pubkey = PublicKey.EncodePoint(false).Skip(1).ToArray();


            //签名的私钥
            byte[] first      = { 0x45, 0x43, 0x53, 0x32, 0x20, 0x00, 0x00, 0x00 };
            byte[] signprikey = first.Concat(pubkey).Concat(prikey).ToArray();

            using (System.Security.Cryptography.CngKey key = System.Security.Cryptography.CngKey.Import(signprikey, System.Security.Cryptography.CngKeyBlobFormat.EccPrivateBlob))
            {
                using (System.Security.Cryptography.ECDsaCng ecdsa = new System.Security.Cryptography.ECDsaCng(key))
                {
                    var hashsrc  = sha256.ComputeHash(data);
                    var signdata = ecdsa.SignHash(hashsrc);
                    return(signdata);
                }
            }
        }
 /// <inheritdoc />
 protected internal override byte[] SignHash(byte[] data)
 {
     using (var cng = new ECDsaCng(this.key))
     {
         return cng.SignHash(data);
     }
 }
Exemple #5
0
 public byte[] Sign(byte[] hash)
 {
     using (ECDsaCng sig = new ECDsaCng(_cngKey))
     {
         sig.HashAlgorithm = CngAlgorithm.ECDsaP256;
         return sig.SignHash(hash);
     }
 }
Exemple #6
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;
 }
Exemple #7
0
 public static string EnCDsa(this string data, CngKey key)
 {
     ECDsa ecdsa = new ECDsaCng(key);
     SHA1 sha1 = System.Security.Cryptography.SHA1.Create();
     byte[] result = ecdsa.SignHash(sha1.ComputeHash(Convert.FromBase64String(data)));
     return Convert.ToBase64String(result);
 }