Java 类org.bouncycastle.asn1.DERSequenceGenerator 实例源码

项目:fabric-api-archive    文件:BouncyCastleCrypto.java   
@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];
    }
}
项目:fabric-api    文件:BouncyCastleCrypto.java   
@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];
    }
}
项目:BitcoinCore    文件:ECDSASignature.java   
/**
 * Encodes R and S as a DER-encoded byte stream
 *
 * @return                          DER-encoded byte stream
 */
public byte[] encodeToDER() {
    byte[] encodedBytes = null;
    try {
        try (ByteArrayOutputStream outStream = new ByteArrayOutputStream(80)) {
            DERSequenceGenerator seq = new DERSequenceGenerator(outStream);
            seq.addObject(new ASN1Integer(r));
            seq.addObject(new ASN1Integer(s));
            seq.close();
            encodedBytes = outStream.toByteArray();
        }
    } catch (IOException exc) {
        throw new IllegalStateException("Unexpected IOException", exc);
    }
    return encodedBytes;
}
项目:bop-bitcoin-client    文件:ECKeyPair.java   
@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;
}
项目:WalletCordova    文件:ECKeyPair.java   
@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;
}
项目:ipack    文件:CMSCompressedDataStreamGenerator.java   
/**
 * @deprecated use open(OutputStream, ASN1ObjectIdentifier, ContentCompressor)
 */
public OutputStream open(
    OutputStream  out,        
    String        contentOID,
    String        compressionOID) 
    throws IOException
{
    BERSequenceGenerator sGen = new BERSequenceGenerator(out);

    sGen.addObject(CMSObjectIdentifiers.compressedData);

    //
    // Compressed Data
    //
    BERSequenceGenerator cGen = new BERSequenceGenerator(sGen.getRawOutputStream(), 0, true);

    cGen.addObject(new ASN1Integer(0));

    //
    // AlgorithmIdentifier
    //
    DERSequenceGenerator algGen = new DERSequenceGenerator(cGen.getRawOutputStream());

    algGen.addObject(new ASN1ObjectIdentifier(ZLIB));

    algGen.close();

    //
    // Encapsulated ContentInfo
    //
    BERSequenceGenerator eiGen = new BERSequenceGenerator(cGen.getRawOutputStream());

    eiGen.addObject(new ASN1ObjectIdentifier(contentOID));

    OutputStream octetStream = CMSUtils.createBEROctetOutputStream(
        eiGen.getRawOutputStream(), 0, true, _bufferSize);

    return new CmsCompressedOutputStream(
        new DeflaterOutputStream(octetStream), sGen, cGen, eiGen);
}
项目:-deprecated-hlp-candidate    文件:PrivateKey.java   
/**
 * Sign a digest with this key.
 *
 * @param hash arbitrary data
 * @return signature
 */
