PEM-encode a DER blob under the given label. The output uses the canonical 64-char line wrapping (matches `openssl` output).
(label: &str, der: &[u8])
| 14 | /// PEM-encode a DER blob under the given label. The output uses the |
| 15 | /// canonical 64-char line wrapping (matches `openssl` output). |
| 16 | pub fn pem_encode(label: &str, der: &[u8]) -> String { |
| 17 | use base64::Engine; |
| 18 | let b64 = base64::engine::general_purpose::STANDARD.encode(der); |
| 19 | let mut out = String::with_capacity(b64.len() + 64); |
| 20 | out.push_str("-----BEGIN "); |
| 21 | out.push_str(label); |
| 22 | out.push_str("-----\n"); |
| 23 | for chunk in b64.as_bytes().chunks(64) { |
| 24 | out.push_str(std::str::from_utf8(chunk).expect("base64 is ascii")); |
| 25 | out.push('\n'); |
| 26 | } |
| 27 | out.push_str("-----END "); |
| 28 | out.push_str(label); |
| 29 | out.push_str("-----\n"); |
| 30 | out |
| 31 | } |
| 32 | |
| 33 | /// Write a DER certificate as a PEM-encoded `CERTIFICATE` block. |
| 34 | /// |