示例#1
0
        public TransactionOutput(Key key)
        {
            MemoryStream stream = new MemoryStream();
            BinaryWriter bw = new BinaryWriter(stream);

            byte[] pubKey = key.GetPublicBytes();
            bw.Write((byte)pubKey.Length);
            bw.Write(pubKey);
            bw.Write((byte)0xac);

            bw.Close();
        }
示例#2
0
        Block CreateBlock(Key key)
        {
            Block block = new Block();

            // Create transaction from nowhere to us!
            Transaction tx = new Transaction();
            TransactionInput input = new TransactionInput();
            TransactionOutput output = new TransactionOutput(key);
            tx.AddInput(input);
            tx.AddOutput(output);

            block.AddTransaction(tx);

            Transaction[] transactions = mBitcoin.GetTransactions();

            return block;
        }
示例#3
0
		/// <summary>
		/// Calls ECDH_compute_key()
		/// </summary>
		/// <returns>The key.</returns>
		/// <param name="b">The blue component.</param>
		/// <param name="buf">Buffer.</param>
		/// <param name="kdf">Kdf.</param>
		public int ComputeKey(Key b, byte[] buf, ComputeKeyHandler kdf)
		{
			ComputeKeyThunk thunk = new ComputeKeyThunk(kdf);
			return Native.ExpectSuccess(
				Native.ECDH_compute_key(buf, buf.Length, b.PublicKey.Handle, ptr, thunk.Wrapper)
			);
		}
		public void CanCreateFromEC()
		{
			using (var ec = new Key())
			{
				using (var group = Group.FromCurveName(Objects.NID.X9_62_prime256v1))
				{
					ec.Group = group;
				}
				ec.GenerateKey();
				using (var key = new CryptoKey(ec))
				{
					Assert.AreEqual(CryptoKey.KeyType.EC, key.Type);
					Assert.AreEqual(ec.Size, key.Size);
					Assert.AreEqual(ec.Handle, key.GetEC().Handle);
				}

				using (var key = new CryptoKey())
				{
					key.Assign(ec);
					Assert.AreEqual(ec.Handle, key.GetEC().Handle);
				}
			}
		}
示例#5
0
		public void test_builtin() {
			/* fill digest values with some random data */
			byte[] digest = Random.PseudoBytes(20);
			byte[] wrong_digest = Random.PseudoBytes(20);

			/* create and verify a ecdsa signature with every availble curve
			 * (with ) */
			Console.WriteLine("testing ECDSA_sign() and ECDSA_verify() with some internal curves:");
			
			/* get a list of all internal curves */
			BuiltinCurve[] curves = BuiltinCurve.Get();
			
			/* now create and verify a signature for every curve */
			foreach (BuiltinCurve curve in curves) {
				if (curve.Object.NID == Objects.NID.ipsec4.NID)
					continue;
				
				/* create new ecdsa key (== EC_KEY) */
				using(Key eckey = new Key()) {

					using(Group group = Group.FromCurveName(curve.Object)) {
						eckey.Group = group;
					}
					
					if (eckey.Group.Degree < 160) {
						/* drop the curve */ 
						continue;
					}
					
					Console.Write("{0}: ", curve.Object.ShortName);
					
					/* create key */
					eckey.GenerateKey();
					
					/* create second key */
					using(Key wrong_eckey = new Key()) {
						using(Group group = Group.FromCurveName(curve.Object)) {
							wrong_eckey.Group = group;
						}
						
						wrong_eckey.GenerateKey();
						Console.Write(".");
						
						/* check key */
						Assert.IsTrue(eckey.CheckKey());
						Console.Write(".");
						
						/* create signature */
						byte[] signature = new byte[eckey.Size];
						eckey.Sign(0, digest, signature);
						Console.Write(".");
						
						/* verify signature */
						Assert.IsTrue(eckey.Verify(0, digest, signature));
						Console.Write(".");
						
						/* verify signature with the wrong key */
						Assert.IsFalse(wrong_eckey.Verify(0, digest, signature));
						Console.Write(".");
						
						/* wrong digest */
						Assert.IsFalse(eckey.Verify(0, wrong_digest, signature));
						Console.Write(".");
						
						Console.WriteLine(" ok");						
					}
				}
			}
		}
示例#6
0
 /// <summary>
 /// Calls EVP_PKEY_assign()
 /// </summary>
 /// <param name="key">Key.</param>
 public void Assign(EC.Key key)
 {
     key.AddRef();
     Native.ExpectSuccess(Native.EVP_PKEY_assign(ptr, (int)KeyType.EC, key.Handle));
 }
示例#7
0
 /// <summary>
 /// Calls EVP_PKEY_set1_EC()
 /// </summary>
 /// <param name="ec"></param>
 public CryptoKey(EC.Key ec)
     : this()
 {
     Native.ExpectSuccess(Native.EVP_PKEY_set1_EC_KEY(ptr, ec.Handle));
 }