org.bouncycastle.asn1.DERSequenceGenerator Java Examples

The following examples show how to use org.bouncycastle.asn1.DERSequenceGenerator. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ECDSASignatureVerifierContext.java    From strimzi-kafka-oauth with Apache License 2.0 6 votes vote down vote up
static byte[] concatenatedRSToASN1DER(final byte[] signature, int signLength) {
    int len = signLength / 2;
    int arraySize = len + 1;

    byte[] r = new byte[arraySize];
    byte[] s = new byte[arraySize];
    System.arraycopy(signature, 0, r, 1, len);
    System.arraycopy(signature, len, s, 1, len);
    BigInteger rBigInteger = new BigInteger(r);
    BigInteger sBigInteger = new BigInteger(s);

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    try {
        DERSequenceGenerator seqGen = new DERSequenceGenerator(bos);

        seqGen.addObject(new ASN1Integer(rBigInteger.toByteArray()));
        seqGen.addObject(new ASN1Integer(sBigInteger.toByteArray()));
        seqGen.close();
        bos.close();
    } catch (IOException e) {
        throw new RuntimeException("Failed to generate ASN.1 DER signature", e);
    }
    return bos.toByteArray();
}
 
Example #2
Source File: BouncyCastleCrypto.java    From fabric-api-archive with Apache License 2.0 6 votes vote down vote up
@Override
public byte[] sign(byte[] hash, byte[] privateKey) {
    ECDSASigner signer = new ECDSASigner(new HMacDSAKCalculator(new SHA256Digest()));
    signer.init(true, new ECPrivateKeyParameters(new BigInteger(privateKey), domain));
    BigInteger[] signature = signer.generateSignature(hash);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        DERSequenceGenerator seq = new DERSequenceGenerator(baos);
        seq.addObject(new ASN1Integer(signature[0]));
        seq.addObject(new ASN1Integer(toCanonicalS(signature[1])));
        seq.close();
        return baos.toByteArray();
    } catch (IOException e) {
        return new byte[0];
    }
}
 
Example #3
Source File: BouncyCastleCrypto.java    From fabric-api with Apache License 2.0 6 votes vote down vote up
@Override
public byte[] sign(byte[] hash, byte[] privateKey) {
    ECDSASigner signer = new ECDSASigner(new HMacDSAKCalculator(new SHA256Digest()));
    signer.init(true, new ECPrivateKeyParameters(new BigInteger(privateKey), domain));
    BigInteger[] signature = signer.generateSignature(hash);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        DERSequenceGenerator seq = new DERSequenceGenerator(baos);
        seq.addObject(new ASN1Integer(signature[0]));
        seq.addObject(new ASN1Integer(toCanonicalS(signature[1])));
        seq.close();
        return baos.toByteArray();
    } catch (IOException e) {
        return new byte[0];
    }
}
 
Example #4
Source File: ECKeyPair.java    From bop-bitcoin-client with Apache License 2.0 6 votes vote down vote up
@Override
public byte[] sign (byte[] hash) throws ValidationException
{
	if ( priv == null )
	{
		throw new ValidationException ("Need private key to sign");
	}
	ECDSASigner signer = new ECDSASigner (new HMacDSAKCalculator (new SHA256Digest ()));
	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 ASN1Integer (signature[0]));
		seq.addObject (new ASN1Integer (signature[1]));
		seq.close ();
		return s.toByteArray ();
	}
	catch ( IOException e )
	{
	}
	return null;
}
 
Example #5
Source File: ECKeyPair.java    From WalletCordova with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Override
public byte[] sign (byte[] hash) throws ValidationException
{
	if ( priv == null )
	{
		throw new ValidationException ("Need private key to sign");
	}
	ECDSASigner signer = new ECDSASigner (new HMacDSAKCalculator (new SHA256Digest ()));
	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 ASN1Integer (signature[0]));
		seq.addObject (new ASN1Integer (signature[1]));
		seq.close ();
		return s.toByteArray ();
	}
	catch ( IOException e )
	{
	}
	return null;
}
 
Example #6
Source File: ECDSASignatureProvider.java    From keycloak with Apache License 2.0 6 votes vote down vote up
public static byte[] concatenatedRSToASN1DER(final byte[] signature, int signLength) throws IOException {
    int len = signLength / 2;
    int arraySize = len + 1;

    byte[] r = new byte[arraySize];
    byte[] s = new byte[arraySize];
    System.arraycopy(signature, 0, r, 1, len);
    System.arraycopy(signature, len, s, 1, len);
    BigInteger rBigInteger = new BigInteger(r);
    BigInteger sBigInteger = new BigInteger(s);

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    DERSequenceGenerator seqGen = new DERSequenceGenerator(bos);

    seqGen.addObject(new ASN1Integer(rBigInteger.toByteArray()));
    seqGen.addObject(new ASN1Integer(sBigInteger.toByteArray()));
    seqGen.close();
    bos.close();

    return bos.toByteArray();
}
 