public byte[] sign(byte[] hash) {
    ECDSASigner signer = new ECDSASigner(new HMacDSAKCalculator(new SHA256Digest()));
    signer.init(true, new ECPrivateKeyParameters(priv, 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 null;
}
项目:fabric-sdk-java    文件:CryptoPrimitives.java   
/**
 * 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 {
    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);

        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);
    }

}
项目:fabric-sdk-java    文件:SDKUtils.java   
/**
 * 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 == client) {
        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());

}
项目:bushido-java-core    文件:ECKey.java   
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();
}
项目:blockio-java    文件:SigningUtils.java   
static String signData(String input, byte[] key) throws BlockIOException {
    ECDSASigner signer = new ECDSASigner(new HMacDSAKCalculator(new SHA256Digest()));
    X9ECParameters params = SECNamedCurves.getByName("secp256k1");
    ECDomainParameters ecParams = new ECDomainParameters(params.getCurve(), params.getG(), params.getN(), params.getH());
    BigInteger priv = new BigInteger(1, key);
    ECPrivateKeyParameters privKey = new ECPrivateKeyParameters(priv, ecParams);

    signer.init(true, privKey);
    BigInteger[] sigs = signer.generateSignature(fromHex(input));

    BigInteger r = sigs[0];
    BigInteger s = sigs[1];

    // BIP62: "S must be less than or equal to half of the Group Order N"
    BigInteger overTwo = params.getN().shiftRight(1);
    if (s.compareTo(overTwo) == 1) {
        s = params.getN().subtract(s);
    }

    try {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        DERSequenceGenerator seq = new DERSequenceGenerator(bos);
        seq.addObject(new ASN1Integer(r));
        seq.addObject(new ASN1Integer(s));
        seq.close();
        return toHex(bos.toByteArray());
    } catch (IOException e) {
        throw new BlockIOException("That should never happen... File an issue report.");  // Cannot happen.
    }
}
项目:irma_future_id    文件:CMSCompressedDataStreamGenerator.java   
/**
 * @deprecated use open(OutputStream, ASN1ObjectIdentifier, ContentCompressor)
 */
public OutputStream open(
    OutputStream  out,        
    String        contentOID,
    String        compressionOID) 
    throws IOException
{
    BERSequenceGenerator sGen = new BERSequenceGenerator(out);

    sGen.addObject(CMSObjectIdentifiers.compressedData);

    //
    // Compressed Data
    //
    BERSequenceGenerator cGen = new BERSequenceGenerator(sGen.getRawOutputStream(), 0, true);

    cGen.addObject(new ASN1Integer(0));

    //
    // AlgorithmIdentifier
    //
    DERSequenceGenerator algGen = new DERSequenceGenerator(cGen.getRawOutputStream());

    algGen.addObject(new ASN1ObjectIdentifier(ZLIB));

    algGen.close();

    //
    // Encapsulated ContentInfo
    //
    BERSequenceGenerator eiGen = new BERSequenceGenerator(cGen.getRawOutputStream());

    eiGen.addObject(new ASN1ObjectIdentifier(contentOID));

    OutputStream octetStream = CMSUtils.createBEROctetOutputStream(
        eiGen.getRawOutputStream(), 0, true, _bufferSize);

    return new CmsCompressedOutputStream(
        new DeflaterOutputStream(octetStream), sGen, cGen, eiGen);
}
项目:irma_future_id    文件:ASN1SequenceParserTest.java   
public void testDERWriting()
    throws Exception
{
   ByteArrayOutputStream bOut = new ByteArrayOutputStream();
   DERSequenceGenerator  seqGen = new DERSequenceGenerator(bOut);

   seqGen.addObject(new DERInteger(BigInteger.valueOf(0)));

   seqGen.addObject(new DERObjectIdentifier("1.1"));

   seqGen.close();

   assertTrue("basic DER writing test failed.", Arrays.equals(seqData, bOut.toByteArray()));
}
项目:irma_future_id    文件:ASN1SequenceParserTest.java   
public void testDERExplicitTaggedSequenceWriting()
    throws Exception
{
   ByteArrayOutputStream bOut = new ByteArrayOutputStream();
   DERSequenceGenerator  seqGen = new DERSequenceGenerator(bOut, 1, true);

   seqGen.addObject(new DERInteger(BigInteger.valueOf(0)));

   seqGen.addObject(new DERObjectIdentifier("1.1"));

   seqGen.close();

   assertTrue("explicit tag writing test failed.", Arrays.equals(expTagSeqData, bOut.toByteArray()));
}
项目:irma_future_id    文件:ASN1SequenceParserTest.java   
public void testDERImplicitTaggedSequenceWriting()
    throws Exception
{
   ByteArrayOutputStream bOut = new ByteArrayOutputStream();
   DERSequenceGenerator  seqGen = new DERSequenceGenerator(bOut, 1, false);

   seqGen.addObject(new DERInteger(BigInteger.valueOf(0)));

   seqGen.addObject(new DERObjectIdentifier("1.1"));

   seqGen.close();

   assertTrue("implicit tag writing test failed.", Arrays.equals(implTagSeqData, bOut.toByteArray()));
}
项目:bc-java    文件:CMSCompressedDataStreamGenerator.java   
/**
 * @deprecated use open(OutputStream, ASN1ObjectIdentifier, ContentCompressor)
 */
public OutputStream open(
    OutputStream  out,        
    String        contentOID,
    String        compressionOID) 
    throws IOException
{
    BERSequenceGenerator sGen = new BERSequenceGenerator(out);

    sGen.addObject(CMSObjectIdentifiers.compressedData);

    //
    // Compressed Data
    //
    BERSequenceGenerator cGen = new BERSequenceGenerator(sGen.getRawOutputStream(), 0, true);

    cGen.addObject(new ASN1Integer(0));

    //
    // AlgorithmIdentifier
    //
    DERSequenceGenerator algGen = new DERSequenceGenerator(cGen.getRawOutputStream());

    algGen.addObject(new ASN1ObjectIdentifier(ZLIB));

    algGen.close();

    //
    // Encapsulated ContentInfo
    //
    BERSequenceGenerator eiGen = new BERSequenceGenerator(cGen.getRawOutputStream());

    eiGen.addObject(new ASN1ObjectIdentifier(contentOID));

    OutputStream octetStream = CMSUtils.createBEROctetOutputStream(
        eiGen.getRawOutputStream(), 0, true, _bufferSize);

    return new CmsCompressedOutputStream(
        new DeflaterOutputStream(octetStream), sGen, cGen, eiGen);
}
项目:bc-java    文件:ASN1SequenceParserTest.java   
public void testDERWriting()
    throws Exception
{
   ByteArrayOutputStream bOut = new ByteArrayOutputStream();
   DERSequenceGenerator  seqGen = new DERSequenceGenerator(bOut);

   seqGen.addObject(new DERInteger(BigInteger.valueOf(0)));

   seqGen.addObject(new DERObjectIdentifier("1.1"));

   seqGen.close();

   assertTrue("basic DER writing test failed.", Arrays.equals(seqData, bOut.toByteArray()));
}
项目:bc-java    文件:ASN1SequenceParserTest.java   
public void testDERExplicitTaggedSequenceWriting()
    throws Exception
{
   ByteArrayOutputStream bOut = new ByteArrayOutputStream();
   DERSequenceGenerator  seqGen = new DERSequenceGenerator(bOut, 1, true);

   seqGen.addObject(new DERInteger(BigInteger.valueOf(0)));

   seqGen.addObject(new DERObjectIdentifier("1.1"));

   seqGen.close();

   assertTrue("explicit tag writing test failed.", Arrays.equals(expTagSeqData, bOut.toByteArray()));
}
项目:bc-java    文件:ASN1SequenceParserTest.java   
public void testDERImplicitTaggedSequenceWriting()
    throws Exception
{
   ByteArrayOutputStream bOut = new ByteArrayOutputStream();
   DERSequenceGenerator  seqGen = new DERSequenceGenerator(bOut, 1, false);

   seqGen.addObject(new DERInteger(BigInteger.valueOf(0)));

   seqGen.addObject(new DERObjectIdentifier("1.1"));

   seqGen.close();

   assertTrue("implicit tag writing test failed.", Arrays.equals(implTagSeqData, bOut.toByteArray()));
}
项目:jStellarAPI    文件:StellarSigner.java   
/**
 * What we get back from the signer are the two components of a signature, r and s. To get a flat byte stream
 * of the type used by Bitcoin we have to encode them using DER encoding, which is just a way to pack the two
 * components into a structure.
 */
public byte[] encodeToDER() {
    try {
        // 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.toByteArray();
    } catch (IOException e) {
        throw new RuntimeException(e);  // Cannot happen.
    }
}
项目:irma_future_id    文件:ASN1SequenceParserTest.java   
public void testNestedDERWriting()
    throws Exception
{
   ByteArrayOutputStream bOut = new ByteArrayOutputStream();
   DERSequenceGenerator seqGen1 = new DERSequenceGenerator(bOut);

   seqGen1.addObject(new DERInteger(BigInteger.valueOf(0)));

   seqGen1.addObject(new DERObjectIdentifier("1.1"));

   DERSequenceGenerator seqGen2 = new DERSequenceGenerator(seqGen1.getRawOutputStream());

   seqGen2.addObject(new DERInteger(BigInteger.valueOf(1)));

   seqGen2.close();

   seqGen1.close();

   assertTrue("nested DER writing test failed.", Arrays.equals(nestedSeqData, bOut.toByteArray()));
}
项目:irma_future_id    文件:ASN1SequenceParserTest.java   
public void testNestedExplicitTagDERWriting()
    throws Exception
{
   ByteArrayOutputStream bOut = new ByteArrayOutputStream();
   DERSequenceGenerator  seqGen1 = new DERSequenceGenerator(bOut);

   seqGen1.addObject(new DERInteger(BigInteger.valueOf(0)));

   seqGen1.addObject(new DERObjectIdentifier("1.1"));

   DERSequenceGenerator seqGen2 = new DERSequenceGenerator(seqGen1.getRawOutputStream(), 1, true);

   seqGen2.addObject(new DERInteger(BigInteger.valueOf(1)));

   seqGen2.close();

   seqGen1.close();

   assertTrue("nested explicit tagged DER writing test failed.", Arrays.equals(nestedSeqExpTagData, bOut.toByteArray()));
}
项目:irma_future_id    文件:ASN1SequenceParserTest.java   
public void testNestedImplicitTagDERWriting()
    throws Exception
{
   ByteArrayOutputStream bOut = new ByteArrayOutputStream();
   DERSequenceGenerator  seqGen1 = new DERSequenceGenerator(bOut);

   seqGen1.addObject(new DERInteger(BigInteger.valueOf(0)));

   seqGen1.addObject(new DERObjectIdentifier("1.1"));

   DERSequenceGenerator seqGen2 = new DERSequenceGenerator(seqGen1.getRawOutputStream(), 1, false);

   seqGen2.addObject(new DERInteger(BigInteger.valueOf(1)));

   seqGen2.close();

   seqGen1.close();

   assertTrue("nested implicit tagged DER writing test failed.", Arrays.equals(nestedSeqImpTagData, bOut.toByteArray()));
}
项目:irma_future_id    文件:ASN1SequenceParserTest.java   
public void testNestedBERDERWriting()
    throws Exception
{
   ByteArrayOutputStream bOut = new ByteArrayOutputStream();
   BERSequenceGenerator seqGen1 = new BERSequenceGenerator(bOut);

   seqGen1.addObject(new DERInteger(BigInteger.valueOf(0)));

   seqGen1.addObject(new DERObjectIdentifier("1.1"));

   DERSequenceGenerator seqGen2 = new DERSequenceGenerator(seqGen1.getRawOutputStream());

   seqGen2.addObject(new DERInteger(BigInteger.valueOf(1)));

   seqGen2.close();

   seqGen1.close();

   assertTrue("nested BER/DER writing test failed.", Arrays.equals(berDERNestedSeqData, bOut.toByteArray()));
}
项目:irma_future_id    文件:OctetStringTest.java   
public void testNestedStructure()
    throws Exception
{
    ByteArrayOutputStream bOut = new ByteArrayOutputStream();

    BERSequenceGenerator sGen = new BERSequenceGenerator(bOut);

    sGen.addObject(new DERObjectIdentifier(CMSObjectIdentifiers.compressedData.getId()));

    BERSequenceGenerator cGen = new BERSequenceGenerator(sGen.getRawOutputStream(), 0, true);

    cGen.addObject(new DERInteger(0));

    //
    // AlgorithmIdentifier
    //
    DERSequenceGenerator algGen = new DERSequenceGenerator(cGen.getRawOutputStream());

    algGen.addObject(new DERObjectIdentifier("1.2"));

    algGen.close();

    //
    // Encapsulated ContentInfo
    //
    BERSequenceGenerator eiGen = new BERSequenceGenerator(cGen.getRawOutputStream());

    eiGen.addObject(new DERObjectIdentifier("1.1"));

    BEROctetStringGenerator octGen = new BEROctetStringGenerator(eiGen.getRawOutputStream(), 0, true);

    //
    // output containing zeroes
    //
    OutputStream out = octGen.getOctetOutputStream();

    out.write(new byte[] { 1, 2, 3, 4 });
    out.write(new byte[4]);
    out.write(new byte[20]);

    out.close();
    eiGen.close();
    cGen.close();
    sGen.close();

    //
    // reading back
    //
    ASN1StreamParser aIn = new ASN1StreamParser(bOut.toByteArray());

    ContentInfoParser cp = new ContentInfoParser((ASN1SequenceParser)aIn.readObject());

    CompressedDataParser comData = new CompressedDataParser((ASN1SequenceParser)cp.getContent(BERTags.SEQUENCE));
    ContentInfoParser     content = comData.getEncapContentInfo();

    ASN1OctetStringParser bytes = (ASN1OctetStringParser)content.getContent(BERTags.OCTET_STRING);

    InputStream in = bytes.getOctetStream();
    int         count = 0;

    while (in.read() >= 0)
    {
        count++;
    }

    assertEquals(28, count);
}
项目:bc-java    文件:ASN1SequenceParserTest.java   
public void testNestedDERWriting()
    throws Exception
{
   ByteArrayOutputStream bOut = new ByteArrayOutputStream();
   DERSequenceGenerator seqGen1 = new DERSequenceGenerator(bOut);

   seqGen1.addObject(new DERInteger(BigInteger.valueOf(0)));

   seqGen1.addObject(new DERObjectIdentifier("1.1"));

   DERSequenceGenerator seqGen2 = new DERSequenceGenerator(seqGen1.getRawOutputStream());

   seqGen2.addObject(new DERInteger(BigInteger.valueOf(1)));

   seqGen2.close();

   seqGen1.close();

   assertTrue("nested DER writing test failed.", Arrays.equals(nestedSeqData, bOut.toByteArray()));
}
项目:bc-java    文件:ASN1SequenceParserTest.java   
public void testNestedExplicitTagDERWriting()
    throws Exception
{
   ByteArrayOutputStream bOut = new ByteArrayOutputStream();
   DERSequenceGenerator  seqGen1 = new DERSequenceGenerator(bOut);

   seqGen1.addObject(new DERInteger(BigInteger.valueOf(0)));

   seqGen1.addObject(new DERObjectIdentifier("1.1"));

   DERSequenceGenerator seqGen2 = new DERSequenceGenerator(seqGen1.getRawOutputStream(), 1, true);

   seqGen2.addObject(new DERInteger(BigInteger.valueOf(1)));

   seqGen2.close();

   seqGen1.close();

   assertTrue("nested explicit tagged DER writing test failed.", Arrays.equals(nestedSeqExpTagData, bOut.toByteArray()));
}
项目:bc-java    文件:ASN1SequenceParserTest.java   
public void testNestedImplicitTagDERWriting()
    throws Exception
{
   ByteArrayOutputStream bOut = new ByteArrayOutputStream();
   DERSequenceGenerator  seqGen1 = new DERSequenceGenerator(bOut);

   seqGen1.addObject(new DERInteger(BigInteger.valueOf(0)));

   seqGen1.addObject(new DERObjectIdentifier("1.1"));

   DERSequenceGenerator seqGen2 = new DERSequenceGenerator(seqGen1.getRawOutputStream(), 1, false);

   seqGen2.addObject(new DERInteger(BigInteger.valueOf(1)));

   seqGen2.close();

   seqGen1.close();

   assertTrue("nested implicit tagged DER writing test failed.", Arrays.equals(nestedSeqImpTagData, bOut.toByteArray()));
}
项目:bc-java    文件:ASN1SequenceParserTest.java   
public void testNestedBERDERWriting()
    throws Exception
{
   ByteArrayOutputStream bOut = new ByteArrayOutputStream();
   BERSequenceGenerator seqGen1 = new BERSequenceGenerator(bOut);

   seqGen1.addObject(new DERInteger(BigInteger.valueOf(0)));

   seqGen1.addObject(new DERObjectIdentifier("1.1"));

   DERSequenceGenerator seqGen2 = new DERSequenceGenerator(seqGen1.getRawOutputStream());

   seqGen2.addObject(new DERInteger(BigInteger.valueOf(1)));

   seqGen2.close();

   seqGen1.close();

   assertTrue("nested BER/DER writing test failed.", Arrays.equals(berDERNestedSeqData, bOut.toByteArray()));
}
项目:bc-java    文件:OctetStringTest.java   
public void testNestedStructure()
    throws Exception
{
    ByteArrayOutputStream bOut = new ByteArrayOutputStream();

    BERSequenceGenerator sGen = new BERSequenceGenerator(bOut);

    sGen.addObject(new DERObjectIdentifier(CMSObjectIdentifiers.compressedData.getId()));

    BERSequenceGenerator cGen = new BERSequenceGenerator(sGen.getRawOutputStream(), 0, true);

    cGen.addObject(new DERInteger(0));

    //
    // AlgorithmIdentifier
    //
    DERSequenceGenerator algGen = new DERSequenceGenerator(cGen.getRawOutputStream());

    algGen.addObject(new DERObjectIdentifier("1.2"));

    algGen.close();

    //
    // Encapsulated ContentInfo
    //
    BERSequenceGenerator eiGen = new BERSequenceGenerator(cGen.getRawOutputStream());

    eiGen.addObject(new DERObjectIdentifier("1.1"));

    BEROctetStringGenerator octGen = new BEROctetStringGenerator(eiGen.getRawOutputStream(), 0, true);

    //
    // output containing zeroes
    //
    OutputStream out = octGen.getOctetOutputStream();

    out.write(new byte[] { 1, 2, 3, 4 });
    out.write(new byte[4]);
    out.write(new byte[20]);

    out.close();
    eiGen.close();
    cGen.close();
    sGen.close();

    //
    // reading back
    //
    ASN1StreamParser aIn = new ASN1StreamParser(bOut.toByteArray());

    ContentInfoParser cp = new ContentInfoParser((ASN1SequenceParser)aIn.readObject());

    CompressedDataParser comData = new CompressedDataParser((ASN1SequenceParser)cp.getContent(BERTags.SEQUENCE));
    ContentInfoParser     content = comData.getEncapContentInfo();

    ASN1OctetStringParser bytes = (ASN1OctetStringParser)content.getContent(BERTags.OCTET_STRING);

    InputStream in = bytes.getOctetStream();
    int         count = 0;

    while (in.read() >= 0)
    {
        count++;
    }

    assertEquals(28, count);
}
项目:ReCRED_FIDO_UAF_OIDC    文件:Asn1.java   
/**
 * 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();
}
项目:UAF    文件:Asn1.java   
/**
 * 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();
}