2 * Novacoin classes library
3 * Copyright (C) 2015 Alex D. (balthazar.ad@gmail.com)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as
7 * published by the Free Software Foundation, either version 3 of the
8 * License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 using Org.BouncyCastle.Crypto;
20 using Org.BouncyCastle.Crypto.Generators;
21 using Org.BouncyCastle.Crypto.Parameters;
22 using Org.BouncyCastle.Math;
23 using Org.BouncyCastle.Math.EC;
24 using Org.BouncyCastle.Security;
26 using System.Collections.Generic;
32 public class CKeyPair : CKey
34 private ECPrivateKeyParameters _Private;
37 /// Initialize new CKeyPair instance with random secret.
39 public CKeyPair(bool Compressed=true)
41 ECKeyGenerationParameters genParams = new ECKeyGenerationParameters(domain, new SecureRandom());
42 ECKeyPairGenerator generator = new ECKeyPairGenerator("ECDSA");
43 generator.Init(genParams);
44 AsymmetricCipherKeyPair ecKeyPair = generator.GenerateKeyPair();
46 _Private = (ECPrivateKeyParameters)ecKeyPair.Private;
47 _Public = (ECPublicKeyParameters)ecKeyPair.Public;
51 _Public = Compress(_Public);
56 /// Init key pair using secret sequence of bytes
58 /// <param name="secretBytes">Byte sequence</param>
59 /// <param name="Compressed">Compression flag</param>
60 public CKeyPair(IEnumerable<byte> secretBytes, bool Compressed=true)
62 // Deserialize secret value
63 BigInteger D = new BigInteger(secretBytes.Take(32).ToArray());
65 if (D.SignValue == -1)
67 List<byte> fixedKeyBytes = secretBytes.Take(32).ToList();
68 fixedKeyBytes.Insert(0, 0x00); // prepend with sign byte
70 D = new BigInteger(fixedKeyBytes.ToArray());
73 // Calculate public key
74 ECPoint Q = curve.G.Multiply(D);
76 _Private = new ECPrivateKeyParameters(D, domain);
77 _Public = new ECPublicKeyParameters(Q, domain);
81 _Public = Compress(_Public);
86 /// Init key pair using secret sequence of bytes
88 /// <param name="secretBytes">Byte sequence</param>
89 public CKeyPair(IEnumerable<byte> secretBytes) :
90 this (secretBytes.Take(32), (secretBytes.Count() == 33 && secretBytes.Last() == 0x01))
94 public CKeyPair(string strBase58)
96 List<byte> rawBytes = AddressTools.Base58DecodeCheck(strBase58).ToList();
97 rawBytes.RemoveAt(0); // Remove key version byte
99 // Deserialize secret value
100 BigInteger D = new BigInteger(rawBytes.Take(32).ToArray());
102 if (D.SignValue == -1)
104 List<byte> secretbytes = rawBytes.Take(32).ToList(); // Copy secret
105 secretbytes.Insert(0, 0x00); // Prepend with sign byte
107 D = new BigInteger(secretbytes.ToArray()); // Try decoding again
110 // Calculate public key
111 ECPoint Q = curve.G.Multiply(D);
113 _Private = new ECPrivateKeyParameters(D, domain);
114 _Public = new ECPublicKeyParameters(Q, domain);
116 if (rawBytes.Count == 33 && rawBytes.Last() == 0x01) // Check compression tag
118 _Public = Compress(_Public);
123 /// Initialize a copy of CKeyPair instance
125 /// <param name="pair">CKyPair instance</param>
126 public CKeyPair(CKeyPair pair)
128 _Public = pair._Public;
129 _Private = pair._Private;
133 /// Create signature for supplied data
135 /// <param name="data">Hash to sigh</param>
136 /// <returns>Signature bytes sequence</returns>
137 public IEnumerable<byte> Sign(Hash sigHash)
139 ISigner signer = SignerUtilities.GetSigner("NONEwithECDSA");
140 signer.Init(true, _Private);
141 signer.BlockUpdate(sigHash.hashBytes, 0, sigHash.hashSize);
143 return signer.GenerateSignature();
146 public CPubKey PubKey
148 get { return new CPubKey(PublicBytes); }
152 /// SecretBytes part of key pair
154 public IEnumerable<byte> SecretBytes
158 List<byte> secretBytes = new List<byte>(_Private.D.ToByteArray());
160 if (secretBytes[0] == 0x00)
163 secretBytes.RemoveAt(0);
168 // Set compression flag
169 secretBytes.Add(0x01);
176 public string ToHex()
178 return Interop.ToHex(SecretBytes);
181 public override string ToString()
183 List<byte> r = new List<byte>();
185 r.Add((byte)(128 + AddrType.PUBKEY_ADDRESS)); // Key version
186 r.AddRange(SecretBytes); // Key data
188 return AddressTools.Base58EncodeCheck(r);