protected ByteArrayOutputStream derByteStream() throws IOException { // Usually 70-72 bytes. ByteArrayOutputStream bos = new ByteArrayOutputStream(72); DERSequenceGenerator seq = new DERSequenceGenerator(bos); seq.addObject(new ASN1Integer(r)); seq.addObject(new ASN1Integer(s)); seq.close(); return bos; }
public void close() throws IOException { writeDEREncoded(BERTags.CONSTRUCTED | BERTags.SEQUENCE, _bOut.toByteArray()); } }
protected ByteArrayOutputStream derByteStream() throws IOException { // Usually 70-72 bytes. ByteArrayOutputStream bos = new ByteArrayOutputStream(72); DERSequenceGenerator seq = new DERSequenceGenerator(bos); seq.addObject(new ASN1Integer(r)); seq.addObject(new ASN1Integer(s)); seq.close(); return bos; }
/** * Close of the generator, writing out the SEQUENCE. * * @throws IOException if the target stream cannot be written. */ public void close() throws IOException { writeDEREncoded(BERTags.CONSTRUCTED | BERTags.SEQUENCE, _bOut.toByteArray()); } }
protected ByteArrayOutputStream derByteStream() throws IOException { // Usually 70-72 bytes. ByteArrayOutputStream bos = new ByteArrayOutputStream(72); DERSequenceGenerator seq = new DERSequenceGenerator(bos); seq.addObject(new ASN1Integer(r)); seq.addObject(new ASN1Integer(s)); seq.close(); return bos; }
protected ByteArrayOutputStream derByteStream() throws IOException { // Usually 70-72 bytes. ByteArrayOutputStream bos = new ByteArrayOutputStream(72); DERSequenceGenerator seq = new DERSequenceGenerator(bos); seq.addObject(new ASN1Integer(r)); seq.addObject(new ASN1Integer(s)); seq.close(); return bos; }
protected ByteArrayOutputStream derByteStream() throws IOException { // Usually 70-72 bytes. ByteArrayOutputStream bos = new ByteArrayOutputStream(72); DERSequenceGenerator seq = new DERSequenceGenerator(bos); seq.addObject(new ASN1Integer(r)); seq.addObject(new ASN1Integer(s)); seq.close(); return bos; }
protected ByteArrayOutputStream derByteStream() throws IOException { // Usually 70-72 bytes. ByteArrayOutputStream bos = new ByteArrayOutputStream(72); DERSequenceGenerator seq = new DERSequenceGenerator(bos); seq.addObject(new DERInteger(r)); seq.addObject(new DERInteger(s)); seq.close(); return bos; } }
protected ByteArrayOutputStream derByteStream() throws IOException { // Usually 70-72 bytes. ByteArrayOutputStream bos = new ByteArrayOutputStream(72); DERSequenceGenerator seq = new DERSequenceGenerator(bos); seq.addObject(new ASN1Integer(r)); seq.addObject(new ASN1Integer(s)); seq.close(); return bos; }
protected ByteArrayOutputStream derByteStream() throws IOException { // Usually 70-72 bytes. ByteArrayOutputStream bos = new ByteArrayOutputStream(72); DERSequenceGenerator seq = new DERSequenceGenerator(bos); seq.addObject(new ASN1Integer(r)); seq.addObject(new ASN1Integer(s)); seq.close(); return bos; }
private static byte[] toDER(BigInteger r, BigInteger s) { ByteArrayOutputStream bos = new ByteArrayOutputStream(72); DERSequenceGenerator seq = null; byte[] res = new byte[0]; try { seq = new DERSequenceGenerator(bos); seq.addObject(new DERInteger(r)); seq.addObject(new DERInteger(s)); seq.close(); res = bos.toByteArray(); return res; } catch (IOException e) { logger.error("Exception while toDER()", e); } return null; }
public byte[] signBTC(byte[] hash) { ECDSASigner signer = new ECDSASigner(); signer.init(true, new ECPrivateKeyParameters(priv, domain)); BigInteger[] signature = signer.generateSignature(hash); ByteArrayOutputStream s = new ByteArrayOutputStream(); try { DERSequenceGenerator seq = new DERSequenceGenerator(s); seq.addObject(new DERInteger(signature[0])); seq.addObject(new DERInteger(signature[1])); seq.close(); return s.toByteArray(); } catch (IOException e) { } return null; }
/** * Output this ECKey as an ASN.1 encoded private key, as understood by OpenSSL or used by the BitCoin reference * implementation in its wallet storage format. */ public byte[] toASN1() { try { ByteArrayOutputStream baos = new ByteArrayOutputStream(400); // ASN1_SEQUENCE(EC_PRIVATEKEY) = { // ASN1_SIMPLE(EC_PRIVATEKEY, version, LONG), // ASN1_SIMPLE(EC_PRIVATEKEY, privateKey, ASN1_OCTET_STRING), // ASN1_EXP_OPT(EC_PRIVATEKEY, parameters, ECPKPARAMETERS, 0), // ASN1_EXP_OPT(EC_PRIVATEKEY, publicKey, ASN1_BIT_STRING, 1) // } ASN1_SEQUENCE_END(EC_PRIVATEKEY) DERSequenceGenerator seq = new DERSequenceGenerator(baos); seq.addObject(new ASN1Integer(1)); // version seq.addObject(new DEROctetString(priv.toByteArray())); seq.addObject(new DERTaggedObject(0, SECNamedCurves.getByName("secp256k1").toASN1Primitive())); seq.addObject(new DERTaggedObject(1, new DERBitString(getPubKey()))); seq.close(); return baos.toByteArray(); } catch (IOException e) { throw new RuntimeException(e); // Cannot happen, writing to memory stream. } }
/** * Output this UldECKey as an ASN.1 encoded private key, as understood by OpenSSL or used by Bitcoin Core * in its wallet storage format. * @throws UldECKey.MissingPrivateKeyException if the private key is missing or encrypted. */ public byte[] toASN1() { try { byte[] privKeyBytes = getPrivKeyBytes(); ByteArrayOutputStream baos = new ByteArrayOutputStream(400); // ASN1_SEQUENCE(EC_PRIVATEKEY) = { // ASN1_SIMPLE(EC_PRIVATEKEY, version, LONG), // ASN1_SIMPLE(EC_PRIVATEKEY, privateKey, ASN1_OCTET_STRING), // ASN1_EXP_OPT(EC_PRIVATEKEY, parameters, ECPKPARAMETERS, 0), // ASN1_EXP_OPT(EC_PRIVATEKEY, publicKey, ASN1_BIT_STRING, 1) // } ASN1_SEQUENCE_END(EC_PRIVATEKEY) DERSequenceGenerator seq = new DERSequenceGenerator(baos); seq.addObject(new ASN1Integer(1)); // version seq.addObject(new DEROctetString(privKeyBytes)); seq.addObject(new DERTaggedObject(0, CURVE_PARAMS.toASN1Primitive())); seq.addObject(new DERTaggedObject(1, new DERBitString(getPubKey()))); seq.close(); return baos.toByteArray(); } catch (IOException e) { throw new RuntimeException(e); // Cannot happen, writing to memory stream. } }
/** * Output this ECKey as an ASN.1 encoded private key, as understood by OpenSSL or used by Bitcoin Core * in its wallet storage format. * @throws org.bitcoinj.core.ECKey.MissingPrivateKeyException if the private key is missing or encrypted. */ public byte[] toASN1() { try { byte[] privKeyBytes = getPrivKeyBytes(); ByteArrayOutputStream baos = new ByteArrayOutputStream(400); // ASN1_SEQUENCE(EC_PRIVATEKEY) = { // ASN1_SIMPLE(EC_PRIVATEKEY, version, LONG), // ASN1_SIMPLE(EC_PRIVATEKEY, privateKey, ASN1_OCTET_STRING), // ASN1_EXP_OPT(EC_PRIVATEKEY, parameters, ECPKPARAMETERS, 0), // ASN1_EXP_OPT(EC_PRIVATEKEY, publicKey, ASN1_BIT_STRING, 1) // } ASN1_SEQUENCE_END(EC_PRIVATEKEY) DERSequenceGenerator seq = new DERSequenceGenerator(baos); seq.addObject(new ASN1Integer(1)); // version seq.addObject(new DEROctetString(privKeyBytes)); seq.addObject(new DERTaggedObject(0, CURVE_PARAMS.toASN1Primitive())); seq.addObject(new DERTaggedObject(1, new DERBitString(getPubKey()))); seq.close(); return baos.toByteArray(); } catch (IOException e) { throw new RuntimeException(e); // Cannot happen, writing to memory stream. } }
/** * Output this ECKey as an ASN.1 encoded private key, as understood by OpenSSL or used by Bitcoin Core * in its wallet storage format. * @throws org.bitcoinj.core.ECKey.MissingPrivateKeyException if the private key is missing or encrypted. */ public byte[] toASN1() { try { byte[] privKeyBytes = getPrivKeyBytes(); ByteArrayOutputStream baos = new ByteArrayOutputStream(400); // ASN1_SEQUENCE(EC_PRIVATEKEY) = { // ASN1_SIMPLE(EC_PRIVATEKEY, version, LONG), // ASN1_SIMPLE(EC_PRIVATEKEY, privateKey, ASN1_OCTET_STRING), // ASN1_EXP_OPT(EC_PRIVATEKEY, parameters, ECPKPARAMETERS, 0), // ASN1_EXP_OPT(EC_PRIVATEKEY, publicKey, ASN1_BIT_STRING, 1) // } ASN1_SEQUENCE_END(EC_PRIVATEKEY) DERSequenceGenerator seq = new DERSequenceGenerator(baos); seq.addObject(new ASN1Integer(1)); // version seq.addObject(new DEROctetString(privKeyBytes)); seq.addObject(new DERTaggedObject(0, CURVE_PARAMS.toASN1Primitive())); seq.addObject(new DERTaggedObject(1, new DERBitString(getPubKey()))); seq.close(); return baos.toByteArray(); } catch (IOException e) { throw new RuntimeException(e); // Cannot happen, writing to memory stream. } }
/** * Output this ECKey as an ASN.1 encoded private key, as understood by OpenSSL or used by Bitcoin Core * in its wallet storage format. * @throws org.bitcoinj.core.ECKey.MissingPrivateKeyException if the private key is missing or encrypted. */ public byte[] toASN1() { try { byte[] privKeyBytes = getPrivKeyBytes(); ByteArrayOutputStream baos = new ByteArrayOutputStream(400); // ASN1_SEQUENCE(EC_PRIVATEKEY) = { // ASN1_SIMPLE(EC_PRIVATEKEY, version, LONG), // ASN1_SIMPLE(EC_PRIVATEKEY, privateKey, ASN1_OCTET_STRING), // ASN1_EXP_OPT(EC_PRIVATEKEY, parameters, ECPKPARAMETERS, 0), // ASN1_EXP_OPT(EC_PRIVATEKEY, publicKey, ASN1_BIT_STRING, 1) // } ASN1_SEQUENCE_END(EC_PRIVATEKEY) DERSequenceGenerator seq = new DERSequenceGenerator(baos); seq.addObject(new ASN1Integer(1)); // version seq.addObject(new DEROctetString(privKeyBytes)); seq.addObject(new DERTaggedObject(0, CURVE_PARAMS.toASN1Primitive())); seq.addObject(new DERTaggedObject(1, new DERBitString(getPubKey()))); seq.close(); return baos.toByteArray(); } catch (IOException e) { throw new RuntimeException(e); // Cannot happen, writing to memory stream. } }
public static byte[] sign(BigInteger privateKey, byte[] input) { synchronized (EC_PARAMS) { ECDSASigner signer = new ECDSASigner(); ECPrivateKeyParameters privateKeyParam = new ECPrivateKeyParameters(privateKey, EC_PARAMS); signer.init(true, new ParametersWithRandom(privateKeyParam, SECURE_RANDOM)); BigInteger[] sign = signer.generateSignature(input); BigInteger r = sign[0]; BigInteger s = sign[1]; BigInteger largestAllowedS = new BigInteger("7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0", 16);//SECP256K1_N_DIV_2 if (s.compareTo(largestAllowedS) > 0) { //https://github.com/bitcoin/bips/blob/master/bip-0062.mediawiki#low-s-values-in-signatures s = LARGEST_PRIVATE_KEY.subtract(s); } try { ByteArrayOutputStream baos = new ByteArrayOutputStream(72); DERSequenceGenerator derGen = new DERSequenceGenerator(baos); derGen.addObject(new ASN1Integer(r)); derGen.addObject(new ASN1Integer(s)); derGen.close(); return baos.toByteArray(); } catch (IOException e) { throw new RuntimeException(e); } } }
/** * Output this ECKey as an ASN.1 encoded private key, as understood by OpenSSL or used by Bitcoin Core * in its wallet storage format. * @throws org.bitcoinj.core.ECKey.MissingPrivateKeyException if the private key is missing or encrypted. */ public byte[] toASN1() { try { byte[] privKeyBytes = getPrivKeyBytes(); ByteArrayOutputStream baos = new ByteArrayOutputStream(400); // ASN1_SEQUENCE(EC_PRIVATEKEY) = { // ASN1_SIMPLE(EC_PRIVATEKEY, version, LONG), // ASN1_SIMPLE(EC_PRIVATEKEY, privateKey, ASN1_OCTET_STRING), // ASN1_EXP_OPT(EC_PRIVATEKEY, parameters, ECPKPARAMETERS, 0), // ASN1_EXP_OPT(EC_PRIVATEKEY, publicKey, ASN1_BIT_STRING, 1) // } ASN1_SEQUENCE_END(EC_PRIVATEKEY) DERSequenceGenerator seq = new DERSequenceGenerator(baos); seq.addObject(new ASN1Integer(1)); // version seq.addObject(new DEROctetString(privKeyBytes)); seq.addObject(new DERTaggedObject(0, CURVE_PARAMS.toASN1Primitive())); seq.addObject(new DERTaggedObject(1, new DERBitString(getPubKey()))); seq.close(); return baos.toByteArray(); } catch (IOException e) { throw new RuntimeException(e); // Cannot happen, writing to memory stream. } }
public static byte[] sign(BigInteger privateKey, byte[] input) { synchronized (domain) { ECDSASigner signer = new ECDSASigner(new HMacDSAKCalculator(new SHA256Digest())); ECPrivateKeyParameters privateKeyParam = new ECPrivateKeyParameters(privateKey, domain); signer.init(true, new ParametersWithRandom(privateKeyParam, secureRandom)); BigInteger[] sign = signer.generateSignature(input); BigInteger r = sign[0]; BigInteger s = sign[1]; BigInteger largestAllowedS = new BigInteger ("7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0", 16); //SECP256K1_N_DIV_2 if (s.compareTo(largestAllowedS) > 0) { //https://github.com/bitcoin/bips/blob/master/bip-0062.mediawiki#low-s-values-in // -signatures s = LARGEST_PRIVATE_KEY.subtract(s); } try { ByteArrayOutputStream baos = new ByteArrayOutputStream(72); DERSequenceGenerator derGen = new DERSequenceGenerator(baos); derGen.addObject(new ASN1Integer(r)); derGen.addObject(new ASN1Integer(s)); derGen.close(); return baos.toByteArray(); } catch (IOException e) { throw new RuntimeException(e); } } } }