internal void EncodeTo(AsnWriter writer)
        {
            writer.PushSequence();

            writer.WriteObjectIdentifierForCrypto(_bagIdValue);

            Asn1Tag contextSpecific0 = new Asn1Tag(TagClass.ContextSpecific, 0);

            writer.PushSequence(contextSpecific0);
            writer.WriteEncodedValueForCrypto(EncodedBagValue.Span);
            writer.PopSequence(contextSpecific0);

            if (_attributes?.Count > 0)
            {
                List <AttributeAsn> attrs = CmsSigner.BuildAttributes(_attributes);

                writer.PushSetOf();

                foreach (AttributeAsn attr in attrs)
                {
                    attr.Encode(writer);
                }

                writer.PopSetOf();
            }

            writer.PopSequence();
        }
        private AsnWriter WritePkcs8()
        {
            PrivateKeyInfoAsn info = new PrivateKeyInfoAsn
            {
                PrivateKeyAlgorithm =
                {
                    Algorithm = AlgorithmId,
                },
                PrivateKey = PrivateKeyBytes,
            };

            if (AlgorithmParameters?.Length > 0)
            {
                info.PrivateKeyAlgorithm.Parameters = AlgorithmParameters;
            }

            if (Attributes.Count > 0)
            {
                info.Attributes = PkcsHelpers.NormalizeAttributeSet(CmsSigner.BuildAttributes(Attributes).ToArray());
            }

            // Write in BER in case any of the provided fields was BER.
            AsnWriter writer = new AsnWriter(AsnEncodingRules.BER);

            info.Encode(writer);
            return(writer);
        }