2121import java .security .SecureRandom ;
2222
2323import org .bouncycastle .asn1 .ASN1InputStream ;
24- import org .bouncycastle .asn1 .DERInteger ;
24+ import org .bouncycastle .asn1 .ASN1Integer ;
2525import org .bouncycastle .asn1 .DERSequenceGenerator ;
2626import org .bouncycastle .asn1 .DLSequence ;
2727import org .bouncycastle .asn1 .sec .SECNamedCurves ;
2828import org .bouncycastle .asn1 .x9 .X9ECParameters ;
2929import org .bouncycastle .crypto .AsymmetricCipherKeyPair ;
30+ import org .bouncycastle .crypto .digests .SHA256Digest ;
3031import org .bouncycastle .crypto .generators .ECKeyPairGenerator ;
3132import org .bouncycastle .crypto .params .ECDomainParameters ;
3233import org .bouncycastle .crypto .params .ECKeyGenerationParameters ;
3334import org .bouncycastle .crypto .params .ECPrivateKeyParameters ;
3435import org .bouncycastle .crypto .params .ECPublicKeyParameters ;
3536import org .bouncycastle .crypto .signers .ECDSASigner ;
36- import org .bouncycastle .math . ec . ECPoint ;
37+ import org .bouncycastle .crypto . signers . HMacDSAKCalculator ;
3738import org .bouncycastle .util .Arrays ;
3839
3940import com .bitsofproof .supernode .api .Address ;
@@ -79,15 +80,7 @@ public static ECKeyPair createNew (boolean compressed)
7980 ECKeyPair k = new ECKeyPair ();
8081 k .priv = privParams .getD ();
8182 k .compressed = compressed ;
82- if ( compressed )
83- {
84- ECPoint q = pubParams .getQ ();
85- k .pub = new ECPoint .Fp (domain .getCurve (), q .getX (), q .getY (), true ).getEncoded ();
86- }
87- else
88- {
89- k .pub = pubParams .getQ ().getEncoded ();
90- }
83+ k .pub = pubParams .getQ ().getEncoded (compressed );
9184 return k ;
9285 }
9386
@@ -144,30 +137,14 @@ public ECKeyPair (byte[] p, boolean compressed) throws ValidationException
144137 }
145138 this .priv = new BigInteger (1 , p ).mod (curve .getN ());
146139 this .compressed = compressed ;
147- if ( compressed )
148- {
149- ECPoint q = curve .getG ().multiply (priv );
150- pub = new ECPoint .Fp (domain .getCurve (), q .getX (), q .getY (), true ).getEncoded ();
151- }
152- else
153- {
154- pub = curve .getG ().multiply (priv ).getEncoded ();
155- }
140+ pub = curve .getG ().multiply (priv ).getEncoded (compressed );
156141 }
157142
158143 public ECKeyPair (BigInteger priv , boolean compressed )
159144 {
160145 this .priv = priv ;
161146 this .compressed = compressed ;
162- if ( compressed )
163- {
164- ECPoint q = curve .getG ().multiply (priv );
165- pub = new ECPoint .Fp (domain .getCurve (), q .getX (), q .getY (), true ).getEncoded ();
166- }
167- else
168- {
169- pub = curve .getG ().multiply (priv ).getEncoded ();
170- }
147+ pub = curve .getG ().multiply (priv ).getEncoded (compressed );
171148 }
172149
173150 @ Override
@@ -177,15 +154,15 @@ public byte[] sign (byte[] hash) throws ValidationException
177154 {
178155 throw new ValidationException ("Need private key to sign" );
179156 }
180- ECDSASigner signer = new ECDSASigner ();
157+ ECDSASigner signer = new ECDSASigner (new HMacDSAKCalculator ( new SHA256Digest ()) );
181158 signer .init (true , new ECPrivateKeyParameters (priv , domain ));
182159 BigInteger [] signature = signer .generateSignature (hash );
183160 ByteArrayOutputStream s = new ByteArrayOutputStream ();
184161 try
185162 {
186163 DERSequenceGenerator seq = new DERSequenceGenerator (s );
187- seq .addObject (new DERInteger (signature [0 ]));
188- seq .addObject (new DERInteger (signature [1 ]));
164+ seq .addObject (new ASN1Integer (signature [0 ]));
165+ seq .addObject (new ASN1Integer (signature [1 ]));
189166 seq .close ();
190167 return s .toByteArray ();
191168 }
@@ -210,8 +187,8 @@ public static boolean verify (byte[] hash, byte[] signature, byte[] pub)
210187 signer .init (false , new ECPublicKeyParameters (curve .getCurve ().decodePoint (pub ), domain ));
211188
212189 DLSequence seq = (DLSequence ) asn1 .readObject ();
213- BigInteger r = ((DERInteger ) seq .getObjectAt (0 )).getPositiveValue ();
214- BigInteger s = ((DERInteger ) seq .getObjectAt (1 )).getPositiveValue ();
190+ BigInteger r = ((ASN1Integer ) seq .getObjectAt (0 )).getPositiveValue ();
191+ BigInteger s = ((ASN1Integer ) seq .getObjectAt (1 )).getPositiveValue ();
215192 return signer .verifySignature (hash , r , s );
216193 }
217194 catch ( Exception e )
@@ -231,6 +208,12 @@ public static boolean verify (byte[] hash, byte[] signature, byte[] pub)
231208 }
232209 }
233210
211+ @ Override
212+ public String toString ()
213+ {
214+ return serializeWIF (this );
215+ }
216+
234217 public static String serializeWIF (Key key )
235218 {
236219 return ByteUtils .toBase58 (bytesWIF (key ));
0 commit comments