Example #7
Source File: ECDSASignature.java    From jingtum-lib-java with MIT License 5 votes vote down vote up
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;
}
 
Example #8
Source File: CryptoPrimitives.java    From fabric-sdk-java with Apache License 2.0 5 votes vote down vote up
/**
 * Sign data with the specified elliptic curve private key.
 *
 * @param privateKey elliptic curve private key.
 * @param data       data to sign
 * @return the signed data.
 * @throws CryptoException
 */
private byte[] ecdsaSignToBytes(ECPrivateKey privateKey, byte[] data) throws CryptoException {
    if (data == null) {
        throw new CryptoException("Data that to be signed is null.");
    }
    if (data.length == 0) {
        throw new CryptoException("Data to be signed was empty.");
    }

    try {
        X9ECParameters params = ECNamedCurveTable.getByName(curveName);
        BigInteger curveN = params.getN();

        Signature sig = SECURITY_PROVIDER == null ? Signature.getInstance(DEFAULT_SIGNATURE_ALGORITHM) :
                Signature.getInstance(DEFAULT_SIGNATURE_ALGORITHM, SECURITY_PROVIDER);
        sig.initSign(privateKey);
        sig.update(data);
        byte[] signature = sig.sign();

        BigInteger[] sigs = decodeECDSASignature(signature);

        sigs = preventMalleability(sigs, curveN);

        try (ByteArrayOutputStream s = new ByteArrayOutputStream()) {

            DERSequenceGenerator seq = new DERSequenceGenerator(s);
            seq.addObject(new ASN1Integer(sigs[0]));
            seq.addObject(new ASN1Integer(sigs[1]));
            seq.close();
            return s.toByteArray();
        }

    } catch (Exception e) {
        throw new CryptoException("Could not sign the message using private key", e);
    }

}
 
Example #9
Source File: SDKUtils.java    From fabric-sdk-java with Apache License 2.0 5 votes vote down vote up
/**
 * used asn1 and get hash
 *
 * @param blockNumber
 * @param previousHash
 * @param dataHash
 * @return byte[]
 * @throws IOException
 * @throws InvalidArgumentException
 */
public static byte[] calculateBlockHash(HFClient client, long blockNumber, byte[] previousHash, byte[] dataHash) throws IOException, InvalidArgumentException {

    if (previousHash == null) {
        throw new InvalidArgumentException("previousHash parameter is null.");
    }
    if (dataHash == null) {
        throw new InvalidArgumentException("dataHash parameter is null.");
    }
    if (null == client) {
        throw new InvalidArgumentException("client parameter is null.");
    }

    CryptoSuite cryptoSuite = client.getCryptoSuite();
    if (null == cryptoSuite) {
        throw new InvalidArgumentException("Client crypto suite has not  been set.");
    }

    ByteArrayOutputStream s = new ByteArrayOutputStream();
    DERSequenceGenerator seq = new DERSequenceGenerator(s);
    seq.addObject(new ASN1Integer(blockNumber));
    seq.addObject(new DEROctetString(previousHash));
    seq.addObject(new DEROctetString(dataHash));
    seq.close();
    return cryptoSuite.hash(s.toByteArray());

}
 
Example #10
Source File: ECKey.java    From bushido-java-core with GNU General Public License v3.0 5 votes vote down vote up
public byte[] sign(byte[] message) throws Exception
{
    if (priv == null) {
        throw new Exception("Unable to sign");
    }
    ECDSASigner signer = new ECDSASigner(new HMacDSAKCalculator(new SHA256Digest()));
    signer.init(true, new ECPrivateKeyParameters(priv, params));
    BigInteger[] signature = signer.generateSignature(message);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    DERSequenceGenerator seqGen = new DERSequenceGenerator(outputStream);
    seqGen.addObject(new ASN1Integer(signature[0]));
    seqGen.addObject(new ASN1Integer(signature[1]));
    seqGen.close();
    return outputStream.toByteArray();
}
 
Example #11
Source File: Asn1.java    From UAF with Apache License 2.0 3 votes vote down vote up
/**
 * DER - From Big Integer rs to byte[]
 * UAF_ALG_SIGN_SECP256K1_ECDSA_SHA256_DER 0x06 DER [ITU-X690-2008] encoded
 * ECDSA signature [RFC5480] on the secp256k1 curve. I.e. a DER encoded
 * SEQUENCE { r INTEGER, s INTEGER }
 * 
 * @param signature
 * @return
 * @throws IOException
 */
public static byte[] getEncoded(BigInteger[] sigs) throws IOException {
	ByteArrayOutputStream bos = new ByteArrayOutputStream(72);
	DERSequenceGenerator seq = new DERSequenceGenerator(bos);
	seq.addObject(new ASN1Integer(sigs[0]));
	seq.addObject(new ASN1Integer(sigs[1]));
	seq.close();
	return bos.toByteArray();
}