| Conditions | 22 |
| Total Lines | 70 |
| Code Lines | 62 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like satcfdi.certifica.pkcs7.create_pkcs7() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | import os |
||
| 13 | def create_pkcs7(data, signer: Signer, hash_algorithm): |
||
| 14 | cert_bytes = signer.certificate_bytes() |
||
| 15 | issuer_der = signer.certificate.get_issuer().der() |
||
| 16 | serial = signer.certificate.get_serial_number() |
||
| 17 | |||
| 18 | hash_object = hashes.Hash(hash_algorithm) |
||
| 19 | hash_object.update(data) |
||
| 20 | digest = hash_object.finalize() |
||
| 21 | |||
| 22 | utctime = to_utc_time(datetime.utcnow()) |
||
| 23 | |||
| 24 | e = Ans1Encoder() |
||
| 25 | with e.seq(): |
||
| 26 | e.oid("1.2.840.113549.1.9.3") |
||
| 27 | with e.set(): |
||
| 28 | e.oid("1.2.840.113549.1.7.1") |
||
| 29 | with e.seq(): |
||
| 30 | e.oid("1.2.840.113549.1.9.5") |
||
| 31 | with e.set(): |
||
| 32 | e(utctime, nr=Numbers.UTCTime) |
||
| 33 | with e.seq(): |
||
| 34 | e.oid("1.2.840.113549.1.9.4") |
||
| 35 | with e.set(): |
||
| 36 | e(digest, nr=Numbers.OctetString) |
||
| 37 | signed_attributes = e.output() |
||
| 38 | |||
| 39 | e = Ans1Encoder() |
||
| 40 | with e.set(): |
||
| 41 | e.write(signed_attributes) |
||
| 42 | signing_data = e.output() |
||
| 43 | |||
| 44 | signature = signer.key.sign( |
||
| 45 | data=signing_data, |
||
| 46 | padding=padding.PKCS1v15(), |
||
| 47 | algorithm=hash_algorithm |
||
| 48 | ) |
||
| 49 | |||
| 50 | e = Ans1Encoder() |
||
| 51 | with e.seq(): |
||
| 52 | e.oid('1.2.840.113549.1.7.2') |
||
| 53 | with e.enter(nr=0, cls=Classes.Context): |
||
| 54 | with e.seq(): |
||
| 55 | e(1, nr=Numbers.Integer) |
||
| 56 | with e.set(): |
||
| 57 | with e.seq(): |
||
| 58 | e.oid('1.3.14.3.2.26') |
||
| 59 | e(nr=Numbers.Null) |
||
| 60 | with e.seq(): |
||
| 61 | e.oid("1.2.840.113549.1.7.1") |
||
| 62 | with e.enter(nr=0, cls=Classes.Context): |
||
| 63 | e(data, nr=Numbers.OctetString) |
||
| 64 | with e.enter(nr=0, cls=Classes.Context): |
||
| 65 | e.write(cert_bytes) |
||
| 66 | with e.set(): |
||
| 67 | with e.seq(): |
||
| 68 | e(1, nr=Numbers.Integer) |
||
| 69 | with e.seq(): |
||
| 70 | e.write(issuer_der) |
||
| 71 | e(serial, nr=Numbers.Integer) |
||
| 72 | with e.seq(): |
||
| 73 | e.oid('1.3.14.3.2.26') |
||
| 74 | e(nr=Numbers.Null) |
||
| 75 | with e.enter(nr=0, cls=Classes.Context): |
||
| 76 | e.write(signed_attributes) |
||
| 77 | with e.seq(): |
||
| 78 | e.oid('1.2.840.113549.1.1.1') |
||
| 79 | e(nr=Numbers.Null) |
||
| 80 | e(signature, nr=Numbers.OctetString) |
||
| 81 | |||
| 82 | return e.output() |
||
| 83 |