@@ -141,10 +141,52 @@ If you want to precisely specify the ASN.1 type, you have to use the `Encoder.en
141
141
142
142
This also allows to encode data progressively, without having to keep everything in memory.
143
143
144
+ DER and CER
145
+ -----------
146
+
147
+ The encoder uses DER (Distinguished Encoding Rules) encoding by default. If you want to use CER (Canonical Encoding Rules) encoding,
148
+ you can do so by calling the `Encoder.start() ` method with a stream as argument:
149
+
150
+ .. code-block :: python
151
+
152
+ stream = open (' output.cer' , ' wb' )
153
+ encoder = asn1.Encoder()
154
+ encoder.start(stream)
155
+
156
+ You can explicitly specify the CER encoding without using a stream:
157
+
158
+ .. code-block :: python
159
+
160
+ encoder = asn1.Encoder()
161
+ encoder.start(Encoder.CER )
162
+
163
+ You can explicitly specify the DER encoding when using a stream:
164
+
165
+ .. code-block :: python
166
+
167
+ stream = open (' output.cer' , ' wb' )
168
+ encoder = asn1.Encoder()
169
+ encoder.start(stream, Encoder.DER )
170
+
171
+ DER has the advantage to be predicatable: there is one and only one way to encode a message using DER. DER is
172
+ commonly used in security-related applications such as X.509 digital certificates. DER uses definite lengths for all
173
+ encoded messages. This means that the length of the encoded message is known in advance. This is useful for encoding
174
+ messages that are fixed in size or that do not change in size over time. The disadvantage of DER is that the encoded
175
+ binary data are kept in memory until the encoding is finished. This can be a problem for very large messages.
176
+
177
+ CER is similar to DER, but it uses indefinite lengths. This means that the length of the encoded message is not
178
+ known in advance. This is useful for encoding messages that may be very large or that may change in size over time.
179
+ The advantage of CER is that the encoded binary data are not kept in memory until the encoding is finished. This
180
+ means that the encoder can start writing the encoded message to a file or a stream as soon as it is available.
181
+
182
+ IMPORTANT: There was a mistake in version 3.0.0 of Python-ASN1 where the encoder used CER encoding by default contrary to the
183
+ previous versions that were using DER. This was fixed in version 3.0.1: CER is the default encoding when using a stream.
184
+ Otherwise, DER is the default encoding.
185
+
144
186
Decoding
145
187
--------
146
188
147
- If you want to decode ASN.1 from DER or BER encoded bytes, use code such as:
189
+ If you want to decode ASN.1 from BER ( DER, CER, ...) encoded bytes, use code such as:
148
190
149
191
.. code-block :: python
150
192
@@ -201,8 +243,7 @@ Constants
201
243
202
244
A few constants are defined in the `asn1 ` module. The
203
245
constants immediately below correspond to ASN.1 tag numbers.
204
- They can be used as
205
- the ``nr `` parameter of the
246
+ They can be used as the ``nr `` parameter of the
206
247
`Encoder.write() ` method, and are returned as the
207
248
first part of a ``(nr, typ, cls) `` tuple as returned by
208
249
`Decoder.peek() ` and
0 commit comments